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:
Complex classes like Jetpack_Network often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Network, and based on these observations, apply Extract Interface, too.
| 1 | <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFilename |
||
| 23 | class Jetpack_Network { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Holds a static copy of Jetpack_Network for the singleton |
||
| 27 | * |
||
| 28 | * @since 2.9 |
||
| 29 | * @var Jetpack_Network |
||
| 30 | */ |
||
| 31 | private static $instance = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * An instance of the connection manager object. |
||
| 35 | * |
||
| 36 | * @since 7.7 |
||
| 37 | * @var Automattic\Jetpack\Connection\Manager |
||
| 38 | */ |
||
| 39 | private $connection; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Name of the network wide settings |
||
| 43 | * |
||
| 44 | * @since 2.9 |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $settings_name = 'jetpack-network-settings'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Defaults for settings found on the Jetpack > Settings page |
||
| 51 | * |
||
| 52 | * @since 2.9 |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $setting_defaults = array( |
||
| 56 | 'auto-connect' => 0, |
||
| 57 | 'sub-site-connection-override' => 1, |
||
| 58 | ); |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Constructor |
||
| 62 | * |
||
| 63 | * @since 2.9 |
||
| 64 | */ |
||
| 65 | private function __construct() { |
||
| 66 | require_once ABSPATH . '/wp-admin/includes/plugin.php'; // For the is_plugin... check. |
||
| 67 | require_once JETPACK__PLUGIN_DIR . 'modules/protect/shared-functions.php'; // For managing the global whitelist. |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Sanity check to ensure the install is Multisite and we |
||
| 71 | * are in Network Admin |
||
| 72 | */ |
||
| 73 | if ( is_multisite() && is_network_admin() ) { |
||
| 74 | add_action( 'network_admin_menu', array( $this, 'add_network_admin_menu' ) ); |
||
| 75 | add_action( 'network_admin_edit_jetpack-network-settings', array( $this, 'save_network_settings_page' ), 10, 0 ); |
||
| 76 | add_filter( 'admin_body_class', array( $this, 'body_class' ) ); |
||
| 77 | |||
| 78 | if ( isset( $_GET['page'] ) && 'jetpack' == $_GET['page'] ) { |
||
| 79 | add_action( 'admin_init', array( $this, 'jetpack_sites_list' ) ); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /* |
||
| 84 | * Things that should only run on multisite |
||
| 85 | */ |
||
| 86 | if ( is_multisite() && is_plugin_active_for_network( 'jetpack/jetpack.php' ) ) { |
||
| 87 | add_action( 'wp_before_admin_bar_render', array( $this, 'add_to_menubar' ) ); |
||
| 88 | |||
| 89 | /* |
||
| 90 | * If admin wants to automagically register new sites set the hook here |
||
| 91 | * |
||
| 92 | * This is a hacky way because xmlrpc is not available on wp_initialize_site |
||
| 93 | */ |
||
| 94 | if ( 1 === $this->get_option( 'auto-connect' ) ) { |
||
| 95 | add_action( 'wp_initialize_site', array( $this, 'do_automatically_add_new_site' ) ); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Sets a connection object. |
||
| 102 | * |
||
| 103 | * @param Automattic\Jetpack\Connection\Manager $connection the connection manager object. |
||
| 104 | */ |
||
| 105 | public function set_connection( Manager $connection ) { |
||
| 106 | $this->connection = $connection; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Sets which modules get activated by default on subsite connection. |
||
| 111 | * Modules can be set in Network Admin > Jetpack > Settings |
||
| 112 | * |
||
| 113 | * @since 2.9 |
||
| 114 | * @deprecated since 7.7.0 |
||
| 115 | * |
||
| 116 | * @param array $modules List of modules. |
||
| 117 | */ |
||
| 118 | public function set_auto_activated_modules( $modules ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
||
| 119 | _deprecated_function( __METHOD__, 'jetpack-7.7' ); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Registers new sites upon creation |
||
| 124 | * |
||
| 125 | * @since 2.9 |
||
| 126 | * @since 7.4.0 Uses a WP_Site object. |
||
| 127 | * @uses wp_initialize_site |
||
| 128 | * |
||
| 129 | * @param WP_Site $site the WordPress site object. |
||
| 130 | **/ |
||
| 131 | public function do_automatically_add_new_site( $site ) { |
||
| 132 | if ( is_a( $site, 'WP_Site' ) ) { |
||
| 133 | $this->do_subsiteregister( $site->id ); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Adds .network-admin class to the body tag |
||
| 139 | * Helps distinguish network admin JP styles from regular site JP styles |
||
| 140 | * |
||
| 141 | * @since 2.9 |
||
| 142 | * |
||
| 143 | * @param String $classes current assigned body classes. |
||
| 144 | * @return String amended class string. |
||
| 145 | */ |
||
| 146 | public function body_class( $classes ) { |
||
| 147 | return trim( $classes ) . ' network-admin '; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Provides access to an instance of Jetpack_Network |
||
| 152 | * |
||
| 153 | * This is how the Jetpack_Network object should *always* be accessed |
||
| 154 | * |
||
| 155 | * @since 2.9 |
||
| 156 | * @return Jetpack_Network |
||
| 157 | */ |
||
| 158 | public static function init() { |
||
| 159 | if ( ! self::$instance || ! is_a( self::$instance, 'Jetpack_Network' ) ) { |
||
| 160 | self::$instance = new Jetpack_Network(); |
||
| 161 | } |
||
| 162 | |||
| 163 | return self::$instance; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Registers the Multisite admin bar menu item shortcut. |
||
| 168 | * This shortcut helps users quickly and easily navigate to the Jetpack Network Admin |
||
| 169 | * menu from anywhere in their network. |
||
| 170 | * |
||
| 171 | * @since 2.9 |
||
| 172 | */ |
||
| 173 | public function register_menubar() { |
||
| 174 | add_action( 'wp_before_admin_bar_render', array( $this, 'add_to_menubar' ) ); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Runs when Jetpack is deactivated from the network admin plugins menu. |
||
| 179 | * Each individual site will need to have Jetpack::disconnect called on it. |
||
| 180 | * Site that had Jetpack individually enabled will not be disconnected as |
||
| 181 | * on Multisite individually activated plugins are still activated when |
||
| 182 | * a plugin is deactivated network wide. |
||
| 183 | * |
||
| 184 | * @since 2.9 |
||
| 185 | **/ |
||
| 186 | public function deactivate() { |
||
| 187 | // Only fire if in network admin. |
||
| 188 | if ( ! is_network_admin() ) { |
||
| 189 | return; |
||
| 190 | } |
||
| 191 | |||
| 192 | $sites = get_sites(); |
||
| 193 | |||
| 194 | foreach ( $sites as $s ) { |
||
| 195 | switch_to_blog( $s->blog_id ); |
||
| 196 | $active_plugins = get_option( 'active_plugins' ); |
||
| 197 | |||
| 198 | /* |
||
| 199 | * If this plugin was activated in the subsite individually |
||
| 200 | * we do not want to call disconnect. Plugins activated |
||
| 201 | * individually (before network activation) stay activated |
||
| 202 | * when the network deactivation occurs |
||
| 203 | */ |
||
| 204 | if ( ! in_array( 'jetpack/jetpack.php', $active_plugins, true ) ) { |
||
| 205 | Jetpack::disconnect(); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | restore_current_blog(); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Adds a link to the Jetpack Network Admin page in the network admin menu bar. |
||
| 213 | * |
||
| 214 | * @since 2.9 |
||
| 215 | **/ |
||
| 216 | public function add_to_menubar() { |
||
| 217 | global $wp_admin_bar; |
||
| 218 | // Don't show for logged out users or single site mode. |
||
| 219 | if ( ! is_user_logged_in() || ! is_multisite() ) { |
||
| 220 | return; |
||
| 221 | } |
||
| 222 | |||
| 223 | $wp_admin_bar->add_node( |
||
| 224 | array( |
||
| 225 | 'parent' => 'network-admin', |
||
| 226 | 'id' => 'network-admin-jetpack', |
||
| 227 | 'title' => 'Jetpack', |
||
| 228 | 'href' => $this->get_url( 'network_admin_page' ), |
||
| 229 | ) |
||
| 230 | ); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Returns various URL strings. Factory like |
||
| 235 | * |
||
| 236 | * $args can be a string or an array. |
||
| 237 | * If $args is an array there must be an element called name for the switch statement |
||
| 238 | * |
||
| 239 | * Currently supports: |
||
| 240 | * - subsiteregister: Pass array( 'name' => 'subsiteregister', 'site_id' => SITE_ID ) |
||
| 241 | * - network_admin_page: Provides link to /wp-admin/network/JETPACK |
||
| 242 | * - subsitedisconnect: Pass array( 'name' => 'subsitedisconnect', 'site_id' => SITE_ID ) |
||
| 243 | * |
||
| 244 | * @since 2.9 |
||
| 245 | * |
||
| 246 | * @param Mixed $args URL parameters. |
||
| 247 | * |
||
| 248 | * @return String |
||
| 249 | **/ |
||
| 250 | public function get_url( $args ) { |
||
| 251 | $url = null; // Default url value. |
||
| 252 | |||
| 253 | if ( is_string( $args ) ) { |
||
| 254 | $name = $args; |
||
| 255 | } else if ( is_array( $args ) ) { |
||
| 256 | $name = $args['name']; |
||
| 257 | } else { |
||
| 258 | return $url; |
||
| 259 | } |
||
| 260 | |||
| 261 | switch ( $name ) { |
||
| 262 | case 'subsiteregister': |
||
| 263 | if ( ! isset( $args['site_id'] ) ) { |
||
| 264 | break; // If there is not a site id present we cannot go further. |
||
| 265 | } |
||
| 266 | $url = network_admin_url( |
||
| 267 | 'admin.php?page=jetpack&action=subsiteregister&site_id=' |
||
| 268 | . $args['site_id'] |
||
| 269 | ); |
||
| 270 | break; |
||
| 271 | |||
| 272 | case 'network_admin_page': |
||
| 273 | $url = network_admin_url( 'admin.php?page=jetpack' ); |
||
| 274 | break; |
||
| 275 | |||
| 276 | case 'subsitedisconnect': |
||
| 277 | if ( ! isset( $args['site_id'] ) ) { |
||
| 278 | break; // If there is not a site id present we cannot go further. |
||
| 279 | } |
||
| 280 | $url = network_admin_url( |
||
| 281 | 'admin.php?page=jetpack&action=subsitedisconnect&site_id=' |
||
| 282 | . $args['site_id'] |
||
| 283 | ); |
||
| 284 | break; |
||
| 285 | } |
||
| 286 | |||
| 287 | return $url; |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Adds the Jetpack menu item to the Network Admin area |
||
| 292 | * |
||
| 293 | * @since 2.9 |
||
| 294 | */ |
||
| 295 | public function add_network_admin_menu() { |
||
| 296 | add_menu_page( 'Jetpack', 'Jetpack', 'jetpack_network_admin_page', 'jetpack', array( $this, 'wrap_network_admin_page' ), 'div', 3 ); |
||
| 297 | $jetpack_sites_page_hook = add_submenu_page( 'jetpack', __( 'Jetpack Sites', 'jetpack' ), __( 'Sites', 'jetpack' ), 'jetpack_network_sites_page', 'jetpack', array( $this, 'wrap_network_admin_page' ) ); |
||
| 298 | $jetpack_settings_page_hook = add_submenu_page( 'jetpack', __( 'Settings', 'jetpack' ), __( 'Settings', 'jetpack' ), 'jetpack_network_settings_page', 'jetpack-settings', array( $this, 'wrap_render_network_admin_settings_page' ) ); |
||
| 299 | add_action( "admin_print_styles-$jetpack_sites_page_hook", array( 'Jetpack_Admin_Page', 'load_wrapper_styles' ) ); |
||
| 300 | add_action( "admin_print_styles-$jetpack_settings_page_hook", array( 'Jetpack_Admin_Page', 'load_wrapper_styles' ) ); |
||
| 301 | /** |
||
| 302 | * As jetpack_register_genericons is by default fired off a hook, |
||
| 303 | * the hook may have already fired by this point. |
||
| 304 | * So, let's just trigger it manually. |
||
| 305 | */ |
||
| 306 | require_once JETPACK__PLUGIN_DIR . '_inc/genericons.php'; |
||
| 307 | jetpack_register_genericons(); |
||
| 308 | |||
| 309 | View Code Duplication | if ( ! wp_style_is( 'jetpack-icons', 'registered' ) ) { |
|
| 310 | wp_register_style( 'jetpack-icons', plugins_url( 'css/jetpack-icons.min.css', JETPACK__PLUGIN_FILE ), false, JETPACK__VERSION ); |
||
| 311 | } |
||
| 312 | |||
| 313 | add_action( 'admin_enqueue_scripts', array( $this, 'admin_menu_css' ) ); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Adds JP menu icon |
||
| 318 | * |
||
| 319 | * @since 2.9 |
||
| 320 | **/ |
||
| 321 | public function admin_menu_css() { |
||
| 322 | wp_enqueue_style( 'jetpack-icons' ); |
||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Provides functionality for the Jetpack > Sites page. |
||
| 327 | * Does not do the display! |
||
| 328 | * |
||
| 329 | * @since 2.9 |
||
| 330 | */ |
||
| 331 | public function jetpack_sites_list() { |
||
| 332 | Jetpack::init(); |
||
| 333 | |||
| 334 | if ( isset( $_GET['action'] ) ) { |
||
| 335 | switch ( $_GET['action'] ) { |
||
| 336 | case 'subsiteregister': |
||
| 337 | /** |
||
| 338 | * Add actual referrer checking. |
||
| 339 | * |
||
| 340 | * @todo check_admin_referer( 'jetpack-subsite-register' ); |
||
| 341 | */ |
||
| 342 | Jetpack::log( 'subsiteregister' ); |
||
| 343 | |||
| 344 | // If !$_GET['site_id'] stop registration and error. |
||
| 345 | View Code Duplication | if ( ! isset( $_GET['site_id'] ) || empty( $_GET['site_id'] ) ) { |
|
| 346 | /** |
||
| 347 | * Log error to state cookie for display later. |
||
| 348 | * |
||
| 349 | * @todo Make state messages show on Jetpack NA pages |
||
| 350 | */ |
||
| 351 | Jetpack::state( 'missing_site_id', esc_html__( 'Site ID must be provided to register a sub-site.', 'jetpack' ) ); |
||
| 352 | break; |
||
| 353 | } |
||
| 354 | |||
| 355 | // Send data to register endpoint and retrieve shadow blog details. |
||
| 356 | $result = $this->do_subsiteregister(); |
||
| 357 | $url = $this->get_url( 'network_admin_page' ); |
||
| 358 | |||
| 359 | if ( is_wp_error( $result ) ) { |
||
| 360 | $url = add_query_arg( 'action', 'connection_failed', $url ); |
||
| 361 | } else { |
||
| 362 | $url = add_query_arg( 'action', 'connected', $url ); |
||
| 363 | } |
||
| 364 | |||
| 365 | wp_safe_redirect( $url ); |
||
| 366 | exit; |
||
| 367 | |||
| 368 | case 'subsitedisconnect': |
||
| 369 | Jetpack::log( 'subsitedisconnect' ); |
||
| 370 | |||
| 371 | View Code Duplication | if ( ! isset( $_GET['site_id'] ) || empty( $_GET['site_id'] ) ) { |
|
| 372 | Jetpack::state( 'missing_site_id', esc_html__( 'Site ID must be provided to disconnect a sub-site.', 'jetpack' ) ); |
||
| 373 | break; |
||
| 374 | } |
||
| 375 | |||
| 376 | $this->do_subsitedisconnect(); |
||
| 377 | break; |
||
| 378 | |||
| 379 | case 'connected': |
||
| 380 | case 'connection_failed': |
||
| 381 | add_action( 'jetpack_notices', array( $this, 'show_jetpack_notice' ) ); |
||
| 382 | break; |
||
| 383 | } |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Shows the Jetpack plugin notices. |
||
| 389 | */ |
||
| 390 | public function show_jetpack_notice() { |
||
| 391 | if ( isset( $_GET['action'] ) && 'connected' == $_GET['action'] ) { |
||
| 392 | $notice = __( 'Site successfully connected.', 'jetpack' ); |
||
| 393 | $classname = 'updated'; |
||
| 394 | } elseif ( isset( $_GET['action'] ) && 'connection_failed' == $_GET['action'] ) { |
||
| 395 | $notice = __( 'Site connection failed!', 'jetpack' ); |
||
| 396 | $classname = 'error'; |
||
| 397 | } |
||
| 398 | ?> |
||
| 399 | <div id="message" class="<?php echo esc_attr( $classname ); ?> jetpack-message jp-connect" style="display:block !important;"> |
||
|
|
|||
| 400 | <p><?php echo esc_html( $notice ); ?></p> |
||
| 401 | </div> |
||
| 402 | <?php |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Disconnect functionality for an individual site |
||
| 407 | * |
||
| 408 | * @since 2.9 |
||
| 409 | * @see Jetpack_Network::jetpack_sites_list() |
||
| 410 | * |
||
| 411 | * @param int $site_id the site identifier. |
||
| 412 | */ |
||
| 413 | public function do_subsitedisconnect( $site_id = null ) { |
||
| 414 | if ( ! current_user_can( 'jetpack_disconnect' ) ) { |
||
| 415 | return; |
||
| 416 | } |
||
| 417 | $site_id = ( is_null( $site_id ) ) ? $_GET['site_id'] : $site_id; |
||
| 418 | switch_to_blog( $site_id ); |
||
| 419 | Jetpack::disconnect(); |
||
| 420 | restore_current_blog(); |
||
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Registers a subsite with the Jetpack servers |
||
| 425 | * |
||
| 426 | * @since 2.9 |
||
| 427 | * @todo Break apart into easier to manage chunks that can be unit tested |
||
| 428 | * @see Jetpack_Network::jetpack_sites_list(); |
||
| 429 | * |
||
| 430 | * @param int $site_id the site identifier. |
||
| 431 | */ |
||
| 432 | public function do_subsiteregister( $site_id = null ) { |
||
| 433 | if ( ! current_user_can( 'jetpack_disconnect' ) ) { |
||
| 434 | return; |
||
| 435 | } |
||
| 436 | |||
| 437 | if ( ( new Status() )->is_development_mode() ) { |
||
| 438 | return; |
||
| 439 | } |
||
| 440 | |||
| 441 | // Figure out what site we are working on. |
||
| 442 | $site_id = ( is_null( $site_id ) ) ? $_GET['site_id'] : $site_id; |
||
| 443 | |||
| 444 | /* |
||
| 445 | * Here we need to switch to the subsite |
||
| 446 | * For the registration process we really only hijack how it |
||
| 447 | * works for an individual site and pass in some extra data here |
||
| 448 | */ |
||
| 449 | switch_to_blog( $site_id ); |
||
| 450 | |||
| 451 | add_filter( 'jetpack_register_request_body', array( $this, 'filter_register_request_body' ) ); |
||
| 452 | add_action( 'jetpack_site_registered_user_token', array( $this, 'filter_register_user_token' ) ); |
||
| 453 | |||
| 454 | // Save the secrets in the subsite so when the wpcom server does a pingback it |
||
| 455 | // will be able to validate the connection. |
||
| 456 | $result = $this->connection->register( 'subsiteregister' ); |
||
| 457 | |||
| 458 | if ( is_wp_error( $result ) || ! $result ) { |
||
| 459 | restore_current_blog(); |
||
| 460 | return $result; |
||
| 461 | } |
||
| 462 | |||
| 463 | Jetpack::activate_default_modules( false, false, array(), false ); |
||
| 464 | |||
| 465 | restore_current_blog(); |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Receives the registration response token. |
||
| 470 | * |
||
| 471 | * @param Object $token the received token. |
||
| 472 | */ |
||
| 473 | public function filter_register_user_token( $token ) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Filters the registration request body to include additional properties. |
||
| 484 | * |
||
| 485 | * @param Array $properties standard register request body properties. |
||
| 486 | * @return Array amended properties. |
||
| 487 | */ |
||
| 488 | public function filter_register_request_body( $properties ) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * A hook handler for adding admin pages and subpages. |
||
| 524 | */ |
||
| 525 | public function wrap_network_admin_page() { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Handles the displaying of all sites on the network that are |
||
| 531 | * dis/connected to Jetpack |
||
| 532 | * |
||
| 533 | * @since 2.9 |
||
| 534 | * @see Jetpack_Network::jetpack_sites_list() |
||
| 535 | */ |
||
| 536 | public function network_admin_page() { |
||
| 537 | global $current_site; |
||
| 538 | $this->network_admin_page_header(); |
||
| 539 | |||
| 540 | $jp = Jetpack::init(); |
||
| 541 | |||
| 542 | // We should be, but ensure we are on the main blog. |
||
| 543 | switch_to_blog( $current_site->blog_id ); |
||
| 544 | $main_active = $jp->is_active(); |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Stylized JP header formatting |
||
| 583 | * |
||
| 584 | * @since 2.9 |
||
| 585 | */ |
||
| 586 | public function network_admin_page_header() { |
||
| 594 | |||
| 595 | /** |
||
| 596 | * Fires when the Jetpack > Settings page is saved. |
||
| 597 | * |
||
| 598 | * @since 2.9 |
||
| 599 | */ |
||
| 600 | public function save_network_settings_page() { |
||
| 663 | |||
| 664 | /** |
||
| 665 | * A hook handler for adding admin pages and subpages. |
||
| 666 | */ |
||
| 667 | public function wrap_render_network_admin_settings_page() { |
||
| 670 | |||
| 671 | /** |
||
| 672 | * A hook rendering the admin settings page. |
||
| 673 | */ |
||
| 674 | public function render_network_admin_settings_page() { |
||
| 700 | |||
| 701 | /** |
||
| 702 | * Updates a site wide option |
||
| 703 | * |
||
| 704 | * @since 2.9 |
||
| 705 | * |
||
| 706 | * @param string $key option name. |
||
| 707 | * @param mixed $value option value. |
||
| 708 | * |
||
| 709 | * @return boolean |
||
| 710 | **/ |
||
| 711 | public function update_option( $key, $value ) { |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Retrieves a site wide option |
||
| 720 | * |
||
| 721 | * @since 2.9 |
||
| 722 | * |
||
| 723 | * @param string $name - Name of the option in the database. |
||
| 724 | **/ |
||
| 725 | public function get_option( $name ) { |
||
| 735 | } |
||
| 736 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: