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 |
||
6 | abstract class Abstract_Jetpack_Site extends SAL_Site { |
||
7 | abstract protected function get_constant( $name ); |
||
8 | |||
9 | abstract protected function current_theme_supports( $feature_name ); |
||
10 | |||
11 | abstract protected function get_theme_support( $feature_name ); |
||
12 | |||
13 | abstract protected function get_jetpack_version(); |
||
14 | |||
15 | abstract protected function get_updates(); |
||
16 | |||
17 | abstract protected function main_network_site(); |
||
18 | |||
19 | abstract protected function wp_version(); |
||
20 | |||
21 | abstract protected function max_upload_size(); |
||
22 | |||
23 | abstract protected function is_main_network(); |
||
24 | |||
25 | abstract protected function is_multi_site(); |
||
26 | |||
27 | abstract protected function is_version_controlled(); |
||
28 | |||
29 | abstract protected function file_system_write_access(); |
||
30 | |||
31 | function before_render() { |
||
33 | |||
34 | function after_render( &$response ) { |
||
35 | // Add the updates only make them visible if the user has manage options permission and the site is the main site of the network |
||
36 | if ( current_user_can( 'manage_options' ) && $this->is_main_site( $response ) ) { |
||
37 | $jetpack_update = $this->get_updates(); |
||
38 | if ( ! empty( $jetpack_update ) ) { |
||
39 | // In previous version of Jetpack 3.4, 3.5, 3.6 we synced the wp_version into to jetpack_updates |
||
40 | unset( $jetpack_update['wp_version'] ); |
||
41 | // In previous version of Jetpack 3.4, 3.5, 3.6 we synced the site_is_version_controlled into to jetpack_updates |
||
42 | unset( $jetpack_update['site_is_version_controlled'] ); |
||
43 | |||
44 | $response['updates'] = $jetpack_update; |
||
45 | } |
||
46 | } |
||
47 | } |
||
48 | |||
49 | function after_render_options( &$options ) { |
||
50 | |||
51 | $options['jetpack_version'] = $this->get_jetpack_version(); |
||
52 | |||
53 | if ( $main_network_site = $this->main_network_site() ) { |
||
54 | $options['main_network_site'] = (string) rtrim( $main_network_site, '/' ); |
||
55 | } |
||
56 | |||
57 | if ( is_array( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) ) { |
||
58 | $options['active_modules'] = (array) array_values( $active_modules ); |
||
59 | } |
||
60 | |||
61 | $options['software_version'] = (string) $this->wp_version(); |
||
62 | $options['max_upload_size'] = $this->max_upload_size(); |
||
63 | |||
64 | // Sites have to prove that they are not main_network site. |
||
65 | // If the sync happends right then we should be able to see that we are not dealing with a network site |
||
66 | $options['is_multi_network'] = (bool) $this->is_main_network(); |
||
67 | $options['is_multi_site'] = (bool) $this->is_multi_site(); |
||
68 | |||
69 | $file_mod_disabled_reasons = array_keys( array_filter( array( |
||
70 | 'automatic_updater_disabled' => (bool) $this->get_constant( 'AUTOMATIC_UPDATER_DISABLED' ), |
||
71 | // WP AUTO UPDATE CORE defaults to minor, '1' if true and '0' if set to false. |
||
72 | 'wp_auto_update_core_disabled' => ! ( (bool) $this->get_constant( 'WP_AUTO_UPDATE_CORE' ) ), |
||
73 | 'is_version_controlled' => (bool) $this->is_version_controlled(), |
||
74 | // By default we assume that site does have system write access if the value is not set yet. |
||
75 | 'has_no_file_system_write_access' => ! (bool) $this->file_system_write_access(), |
||
76 | 'disallow_file_mods' => (bool) $this->get_constant( 'DISALLOW_FILE_MODS' ), |
||
77 | ) ) ); |
||
78 | |||
79 | $options['file_mod_disabled'] = empty( $file_mod_disabled_reasons ) ? false : $file_mod_disabled_reasons; |
||
80 | } |
||
81 | |||
82 | function get_jetpack_modules() { |
||
83 | if ( is_user_member_of_blog() ) { |
||
84 | return array_values( Jetpack_Options::get_option( 'active_modules', array() ) ); |
||
85 | } |
||
86 | |||
87 | return null; |
||
88 | } |
||
89 | |||
90 | function is_vip() { |
||
93 | |||
94 | function is_multisite() { |
||
95 | return (bool) is_multisite(); |
||
96 | } |
||
97 | |||
98 | function is_single_user_site() { |
||
99 | return (bool) Jetpack::is_single_user_site(); |
||
100 | } |
||
101 | |||
102 | function featured_images_enabled() { |
||
105 | |||
106 | function get_post_formats() { |
||
121 | |||
122 | function get_icon() { |
||
123 | $icon_id = get_option( 'site_icon' ); |
||
124 | if ( empty( $icon_id ) ) { |
||
125 | $icon_id = Jetpack_Options::get_option( 'site_icon_id' ); |
||
126 | } |
||
127 | |||
128 | if ( empty( $icon_id ) ) { |
||
129 | return null; |
||
130 | } |
||
131 | |||
132 | $icon = array_filter( array( |
||
133 | 'img' => wp_get_attachment_image_url( $icon_id, 'full' ), |
||
134 | 'ico' => wp_get_attachment_image_url( $icon_id, array( 16, 16 ) ) |
||
135 | ) ); |
||
147 | |||
148 | /** |
||
149 | * Private methods |
||
150 | **/ |
||
151 | |||
152 | private function is_main_site( $response ) { |
||
163 | } |
||
164 |