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 |
||
| 16 | class Jetpack_Monitor { |
||
| 17 | |||
| 18 | public $module = 'monitor'; |
||
| 19 | |||
| 20 | function __construct() { |
||
| 21 | add_action( 'jetpack_modules_loaded', array( $this, 'jetpack_modules_loaded' ) ); |
||
| 22 | add_action( 'jetpack_activate_module_monitor', array( $this, 'activate_module' ) ); |
||
| 23 | } |
||
| 24 | |||
| 25 | public function activate_module() { |
||
| 26 | if ( Jetpack::is_user_connected() ) { |
||
| 27 | self::update_option_receive_jetpack_monitor_notification( true ); |
||
| 28 | } |
||
| 29 | } |
||
| 30 | |||
| 31 | public function jetpack_modules_loaded() { |
||
| 32 | Jetpack::enable_module_configurable( $this->module ); |
||
| 33 | Jetpack::module_configuration_load( $this->module, array( $this, 'jetpack_configuration_load' ) ); |
||
| 34 | Jetpack::module_configuration_screen( $this->module, array( $this, 'jetpack_configuration_screen' ) ); |
||
| 35 | } |
||
| 36 | |||
| 37 | public function jetpack_configuration_load() { |
||
| 38 | if ( Jetpack::is_user_connected() && ! self::is_active() ) { |
||
| 39 | Jetpack::deactivate_module( $this->module ); |
||
| 40 | Jetpack::state( 'message', 'module_deactivated' ); |
||
| 41 | wp_safe_redirect( Jetpack::admin_url( 'page=jetpack' ) ); |
||
| 42 | die(); |
||
|
|
|||
| 43 | } |
||
| 44 | if ( ! empty( $_POST['action'] ) && $_POST['action'] == 'monitor-save' ) { |
||
| 45 | check_admin_referer( 'monitor-settings' ); |
||
| 46 | $this->update_option_receive_jetpack_monitor_notification( isset( $_POST['receive_jetpack_monitor_notification'] ) ); |
||
| 47 | Jetpack::state( 'message', 'module_configured' ); |
||
| 48 | wp_safe_redirect( Jetpack::module_configuration_url( $this->module ) ); |
||
| 49 | exit; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | public function jetpack_configuration_screen() { |
||
| 86 | |||
| 87 | View Code Duplication | public function is_active() { |
|
| 98 | |||
| 99 | public function update_option_receive_jetpack_monitor_notification( $value ) { |
||
| 100 | Jetpack::load_xml_rpc_client(); |
||
| 101 | $xml = new Jetpack_IXR_Client( array( |
||
| 102 | 'user_id' => get_current_user_id() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Checks the status of notifications for current Jetpack site user. |
||
| 118 | * |
||
| 119 | * @since 2.8 |
||
| 120 | * @since 4.1.0 New parameter $die_on_error. |
||
| 121 | * |
||
| 122 | * @param bool $die_on_error Whether to issue a wp_die when an error occurs or return a WP_Error object. |
||
| 123 | * |
||
| 124 | * @return boolean|WP_Error |
||
| 125 | */ |
||
| 126 | static function user_receives_notifications( $die_on_error = true ) { |
||
| 142 | |||
| 143 | View Code Duplication | public function activate_monitor() { |
|
| 156 | |||
| 157 | View Code Duplication | public function deactivate_monitor() { |
|
| 170 | |||
| 171 | /* |
||
| 172 | * Returns date of the last downtime. |
||
| 173 | * |
||
| 174 | * @since 4.0.0 |
||
| 175 | * @return date in YYYY-MM-DD HH:mm:ss format |
||
| 176 | */ |
||
| 177 | public function monitor_get_last_downtime() { |
||
| 197 | |||
| 198 | } |
||
| 199 | |||
| 201 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.