Make WordPress Core

Ticket #32470: 32470.2.diff

File 32470.2.diff, 11.0 KB (added by welcher, 11 years ago)

Corrected diff

  • wp-includes/widgets.php

     
    104104         * @param array $instance The settings for the particular instance of the widget.
    105105         */
    106106        public function widget( $args, $instance ) {
    107                 die('function WP_Widget::widget() must be over-ridden in a sub-class.');
     107                $this->before_widget( $args, $instance );
     108                $this->widget_markup();
     109                $this->after_widget( $args, $instance );
    108110        }
    109111
     112
    110113        /**
     114         * Default treatment of before widget markup
     115         *
     116         * @param $args
     117         * @param $instance
     118         */
     119        public function before_widget( $args, $instance ) {
     120
     121                $title = apply_filters( 'widget_title', ! isset($instance['title']  ) || empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
     122
     123                echo $args['before_widget'];
     124                if ( $title ) {
     125                        echo $args['before_title'] . $title . $args['after_title'];
     126                }
     127        }
     128
     129        /**
     130         * Output the custom markup content for the widget
     131         *
     132         * Subclasses will override this to create their output
     133         */
     134        public function widget_markup() {}
     135
     136        /**
     137         * Default treatment of after widget markup
     138         * @param $args
     139         * @param $instance
     140         */
     141        public function after_widget( $args, $instance ) {
     142                echo $args['after_widget'];
     143        }
     144
     145        /**
    111146         * Update a particular instance.
    112147         *
    113148         * This function should check that $new_instance is set correctly. The newly-calculated
     
    136171         * @return string Default return is 'noform'.
    137172         */
    138173        public function form($instance) {
    139                 echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
     174                echo '<p class="no-options-widget">' . esc_html__( 'There are no options for this widget.' ) . '</p>';
    140175                return 'noform';
    141176        }
    142177
     
    157192         *                                for information on accepted arguments. Default empty array.
    158193         */
    159194        public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
    160                 $this->id_base = empty($id_base) ? preg_replace( '/(wp_)?widget_/', '', strtolower(get_class($this)) ) : strtolower($id_base);
     195                $this->id_base = empty( $id_base ) ? preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) ) : strtolower( $id_base );
    161196                $this->name = $name;
    162197                $this->option_name = 'widget_' . $this->id_base;
    163                 $this->widget_options = wp_parse_args( $widget_options, array('classname' => $this->option_name) );
    164                 $this->control_options = wp_parse_args( $control_options, array('id_base' => $this->id_base) );
     198                $this->widget_options = wp_parse_args( $widget_options, array( 'classname' => $this->option_name ) );
     199                $this->control_options = wp_parse_args( $control_options, array( 'id_base' => $this->id_base ) );
    165200        }
    166201
    167202        /**
     
    204239                return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name;
    205240        }
    206241
     242
    207243        /**
    208244         * Register all widget instances of this widget class.
    209245         *
     
    214250                $settings = $this->get_settings();
    215251                $empty = true;
    216252
    217                 if ( is_array($settings) ) {
    218                         foreach ( array_keys($settings) as $number ) {
    219                                 if ( is_numeric($number) ) {
    220                                         $this->_set($number);
    221                                         $this->_register_one($number);
     253                if ( is_array( $settings ) ) {
     254                        foreach ( array_keys( $settings ) as $number ) {
     255                                if ( is_numeric( $number ) ) {
     256                                        $this->_set( $number );
     257                                        $this->_register_one( $number );
    222258                                        $empty = false;
    223259                                }
    224260                        }
     
    227263                if ( $empty ) {
    228264                        // If there are none, we register the widget's existence with a
    229265                        // generic template
    230                         $this->_set(1);
     266                        $this->_set( 1 );
    231267                        $this->_register_one();
    232268                }
    233269        }
     
    247283        }
    248284
    249285        public function _get_display_callback() {
    250                 return array($this, 'display_callback');
     286                return array( $this, 'display_callback' );
    251287        }
    252288
    253289        public function _get_update_callback() {
    254                 return array($this, 'update_callback');
     290                return array( $this, 'update_callback' );
    255291        }
    256292
    257293        public function _get_form_callback() {
    258                 return array($this, 'form_callback');
     294                return array( $this, 'form_callback' );
    259295        }
    260296
    261297        /**
     
    293329         *     @type int $number Number increment used for multiples of the same widget.
    294330         * }
    295331         */
    296         public function display_callback( $args, $widget_args = 1 ) {
    297                 if ( is_numeric($widget_args) )
     332        final public function display_callback( $args, $widget_args = 1 ) {
     333                if ( is_numeric( $widget_args ) ) {
    298334                        $widget_args = array( 'number' => $widget_args );
     335                }
    299336
    300337                $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    301338                $this->_set( $widget_args['number'] );
     
    302339                $instance = $this->get_settings();
    303340
    304341                if ( array_key_exists( $this->number, $instance ) ) {
    305                         $instance = $instance[$this->number];
     342                        $instance = $instance[ $this->number ];
    306343
    307344                        /**
    308345                         * Filter the settings for a particular widget instance.
     
    342379         *
    343380         * @param int $deprecated Not used.
    344381         */
    345         public function update_callback( $deprecated = 1 ) {
     382        final public function update_callback( $deprecated = 1 ) {
    346383                global $wp_registered_widgets;
    347384
    348385                $all_instances = $this->get_settings();
    349386
    350387                // We need to update the data
    351                 if ( $this->updated )
     388                if ( $this->updated ) {
    352389                        return;
     390                }
    353391
    354392                if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
    355393                        // Delete the settings for this instance of the widget
    356                         if ( isset($_POST['the-widget-id']) )
     394                        if ( isset( $_POST['the-widget-id'] ) ) {
    357395                                $del_id = $_POST['the-widget-id'];
    358                         else
     396                        } else {
    359397                                return;
     398                        }
    360399
    361                         if ( isset($wp_registered_widgets[$del_id]['params'][0]['number']) ) {
    362                                 $number = $wp_registered_widgets[$del_id]['params'][0]['number'];
     400                        if ( isset( $wp_registered_widgets[ $del_id ]['params'][0]['number']) ) {
     401                                $number = $wp_registered_widgets[ $del_id ]['params'][0]['number'];
    363402
    364                                 if ( $this->id_base . '-' . $number == $del_id )
    365                                         unset($all_instances[$number]);
     403                                if ( $this->id_base . '-' . $number == $del_id ) {
     404                                        unset( $all_instances[ $number ] );
     405                                }
    366406                        }
    367407                } else {
    368                         if ( isset($_POST['widget-' . $this->id_base]) && is_array($_POST['widget-' . $this->id_base]) ) {
     408                        if ( isset( $_POST['widget-' . $this->id_base] ) && is_array( $_POST['widget-' . $this->id_base] ) ) {
    369409                                $settings = $_POST['widget-' . $this->id_base];
    370                         } elseif ( isset($_POST['id_base']) && $_POST['id_base'] == $this->id_base ) {
     410                        } elseif ( isset( $_POST['id_base'] ) && $_POST['id_base'] == $this->id_base ) {
    371411                                $num = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['widget_number'];
    372412                                $settings = array( $num => array() );
    373413                        } else {
     
    375415                        }
    376416
    377417                        foreach ( $settings as $number => $new_instance ) {
    378                                 $new_instance = stripslashes_deep($new_instance);
    379                                 $this->_set($number);
     418                                $new_instance = stripslashes_deep( $new_instance );
     419                                $this->_set( $number );
    380420
    381                                 $old_instance = isset($all_instances[$number]) ? $all_instances[$number] : array();
     421                                $old_instance = isset( $all_instances[ $number ]) ? $all_instances[ $number ] : array();
    382422
    383423                                $was_cache_addition_suspended = wp_suspend_cache_addition();
    384424                                if ( $this->is_preview() && ! $was_cache_addition_suspended ) {
     
    406446                                 */
    407447                                $instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this );
    408448                                if ( false !== $instance ) {
    409                                         $all_instances[$number] = $instance;
     449                                        $all_instances[ $number ] = $instance;
    410450                                }
    411451
    412452                                break; // run only once
     
    413453                        }
    414454                }
    415455
    416                 $this->save_settings($all_instances);
     456                $this->save_settings( $all_instances );
    417457                $this->updated = true;
    418458        }
    419459
     
    425465         *
    426466         * @param int|array $widget_args Widget instance number or array of widget arguments.
    427467         */
    428         public function form_callback( $widget_args = 1 ) {
    429                 if ( is_numeric($widget_args) )
     468        final public function form_callback( $widget_args = 1 ) {
     469                if ( is_numeric( $widget_args ) ) {
    430470                        $widget_args = array( 'number' => $widget_args );
     471                }
    431472
    432473                $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
    433474                $all_instances = $this->get_settings();
     
    434475
    435476                if ( -1 == $widget_args['number'] ) {
    436477                        // We echo out a form where 'number' can be set later
    437                         $this->_set('__i__');
     478                        $this->_set( '__i__' );
    438479                        $instance = array();
    439480                } else {
    440                         $this->_set($widget_args['number']);
     481                        $this->_set( $widget_args['number'] );
    441482                        $instance = $all_instances[ $widget_args['number'] ];
    442483                }
    443484
     
    455496
    456497                $return = null;
    457498                if ( false !== $instance ) {
    458                         $return = $this->form($instance);
     499                        $return = $this->form( $instance );
    459500
    460501                        /**
    461502                         * Fires at the end of the widget control form.
     
    516557         */
    517558        public function get_settings() {
    518559
    519                 $settings = get_option($this->option_name);
     560                $settings = get_option( $this->option_name );
    520561
    521                 if ( false === $settings && isset($this->alt_option_name) )
    522                         $settings = get_option($this->alt_option_name);
     562                if ( false === $settings && isset( $this->alt_option_name ) ) {
     563                        $settings = get_option( $this->alt_option_name );
     564                }
    523565
    524                 if ( !is_array($settings) )
     566                if ( ! is_array( $settings ) ) {
    525567                        $settings = array();
     568                }
    526569
    527                 if ( !empty($settings) && !array_key_exists('_multiwidget', $settings) ) {
     570
     571                if ( ! empty( $settings ) && ! array_key_exists( '_multiwidget', $settings ) ) {
    528572                        // old format, convert if single widget
    529                         $settings = wp_convert_widget_settings($this->id_base, $this->option_name, $settings);
     573                        $settings = wp_convert_widget_settings( $this->id_base, $this->option_name, $settings );
    530574                }
    531575
    532576                unset($settings['_multiwidget'], $settings['__i__']);
     
    557601         * @param string $widget_class The name of a {@see WP_Widget} subclass.
    558602         */
    559603        public function register( $widget_class ) {
    560                 $this->widgets[$widget_class] = new $widget_class();
     604                $this->widgets[ $widget_class ] = new $widget_class();
    561605        }
    562606
    563607        /**
     
    580624         */
    581625        public function _register_widgets() {
    582626                global $wp_registered_widgets;
    583                 $keys = array_keys($this->widgets);
    584                 $registered = array_keys($wp_registered_widgets);
    585                 $registered = array_map('_get_widget_id_base', $registered);
     627                $keys = array_keys( $this->widgets );
     628                $registered = array_keys( $wp_registered_widgets );
     629                $registered = array_map( '_get_widget_id_base', $registered );
    586630
    587631                foreach ( $keys as $key ) {
    588632                        // don't register new widget if old widget with the same id is already registered
    589                         if ( in_array($this->widgets[$key]->id_base, $registered, true) ) {
    590                                 unset($this->widgets[$key]);
     633                        if ( in_array( $this->widgets[ $key ]->id_base, $registered, true ) ) {
     634                                unset( $this->widgets[ $key ] );
    591635                                continue;
    592636                        }
    593637
    594                         $this->widgets[$key]->_register();
     638                        $this->widgets[ $key ]->_register();
    595639                }
    596640        }
    597641}
     
    677721function register_widget($widget_class) {
    678722        global $wp_widget_factory;
    679723
    680         $wp_widget_factory->register($widget_class);
     724        $wp_widget_factory->register( $widget_class );
    681725}
    682726
    683727/**
     
    697741function unregister_widget($widget_class) {
    698742        global $wp_widget_factory;
    699743
    700         $wp_widget_factory->unregister($widget_class);
     744        $wp_widget_factory->unregister( $widget_class );
    701745}
    702746
    703747/**