Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 15 | class Google_Translate_Widget extends WP_Widget { |
||
| 16 | static $instance = null; |
||
|
|
|||
| 17 | |||
| 18 | /** |
||
| 19 | * Default widget title. |
||
| 20 | * |
||
| 21 | * @var string $default_title |
||
| 22 | */ |
||
| 23 | var $default_title; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Register widget with WordPress. |
||
| 27 | */ |
||
| 28 | View Code Duplication | function __construct() { |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Enqueue frontend JS scripts. |
||
| 45 | */ |
||
| 46 | public function enqueue_scripts() { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Display the Widget. |
||
| 56 | * |
||
| 57 | * @see WP_Widget::widget() |
||
| 58 | * |
||
| 59 | * @param array $args Display arguments. |
||
| 60 | * @param array $instance The settings for the particular instance of the widget. |
||
| 61 | */ |
||
| 62 | public function widget( $args, $instance ) { |
||
| 63 | // We never should show more than 1 instance of this. |
||
| 64 | if ( null === self::$instance ) { |
||
| 65 | wp_localize_script( 'google-translate-init', '_wp_google_translate_widget', array( 'lang' => get_locale() ) ); |
||
| 66 | wp_enqueue_script( 'google-translate-init' ); |
||
| 67 | wp_enqueue_script( 'google-translate' ); |
||
| 68 | |||
| 69 | $title = $instance['title']; |
||
| 70 | |||
| 71 | if ( false === $title ) { |
||
| 72 | $title = $this->default_title; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
||
| 76 | $title = apply_filters( 'widget_title', $title ); |
||
| 77 | |||
| 78 | echo $args['before_widget']; |
||
| 79 | View Code Duplication | if ( ! empty( $title ) ) { |
|
| 80 | echo $args['before_title'] . esc_html( $title ) . $args['after_title']; |
||
| 81 | } |
||
| 82 | echo '<div id="google_translate_element"></div>'; |
||
| 83 | echo $args['after_widget']; |
||
| 84 | self::$instance = $instance; |
||
| 85 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
||
| 86 | do_action( 'jetpack_stats_extra', 'widget_view', 'google-translate' ); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Widget form in the dashboard. |
||
| 92 | * |
||
| 93 | * @see WP_Widget::form() |
||
| 94 | * |
||
| 95 | * @param array $instance Previously saved values from database. |
||
| 96 | */ |
||
| 97 | public function form( $instance ) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Sanitize widget form values as they are saved. |
||
| 112 | * |
||
| 113 | * @see WP_Widget::update() |
||
| 114 | * |
||
| 115 | * @param array $new_instance Values just sent to be saved. |
||
| 116 | * @param array $old_instance Previously saved values from database. |
||
| 117 | * |
||
| 118 | * @return array $instance Updated safe values to be saved. |
||
| 119 | */ |
||
| 120 | View Code Duplication | public function update( $new_instance, $old_instance ) { |
|
| 128 | |||
| 129 | } |
||
| 130 | |||
| 138 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.