Complex classes like JITM 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 JITM, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class JITM { |
||
| 17 | |||
| 18 | const PACKAGE_VERSION = '1.0'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * Tracking object. |
||
| 22 | * |
||
| 23 | * @var Automattic\Jetpack\Tracking |
||
| 24 | * |
||
| 25 | * @access private |
||
| 26 | */ |
||
| 27 | private $tracking; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * JITM constructor. |
||
| 31 | */ |
||
| 32 | public function __construct() { |
||
| 33 | $this->tracking = new Tracking(); |
||
|
|
|||
| 34 | } |
||
| 35 | |||
| 36 | public function register() { |
||
| 37 | /** |
||
| 38 | * Filter to turn off all just in time messages |
||
| 39 | * |
||
| 40 | * @since 3.7.0 |
||
| 41 | * @since 5.4.0 Correct docblock to reflect default arg value |
||
| 42 | * |
||
| 43 | * @param bool false Whether to show just in time messages. |
||
| 44 | */ |
||
| 45 | if ( ! apply_filters( 'jetpack_just_in_time_msgs', false ) ) { |
||
| 46 | return false; |
||
| 47 | } |
||
| 48 | add_action( 'current_screen', array( $this, 'prepare_jitms' ) ); |
||
| 49 | return true; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Get's the Jetpack emblem |
||
| 54 | * |
||
| 55 | * @return string The Jetpack emblem |
||
| 56 | */ |
||
| 57 | function get_emblem() { |
||
| 58 | $jetpack_logo = new Jetpack_Logo(); |
||
| 59 | return '<div class="jp-emblem">' . $jetpack_logo->get_jp_emblem() . '</div>'; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Prepare actions according to screen and post type. |
||
| 64 | * |
||
| 65 | * @since 3.8.2 |
||
| 66 | * |
||
| 67 | * @uses Jetpack_Autoupdate::get_possible_failures() |
||
| 68 | * |
||
| 69 | * @param object $screen |
||
| 70 | */ |
||
| 71 | function prepare_jitms( $screen ) { |
||
| 72 | if ( ! in_array( |
||
| 73 | $screen->id, |
||
| 74 | array( |
||
| 75 | 'jetpack_page_stats', |
||
| 76 | 'jetpack_page_akismet-key-config', |
||
| 77 | 'admin_page_jetpack_modules', |
||
| 78 | ) |
||
| 79 | ) ) { |
||
| 80 | add_action( 'admin_enqueue_scripts', array( $this, 'jitm_enqueue_files' ) ); |
||
| 81 | add_action( 'admin_notices', array( $this, 'ajax_message' ) ); |
||
| 82 | add_action( 'edit_form_top', array( $this, 'ajax_message' ) ); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * A special filter for WooCommerce, to set a message based on local state. |
||
| 88 | * |
||
| 89 | * @param $message string The current message |
||
| 90 | * |
||
| 91 | * @return array The new message |
||
| 92 | */ |
||
| 93 | static function jitm_woocommerce_services_msg( $content ) { |
||
| 94 | if ( ! function_exists( 'wc_get_base_location' ) ) { |
||
| 95 | return $content; |
||
| 96 | } |
||
| 97 | |||
| 98 | $base_location = wc_get_base_location(); |
||
| 99 | |||
| 100 | switch ( $base_location['country'] ) { |
||
| 101 | case 'US': |
||
| 102 | $content->message = esc_html__( 'New free service: Show USPS shipping rates on your store! Added bonus: print shipping labels without leaving WooCommerce.', 'jetpack' ); |
||
| 103 | break; |
||
| 104 | case 'CA': |
||
| 105 | $content->message = esc_html__( 'New free service: Show Canada Post shipping rates on your store!', 'jetpack' ); |
||
| 106 | break; |
||
| 107 | default: |
||
| 108 | $content->message = ''; |
||
| 109 | } |
||
| 110 | |||
| 111 | return $content; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * A special filter for WooCommerce Call To Action button |
||
| 116 | * |
||
| 117 | * @param $CTA string The existing CTA |
||
| 118 | * |
||
| 119 | * @return string The new CTA |
||
| 120 | */ |
||
| 121 | static function jitm_jetpack_woo_services_install( $CTA ) { |
||
| 122 | return wp_nonce_url( |
||
| 123 | add_query_arg( |
||
| 124 | array( |
||
| 125 | 'wc-services-action' => 'install', |
||
| 126 | ), |
||
| 127 | admin_url( 'admin.php?page=wc-settings' ) |
||
| 128 | ), |
||
| 129 | 'wc-services-install' |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * A special filter for WooCommerce Call To Action button |
||
| 135 | * |
||
| 136 | * @param $CTA string The existing CTA |
||
| 137 | * |
||
| 138 | * @return string The new CTA |
||
| 139 | */ |
||
| 140 | static function jitm_jetpack_woo_services_activate( $CTA ) { |
||
| 141 | return wp_nonce_url( |
||
| 142 | add_query_arg( |
||
| 143 | array( |
||
| 144 | 'wc-services-action' => 'activate', |
||
| 145 | ), |
||
| 146 | admin_url( 'admin.php?page=wc-settings' ) |
||
| 147 | ), |
||
| 148 | 'wc-services-install' |
||
| 149 | ); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Injects the dom to show a JITM inside of |
||
| 154 | */ |
||
| 155 | function ajax_message() { |
||
| 156 | $message_path = $this->get_message_path(); |
||
| 157 | $query_string = _http_build_query( $_GET, '', ',' ); |
||
| 158 | $current_screen = wp_unslash( $_SERVER['REQUEST_URI'] ); |
||
| 159 | ?> |
||
| 160 | <div class="jetpack-jitm-message" |
||
| 161 | data-nonce="<?php echo wp_create_nonce( 'wp_rest' ); ?>" |
||
| 162 | data-message-path="<?php echo esc_attr( $message_path ); ?>" |
||
| 163 | data-query="<?php echo urlencode_deep( $query_string ); ?>" |
||
| 164 | data-redirect="<?php echo urlencode_deep( $current_screen ); ?>" |
||
| 165 | ></div> |
||
| 166 | <?php |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get's the current message path for display of a JITM |
||
| 171 | * |
||
| 172 | * @return string The message path |
||
| 173 | */ |
||
| 174 | function get_message_path() { |
||
| 175 | $screen = get_current_screen(); |
||
| 176 | |||
| 177 | return 'wp:' . $screen->id . ':' . current_filter(); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Function to enqueue jitm css and js |
||
| 182 | */ |
||
| 183 | function jitm_enqueue_files() { |
||
| 184 | $min = ''; // ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; |
||
| 185 | wp_register_style( |
||
| 186 | 'jetpack-jitm-css', |
||
| 187 | plugins_url( "assets/jetpack-admin-jitm{$min}.css", __DIR__ ), |
||
| 188 | false, |
||
| 189 | self::PACKAGE_VERSION . |
||
| 190 | '-201243242' |
||
| 191 | ); |
||
| 192 | wp_style_add_data( 'jetpack-jitm-css', 'rtl', 'replace' ); |
||
| 193 | wp_style_add_data( 'jetpack-jitm-css', 'suffix', $min ); |
||
| 194 | wp_enqueue_style( 'jetpack-jitm-css' ); |
||
| 195 | |||
| 196 | wp_enqueue_script( |
||
| 197 | 'jetpack-jitm-new', |
||
| 198 | Assets::get_file_url_for_environment( '_inc/build/jetpack-jitm.min.js', '_inc/jetpack-jitm.js' ), |
||
| 199 | array( 'jquery' ), |
||
| 200 | self::PACKAGE_VERSION, // TODO: Keep in sync with version specified in composer.json |
||
| 201 | true |
||
| 202 | ); |
||
| 203 | wp_localize_script( |
||
| 204 | 'jetpack-jitm-new', |
||
| 205 | 'jitm_config', |
||
| 206 | array( |
||
| 207 | 'api_root' => esc_url_raw( rest_url() ), |
||
| 208 | 'activate_module_text' => esc_html__( 'Activate', 'jetpack' ), |
||
| 209 | 'activated_module_text' => esc_html__( 'Activated', 'jetpack' ), |
||
| 210 | 'activating_module_text' => esc_html__( 'Activating', 'jetpack' ), |
||
| 211 | ) |
||
| 212 | ); |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Dismisses a JITM feature class so that it will no longer be shown |
||
| 217 | * |
||
| 218 | * @param $id string The id of the JITM that was dismissed |
||
| 219 | * @param $feature_class string The feature class of the JITM that was dismissed |
||
| 220 | * |
||
| 221 | * @return bool Always true |
||
| 222 | */ |
||
| 223 | function dismiss( $id, $feature_class ) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Asks the wpcom API for the current message to display keyed on query string and message path |
||
| 265 | * |
||
| 266 | * @param $message_path string The message path to ask for |
||
| 267 | * @param $query string The query string originally from the front end |
||
| 268 | * |
||
| 269 | * @return array The JITM's to show, or an empty array if there is nothing to show |
||
| 270 | */ |
||
| 271 | function get_messages( $message_path, $query ) { |
||
| 448 | } |
||
| 449 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..