Plugin Directory

Changeset 356609


Ignore:
Timestamp:
03/07/2011 12:11:36 PM (15 years ago)
Author:
ajferg
Message:
 
Location:
flickr-press/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • flickr-press/trunk/admin.css

    r280368 r356609  
    3838form.flickr { }
    3939form.flickr p { clear:left; }
    40 form.flickr p label { width:8em; float:left; padding-top:0.3em; }
     40form.flickr p label { width:11em; float:left; padding-top:0.3em; }
    4141form.flickr p input { width:22em; }
    4242form.flickr p input.save { width:auto; }
     
    5050#tabFlickrAlbums .album p { clear:none; }
    5151
    52 form#FlickrClearAuth { float:right; }
     52form#FlickrClearAuth, form#ClearFlickrCache { float:right; }
  • flickr-press/trunk/flickr-admin.php

    r280368 r356609  
    11<?php
    22
    3 
    4 
    5 
    6 class FlickrPress_Admin extends FlickrPress_Options {
     3/**
     4 *  FlickrPress Admin Class
     5 */
     6class FlickrPress extends FlickrPress_Base {
    77   
    88    function __construct() {
    99        // Pre-load Options
    1010        $this->load_options();
    11         if (!$this->opts) { $this->load_default_options(); }
    1211        // Admin Menu
    1312        add_action('admin_menu', array(&$this,'admin_menu'));
    1413        add_filter('plugin_action_links', array(&$this,'plugin_action_links'), 10, 2);
     14        // Save Settings
     15        add_action('admin_init', array(&$this,'settings_page_save'), 12);
    1516        // Activation hook
    1617        register_activation_hook( FLICKR_PLUGIN_DIR.'/flickr.php', array( &$this, 'activation_hook' ) );
     18        // Only want to load our Flickr wrapper if we have the key set.
     19        if (!empty($this->opts['api']['key']) && !empty($this->opts['api']['secret'])) {
     20            add_action('admin_init', array(&$this,'load_flickr'));
     21        }
     22        // Warnings for admin area
     23        add_action('admin_notices', array(&$this,'admin_warning_pretty_permalinks'));
     24        add_action('admin_notices', array(&$this,'admin_warning_settings'));
     25       
     26    }
     27    // PHP 4 Constructor
     28    function FlickrPress() {
     29        $this->construct();
     30    }
     31   
     32    function admin_warning_pretty_permalinks() {
     33        if ('' != get_option( 'permalink_structure'))   return;
     34        echo '<div id="message" class="updated fade"><p><strong>Warning:</strong> FlickrPress will not work properly until you
     35                <a href="'.admin_url('options-permalink.php').'">set up your Permalinks</a>.</div>'."\n";
     36    }
     37   
     38    function admin_warning_settings() {
     39        if (empty($this->opts['api']['key']) || empty($this->opts['api']['secret'])) {
     40            echo '<div id="message" class="updated fade"><p><strong>Warning:</strong> FlickrPress will not work until you
     41                    <a href="'.admin_url('options-general.php?page=flickr').'">enter your Flickr API Settings</a>.</div>'."\n";
     42        } else
     43        if (empty($this->opts['user']['token'])) {
     44            echo '<div id="message" class="updated fade"><p><strong>Warning:</strong> FlickrPress will not work until you
     45                    <a href="'.admin_url('options-general.php?page=flickr').'">authorize with your Flickr account</a>.</div>'."\n";
     46        } else
     47        if (empty($this->opts['flickr_page'])) {
     48            echo '<div id="message" class="updated fade"><p><strong>Warning:</strong> FlickrPress will not work until you
     49                    <a href="'.admin_url('options-general.php?page=flickr').'">choose a page to show your albumns on</a>.</div>'."\n";
     50        }
    1751    }
    1852   
     
    2660        wp_enqueue_script('flickr', FLICKR_PLUGIN_URL.'/admin.js', array('jquery', 'jquery-ui-tabs') );
    2761        wp_enqueue_style('flickr', FLICKR_PLUGIN_URL.'/admin.css');
    28         // Only want to load our Flickr wrapper on the right pages.
    29         if (!empty($this->opts['api']['key']) && !empty($this->opts['api']['key'])) {
    30             $this->load_flickr();
    31         }
    3262    }
    3363   
     
    3969   
    4070   
    41    
    42    
    43     function settings_page() {
    44         echo '<div class="wrap">'."\n";
    45         echo '<h2>Flickr Settings</h2>'."\n";
    46        
     71    function settings_page_save() {
     72        // Make sure we're supposed to run...
     73        if (empty($_POST))                                          return;
     74        if (!wp_verify_nonce($_POST['_wpnonce'], $this->nonce))     return;
     75        // Clean & Secure all POST data
     76        $_POST = $this->slash_array($_POST);
    4777        // Clear API Details?
    4878        if (isset($_POST['ClearAPI'])) {
    49             if (!wp_verify_nonce($_POST['_wpnonce'], $this->nonce)) { echo '<p class="alert">Invalid Security</p></div>'."\n"; return;  }
    5079            $this->opts['api'] = array();
    5180            $this->opts['user'] = array();
    5281            $this->flickr = false;
    53             echo '<div id="message" class="updated fade"><p><strong>Your API settings have been cleared.  You may now re-enter your details.</strong></p></div>';
    54             $this->save_options();
    55         }
    56         // Save the API Key to Options
     82            $this->save_options();
     83            $this->update_message = '<div id="message" class="updated fade"><p><strong>Your API settings have been cleared.  You may now re-enter your details.</strong></p></div>';
     84        }
     85        // Save general options
     86        if (isset($_POST['SaveGeneral'])) {
     87            $this->opts['flickr_page']      = $_POST['sel_flickr_page'];
     88            $this->opts['flickr_position']  = $_POST['flickrSelPosition'];
     89            $this->opts['new_windows']      = $_POST['flickrOpenWindow'];
     90            $this->opts['hide_private']     = $_POST['flickrHidePrivate'];
     91            $this->save_options();
     92            $this->update_message = '<div id="message" class="updated fade"><p><strong>General settings saved.</strong></p></div>';
     93        }
     94        // Save API Key and Secret
    5795        if (isset($_POST['SaveAPI'])) {
    58             if (!wp_verify_nonce($_POST['_wpnonce'], $this->nonce)) { echo '<p class="alert">Invalid Security</p></div>'."\n"; return;  }
    59             $this->opts = array_merge($this->opts, array(
    60                 'api' => array(
    61                     'key'   => trim(stripslashes($_POST['apiKey'])),
    62                     'secret'=> trim(stripslashes($_POST['apiSecret'])),
    63                 )
    64             ));
    65             $this->save_options();
    66             echo '<div id="message" class="updated fade"><p><strong>Your new settings have been saved.</strong></p></div>';
     96            $this->opts['api'] = array( 'key'=>$_POST['apiKey'], 'secret'=>$_POST['apiSecret']);
     97            $this->save_options();
    6798            $this->load_flickr();
    68         }
    69         // Save authorisation data to Options
     99            $this->update_message = '<div id="message" class="updated fade"><p><strong>Your Flickr API Key has been saved.</strong></p></div>';
     100        }
     101        // Save our Flickr FROB token (authenticate with Flickr)
    70102        if (isset($_POST['SaveFlickrToken'])) {
    71             if (!wp_verify_nonce($_POST['_wpnonce'], $this->nonce)) { echo '<p class="alert">Invalid Security</p></div>'."\n"; return;  }
    72             $frob = trim(stripslashes($_POST['frob']));
    73             $token = $this->flickr->auth_getToken($frob);
     103            $token = $this->flickr->auth_getToken($_POST['frob']);
    74104            $this->opts['user'] = array(
    75105                'ID'    => $token['user']['nsid'],
     
    80110            );
    81111            $this->save_options();
    82             echo '<div id="message" class="updated fade"><p><strong>Flickr Authorised.</strong></p></div>';
    83         }
    84         // Save General Options
    85         if (isset($_POST['SaveGeneral'])) {
    86             $this->opts['flickr_page'] = trim(stripslashes($_POST['sel_flickr_page']));
    87             $this->opts['new_windows'] = (trim($_POST['flickrOpenWindow']=='Yes')) ? true : false;
    88             $this->opts['hide_private'] = (trim($_POST['flickrHidePrivate']=='Yes')) ? true : false;
    89             $this->save_options();
    90             echo '<div id="message" class="updated fade"><p><strong>General Settings Saved.</strong></p></div>';
    91         }
    92         // Save Albums tab
     112            $this->update_message = '<div id="message" class="updated fade"><p><strong>Flickr Authorised.</strong></p></div>';
     113        }
     114        // Save Album Settings
    93115        if (isset($_POST['SaveAlbumSettings'])) {
    94             if (!wp_verify_nonce($_POST['_wpnonce'], $this->nonce)) { echo '<p class="alert">Invalid Security</p></div>'."\n"; return;  }
    95116            $this->opts['hide_albums'] = (is_array($_POST['hide_album'])) ? array_values($_POST['hide_album']) : array();
    96117            $this->save_options();
    97             echo '<div id="message" class="updated fade"><p><strong>Album Settings Saved.</strong></p></div>';
    98         }
    99        
    100         // Warning message:  Must save API Key before plugin is active.
    101         if (empty($this->opts['api']['key']))
    102             echo '<div id="message" class="updated fade"><p>You need to provide an <strong>API Key.</strong></p></div>';
     118            $this->update_message = '<div id="message" class="updated fade"><p><strong>Album Settings Saved.</strong></p></div>';
     119        }
     120        // Force a refresh - clear cached data
     121        if (isset($_POST['ClearFlickrCache'])) {
     122            global $wpdb;
     123            $sql = "DELETE FROM `{$this->flickr->cache_table}`;";
     124            $wpdb->query($sql);
     125            $this->update_message = '<div id="message" class="updated fade"><p><strong>Flickr cache has been cleared.</strong></p></div>';
     126        }
     127       
     128    }
     129   
     130    function settings_page() {
     131        echo '<div class="wrap">'."\n";
     132        echo '<h2>Flickr Settings</h2>'."\n";
     133       
     134        //echo '<pre>'; print_r($_POST); echo '</pre>';
     135        if ($this->update_message)  echo $this->update_message;
    103136       
    104137        // Tickbox
     
    116149</ul><!-- tcTabMenu -->
    117150<div id="tabSettings">
    118   <form id="flickrAPI" class="flickr" method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>">
     151  <form class="flickr" method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>">
    119152    <?php wp_nonce_field($this->nonce); ?>
    120153    <p><label for="sel_flickr_page">Flickr Page</label>
     
    127160                 ));
    128161        ?></p>
     162    <p><label for="flickrSelPosition">Auto-add Position:</label>
     163            <select name="flickrSelPosition" id="flickrSelPosition">
     164            <?php
     165            $poss_positions = array(
     166                'below' => 'After existing page content',
     167                'above' => 'Before existing page content',
     168            );
     169            foreach ($poss_positions as $pos => $label) {
     170                $sel = ($this->opts['flickr_position'] == $pos) ? ' selected="selected"' : '';
     171                echo '<option value="'.$pos.'"'.$sel.'>'.$label.'</option>';
     172            }
     173            ?>
     174            </select></p>
    129175    <p class="tick"><label><input type="checkbox" name="flickrOpenWindow" id="flickrOpenWindow" value="Yes" <?php echo $sel_window; ?>/>
    130176        Open links to Flickr in a new window?</label></p>
     
    134180        <input type="submit" name="SaveGeneral" value="Save Changes" class="button-primary save" /></p>
    135181  </form>
    136 </div><!-- tab -->
     182</div>
    137183<div id="tabFlickrAPI">
    138     <?php
    139     if (!is_object($this->flickr)) {
    140         // Ask for Flickr Key + Secret
    141         ?>
    142           <form id="flickrAPI" class="flickr" method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>#tabFlickrAPI">
    143             <?php wp_nonce_field($this->nonce); ?>
    144             <p><label for="apiKey">Flickr API Key</label>
    145                 <input type="text" name="apiKey" id="apiKey" value="<?php echo $this->opts['api']['key']; ?>" /></p>
    146             <p><label for="apiSecret">Shared Secret</label>
    147                 <input type="text" name="apiSecret" id="apiSecret" value="<?php echo $this->opts['api']['secret']; ?>" /></p>
    148             <p><label>&nbsp;</label>
    149                 <input type="submit" name="SaveAPI" value="Save Changes" class="button-primary save" /></p>
    150           </form>
    151           <p>Sign into Flickr and <a href="http://www.flickr.com/services/apps/" target="_blank">get your API key here</a>.</p>
    152184        <?php
    153     } else {
     185        if (!is_object($this->flickr)) {
     186            // Ask for Flickr Key + Secret
     187            ?>
     188            <form class="flickr" method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>#tabFlickrAPI">
     189                <?php wp_nonce_field($this->nonce); ?>
     190                <p><label for="apiKey">Flickr API Key</label>
     191                    <input type="text" name="apiKey" id="apiKey" value="<?php echo $this->opts['api']['key']; ?>" /></p>
     192                <p><label for="apiSecret">Shared Secret</label>
     193                    <input type="text" name="apiSecret" id="apiSecret" value="<?php echo $this->opts['api']['secret']; ?>" /></p>
     194                <p><label>&nbsp;</label>
     195                    <input type="submit" name="SaveAPI" value="Save Changes" class="button-primary save" /></p>
     196                <p>Sign into Flickr and <a href="http://www.flickr.com/services/apps/" target="_blank">get your API key here</a>.</p>
     197            </form>
     198            <?php
     199        } else
    154200        if ($this->flickr !== false && empty($this->opts['user']['token'])) {
    155201            // We have the key, but not yet authorized
    156202            $frob = $this->flickr->auth_getFrob();
    157203            $api_sig = md5( $this->flickr->secret.'api_key'.$this->flickr->api_key.'frob'.$frob.'perms'.$this->perms);
    158         ?>
    159           <p><strong>Flickr API Key:</strong> <?php echo $this->opts['api']['key']; ?></p>
    160           <p><strong>Shared Secret:</strong> <?php echo $this->opts['api']['secret']; ?></p>
    161          
    162           <p>You must now link your flickr account to this site.</p>
    163           <form id="FlickrGetAuth" method="get" action="http://flickr.com/services/auth/" target="_blank">
    164             <input type="hidden" name="api_key" value="<?php echo $this->flickr->api_key; ?>" />
    165             <input type="hidden" name="frob" value="<?php echo $frob; ?>" />
    166             <input type="hidden" name="perms" value="<?php echo $this->perms; ?>" />
    167             <input type="hidden" name="api_sig" value="<?php echo $api_sig; ?>" />
    168             <p>Step 1: <input type="submit" class="button-secondary" value="Authorize Flickr" /></p>
    169           </form>
    170           <form id="FlickrGetToken" method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>">
    171             <?php wp_nonce_field($this->nonce); ?>
    172             <input type="hidden" name="frob" value="<?php echo $frob; ?>" />
    173             <p>Step 2: <input type="submit" name="SaveFlickrToken" value="Save Changes" class="button-secondary" /></p>
    174           </form>
    175         <?php
     204            ?>
     205            <form id="FlickrClearAuth" method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>#tabFlickrAPI">
     206                <?php wp_nonce_field($this->nonce); ?>
     207                <input type="submit" name="ClearAPI" value="Clear these details" class="button-secondary" />
     208            </form>
     209            <p><strong>Flickr API Key:</strong> <?php echo $this->opts['api']['key']; ?></p>
     210            <p><strong>Shared Secret:</strong> <?php echo $this->opts['api']['secret']; ?></p>
     211            <p>You must now link your flickr account to this site.</p>
     212            <form id="FlickrGetAuth" method="get" action="http://flickr.com/services/auth/" target="_blank">
     213                <input type="hidden" name="api_key" value="<?php echo $this->flickr->api_key; ?>" />
     214                <input type="hidden" name="frob" value="<?php echo $frob; ?>" />
     215                <input type="hidden" name="perms" value="<?php echo $this->perms; ?>" />
     216                <input type="hidden" name="api_sig" value="<?php echo $api_sig; ?>" />
     217                <p>Step 1: <input type="submit" class="button-secondary" value="Authorize Flickr" /></p>
     218            </form>
     219            <form id="FlickrGetToken" method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>">
     220                <?php wp_nonce_field($this->nonce); ?>
     221                <input type="hidden" name="frob" value="<?php echo $frob; ?>" />
     222                <p>Step 2: <input type="submit" name="SaveFlickrToken" value="Save Changes" class="button-secondary" /></p>
     223            </form>
     224            <?php
    176225        } else {
    177         ?>
     226            ?>
    178227          <form id="FlickrClearAuth" method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>#tabFlickrAPI">
    179228             <?php wp_nonce_field($this->nonce); ?>
     
    183232          <p><strong>Shared Secret:</strong> <?php echo $this->opts['api']['secret']; ?></p>
    184233          <p>Your are now linked to your Flickr account, and can display photos on your site.</p>
    185         <?php
    186         }
    187     }
    188     ?>
    189 </div><!-- tab -->
     234            <?php
     235        }
     236        ?>
     237</div>
    190238<?php   if (is_object($this->flickr) && !empty($this->opts['user']['token'])) { ?>
    191239<div id="tabFlickrAlbums">
    192     <form id="flickrAlbums" class="flickr" method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>#tabFlickrAlbums">
    193     <?php wp_nonce_field($this->nonce); ?>
     240  <form id="ClearFlickrCache" method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>#tabFlickrAlbums">
     241     <?php wp_nonce_field($this->nonce); ?>
     242     <input type="submit" name="ClearFlickrCache" value="Clear the Flickr Cache" class="button-secondary" />
     243  </form>
     244  <form class="flickr" method="post" action="<?php echo esc_url($_SERVER['REQUEST_URI']); ?>#tabFlickrAlbums">
     245    <?php wp_nonce_field($this->nonce); ?>
    194246    <?php
    195247    $result = $this->flickr->photosets_getList($this->opts['user']['ID']);
     
    217269    <p><label>&nbsp;</label>
    218270        <input type="submit" name="SaveAlbumSettings" value="Save Changes" class="button-primary save" /></p>
    219     </form>
     271  </form>
    220272</div>
    221 <?php   } // flickr object                                                      ?>
     273<?php   }   // if $flickr token set     ?>
    222274</div><!-- tabContainer -->
     275
     276
    223277        <?php
    224        
     278//      echo '<pre>'; print_r($this->opts); echo '</pre>';
     279//      echo '<pre>'; print_r($this->flickr); echo '</pre>';
    225280        echo '</div>'."\n";
    226         //echo '<pre>'; print_r($this->opts); echo '</pre>';
    227     }
    228    
     281    }
    229282   
    230283}
     
    232285
    233286
    234 
    235287?>
  • flickr-press/trunk/flickr-public.php

    r280368 r356609  
    11<?php
    22
    3 
    43/**
    5  *  Public display
     4 *  FlickrPress Public Class
    65 */
    7 class FlickrPress_Public extends FlickrPress_Options {
    8    
    9     /**
    10      *  Public display
     6class FlickrPress extends FlickrPress_Base {
     7   
     8    /** 
     9     *  Constructor
    1110     */
    1211    function __construct() {
    1312        // Pre-load Options
    1413        $this->load_options();
    15         if (!$this->opts) { $this->load_default_options(); }
    16         // Let's get started...
     14        // Let's get started... This will hijack page requests for use of FlickrPress
    1715        add_action('template_redirect', array(&$this,'hijack_page'));
     16    }
     17    // PHP 4 Constructor
     18    function FlickrPress() {
     19        $this->construct();
    1820    }
    1921   
     
    5759        return $image;
    5860    }
    59 
    60    
    61 
     61   
    6262    /**
    6363     *  Response Headers
     
    7474    }
    7575   
     76   
    7677    /**
    7778     *  Hijack page content
     
    8384        $request_uri = (($_SERVER['HTTPS'] != 'on') ? 'http://' : 'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    8485        if (substr($request_uri, 0, strlen($permalink)) !== $permalink) return;
    85         // Load flickr object
     86        // Load flickr object - only if we're on a FlickrPress page
    8687        if (empty($this->opts['user']['token'])) return;
    8788        $this->load_flickr();
     
    8990        wp_enqueue_script('flickrpress', FLICKR_PLUGIN_URL.'/custom.js', array('jquery') );
    9091        wp_enqueue_style('flickrpress', FLICKR_PLUGIN_URL.'/custom.css');
    91        
    9292        //echo '<h1>Marker: Running Flickr Hijack</h1>';
    9393       
     
    106106        }
    107107    }
    108    
    109    
    110     // Filter wp_title to get <title>  But some themes don't just use wp_title, keep that in mind.
    111108   
    112109   
     
    206203        }
    207204        echo '</div><!-- flickr_photosets -->'."\n";
    208         $content .= ob_get_clean();
     205        $output = ob_get_clean();
     206        // Append or Prepend content?
     207        if ($this->opts['flickr_position'] == 'below')  $content = $content.$output;
     208        else                                            $content = $output.$content;
     209        // Return content
    209210        return $content;
    210211    }
     
    283284    }
    284285   
    285    
    286286}
    287287
    288 
    289 
    290288?>
  • flickr-press/trunk/flickr.php

    r281073 r356609  
    33Plugin Name:    Flickr Press
    44Description:    Use this plugin to display your Flickr photosets on your WordPress site.
    5 Version:        1.2
     5Version:        1.3
    66Author:         Anthony Ferguson
    77Author URI:     http://www.fergusweb.net/
     
    99*/
    1010
    11 /*
    12 Make this worth with Thesis & other themes too.
    13 
    14 Offer a tickbox for "Enable Lightbox"
    15 That way, clicking on a photo-page opens Lightbox for full-size ?
    16 
    17 Make sure caching works - do I need a "refresh" / purge cache function?
    18 
    19 Close comments on Album & Photo Views
    20 */
    2111
    2212// Definitions
     
    2616// Include required files
    2717require_once(FLICKR_PLUGIN_DIR.'/phpflickr-3.0/phpFlickr.php');
    28 require_once(FLICKR_PLUGIN_DIR.'/flickr-includes.php');
    29 require_once(FLICKR_PLUGIN_DIR.'/flickr-admin.php');
    30 require_once(FLICKR_PLUGIN_DIR.'/flickr-public.php');
    3118
    32 // Launch Plugin
    3319if (is_admin()) {
    34     $FlickrAdmin = new FlickrPress_Admin();
     20    require_once(FLICKR_PLUGIN_DIR.'/flickr-admin.php');
    3521} else {
    36     $FlickrPublic = new FlickrPress_Public();
     22    require_once(FLICKR_PLUGIN_DIR.'/flickr-public.php');
     23}
     24
     25// Launch whichever class we loaded (public | admin)
     26new FlickrPress();
     27
     28
     29
     30/**
     31 *  Extend the phpFlickr class to overwrite a few things.
     32 */
     33class phpFlickrPress extends phpFlickr {
     34    var $hide_private = true;
     35    var $max_cache_rows = 1000;
     36    var $cache_expire   = false;        // Seconds, from $this->enableCache()
     37   
     38    function enableCache($table, $cache_expire=60) {
     39        global $wpdb;
     40        // Set up connection
     41        $this->cache = 'db';
     42        $this->cache_table = $wpdb->prefix.$table;
     43        $this->cache_db = $wpdb->dbh;
     44        $this->cache_expire = $cache_expire;
     45        // Will remove any expired rows from database - runs on load.
     46        $sql = "DELETE FROM `{$this->cache_table}` WHERE `expiration` <= '".$this->current_time()."'";
     47        $wpdb->query($sql);
     48//      echo $sql.'<br><br>';
     49       
     50        return; //??        Not using 'max_cache_rows' just yet.  Come back for this later.
     51       
     52        // If we have more than the max rows cached, purge all expired and start over
     53        $result = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM {$this->cache_table}", $this->cache_db));
     54        if ( $result[0] > $this->max_cache_rows ) {
     55            mysql_query("DELETE FROM $table WHERE expiration < DATE_SUB(NOW(), INTERVAL {$this->cache_expire} second)", $db);       // TODO:  Use WP time function instead of SQL. Consistent.
     56            mysql_query('OPTIMIZE TABLE ' . $this->cache_table, $db);
     57        }
     58    }
     59   
     60    // Use this for a request to filter private photos.  Calls parent request() to avoid duplicating a very big function
     61    function request($command, $args=array(), $nocache=false) {
     62        if ($this->hide_private) {
     63            $args['privacy_filter'] = 1;
     64        }
     65        parent::request($command, $args, $nocache);
     66    }
     67   
     68    function hash_request($request) {
     69        foreach ($request as $key=>$val) {
     70            if (empty($val) || $key=='api_sig')     unset($request[$key]);
     71            else                                    $request[$key] = (string) $val;
     72        }
     73        return md5(serialize($request));
     74    }
     75    function cache($request, $response) {
     76        global $wpdb;
     77        // Prep our 'request' key
     78        $hash = $this->hash_request($request);
     79        // Figure out if UPDATE or INSERT is appropriate
     80        $result = $wpdb->get_var("SELECT COUNT(*) FROM `{$this->cache_table}` WHERE `request` = '{$hash}'");
     81        if ($result) {
     82            $sql = "UPDATE `{$this->cache_table}` SET `response` = '".$wpdb->escape($response)."', `expiration` = '".$this->expiry_time()."' WHERE `request` = '{$hash}';";
     83        } else {
     84            $sql = "INSERT INTO `{$this->cache_table}` (`request` , `response` , `expiration`) VALUES ( '{$hash}', '".$wpdb->escape($response)."', '".$this->expiry_time()."' );";
     85        }
     86        $wpdb->query($sql);
     87    }
     88   
     89    function getCached($request) {
     90        global $wpdb;
     91        // Prep our 'request' key
     92        $hash = $this->hash_request($request);
     93        // Check database for cached result for this request.  Returns unparsed result, or FALSE
     94        $row = $wpdb->get_row("SELECT `response` FROM `{$this->cache_table}` WHERE `request` = '{$hash}' AND `expiration` > '".$this->current_time()."'");
     95        return (is_object($row)) ? $row->response : false;
     96    }
     97   
     98    function current_time() {
     99        return gmdate( 'Y-m-d H:i:s', (time() + (get_option('gmt_offset') * 3600)));
     100    }
     101    function expiry_time() {
     102        return gmdate( 'Y-m-d H:i:s', (strtotime("+{$this->cache_expire} seconds", time() + (get_option('gmt_offset') * 3600))));
     103    }
     104   
    37105}
    38106
    39107
    40108
     109/**
     110 *  Base Class
     111 *      Contains our common functions & variables
     112 *      Other classes will extend this
     113 */
     114class FlickrPress_Base {
     115    protected $option_key   = 'flickr-options';
     116    protected $nonce        = 'flickrpress';
     117    protected $perms        = 'read';               // The permissions we require from Flickr
     118    protected $opts         = false;
     119    protected $flickr       = false;                // Will store our flickr wrapper when relevant.
     120    protected $cache_table  = 'flickr_cache';       // Use this for the Flickr Cache.  Prefix added later.
     121
     122    // Default Options
     123    function load_default_options() {
     124        $this->opts = array(
     125            'api'               => array( 'key'=>'', 'secret'=>''),
     126            'user'              => array('ID'=>'', 'login'=>'', 'name'=>'', 'token'=>'', 'frob'=>''),
     127            'flickr_page'       => false,
     128            'flickr_position'   => 'below', // insert before/after existing content (initial list of albums)
     129            'new_windows'       => true,
     130            'hide_private'      => true,
     131            'hide_albums'       => array(), // array of album IDs to NOT display on blog.
     132        );
     133    }
     134    // Option Helpers
     135    function load_options() {
     136        $this->opts = get_option($this->option_key);
     137        if (!$this->opts || !is_array($this->opts))     $this->load_default_options();
     138    }
     139    function save_options($options = false) {
     140        if (!$options) { $options = $this->opts; }
     141        update_option($this->option_key, $options);
     142        $this->load_options();
     143    }
     144   
     145    // $_POST cleaner : recursive
     146    function slash_array($array=false) {
     147        if (!is_array($array)) { return trim(stripslashes($array)); }
     148        foreach($array as $key=>$value) {
     149                $array[$key] = (!is_array($value)) ? trim(stripslashes($value)) : $this->slash_array($value);
     150        }
     151        return $array;
     152    }
     153
     154    // Load the Flickr wrapper
     155    function load_flickr() {
     156        // Ensure options are loaded
     157        if (!$this->opts) return false;
     158        // Setup Object
     159        $this->flickr = new phpFlickrPress($this->opts['api']['key'], $this->opts['api']['secret']);
     160        $this->flickr->hide_private = $this->opts['hide_private'];
     161        $this->flickr->enableCache($this->cache_table);
     162    }
     163   
     164    // Create table on activation
     165    // Packaged class attempts creation on every call, this is a more efficient method
     166    function activation_hook() {
     167        global $wpdb;
     168        $this->cache_table = $wpdb->prefix.$this->cache_table;
     169        if ($wpdb->get_var("show tables like '{$this->cache_table}'") == $this->cache_table) return;
     170        $sql = "CREATE TABLE ".$this->cache_table." (
     171                    request CHAR(35) NOT NULL,
     172                    response MEDIUMTEXT NOT NULL,
     173                    expiration DATETIME NOT NULL,
     174                    INDEX (request)
     175                ) TYPE = MYISAM;";
     176        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
     177        dbDelta($sql);
     178    }
     179   
     180}
     181
     182
     183
     184
     185
     186
     187
     188
     189
     190
     191
     192
     193
     194
     195
    41196?>
  • flickr-press/trunk/readme.txt

    r280368 r356609  
    22Contributors:       ajferg
    33Tags:               flickr, photo, gallery
    4 Requires at least:  3.0
    5 Tested up to:       3.0.1
    6 Stable tag:         1.2
     4Requires at least:  3.1
     5Tested up to:       3.1
     6Stable tag:         1.3
    77
    88Use this plugin to display your Flickr photosets on your WordPress site.
     
    3131Sure - just mark the photo as 'private' in your Flickr account, and tick the "Hide private photos" setting your WordPress admin area.
    3232
     33= Why am I getting 404 errors? =
     34Maybe you haven't set up "pretty permalinks" yet.  If you're still using the default permalink structure (site.com/?p=3) then you're going to have some problems.
     35
     36= How do I apply my own CSS rules? =
     37The plugin contains a file called "skeleton.css".  Copy/paste the contents of this to your theme's custom CSS file, and over-ride any style rules you like.
     38
    3339== Changelog ==
     40
     41= 1.3 =
     42* Better "Clear API Settings" buttons should you need to unlink from your Flickr account
     43* Better cache system to reduce calls to Flickr API
     44* Admin includes button to "Clear Flickr Cache", causing you to immediately refresh all your data from Flickr
     45* Extensively re-written to fix some reported bugs
     46* Feature: You can now select whether to insert your initial list of albums before or after your existing page content
    3447
    3548= 1.2 =
Note: See TracChangeset for help on using the changeset viewer.