WordPress.org

Plugin Directory

Changeset 545656


Ignore:
Timestamp:
05/18/12 07:48:27 (2 years ago)
Author:
cnorris23
Message:

Begin move to using Settings API

Location:
edit-author-slug/trunk/includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • edit-author-slug/trunk/includes/admin-functions.php

    r545252 r545656  
    99 * @author Brandon Allen 
    1010 */ 
     11 
     12/** Nicename ******************************************************************/ 
    1113 
    1214/** 
     
    143145 
    144146/** 
    145  * Add Author Base settings field to 'Permalink' options page. 
    146  * 
    147  * Adds a settings field for Author Base in the 'Optional' settings 
    148  * section along with Category Base and Tag Base. 
    149  * 
    150  * @since 0.4.0 
    151  * 
    152  * @uses add_settings_field() To add the settings field 
    153  */ 
    154 function ba_eas_add_author_base_settings_field() { 
    155     /** 
    156      * Register setting doesn't work on options-permalink.php 
    157      * see Trac ticket #9296 (http://core.trac.wordpress.org/ticket/9296) 
    158      * register_setting( 'ba_edit_author_slug', 'ba_edit_author_slug', array( $this, 'sanitize_author_base' ) ); 
    159      */ 
    160     add_settings_field( 'ba-eas-author-base', __( 'Author Base', 'edit-author-slug' ), 'ba_eas_author_base_settings_html', 'permalink', 'optional' ); 
    161 } 
    162  
    163 /** 
    164  * Add Author Base settings html to Options > Permalinks. 
    165  * 
    166  * Adds a settings field for Author Base in the 'Optional' 
    167  * settings section along with Category Base and Tag Base. 
    168  * 
    169  * @since 0.4.0 
    170  * 
    171  * @uses esc_attr() To sanitize the author base 
    172  */ 
    173 function ba_eas_author_base_settings_html() { 
    174     global $ba_eas; 
    175  
    176     echo '<input id="ba-eas-author-base" name="ba-eas-author-base" type="text" value="' . esc_attr( $ba_eas->author_base ) . '" class="regular-text code" />'; 
    177 } 
    178  
    179 /** 
    180  * Sanitize author base and add to database. 
    181  * 
    182  * This is a workaround until ticket #9296 is resolved 
    183  * (http://core.trac.wordpress.org/ticket/9296) 
    184  * 
    185  * @since 0.8.0 
    186  * 
    187  * @uses check_admin_referer() To verify the nonce and check referer 
    188  * @uses _wp_filter_taxonomy_base() To remove any manually prepended /index.php/. 
    189  * @uses update_option() To update Edit Author Slug options 
    190  * @uses flush_rewrite_rules() To update Edit Author Slug options 
    191  */ 
    192 function ba_eas_sanitize_author_base() { 
    193     global $ba_eas, $wp_rewrite; 
    194  
    195     if ( isset( $_POST['permalink_structure'] ) || isset( $_POST['category_base'] ) ) { 
    196         check_admin_referer( 'update-permalink' ); 
    197  
    198         if ( isset( $_POST['ba-eas-author-base'] ) ) { 
    199             $ba_eas->author_base = trim( $_POST['ba-eas-author-base'] ); 
    200  
    201             // Filer and sanitize the new author_base 
    202             if ( !empty( $ba_eas->author_base ) ) { 
    203                 $ba_eas->author_base = str_replace( '#', '', $ba_eas->author_base     ); 
    204                 $ba_eas->author_base = _wp_filter_taxonomy_base( $ba_eas->author_base ); 
    205             } 
    206  
    207             // Do we need to update the author_base 
    208             if ( $ba_eas->author_base != $ba_eas->original_author_base ) { 
    209                 // Setup the new author_base 
    210                 $ba_eas->options['author_base'] = $ba_eas->author_base; 
    211  
    212                 // Update options with new author_base 
    213                 update_option( 'ba_edit_author_slug', $ba_eas->options ); 
    214  
    215                 // Update the author_base in the WP_Rewrite object 
    216                 if ( !empty( $ba_eas->author_base ) ) 
    217                     $wp_rewrite->author_base = $ba_eas->author_base; 
    218  
    219                 // Courtesy flush 
    220                 flush_rewrite_rules( false ); 
    221             } 
    222         } 
    223     } 
    224 } 
    225  
    226 /** 
    227147 * Add 'Author Slug' column to Users page. 
    228148 * 
     
    270190} 
    271191 
     192/** Author Base **************************************************************/ 
     193 
     194/** 
     195 * Sanitize author base and add to database. 
     196 * 
     197 * This is a workaround until ticket #9296 is resolved 
     198 * (http://core.trac.wordpress.org/ticket/9296) 
     199 * 
     200 * @since 0.8.0 
     201 * 
     202 * @uses check_admin_referer() To verify the nonce and check referer 
     203 * @uses _wp_filter_taxonomy_base() To remove any manually prepended /index.php/. 
     204 * @uses update_option() To update Edit Author Slug options 
     205 * @uses flush_rewrite_rules() To update Edit Author Slug options 
     206 */ 
     207function ba_eas_sanitize_author_base() { 
     208    global $ba_eas, $wp_rewrite; 
     209 
     210    if ( isset( $_POST['permalink_structure'] ) || isset( $_POST['category_base'] ) ) { 
     211        check_admin_referer( 'update-permalink' ); 
     212 
     213        if ( isset( $_POST['ba-eas-author-base'] ) ) { 
     214            $ba_eas->author_base = trim( $_POST['ba-eas-author-base'] ); 
     215 
     216            // Filter and sanitize the new author_base 
     217            if ( !empty( $ba_eas->author_base ) ) { 
     218                $ba_eas->author_base = preg_replace('#/+#', '/', '/' . str_replace( '#', '', $ba_eas->author_base ) ); 
     219                // Add to filter, see option_category_base 
     220                //$ba_eas->author_base = _wp_filter_taxonomy_base( $ba_eas->author_base ); 
     221            } 
     222 
     223            // Do we need to update the author_base 
     224            if ( $ba_eas->author_base != $ba_eas->original_author_base ) { 
     225                // Setup the new author_base 
     226                $ba_eas->options['author_base'] = $ba_eas->author_base; 
     227 
     228                // Update options with new author_base 
     229                update_option( 'ba_edit_author_slug', $ba_eas->options ); 
     230 
     231                // Update the author_base in the WP_Rewrite object 
     232                if ( !empty( $ba_eas->author_base ) ) 
     233                    $wp_rewrite->author_base = $ba_eas->author_base; 
     234 
     235                // Courtesy flush 
     236                flush_rewrite_rules( false ); 
     237            } 
     238        } 
     239    } 
     240} 
     241 
     242/** Settings *****************************************************************/ 
     243 
     244/** 
     245 * Add the Edit Author Slug Settings Menu. 
     246 * 
     247 * @since 0.9.0 
     248 * 
     249 * @uses add_submenu_page() To add the Edit Author Slug submenu 
     250 */ 
     251function ba_eas_add_settings_menu() { 
     252    add_options_page( __( 'Edit Author Slug Settings', 'edit-author-slug' ), __( 'Edit Author Slug', 'edit-author-slug' ), 'edit_users', 'edit-author-slug', 'ba_eas_settings_page_html' ); 
     253} 
     254 
     255/** 
     256 * Output HTML for settings page. 
     257 * 
     258 * @since 0.9.0 
     259 * 
     260 * @uses add_submenu_page() To add the Edit Author Slug submenu 
     261 */ 
     262function ba_eas_settings_page_html() { 
     263?> 
     264 
     265    <div class="wrap"> 
     266 
     267        <?php screen_icon(); ?> 
     268 
     269        <h2><?php _e( 'Edit Author Slug Settings', 'edit-author-slug' ); ?></h2> 
     270 
     271        <form action="options.php" method="post"> 
     272 
     273            <?php settings_fields( 'edit-author-slug' ); ?> 
     274 
     275            <?php do_settings_sections( 'edit-author-slug' ); ?> 
     276 
     277            <p class="submit"> 
     278                <input type="submit" name="submit" class="button-primary" value="<?php _e( 'Save Changes', 'edit-author-slug' ); ?>" /> 
     279            </p> 
     280        </form> 
     281    </div> 
     282 
     283<?php 
     284} 
     285 
     286/** 
     287 * Add Author Base settings field to 'Permalink' options page. 
     288 * 
     289 * Adds a settings field for Author Base in the 'Optional' settings 
     290 * section along with Category Base and Tag Base. 
     291 * 
     292 * @since 0.9.0 
     293 * 
     294 * @uses add_settings_field() To add the settings field 
     295 */ 
     296function ba_eas_register_admin_settings() { 
     297    // Add the Author Base section 
     298    add_settings_section( 'ba_eas_author_base', __( 'Author Base', 'edit-author-slug' ), 'ba_eas_admin_setting_callback_author_base_section', 'edit-author-slug' ); 
     299 
     300    // Author Base setting 
     301    add_settings_field( '_ba_eas_author_base', __( 'Author Base', 'edit-author-slug' ), 'ba_eas_admin_setting_callback_author_base', 'edit-author-slug', 'ba_eas_author_base' ); 
     302    register_setting( 'edit-author-slug', '_ba_eas_author_base', 'ba_eas_sanitize_author_base' ); 
     303} 
     304 
     305/** 
     306 * Add Author Base settings section. 
     307 * 
     308 * @since 0.9.0 
     309 */ 
     310function ba_eas_admin_setting_callback_author_base_section() { 
     311?> 
     312 
     313    <p><?php _e( "Change your author base to something more fun. <em>Defaults to 'author'</em>.", 'edit-author-slug' ); ?></p> 
     314 
     315<?php 
     316} 
     317 
     318/** 
     319 * Add Author Base settings field. 
     320 * 
     321 * @since 0.9.0 
     322 * 
     323 * @uses esc_attr() To sanitize the author base 
     324 */ 
     325function ba_eas_admin_setting_callback_author_base() { 
     326    global $ba_eas; 
     327?> 
     328 
     329    <input id="_ba_eas_author_base" name="_ba_eas_author_base" type="text" value="<?php esc_attr( $ba_eas->author_base ); ?>" class="regular-text code" /> 
     330 
     331<?php 
     332} 
     333 
     334/** 
     335 * Output settings API option 
     336 * 
     337 * @since 0.9.0 
     338 * 
     339 * @uses ba_eas_get_form_option() 
     340 * 
     341 * @param string $option 
     342 * @param string $default 
     343 * @param bool $slug 
     344 */ 
     345function ba_eas_form_option( $option, $default = '' , $slug = false ) { 
     346    echo ba_eas_get_form_option( $option, $default, $slug ); 
     347} 
     348 
     349/** 
     350 * Return settings API option 
     351 * 
     352 * @since 0.9.0 
     353 * 
     354 * @uses get_option() 
     355 * @uses esc_attr() 
     356 * @uses apply_filters() 
     357 * 
     358 * @param string $option 
     359 * @param string $default 
     360 * @param bool $slug 
     361 */ 
     362function ba_eas_get_form_option( $option, $default = '', $slug = false ) { 
     363 
     364    // Get the option and sanitize it 
     365    $value = get_option( $option, $default ); 
     366 
     367    // Slug? 
     368    if ( true === $slug ) 
     369        $value = esc_attr( apply_filters( 'editable_slug', $value ) ); 
     370 
     371    // Not a slug 
     372    else 
     373        $value = esc_attr( $value ); 
     374 
     375    // Fallback to default 
     376    if ( empty( $value ) ) 
     377        $value = $default; 
     378 
     379    // Allow plugins to further filter the output 
     380    return apply_filters( 'ba_eas_get_form_option', $value, $option ); 
     381} 
     382 
     383/** Upgrade *******************************************************************/ 
     384 
    272385/** 
    273386 * Upgrade Edit Author Slug. 
     
    299412} 
    300413 
    301 /** 
    302  * Show the message warning users using WP 3.1.2 or less that they need 
    303  * to upgrade WP if they want to enjoy the tasty goodness that will start 
    304  * pouring out in 0.9+. 
    305  * 
    306  * @since 0.8.0 
    307  * 
    308  * @global $wp_list_table Edit Author Slug object 
    309  * @uses esc_url() To sanitize the info link 
    310  * @uses esc_attr() To sanitize the info title 
    311  */ 
    312 function ba_eas_eol_for_less_than_wp_3_2_message() { 
    313     if ( version_compare( $GLOBALS['wp_version'], '3.1.4', '>' ) ) 
    314         return; 
    315  
    316     global $wp_list_table; 
    317  
    318     $details_url = 'http://brandonallen.org/2011/12/15/edit-author-slug-0-8-and-beyond/'; 
    319     $details_title = 'Edit Author Slug 0.8 and Beyond'; 
    320  
    321     echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">'; 
    322  
    323     printf( __( 'Version 0.8 is the last update of Edit Author Slug that will officially support WordPress 3.1.4 or less. Please consider upgrading to the latest version of WordPress if you\'d like to take advantage of upcoming features. <a href="%1$s" title="%2$s">View more information about this notice</a>.', 'edit-author-slug' ), esc_url( $details_url ), esc_attr( $details_title ) ); 
    324  
    325     echo '</div></td></tr>'; 
    326  
    327 } 
    328  
    329414?> 
  • edit-author-slug/trunk/includes/hooks.php

    r471484 r545656  
    1212// Admin 
    1313if ( is_admin() ) { 
    14     // Author base settings 
    15     add_action( 'admin_init', 'ba_eas_sanitize_author_base'           ); 
    16     add_action( 'admin_init', 'ba_eas_add_author_base_settings_field' ); 
    1714 
    1815    // Cleanup options array 
     
    2825    add_filter( 'manage_users_custom_column', 'ba_eas_author_slug_custom_column', 10, 3 ); 
    2926 
    30     // WP < 3.2 EOL message 
    31     add_action( 'after_plugin_row_' . $this->plugin_basename, 'ba_eas_eol_for_less_than_wp_3_2_message' ); 
     27    // Settings 
     28    add_action( 'admin_menu', 'ba_eas_add_settings_menu' ); 
     29    add_action( 'admin_init', 'ba_eas_register_admin_settings'        ); 
    3230 
    3331} 
Note: See TracChangeset for help on using the changeset viewer.