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 |
||
| 19 | class WP_Customize_Panel { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Incremented with each new class instantiation, then stored in $instance_number. |
||
| 23 | * |
||
| 24 | * Used when sorting two instances whose priorities are equal. |
||
| 25 | * |
||
| 26 | * @since 4.1.0 |
||
| 27 | * |
||
| 28 | * @static |
||
| 29 | * @access protected |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | protected static $instance_count = 0; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Order in which this instance was created in relation to other instances. |
||
| 36 | * |
||
| 37 | * @since 4.1.0 |
||
| 38 | * @access public |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | public $instance_number; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * WP_Customize_Manager instance. |
||
| 45 | * |
||
| 46 | * @since 4.0.0 |
||
| 47 | * @access public |
||
| 48 | * @var WP_Customize_Manager |
||
| 49 | */ |
||
| 50 | public $manager; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Unique identifier. |
||
| 54 | * |
||
| 55 | * @since 4.0.0 |
||
| 56 | * @access public |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | public $id; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Priority of the panel, defining the display order of panels and sections. |
||
| 63 | * |
||
| 64 | * @since 4.0.0 |
||
| 65 | * @access public |
||
| 66 | * @var integer |
||
| 67 | */ |
||
| 68 | public $priority = 160; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Capability required for the panel. |
||
| 72 | * |
||
| 73 | * @since 4.0.0 |
||
| 74 | * @access public |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | public $capability = 'edit_theme_options'; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Theme feature support for the panel. |
||
| 81 | * |
||
| 82 | * @since 4.0.0 |
||
| 83 | * @access public |
||
| 84 | * @var string|array |
||
| 85 | */ |
||
| 86 | public $theme_supports = ''; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Title of the panel to show in UI. |
||
| 90 | * |
||
| 91 | * @since 4.0.0 |
||
| 92 | * @access public |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | public $title = ''; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Description to show in the UI. |
||
| 99 | * |
||
| 100 | * @since 4.0.0 |
||
| 101 | * @access public |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | public $description = ''; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Auto-expand a section in a panel when the panel is expanded when the panel only has the one section. |
||
| 108 | * |
||
| 109 | * @since 4.7.4 |
||
| 110 | * @access public |
||
| 111 | * @var bool |
||
| 112 | */ |
||
| 113 | public $auto_expand_sole_section = false; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Customizer sections for this panel. |
||
| 117 | * |
||
| 118 | * @since 4.0.0 |
||
| 119 | * @access public |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | public $sections; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Type of this panel. |
||
| 126 | * |
||
| 127 | * @since 4.1.0 |
||
| 128 | * @access public |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | public $type = 'default'; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Active callback. |
||
| 135 | * |
||
| 136 | * @since 4.1.0 |
||
| 137 | * @access public |
||
| 138 | * |
||
| 139 | * @see WP_Customize_Section::active() |
||
| 140 | * |
||
| 141 | * @var callable Callback is called with one argument, the instance of |
||
| 142 | * WP_Customize_Section, and returns bool to indicate whether |
||
| 143 | * the section is active (such as it relates to the URL currently |
||
| 144 | * being previewed). |
||
| 145 | */ |
||
| 146 | public $active_callback = ''; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Constructor. |
||
| 150 | * |
||
| 151 | * Any supplied $args override class property defaults. |
||
| 152 | * |
||
| 153 | * @since 4.0.0 |
||
| 154 | * |
||
| 155 | * @param WP_Customize_Manager $manager Customizer bootstrap instance. |
||
| 156 | * @param string $id An specific ID for the panel. |
||
| 157 | * @param array $args Panel arguments. |
||
| 158 | */ |
||
| 159 | View Code Duplication | public function __construct( $manager, $id, $args = array() ) { |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Check whether panel is active to current Customizer preview. |
||
| 180 | * |
||
| 181 | * @since 4.1.0 |
||
| 182 | * @access public |
||
| 183 | * |
||
| 184 | * @return bool Whether the panel is active to the current preview. |
||
| 185 | */ |
||
| 186 | final public function active() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Default callback used when invoking WP_Customize_Panel::active(). |
||
| 205 | * |
||
| 206 | * Subclasses can override this with their specific logic, or they may |
||
| 207 | * provide an 'active_callback' argument to the constructor. |
||
| 208 | * |
||
| 209 | * @since 4.1.0 |
||
| 210 | * @access public |
||
| 211 | * |
||
| 212 | * @return bool Always true. |
||
| 213 | */ |
||
| 214 | public function active_callback() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Gather the parameters passed to client JavaScript via JSON. |
||
| 220 | * |
||
| 221 | * @since 4.1.0 |
||
| 222 | * |
||
| 223 | * @return array The array to be exported to the client as JSON. |
||
|
|
|||
| 224 | */ |
||
| 225 | public function json() { |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Checks required user capabilities and whether the theme has the |
||
| 237 | * feature support required by the panel. |
||
| 238 | * |
||
| 239 | * @since 4.0.0 |
||
| 240 | * |
||
| 241 | * @return bool False if theme doesn't support the panel or the user doesn't have the capability. |
||
| 242 | */ |
||
| 243 | View Code Duplication | final public function check_capabilities() { |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Get the panel's content template for insertion into the Customizer pane. |
||
| 257 | * |
||
| 258 | * @since 4.1.0 |
||
| 259 | * |
||
| 260 | * @return string Content for the panel. |
||
| 261 | */ |
||
| 262 | final public function get_content() { |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Check capabilities and render the panel. |
||
| 270 | * |
||
| 271 | * @since 4.0.0 |
||
| 272 | */ |
||
| 273 | final public function maybe_render() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Render the panel container, and then its contents (via `this->render_content()`) in a subclass. |
||
| 302 | * |
||
| 303 | * Panel containers are now rendered in JS by default, see WP_Customize_Panel::print_template(). |
||
| 304 | * |
||
| 305 | * @since 4.0.0 |
||
| 306 | * @access protected |
||
| 307 | */ |
||
| 308 | protected function render() {} |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Render the panel UI in a subclass. |
||
| 312 | * |
||
| 313 | * Panel contents are now rendered in JS by default, see WP_Customize_Panel::print_template(). |
||
| 314 | * |
||
| 315 | * @since 4.1.0 |
||
| 316 | * @access protected |
||
| 317 | */ |
||
| 318 | protected function render_content() {} |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Render the panel's JS templates. |
||
| 322 | * |
||
| 323 | * This function is only run for panel types that have been registered with |
||
| 324 | * WP_Customize_Manager::register_panel_type(). |
||
| 325 | * |
||
| 326 | * @since 4.3.0 |
||
| 327 | * |
||
| 328 | * @see WP_Customize_Manager::register_panel_type() |
||
| 329 | */ |
||
| 330 | public function print_template() { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * An Underscore (JS) template for rendering this panel's container. |
||
| 343 | * |
||
| 344 | * Class variables for this panel class are available in the `data` JS object; |
||
| 345 | * export custom variables by overriding WP_Customize_Panel::json(). |
||
| 346 | * |
||
| 347 | * @see WP_Customize_Panel::print_template() |
||
| 348 | * |
||
| 349 | * @since 4.3.0 |
||
| 350 | * @access protected |
||
| 351 | */ |
||
| 352 | protected function render_template() { |
||
| 363 | |||
| 364 | /** |
||
| 365 | * An Underscore (JS) template for this panel's content (but not its container). |
||
| 366 | * |
||
| 367 | * Class variables for this panel class are available in the `data` JS object; |
||
| 368 | * export custom variables by overriding WP_Customize_Panel::json(). |
||
| 369 | * |
||
| 370 | * @see WP_Customize_Panel::print_template() |
||
| 371 | * |
||
| 372 | * @since 4.3.0 |
||
| 373 | * @access protected |
||
| 374 | */ |
||
| 375 | protected function content_template() { |
||
| 396 | } |
||
| 397 | |||
| 400 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.