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_Section { |
||
| 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 3.4.0 |
||
| 47 | * @access public |
||
| 48 | * @var WP_Customize_Manager |
||
| 49 | */ |
||
| 50 | public $manager; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Unique identifier. |
||
| 54 | * |
||
| 55 | * @since 3.4.0 |
||
| 56 | * @access public |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | public $id; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Priority of the section which informs load order of sections. |
||
| 63 | * |
||
| 64 | * @since 3.4.0 |
||
| 65 | * @access public |
||
| 66 | * @var integer |
||
| 67 | */ |
||
| 68 | public $priority = 160; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Panel in which to show the section, making it a sub-section. |
||
| 72 | * |
||
| 73 | * @since 4.0.0 |
||
| 74 | * @access public |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | public $panel = ''; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Capability required for the section. |
||
| 81 | * |
||
| 82 | * @since 3.4.0 |
||
| 83 | * @access public |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | public $capability = 'edit_theme_options'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Theme feature support for the section. |
||
| 90 | * |
||
| 91 | * @since 3.4.0 |
||
| 92 | * @access public |
||
| 93 | * @var string|array |
||
| 94 | */ |
||
| 95 | public $theme_supports = ''; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Title of the section to show in UI. |
||
| 99 | * |
||
| 100 | * @since 3.4.0 |
||
| 101 | * @access public |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | public $title = ''; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Description to show in the UI. |
||
| 108 | * |
||
| 109 | * @since 3.4.0 |
||
| 110 | * @access public |
||
| 111 | * @var string |
||
| 112 | */ |
||
| 113 | public $description = ''; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Customizer controls for this section. |
||
| 117 | * |
||
| 118 | * @since 3.4.0 |
||
| 119 | * @access public |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | public $controls; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Type of this section. |
||
| 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 | * Show the description or hide it behind the help icon. |
||
| 150 | * |
||
| 151 | * @since 4.7.0 |
||
| 152 | * @access public |
||
| 153 | * |
||
| 154 | * @var bool Indicates whether the Section's description should be |
||
| 155 | * hidden behind a help icon ("?") in the Section header, |
||
| 156 | * similar to how help icons are displayed on Panels. |
||
| 157 | */ |
||
| 158 | public $description_hidden = false; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Constructor. |
||
| 162 | * |
||
| 163 | * Any supplied $args override class property defaults. |
||
| 164 | * |
||
| 165 | * @since 3.4.0 |
||
| 166 | * |
||
| 167 | * @param WP_Customize_Manager $manager Customizer bootstrap instance. |
||
| 168 | * @param string $id An specific ID of the section. |
||
| 169 | * @param array $args Section arguments. |
||
| 170 | */ |
||
| 171 | View Code Duplication | public function __construct( $manager, $id, $args = array() ) { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Check whether section is active to current Customizer preview. |
||
| 192 | * |
||
| 193 | * @since 4.1.0 |
||
| 194 | * @access public |
||
| 195 | * |
||
| 196 | * @return bool Whether the section is active to the current preview. |
||
| 197 | */ |
||
| 198 | final public function active() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Default callback used when invoking WP_Customize_Section::active(). |
||
| 217 | * |
||
| 218 | * Subclasses can override this with their specific logic, or they may provide |
||
| 219 | * an 'active_callback' argument to the constructor. |
||
| 220 | * |
||
| 221 | * @since 4.1.0 |
||
| 222 | * @access public |
||
| 223 | * |
||
| 224 | * @return true Always true. |
||
|
|
|||
| 225 | */ |
||
| 226 | public function active_callback() { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Gather the parameters passed to client JavaScript via JSON. |
||
| 232 | * |
||
| 233 | * @since 4.1.0 |
||
| 234 | * |
||
| 235 | * @return array The array to be exported to the client as JSON. |
||
| 236 | */ |
||
| 237 | public function json() { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Checks required user capabilities and whether the theme has the |
||
| 256 | * feature support required by the section. |
||
| 257 | * |
||
| 258 | * @since 3.4.0 |
||
| 259 | * |
||
| 260 | * @return bool False if theme doesn't support the section or user doesn't have the capability. |
||
| 261 | */ |
||
| 262 | View Code Duplication | final public function check_capabilities() { |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Get the section's content for insertion into the Customizer pane. |
||
| 276 | * |
||
| 277 | * @since 4.1.0 |
||
| 278 | * |
||
| 279 | * @return string Contents of the section. |
||
| 280 | */ |
||
| 281 | final public function get_content() { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Check capabilities and render the section. |
||
| 289 | * |
||
| 290 | * @since 3.4.0 |
||
| 291 | */ |
||
| 292 | final public function maybe_render() { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Render the section UI in a subclass. |
||
| 320 | * |
||
| 321 | * Sections are now rendered in JS by default, see WP_Customize_Section::print_template(). |
||
| 322 | * |
||
| 323 | * @since 3.4.0 |
||
| 324 | */ |
||
| 325 | protected function render() {} |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Render the section's JS template. |
||
| 329 | * |
||
| 330 | * This function is only run for section types that have been registered with |
||
| 331 | * WP_Customize_Manager::register_section_type(). |
||
| 332 | * |
||
| 333 | * @since 4.3.0 |
||
| 334 | * @access public |
||
| 335 | * |
||
| 336 | * @see WP_Customize_Manager::render_template() |
||
| 337 | */ |
||
| 338 | public function print_template() { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * An Underscore (JS) template for rendering this section. |
||
| 348 | * |
||
| 349 | * Class variables for this section class are available in the `data` JS object; |
||
| 350 | * export custom variables by overriding WP_Customize_Section::json(). |
||
| 351 | * |
||
| 352 | * @since 4.3.0 |
||
| 353 | * @access protected |
||
| 354 | * |
||
| 355 | * @see WP_Customize_Section::print_template() |
||
| 356 | */ |
||
| 357 | protected function render_template() { |
||
| 394 | } |
||
| 395 | |||
| 407 |
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.