Advertisement
Guest User

Custom cap trimmed options page

a guest
Aug 2nd, 2010
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.54 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Editor Options
  4. */
  5. /*
  6. Copyright (C) 2010 - t31os (not that i really care anyway)
  7.  
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  21. */
  22. if( !class_exists( 'editor_options' ) ) {
  23.    
  24.     class editor_options {
  25.  
  26.         // Constructor
  27.         function editor_options() {
  28.             if( is_admin() )               
  29.                 add_action( 'admin_menu' , array( &$this , 'register_plugin_pages' ) );
  30.         }
  31.        
  32.         // Register the plugin pages
  33.         function register_plugin_pages() {
  34.            
  35.             // Parent menu, options general
  36.             add_menu_page(
  37.                 'General Settings',                    // Page title - What you see in the browser title
  38.                 'Settings',                            // Menu title - What the menu item is named
  39.                 'update_options_general',              // Capability - Capability required to manage this plugin
  40.                 'editor_options_general',              // Admin Page - The page name used to query for the plugin
  41.                 array( &$this , 'do_options' )         // Callback   - The function to be called when the plugin page is requested
  42.             );
  43.             // Add options general as a submenu so the menu title can differ from the menu heading (above)
  44.             add_submenu_page(
  45.                 'editor_options_general',              // Parent menu (self)
  46.                 'General Settings',                    // Page title - What you see in the browser title
  47.                 'General',                             // Menu title - What the menu item is named
  48.                 'update_options_general',              // Capability - Capability required to manage this plugin
  49.                 'editor_options_general',              // Admin Page - The page name used to query for the plugin
  50.                 array( &$this , 'do_options' )         // Callback   - The function to be called when the plugin page is requested
  51.             ); 
  52.             // Add the privacy submenu
  53.             add_submenu_page(
  54.                 'editor_options_general',              // Parent menu
  55.                 'Privacy Settings',                    // Page title - What you see in the browser title
  56.                 'Privacy',                             // Menu title - What the menu item is named
  57.                 'update_options_privacy',              // Capability - Capability required to manage this plugin
  58.                 'editor_options_privacy',              // Admin Page - The page name used to query for the plugin
  59.                 array( &$this , 'do_options' )         // Callback   - The function to be called when the plugin page is requested
  60.             );
  61.  
  62.         }
  63.  
  64.         // Print out the plugin page
  65.         function do_options() {
  66.            
  67.             // There should always be a request page, it's part of the URL for the pages
  68.             if( !isset( $_REQUEST['page'] ) )
  69.                 wp_die('The plugin page was not requested');
  70.            
  71.             // Now we know it's set, store
  72.             $requested_page = $_REQUEST['page'];
  73.            
  74.             // Make sure user has the privs
  75.             if( 'editor_options_privacy' == $requested_page ) {
  76.                 if( !current_user_can( 'update_options_privacy' ) )
  77.                     wp_die('Cheatin Huh!');
  78.                 $plugin_page = 'Privacy';
  79.             }
  80.             // Make sure user has the privs
  81.             elseif( 'editor_options_general' == $requested_page ) {
  82.                 if( !current_user_can( 'update_options_general' ) )
  83.                     wp_die('Cheatin Huh!');
  84.                 $plugin_page = 'General';
  85.             }
  86.             else {
  87.                 // Else something went wrong, bail out.
  88.                 wp_die('Are you sure you requested the right page?');
  89.             }
  90.             // Build valid options array
  91.             $valid_options = array('Submit');
  92.            
  93.             // If changes were submitted
  94.             if( $_POST ) {
  95.                 // Add general options
  96.                 if( 'General' == $plugin_page ) {
  97.                     $valid_options[] = 'blogname';
  98.                     $valid_options[] = 'blogdescription';
  99.                 }
  100.                 // Add privacy option
  101.                 elseif( 'Privacy' == $plugin_page ) {
  102.                     $valid_options[] = 'blog_public';
  103.                 }
  104.                 // If anything else was submitted along with the data, well it's not suppose to be there, so again just bail out..
  105.                 foreach( $_POST as $name => $data ) {
  106.                     if( !in_array( $name, $valid_options ) ) {
  107.                         wp_die('Invalid option found on submission, settings could not be saved.');
  108.                     }  
  109.                 }
  110.                 // If we've made this far, we should have options to save (and only valid ones)
  111.                 $options = array();
  112.                 // Check which page was requested again
  113.                 if( 'General' == $plugin_page ) {
  114.                     // General options, let's escape any HTML
  115.                     $options = array_map('esc_html', $_POST );
  116.                 }
  117.                 elseif( 'Privacy' == $plugin_page ) {
  118.                     // Privacy page, on / off setting, so only need a 0 vs 1 comparision check
  119.                     if( isset( $_POST['blog_public'] ) ) {
  120.                         if( 1 == $_POST['blog_public'] )
  121.                             $options['blog_public'] = '1';
  122.                         else
  123.                             $options['blog_public'] = '0';
  124.                     }
  125.                 }
  126.                 if( !empty( $options ) ) {
  127.                     unset($_POST['submit']); // Let's not save the submit buttons value
  128.                     foreach( $options as $option_name => $option_value ) {
  129.                         update_option($option_name,$option_value);
  130.                     }
  131.                 }
  132.             }
  133.             ?>
  134.            
  135.             <div class="wrap">
  136.                 <?php screen_icon('options-general'); ?>
  137.                 <h2><?php echo $plugin_page; ?> Settings</h2>
  138.  
  139.                 <form method="post" action="admin.php?page=<?php echo $requested_page; ?>">
  140.                    
  141.                     <table class="form-table">
  142.                         <?php
  143.                         switch( $plugin_page ) {
  144.                             case 'General':
  145.                         ?>
  146.                        
  147.                         <tr valign="top">
  148.                             <th scope="row"><label for="blogname"><?php _e('Site Title') ?></label></th>
  149.                             <td><input name="blogname" type="text" id="blogname" value="<?php form_option('blogname'); ?>" class="regular-text" /></td>
  150.                             </tr>
  151.                             <tr valign="top">
  152.                             <th scope="row"><label for="blogdescription"><?php _e('Tagline') ?></label></th>
  153.                             <td><input name="blogdescription" type="text" id="blogdescription"  value="<?php form_option('blogdescription'); ?>" class="regular-text" />
  154.                             <span class="description"><?php _e('In a few words, explain what this site is about.') ?></span></td>
  155.                         </tr>
  156.                        
  157.                         <?php
  158.                             break;
  159.                             case 'Privacy':
  160.                         ?>
  161.                         <tr valign="top">
  162.                             <th scope="row"><?php _e('Site Visibility') ?> </th>
  163.                             <td><fieldset><legend class="screen-reader-text"><span><?php _e('Site Visibility') ?> </span></legend>
  164.                             <input id="blog-public" type="radio" name="blog_public" value="1" <?php checked('1', get_option('blog_public')); ?> />
  165.                             <label for="blog-public"><?php _e('I would like my site to be visible to everyone, including search engines (like Google, Bing, Technorati) and archivers');?></label><br/>
  166.                             <input id="blog-norobots" type="radio" name="blog_public" value="0" <?php checked('0', get_option('blog_public')); ?> />
  167.                             <label for="blog-norobots"><?php _e('I would like to block search engines, but allow normal visitors'); ?></label>
  168.                             <?php do_action('blog_privacy_selector'); ?>
  169.                             </fieldset></td>
  170.                         </tr>
  171.                         <?php
  172.                             break;
  173.                         }
  174.                         ?>
  175.                     </table>
  176.                    
  177.                     <p class="submit">
  178.                         <input type="submit" name="Submit" class="button-secondary action" value="Save" />
  179.                     </p>
  180.                 </form>
  181.             </div>
  182.            
  183.             <?php
  184.         }
  185.        
  186.     }
  187.    
  188.     // Instantiate the class
  189.     $editor_options = new editor_options();
  190.  
  191. }
  192. // End class exists statement
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement