| | 16 | interface 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 | |
| | 44 | interface 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 | |
| | 57 | interface 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 | |
| | 71 | interface 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 | |
| | 108 | interface 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 | |
| | 124 | * Defines the WordPress Widget type. |
| | 125 | * |
| | 126 | * All widgets must implement this empty interface for type hinting. |
| | 127 | */ |
| | 128 | interface WP_Widget_ControlInterface { } |
| | 129 | |
| | 130 | /** |
| | 131 | * Stub of defaults for implementing interface. |
| | 132 | */ |
| | 133 | final 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 | */ |
| | 154 | final 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 | /** |
| 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 | } |