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 |
||
| 4 | abstract class Abstract_Jetpack_Site extends SAL_Site { |
||
| 5 | abstract protected function get_constant( $name ); |
||
| 6 | |||
| 7 | abstract protected function current_theme_supports( $feature_name ); |
||
| 8 | |||
| 9 | abstract protected function get_theme_support( $feature_name ); |
||
| 10 | |||
| 11 | abstract protected function get_mock_option( $name ); |
||
| 12 | |||
| 13 | abstract public function get_jetpack_version(); |
||
| 14 | |||
| 15 | abstract public 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_version_controlled(); |
||
| 26 | |||
| 27 | abstract protected function file_system_write_access(); |
||
| 28 | |||
| 29 | function before_render() { |
||
| 30 | } |
||
| 31 | |||
| 32 | function after_render( &$response ) { |
||
| 33 | // Add the updates only make them visible if the user has manage options permission and the site is the main site of the network |
||
| 34 | if ( current_user_can( 'manage_options' ) && $this->is_main_site( $response ) ) { |
||
| 35 | $jetpack_update = $this->get_updates(); |
||
| 36 | if ( ! empty( $jetpack_update ) ) { |
||
| 37 | // In previous version of Jetpack 3.4, 3.5, 3.6 we synced the wp_version into to jetpack_updates |
||
| 38 | unset( $jetpack_update['wp_version'] ); |
||
| 39 | // In previous version of Jetpack 3.4, 3.5, 3.6 we synced the site_is_version_controlled into to jetpack_updates |
||
| 40 | unset( $jetpack_update['site_is_version_controlled'] ); |
||
| 41 | |||
| 42 | $response['updates'] = $jetpack_update; |
||
| 43 | } |
||
| 44 | } |
||
| 45 | } |
||
| 46 | |||
| 47 | function after_render_options( &$options ) { |
||
| 48 | |||
| 49 | $options['jetpack_version'] = $this->get_jetpack_version(); |
||
| 50 | |||
| 51 | if ( $main_network_site = $this->main_network_site() ) { |
||
| 52 | $options['main_network_site'] = (string) rtrim( $main_network_site, '/' ); |
||
| 53 | } |
||
| 54 | |||
| 55 | if ( is_array( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) ) { |
||
| 56 | $options['active_modules'] = (array) array_values( $active_modules ); |
||
| 57 | } |
||
| 58 | |||
| 59 | $options['software_version'] = (string) $this->wp_version(); |
||
| 60 | $options['max_upload_size'] = $this->max_upload_size(); |
||
| 61 | $options['wp_memory_limit'] = $this->wp_memory_limit(); |
||
| 62 | $options['wp_max_memory_limit'] = $this->wp_max_memory_limit(); |
||
| 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_multisite(); |
||
| 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 featured_images_enabled() { |
||
| 95 | return $this->current_theme_supports( 'post-thumbnails' ); |
||
| 96 | } |
||
| 97 | |||
| 98 | function get_post_formats() { |
||
| 99 | // deprecated - see separate endpoint. get a list of supported post formats |
||
| 100 | $all_formats = get_post_format_strings(); |
||
| 101 | $supported = $this->get_theme_support( 'post-formats' ); |
||
| 113 | |||
| 114 | function get_icon() { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Private methods |
||
| 142 | **/ |
||
| 143 | |||
| 144 | private function is_main_site( $response ) { |
||
| 155 | |||
| 156 | // For Jetpack sites this will always return false |
||
| 157 | protected function is_a8c_publication( $post_id ) { |
||
| 158 | return false; |
||
| 159 | } |
||
| 160 | } |
||
| 161 |