WordPress.org

Make WordPress Core

Ticket #18285: 16413.class-wp-settings-page.diff

File 16413.class-wp-settings-page.diff, 8.5 KB (added by scribu, 7 years ago)

Basic wrapper class + media settings page rework, as an example

  • wp-admin/includes/template.php

     
    13361336        echo $output;
    13371337}
    13381338
     1339class WP_Settings_Page {
     1340        protected $settings_group;
     1341
     1342        function add_section( $section_id, $section_title, $fields = array() ) {
     1343                if ( method_exists( $this, 'section_' . $section_id ) )
     1344                        $callback = array( $this, 'section_' . $section_id );
     1345                else
     1346                        $callback = '__return_false';
     1347
     1348                add_settings_section( $section_id, $section_title, $callback, $this->settings_group );
     1349
     1350                foreach ( $fields as $id => $title ) {
     1351                        $this->add_field( $id, $title, $section_id );
     1352                }
     1353        }
     1354
     1355        function add_field( $id, $title, $section ) {
     1356                add_settings_field( $id, $title, array( $this, 'field_' . $id ), $this->settings_group, $section );
     1357        }
     1358}
     1359
    13391360/**
    13401361 * {@internal Missing Short Description}}
    13411362 *
  • wp-admin/options-media.php

     
    2727
    2828include('./admin-header.php');
    2929
    30 ?>
     30class WP_Settings_Media extends WP_Settings_Page {
     31        protected $settings_group = 'media';
    3132
    32 <div class="wrap">
    33 <?php screen_icon(); ?>
    34 <h2><?php echo esc_html( $title ); ?></h2>
     33        function __construct() {
     34                $this->add_section( 'media', __( 'Image sizes' ), array(
     35                        'thumbnail_size' => __( 'Thumbnail size' ),
     36                        'medium_size' => __( 'Medium size' ),
     37                        'large_size' => __( 'Large size' ),
     38                ) );
    3539
    36 <form action="options.php" method="post">
    37 <?php settings_fields('media'); ?>
     40                $this->add_section( 'embeds', __( 'Embeds' ), array(
     41                        'auto_embeds' => __( 'Auto-embeds' ),
     42                        'maximum_embed_size' => __( 'Maximum embed size' ),
     43                ) );
    3844
    39 <h3><?php _e('Image sizes') ?></h3>
    40 <p><?php _e('The sizes listed below determine the maximum dimensions in pixels to use when inserting an image into the body of a post.'); ?></p>
     45                if ( !is_multisite() ) {
     46                        $this->add_section( 'uploads', __( 'Uploading files' ), array(
     47                                'upload_path' => __( 'Store uploads in this folder' ),
     48                                'upload_url_path' => __( 'Full URL path to files' ),
     49                                'upload_folders' => __( 'Uploads organization' ),
     50                        ) );
     51                }
     52        }
    4153
    42 <table class="form-table">
    43 <tr valign="top">
    44 <th scope="row"><?php _e('Thumbnail size') ?></th>
    45 <td>
     54
     55        function section_media() {
     56?>
     57        <p><?php _e( 'The sizes listed below determine the maximum dimensions in pixels to use when inserting an image into the body of a post.' ); ?></p>
     58<?php
     59        }
     60
     61        function field_thumbnail_size() {
     62?>
    4663<label for="thumbnail_size_w"><?php _e('Width'); ?></label>
    4764<input name="thumbnail_size_w" type="text" id="thumbnail_size_w" value="<?php form_option('thumbnail_size_w'); ?>" class="small-text" />
    4865<label for="thumbnail_size_h"><?php _e('Height'); ?></label>
    4966<input name="thumbnail_size_h" type="text" id="thumbnail_size_h" value="<?php form_option('thumbnail_size_h'); ?>" class="small-text" /><br />
    5067<input name="thumbnail_crop" type="checkbox" id="thumbnail_crop" value="1" <?php checked('1', get_option('thumbnail_crop')); ?>/>
    5168<label for="thumbnail_crop"><?php _e('Crop thumbnail to exact dimensions (normally thumbnails are proportional)'); ?></label>
    52 </td>
    53 </tr>
     69<?php
     70        }
    5471
    55 <tr valign="top">
    56 <th scope="row"><?php _e('Medium size') ?></th>
    57 <td><fieldset><legend class="screen-reader-text"><span><?php _e('Medium size'); ?></span></legend>
     72        function field_medium_size() {
     73?>
     74<fieldset><legend class="screen-reader-text"><span><?php _e('Medium size'); ?></span></legend>
    5875<label for="medium_size_w"><?php _e('Max Width'); ?></label>
    5976<input name="medium_size_w" type="text" id="medium_size_w" value="<?php form_option('medium_size_w'); ?>" class="small-text" />
    6077<label for="medium_size_h"><?php _e('Max Height'); ?></label>
    6178<input name="medium_size_h" type="text" id="medium_size_h" value="<?php form_option('medium_size_h'); ?>" class="small-text" />
    62 </fieldset></td>
    63 </tr>
     79</fieldset>
     80<?php
     81        }
    6482
    65 <tr valign="top">
    66 <th scope="row"><?php _e('Large size') ?></th>
    67 <td><fieldset><legend class="screen-reader-text"><span><?php _e('Large size'); ?></span></legend>
     83        function field_large_size() {
     84?>
     85<fieldset><legend class="screen-reader-text"><span><?php _e('Large size'); ?></span></legend>
    6886<label for="large_size_w"><?php _e('Max Width'); ?></label>
    6987<input name="large_size_w" type="text" id="large_size_w" value="<?php form_option('large_size_w'); ?>" class="small-text" />
    7088<label for="large_size_h"><?php _e('Max Height'); ?></label>
    7189<input name="large_size_h" type="text" id="large_size_h" value="<?php form_option('large_size_h'); ?>" class="small-text" />
    72 </fieldset></td>
    73 </tr>
     90</fieldset>
     91<?php
     92        }
    7493
    75 <?php do_settings_fields('media', 'default'); ?>
    76 </table>
    77 
    78 <h3><?php _e('Embeds') ?></h3>
    79 
    80 <table class="form-table">
    81 
    82 <tr valign="top">
    83 <th scope="row"><?php _e('Auto-embeds'); ?></th>
    84 <td><fieldset><legend class="screen-reader-text"><span><?php _e('When possible, embed the media content from a URL directly onto the page. For example: links to Flickr and YouTube.'); ?></span></legend>
     94        function field_auto_embeds() {
     95?>
     96<fieldset><legend class="screen-reader-text"><span><?php _e('When possible, embed the media content from a URL directly onto the page. For example: links to Flickr and YouTube.'); ?></span></legend>
    8597<label for="embed_autourls"><input name="embed_autourls" type="checkbox" id="embed_autourls" value="1" <?php checked( '1', get_option('embed_autourls') ); ?>/> <?php _e('When possible, embed the media content from a URL directly onto the page. For example: links to Flickr and YouTube.'); ?></label>
    86 </fieldset></td>
    87 </tr>
     98</fieldset>
     99<?php
     100        }
    88101
    89 <tr valign="top">
    90 <th scope="row"><?php _e('Maximum embed size') ?></th>
    91 <td>
     102        function field_maximum_embed_size() {
     103?>
    92104<label for="embed_size_w"><?php _e('Width'); ?></label>
    93105<input name="embed_size_w" type="text" id="embed_size_w" value="<?php form_option('embed_size_w'); ?>" class="small-text" />
    94106<label for="embed_size_h"><?php _e('Height'); ?></label>
    95107<input name="embed_size_h" type="text" id="embed_size_h" value="<?php form_option('embed_size_h'); ?>" class="small-text" />
    96108<?php if ( !empty($content_width) ) echo '<br />' . __("If the width value is left blank, embeds will default to the max width of your theme."); ?>
    97 </td>
    98 </tr>
     109<?php
     110        }
    99111
    100 <?php do_settings_fields('media', 'embeds'); ?>
    101 </table>
    102 
    103 <?php if ( !is_multisite() ) : ?>
    104 <h3><?php _e('Uploading Files'); ?></h3>
    105 <table class="form-table">
    106 <tr valign="top">
    107 <th scope="row"><label for="upload_path"><?php _e('Store uploads in this folder'); ?></label></th>
    108 <td><input name="upload_path" type="text" id="upload_path" value="<?php echo esc_attr(get_option('upload_path')); ?>" class="regular-text code" />
     112        function field_upload_path() {
     113?>
     114<input name="upload_path" type="text" id="upload_path" value="<?php echo esc_attr(get_option('upload_path')); ?>" class="regular-text code" />
    109115<span class="description"><?php _e('Default is <code>wp-content/uploads</code>'); ?></span>
    110 </td>
    111 </tr>
     116<?php
     117        }
    112118
    113 <tr valign="top">
    114 <th scope="row"><label for="upload_url_path"><?php _e('Full URL path to files'); ?></label></th>
    115 <td><input name="upload_url_path" type="text" id="upload_url_path" value="<?php echo esc_attr( get_option('upload_url_path')); ?>" class="regular-text code" />
     119        function field_upload_url_path() {
     120?>
     121<input name="upload_url_path" type="text" id="upload_url_path" value="<?php echo esc_attr( get_option('upload_url_path')); ?>" class="regular-text code" />
    116122<span class="description"><?php _e('Configuring this is optional. By default, it should be blank.'); ?></span>
    117 </td>
    118 </tr>
     123<?php
     124        }
    119125
    120 <tr>
    121 <th scope="row" colspan="2" class="th-full">
     126        function field_upload_folders() {
     127?>
    122128<label for="uploads_use_yearmonth_folders">
    123129<input name="uploads_use_yearmonth_folders" type="checkbox" id="uploads_use_yearmonth_folders" value="1"<?php checked('1', get_option('uploads_use_yearmonth_folders')); ?> />
    124130<?php _e('Organize my uploads into month- and year-based folders'); ?>
    125 </label>
    126 </th>
    127 </tr>
     131<?php
     132        }
     133}
    128134
    129 <?php do_settings_fields('media', 'uploads'); ?>
    130 </table>
    131 <?php endif; ?>
     135$wp_settings_page = new WP_Settings_Media;
     136?>
    132137
     138<div class="wrap">
     139<?php screen_icon(); ?>
     140<h2><?php echo esc_html( $title ); ?></h2>
     141
     142<form action="options.php" method="post">
     143<?php settings_fields('media'); ?>
    133144<?php do_settings_sections('media'); ?>
    134145
    135146<?php submit_button(); ?>
    136147
    137148</form>
    138 
    139149</div>
    140150
    141151<?php include('./admin-footer.php'); ?>