Plugin Directory

Changeset 800481


Ignore:
Timestamp:
11/07/2013 03:42:21 PM (12 years ago)
Author:
tobyberesford
Message:

added cache options page

File:
1 edited

Legend:

Unmodified
Added
Removed
  • leaderboarded/trunk/leaderboarded.php

    r800441 r800481  
    4949    ), $atts));
    5050
    51     // Create the Cache Key
    52     $cacheKey = 'leaderboarded:' . $slug . ':' . $release;
     51    //check if caching is enabled
     52    $bool_use_cache = get_option( 'leaderboarded_use_caching' );
    5353
    54     // Create the URL for the Leaderboard that we're going to get
    55     $url = 'http://www.leaderboarded.com/display/' . $slug . '/' . $release . '/embed/' . $cut . '/' . $theme;
    56 
    57     // Try getting the Cached Version (daily one)
    58     $theBody = wp_cache_get($cacheKey . ':' . 'd', 'leaderboarded');
    59 
    60     // Try getting the Cached Version (weekly one)
    61     if ($theBody === false) {
    62         $theBody = wp_cache_get($cacheKey . ':' . 'w', 'leaderboarded');
     54    //if its enabled then try and get a cached version first
     55    if ($bool_use_cache) {
     56        // Create the Cache Key
     57        $cacheKey = 'leaderboarded:' . $slug . ':' . $release;
     58   
     59        // Create the URL for the Leaderboard that we're going to get
     60        $url = 'http://www.leaderboarded.com/display/' . $slug . '/' . $release . '/embed/' . $cut . '/' . $theme;
     61   
     62        // Try getting the Cached Version (daily one)
     63        $theBody = wp_cache_get($cacheKey . ':' . 'd', 'leaderboarded');
     64   
     65        // Try getting the Cached Version (weekly one)
     66        if ($theBody === false) {
     67            $theBody = wp_cache_get($cacheKey . ':' . 'w', 'leaderboarded');
     68        }
     69    } else {
     70        $theBody = false;
    6371    }
    64 
     72   
    6573    // Check we got something, if we didn't then try fetching a live one
    6674    if ($theBody === false) {
     
    6977        // Show a message when we get an error
    7078        if (is_wp_error($theBody)) {
    71             return 'Leaderboarded Plugin: could not find the leaderboard you asked for.';
     79            return 'The leaderboard is not currently available, please try back later or refresh the page.';
    7280        } else {
    7381            // Grab the Body and Cache the Leaderboard
     
    8492}
    8593
     94/* Settings menu */
     95add_action( 'admin_menu', 'leaderboarded_plugin_menu' );
     96
     97/** Step 1. */
     98function leaderboarded_plugin_menu() {
     99    add_options_page( 'Leaderboarded Options', 'Leaderboarded', 'manage_options', 'leaderboarded_plugin_options', 'leaderboarded_options' );
     100}
     101
     102/** Step 3. */
     103function leaderboarded_options() {
     104    if ( !current_user_can( 'manage_options' ) )  {
     105        wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
     106    }
     107   // variables for the field and option names
     108    $opt_name = 'leaderboarded_use_caching';
     109   $hidden_field_name = 'leaderboarded_submit_hidden';
     110    $data_field_name = 'leaderboarded_use_caching_field';
     111
     112    // Read in existing option value from database
     113    $opt_val = get_option( $opt_name );
     114
     115    // See if the user has posted us some information
     116    // If they did, this hidden field will be set to 'Y'
     117    if( isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y' ) {
     118        // Read their posted value
     119        $opt_val = $_POST[ $data_field_name ];
     120
     121        // Save the posted value in the database
     122        update_option( $opt_name, $opt_val );
     123
     124        // Put an settings updated message on the screen
     125
    86126?>
     127<div class="updated"><p><strong><?php _e('Settings saved.', 'leaderboarded_options' ); ?></strong></p></div>
     128<?php
     129
     130        // Did they ask to clear cache
     131        $bool_clear_cache = $_POST[ 'clear_cache_now' ];
     132       
     133        if ($bool_clear_cache) {
     134           
     135            //clear all the caches Rodney!
     136            wp_cache_flush();
     137            // Put an settings updated message on the screen
     138
     139            ?>
     140            <div class="updated"><p><strong><?php _e('Cache cleared.', 'leaderboarded_options' ); ?></strong></p></div>
     141            <?php
     142
     143        }
     144         
     145
     146    }
     147
     148    // Now display the settings editing screen
     149
     150    echo '<div class="wrap">';
     151
     152    // header
     153
     154    echo "<h2>" . __( 'Leaderboarded Plugin Settings', 'leaderboarded_options' ) . "</h2>";
     155
     156    // settings form
     157   
     158    ?>
     159
     160<form name="form1" method="post" action="">
     161<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y">
     162
     163<p><?php _e("Cache leaderboards to improve performance:", 'leaderboarded_options' ); ?>
     164<input type="checkbox" name="<?php echo $data_field_name; ?>" value="1" <?php if ($opt_val) {echo "checked";} ?>>
     165</p>
     166<p><?php _e("Clear cache now (note this flushes all caches for your wordpress site)", 'leaderboarded_options' ); ?>
     167<input type="checkbox" name="clear_cache_now" value="1">
     168</p><hr />
     169<hr />
     170
     171<p class="submit">
     172<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" />
     173</p>
     174
     175</form>
     176</div>
     177<?
     178}
     179
     180?>
Note: See TracChangeset for help on using the changeset viewer.