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_Sync_Module_Themes 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_Sync_Module_Themes, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class Jetpack_Sync_Module_Themes extends Jetpack_Sync_Module { |
||
| 4 | function name() { |
||
| 7 | |||
| 8 | public function init_listeners( $callable ) { |
||
| 33 | |||
| 34 | public function sync_widget_edit( $instance, $new_instance, $old_instance, $widget_object ) { |
||
| 35 | if ( empty( $old_instance ) ) { |
||
| 36 | return; |
||
| 37 | } |
||
| 38 | |||
| 39 | $widget = array( |
||
| 40 | 'name' => $widget_object->name, |
||
| 41 | 'id' => $widget_object->id, |
||
| 42 | ); |
||
| 43 | /** |
||
| 44 | * Trigger action to alert $callable sync listener that a widget was edited |
||
| 45 | * |
||
| 46 | * @since 5.0.0 |
||
| 47 | * |
||
| 48 | * @param string $widget_name , Name of edited widget |
||
| 49 | */ |
||
| 50 | do_action( 'jetpack_widget_edited', $widget ); |
||
| 51 | |||
| 52 | return $instance; |
||
| 53 | } |
||
| 54 | |||
| 55 | public function sync_network_allowed_themes_change( $option, $value, $old_value, $network_id ) { |
||
| 56 | $all_enabled_theme_slugs = array_keys( $value ); |
||
| 57 | |||
| 58 | if ( count( $old_value ) > count( $value ) ) { |
||
| 59 | |||
| 60 | $delete_theme_call = $this->get_delete_theme_call(); |
||
| 61 | $is_theme_deletion = empty( $delete_theme_call ) ? false : true; |
||
| 62 | |||
| 63 | $newly_disabled_theme_names = array_keys( array_diff_key( $old_value, $value ) ); |
||
| 64 | $newly_disabled_themes = $this->get_theme_details_for_slugs( $newly_disabled_theme_names ); |
||
| 65 | /** |
||
| 66 | * Trigger action to alert $callable sync listener that network themes were disabled |
||
| 67 | * |
||
| 68 | * @since 5.0.0 |
||
| 69 | * |
||
| 70 | * @param mixed $newly_disabled_themes, Array of info about network disabled themes |
||
| 71 | * @param mixed $all_enabled_theme_slugs, Array of slugs of all enabled themes |
||
| 72 | * @param bool $is_theme_deletion, Whether a theme was deleted |
||
| 73 | */ |
||
| 74 | do_action( 'jetpack_network_disabled_themes', $newly_disabled_themes, $all_enabled_theme_slugs, $is_theme_deletion ); |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | |||
| 78 | $newly_enabled_theme_names = array_keys( array_diff_key( $value, $old_value ) ); |
||
| 79 | $newly_enabled_themes = $this->get_theme_details_for_slugs( $newly_enabled_theme_names ); |
||
| 80 | /** |
||
| 81 | * Trigger action to alert $callable sync listener that network themes were enabled |
||
| 82 | * |
||
| 83 | * @since 5.0.0 |
||
| 84 | * |
||
| 85 | * @param mixed $newly_enabled_themes , Array of info about network enabled themes |
||
| 86 | * @param mixed $all_enabled_theme_slugs, Array of slugs of all enabled themes |
||
| 87 | */ |
||
| 88 | do_action( 'jetpack_network_enabled_themes', $newly_enabled_themes, $all_enabled_theme_slugs ); |
||
| 89 | } |
||
| 90 | |||
| 91 | private function get_theme_details_for_slugs( $theme_slugs ) { |
||
| 92 | $theme_data = array(); |
||
| 93 | foreach ( $theme_slugs as $slug ) { |
||
| 94 | $theme = wp_get_theme( $slug ); |
||
| 95 | $theme_data[ $slug ] = array( |
||
| 96 | 'name' => $theme->get( 'Name' ), |
||
| 97 | 'version' => $theme->get( 'Version' ), |
||
| 98 | 'uri' => $theme->get( 'ThemeURI' ), |
||
| 99 | 'slug' => $slug, |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | return $theme_data; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function detect_theme_edit( $redirect_url ) { |
||
| 106 | $url = wp_parse_url( admin_url( $redirect_url ) ); |
||
| 107 | $theme_editor_url = wp_parse_url( admin_url( 'theme-editor.php' ) ); |
||
| 108 | |||
| 109 | if ( $theme_editor_url['path'] !== $url['path'] ) { |
||
| 110 | return $redirect_url; |
||
| 111 | } |
||
| 112 | |||
| 113 | $query_params = array(); |
||
| 114 | wp_parse_str( $url['query'], $query_params ); |
||
| 115 | if ( |
||
| 116 | ! isset( $_POST['newcontent'] ) || |
||
| 117 | ! isset( $query_params['file'] ) || |
||
| 118 | ! isset( $query_params['theme'] ) || |
||
| 119 | ! isset( $query_params['updated'] ) |
||
| 120 | ) { |
||
| 121 | return $redirect_url; |
||
| 122 | } |
||
| 123 | $theme = wp_get_theme( $query_params['theme'] ); |
||
| 124 | $theme_data = array( |
||
| 125 | 'name' => $theme->get('Name'), |
||
| 126 | 'version' => $theme->get('Version'), |
||
| 127 | 'uri' => $theme->get( 'ThemeURI' ), |
||
| 128 | ); |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Trigger action to alert $callable sync listener that a theme was edited |
||
| 132 | * |
||
| 133 | * @since 5.0.0 |
||
| 134 | * |
||
| 135 | * @param string $query_params['theme'], Slug of edited theme |
||
| 136 | * @param string $theme_data, Information about edited them |
||
| 137 | */ |
||
| 138 | do_action( 'jetpack_edited_theme', $query_params['theme'], $theme_data ); |
||
| 139 | |||
| 140 | return $redirect_url; |
||
| 141 | } |
||
| 142 | |||
| 143 | public function detect_theme_deletion() { |
||
| 144 | $delete_theme_call = $this->get_delete_theme_call(); |
||
| 145 | if ( empty( $delete_theme_call ) ) { |
||
| 146 | return; |
||
| 147 | } |
||
| 148 | |||
| 149 | $slug = $delete_theme_call['args'][0]; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Signals to the sync listener that a theme was deleted and a sync action |
||
| 153 | * reflecting the deletion and theme slug should be sent |
||
| 154 | * |
||
| 155 | * @since 5.0.0 |
||
| 156 | * |
||
| 157 | * @param string $slug Theme slug |
||
| 158 | */ |
||
| 159 | do_action( 'jetpack_deleted_theme', $slug ); |
||
| 160 | } |
||
| 161 | |||
| 162 | public function check_upgrader( $upgrader, $details) { |
||
| 204 | |||
| 205 | public function init_full_sync_listeners( $callable ) { |
||
| 208 | |||
| 209 | public function sync_theme_support() { |
||
| 220 | |||
| 221 | public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) { |
||
| 234 | |||
| 235 | public function estimate_full_sync_actions( $config ) { |
||
| 238 | |||
| 239 | public function init_before_send() { |
||
| 242 | |||
| 243 | function get_full_sync_actions() { |
||
| 246 | |||
| 247 | function expand_theme_data() { |
||
| 250 | |||
| 251 | function get_widget_name( $widget_id ) { |
||
| 255 | |||
| 256 | function get_sidebar_name( $sidebar_id ) { |
||
| 260 | |||
| 261 | function sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar ) { |
||
| 262 | $added_widgets = array_diff( $new_widgets, $old_widgets ); |
||
| 263 | if ( empty( $added_widgets ) ) { |
||
| 264 | return array(); |
||
| 265 | } |
||
| 298 | |||
| 299 | function sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $inactive_widgets ) { |
||
| 331 | |||
| 332 | function sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar ) { |
||
| 356 | |||
| 357 | function sync_sidebar_widgets_actions( $old_value, $new_value ) { |
||
| 411 | |||
| 412 | private function get_theme_support_info() { |
||
| 430 | |||
| 431 | private function get_delete_theme_call() { |
||
| 442 | } |
||
| 443 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.