Make WordPress Core

Ticket #32470: 32470-interface.patch

File 32470-interface.patch, 7.3 KB (added by jacobsantos, 11 years ago)

32470.4.diff using interfaces

  • wp-includes/widgets.php

     
    1313 * @subpackage Widgets
    1414 */
    1515
     16interface WP_Widget_AdminInterface {
     17
     18        /**
     19         * Update a particular instance.
     20         *
     21         * This function should check that $new_instance is set correctly. The newly-calculated
     22         * value of `$instance` should be returned. If false is returned, the instance won't be
     23         * saved/updated.
     24         *
     25         * @param array $new_instance
     26         * New settings for this instance as input by the user via {@see WP_Widget::form()}.
     27         * @param array $old_instance
     28         * Old settings for this instance.
     29         * @return array
     30         * Settings to save or bool false to cancel saving.
     31         */
     32        public function update( $new_instance, $old_instance );
     33
     34        /**
     35         * Output the settings update form.
     36         *
     37         * @param array $instance Current settings.
     38         * @return string Default return is 'noform'.
     39         */
     40        public function form( $instance );
     41
     42}
     43
     44interface WP_Widget_Display_TitleInterface {
     45
     46        /**
     47         * Return the title of the widget
     48         *
     49         * @param $instance
     50         *
     51         * @return mixed|void
     52         */
     53        public function widget_title( $instance );
     54
     55}
     56
     57interface WP_Widget_DisplayInterface extends WP_Widget_Display_TitleInterface {
     58
     59        /**
     60         * Echo the widget content.
     61         *
     62         * @param array $args
     63         * Display arguments including before_title, after_title, before_widget, and after_widget.
     64         * @param array $instance
     65         * The settings for the particular instance of the widget.
     66         */
     67        public function widget( $args, $instance );
     68
     69}
     70
     71interface WP_Widget_Display_EventInterface extends WP_Widget_Display_TitleInterface {
     72
     73        /**
     74         * Verify settings
     75         *
     76         * Used to determine whether we should render the widget or not.
     77         *
     78         * @param array $instance
     79         * @return bool
     80         */
     81        public function verify_settings( $instance );
     82
     83        /**
     84         * Default treatment of before widget markup
     85         *
     86         * @param $args
     87         * @param $instance
     88         */
     89        public function before_widget( $args, $instance );
     90
     91        /**
     92         * Output the custom markup content for the widget
     93         *
     94         * Subclasses will override this to create their output
     95         */
     96        public function widget_markup( $args, $instance );
     97
     98        /**
     99         * Default treatment of after widget markup
     100         *
     101         * @param $args
     102         * @param $instance
     103         */
     104        public function after_widget( $args, $instance );
     105
     106}
     107
     108interface WP_Widget_SettingInterface {
     109
     110        /**
     111         * @return array
     112         *     array(
     113         *         'id_base' => 'whatever',
     114         *         'name' => 'whatever-name',
     115         *         'widget_options' => array(),
     116         *         'control_options' => array(),
     117         *     )
     118         */
     119        public function settings();
     120
     121}
     122
    16123/**
     124 * Defines the WordPress Widget type.
     125 *
     126 * All widgets must implement this empty interface for type hinting.
     127 */
     128interface WP_Widget_ControlInterface { }
     129
     130/**
     131 * Stub of defaults for implementing interface.
     132 */
     133final class WP_Widget_Admin_Form_Missing implements WP_Widget_AdminInterface {
     134
     135        /**
     136         * @internal Inherits phpdoc from interface.
     137         */
     138        public function update( $new_instance, $old_instance ) {
     139                return $new_instance;
     140        }
     141
     142        /**
     143         * @internal Inherits phpdoc from interface.
     144         */
     145        public function form( $instance ) {
     146                echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
     147                return 'noform';
     148        }
     149}
     150
     151/**
     152 * Stub of widget that displays title and before widget and after widget content.
     153 */
     154final class WP_Widget_Display_Nothing implements WP_Widget_Display_EventInterface {
     155
     156        /**
     157         * @internal Inherits phpdoc from interface.
     158         */
     159        public function verify_settings( $instance ) {
     160                return true;
     161        }
     162
     163        /**
     164         * @internal Inherits phpdoc from interface.
     165         */
     166        public function before_widget( $args, $instance ) {
     167                $title = $this->widget_title( $instance );
     168                echo $args['before_widget'];
     169                if ( $title ) {
     170                        echo $args['before_title'] . $title . $args['after_title'];
     171                }
     172        }
     173
     174        /**
     175         * @internal Inherits phpdoc from interface.
     176         */
     177        public function widget_markup( $args, $instance ) {}
     178
     179        /**
     180         * @internal Inherits phpdoc from interface.
     181         */
     182        public function after_widget( $args, $instance ) {
     183                echo $args['after_widget'];
     184        }
     185
     186        /**
     187         * @internal Inherits phpdoc from interface.
     188         */
     189        public function widget_title( $instance ) {
     190                $id_base = preg_replace( '/(wp_)?widget_/', '', strtolower(get_class($this)) );
     191                if (property_exists($this, 'id_base') ) {
     192                        $id_base = $this->id_base;
     193                } else if ( $this instanceof WP_Widget_SettingInterface ) {
     194                        $settings = $this->settings();
     195                        $id_base = isset($settings['id_base']) ? $settings['id_base'] : $id_base;
     196                }
     197                return widget_title_filter( $instance, $id_base );
     198        }
     199}
     200
     201/**
    17202 * This class must be extended for each widget and WP_Widget::widget(), WP_Widget::update()
    18203 * and WP_Widget::form() need to be over-ridden.
    19204 *
     
    21206 * @subpackage Widgets
    22207 * @since 2.8.0
    23208 */
    24 class WP_Widget {
     209class WP_Widget implements WP_Widget_ControlInterface, WP_Widget_DisplayInterface, WP_Widget_AdminInterface {
    25210
    26211        /**
    27212         * Root ID for all widgets of this type.
     
    89274         */
    90275        public $updated = false;
    91276
     277        /**
     278         *
     279         * @var type
     280         */
     281        public $admin_instance = null;
     282
    92283        // Member functions that you must over-ride.
    93284
    94285        /**
     
    104295         * @param array $instance The settings for the particular instance of the widget.
    105296         */
    106297        public function widget( $args, $instance ) {
    107                 die('function WP_Widget::widget() must be over-ridden in a sub-class.');
     298                if ($this instanceof WP_Widget_Display_EventInterface) {
     299                        //verify that we should be showing the widget
     300                        if ( ! $this->verify_settings( $instance ) ) {
     301                                return;
     302                        }
     303
     304                        $this->before_widget( $args, $instance );
     305                        $this->widget_markup( $args, $instance );
     306                        $this->after_widget( $args, $instance );
     307                } else {
     308                        die('function WP_Widget::widget() must be over-ridden in a sub-class.');
     309                }
    108310        }
    109311
    110312        /**
     313         * Return the title of the widget
     314         *
     315         * @param $instance
     316         *
     317         * @return mixed|void
     318         */
     319        public function widget_title( $instance ) {
     320                return widget_title_filter( $instance, $this->id_base );
     321        }
     322
     323        /**
    111324         * Update a particular instance.
    112325         *
    113326         * This function should check that $new_instance is set correctly. The newly-calculated
     
    123336         * @return array Settings to save or bool false to cancel saving.
    124337         */
    125338        public function update( $new_instance, $old_instance ) {
    126                 return $new_instance;
     339                $obj = $this->admin_instance;
     340                if ( ! ( $obj instanceof WP_Widget_AdminInterface ) ) {
     341                        $obj = new WP_Widget_Admin_Form_Missing;
     342                }
     343                return $obj->update( $new_instance, $old_instance );
    127344        }
    128345
    129346        /**
     
    136353         * @return string Default return is 'noform'.
    137354         */
    138355        public function form($instance) {
    139                 echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
    140                 return 'noform';
     356                $obj = $this->admin_instance;
     357                if ( ! ( $obj instanceof WP_Widget_AdminInterface ) ) {
     358                        $obj = new WP_Widget_Admin_Form_Missing;
     359                }
     360                return $obj->form( $instance );
    141361        }
    142362
    143363        // Functions you'll need to call.
     
    689909
    690910/* Template tags & API functions */
    691911
     912function widget_title_filter($instance, $id_base) {
     913        return apply_filters( 'widget_title', ! isset($instance['title']  ) || empty( $instance['title'] ) ? '' : $instance['title'], $instance, $id_base );
     914}
     915
    692916/**
    693917 * Register a widget
    694918 *