Ticket #32470: 32470.2.diff
| File 32470.2.diff, 11.0 KB (added by , 11 years ago) |
|---|
-
wp-includes/widgets.php
104 104 * @param array $instance The settings for the particular instance of the widget. 105 105 */ 106 106 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 ); 108 110 } 109 111 112 110 113 /** 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 /** 111 146 * Update a particular instance. 112 147 * 113 148 * This function should check that $new_instance is set correctly. The newly-calculated … … 136 171 * @return string Default return is 'noform'. 137 172 */ 138 173 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>'; 140 175 return 'noform'; 141 176 } 142 177 … … 157 192 * for information on accepted arguments. Default empty array. 158 193 */ 159 194 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 ); 161 196 $this->name = $name; 162 197 $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 ) ); 165 200 } 166 201 167 202 /** … … 204 239 return 'widget-' . $this->id_base . '-' . $this->number . '-' . $field_name; 205 240 } 206 241 242 207 243 /** 208 244 * Register all widget instances of this widget class. 209 245 * … … 214 250 $settings = $this->get_settings(); 215 251 $empty = true; 216 252 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 ); 222 258 $empty = false; 223 259 } 224 260 } … … 227 263 if ( $empty ) { 228 264 // If there are none, we register the widget's existence with a 229 265 // generic template 230 $this->_set( 1);266 $this->_set( 1 ); 231 267 $this->_register_one(); 232 268 } 233 269 } … … 247 283 } 248 284 249 285 public function _get_display_callback() { 250 return array( $this, 'display_callback');286 return array( $this, 'display_callback' ); 251 287 } 252 288 253 289 public function _get_update_callback() { 254 return array( $this, 'update_callback');290 return array( $this, 'update_callback' ); 255 291 } 256 292 257 293 public function _get_form_callback() { 258 return array( $this, 'form_callback');294 return array( $this, 'form_callback' ); 259 295 } 260 296 261 297 /** … … 293 329 * @type int $number Number increment used for multiples of the same widget. 294 330 * } 295 331 */ 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 ) ) { 298 334 $widget_args = array( 'number' => $widget_args ); 335 } 299 336 300 337 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) ); 301 338 $this->_set( $widget_args['number'] ); … … 302 339 $instance = $this->get_settings(); 303 340 304 341 if ( array_key_exists( $this->number, $instance ) ) { 305 $instance = $instance[ $this->number];342 $instance = $instance[ $this->number ]; 306 343 307 344 /** 308 345 * Filter the settings for a particular widget instance. … … 342 379 * 343 380 * @param int $deprecated Not used. 344 381 */ 345 public function update_callback( $deprecated = 1 ) {382 final public function update_callback( $deprecated = 1 ) { 346 383 global $wp_registered_widgets; 347 384 348 385 $all_instances = $this->get_settings(); 349 386 350 387 // We need to update the data 351 if ( $this->updated ) 388 if ( $this->updated ) { 352 389 return; 390 } 353 391 354 392 if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) { 355 393 // Delete the settings for this instance of the widget 356 if ( isset( $_POST['the-widget-id']) )394 if ( isset( $_POST['the-widget-id'] ) ) { 357 395 $del_id = $_POST['the-widget-id']; 358 else396 } else { 359 397 return; 398 } 360 399 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']; 363 402 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 } 366 406 } 367 407 } 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] ) ) { 369 409 $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 ) { 371 411 $num = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['widget_number']; 372 412 $settings = array( $num => array() ); 373 413 } else { … … 375 415 } 376 416 377 417 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 ); 380 420 381 $old_instance = isset( $all_instances[$number]) ? $all_instances[$number] : array();421 $old_instance = isset( $all_instances[ $number ]) ? $all_instances[ $number ] : array(); 382 422 383 423 $was_cache_addition_suspended = wp_suspend_cache_addition(); 384 424 if ( $this->is_preview() && ! $was_cache_addition_suspended ) { … … 406 446 */ 407 447 $instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this ); 408 448 if ( false !== $instance ) { 409 $all_instances[ $number] = $instance;449 $all_instances[ $number ] = $instance; 410 450 } 411 451 412 452 break; // run only once … … 413 453 } 414 454 } 415 455 416 $this->save_settings( $all_instances);456 $this->save_settings( $all_instances ); 417 457 $this->updated = true; 418 458 } 419 459 … … 425 465 * 426 466 * @param int|array $widget_args Widget instance number or array of widget arguments. 427 467 */ 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 ) ) { 430 470 $widget_args = array( 'number' => $widget_args ); 471 } 431 472 432 473 $widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) ); 433 474 $all_instances = $this->get_settings(); … … 434 475 435 476 if ( -1 == $widget_args['number'] ) { 436 477 // We echo out a form where 'number' can be set later 437 $this->_set( '__i__');478 $this->_set( '__i__' ); 438 479 $instance = array(); 439 480 } else { 440 $this->_set( $widget_args['number']);481 $this->_set( $widget_args['number'] ); 441 482 $instance = $all_instances[ $widget_args['number'] ]; 442 483 } 443 484 … … 455 496 456 497 $return = null; 457 498 if ( false !== $instance ) { 458 $return = $this->form( $instance);499 $return = $this->form( $instance ); 459 500 460 501 /** 461 502 * Fires at the end of the widget control form. … … 516 557 */ 517 558 public function get_settings() { 518 559 519 $settings = get_option( $this->option_name);560 $settings = get_option( $this->option_name ); 520 561 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 } 523 565 524 if ( ! is_array($settings) )566 if ( ! is_array( $settings ) ) { 525 567 $settings = array(); 568 } 526 569 527 if ( !empty($settings) && !array_key_exists('_multiwidget', $settings) ) { 570 571 if ( ! empty( $settings ) && ! array_key_exists( '_multiwidget', $settings ) ) { 528 572 // 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 ); 530 574 } 531 575 532 576 unset($settings['_multiwidget'], $settings['__i__']); … … 557 601 * @param string $widget_class The name of a {@see WP_Widget} subclass. 558 602 */ 559 603 public function register( $widget_class ) { 560 $this->widgets[ $widget_class] = new $widget_class();604 $this->widgets[ $widget_class ] = new $widget_class(); 561 605 } 562 606 563 607 /** … … 580 624 */ 581 625 public function _register_widgets() { 582 626 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 ); 586 630 587 631 foreach ( $keys as $key ) { 588 632 // 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 ] ); 591 635 continue; 592 636 } 593 637 594 $this->widgets[ $key]->_register();638 $this->widgets[ $key ]->_register(); 595 639 } 596 640 } 597 641 } … … 677 721 function register_widget($widget_class) { 678 722 global $wp_widget_factory; 679 723 680 $wp_widget_factory->register( $widget_class);724 $wp_widget_factory->register( $widget_class ); 681 725 } 682 726 683 727 /** … … 697 741 function unregister_widget($widget_class) { 698 742 global $wp_widget_factory; 699 743 700 $wp_widget_factory->unregister( $widget_class);744 $wp_widget_factory->unregister( $widget_class ); 701 745 } 702 746 703 747 /**