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 |
||
| 7 | class Jetpack_Sync_Functions { |
||
| 8 | const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_'; |
||
| 9 | const HTTPS_CHECK_HISTORY = 5; |
||
| 10 | |||
| 11 | public static function get_modules() { |
||
| 16 | |||
| 17 | public static function get_taxonomies() { |
||
| 22 | |||
| 23 | public static function get_post_types() { |
||
| 28 | |||
| 29 | public static function get_post_type_features() { |
||
| 34 | |||
| 35 | public static function get_hosting_provider() { |
||
| 50 | |||
| 51 | public static function rest_api_allowed_post_types() { |
||
| 55 | |||
| 56 | public static function rest_api_allowed_public_metadata() { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Finds out if a site is using a version control system. |
||
| 63 | * @return bool |
||
| 64 | **/ |
||
| 65 | public static function is_version_controlled() { |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns true if the site has file write access false otherwise. |
||
| 77 | * @return bool |
||
| 78 | **/ |
||
| 79 | public static function file_system_write_access() { |
||
| 100 | |||
| 101 | public static function home_url() { |
||
| 102 | return self::get_protocol_normalized_url( |
||
| 103 | 'home_url', |
||
| 104 | self::normalize_www_in_url( 'home', 'home_url' ) |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | |||
| 108 | public static function site_url() { |
||
| 109 | return self::get_protocol_normalized_url( |
||
| 110 | 'site_url', |
||
| 111 | self::normalize_www_in_url( 'siteurl', 'site_url' ) |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | public static function main_network_site_url() { |
||
| 116 | return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() ); |
||
| 117 | } |
||
| 118 | |||
| 119 | public static function get_protocol_normalized_url( $callable, $new_value ) { |
||
| 120 | $option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable; |
||
| 121 | |||
| 122 | $parsed_url = wp_parse_url( $new_value ); |
||
| 123 | if ( ! $parsed_url ) { |
||
| 124 | return $new_value; |
||
| 125 | } |
||
| 126 | |||
| 127 | $scheme = $parsed_url['scheme']; |
||
| 128 | $scheme_history = get_option( $option_key, array() ); |
||
| 129 | $scheme_history[] = $scheme; |
||
| 130 | |||
| 131 | // Limit length to self::HTTPS_CHECK_HISTORY |
||
| 132 | $scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) ); |
||
| 133 | |||
| 134 | update_option( $option_key, $scheme_history ); |
||
| 135 | |||
| 136 | $forced_scheme = in_array( 'https', $scheme_history ) ? 'https' : 'http'; |
||
| 137 | |||
| 138 | return set_url_scheme( $new_value, $forced_scheme ); |
||
| 139 | } |
||
| 140 | |||
| 141 | public static function normalize_www_in_url( $option, $url_function ) { |
||
| 142 | $url = wp_parse_url( call_user_func( $url_function ) ); |
||
| 143 | $option_url = wp_parse_url( get_option( $option ) ); |
||
| 144 | |||
| 145 | if ( ! $option_url || ! $url ) { |
||
| 146 | return $url; |
||
| 147 | } |
||
| 148 | |||
| 149 | View Code Duplication | if ( $url[ 'host' ] === "www.{$option_url[ 'host' ]}" ) { |
|
| 150 | // remove www if not present in option URL |
||
| 151 | $url[ 'host' ] = $option_url[ 'host' ]; |
||
| 152 | } |
||
| 153 | View Code Duplication | if ( $option_url[ 'host' ] === "www.{$url[ 'host' ]}" ) { |
|
| 154 | // add www if present in option URL |
||
| 155 | $url[ 'host' ] = $option_url[ 'host' ]; |
||
| 156 | } |
||
| 157 | |||
| 158 | $normalized_url = "{$url['scheme']}://{$url['host']}"; |
||
| 159 | if ( isset( $url['path'] ) ) { |
||
| 160 | $normalized_url .= "{$url['path']}"; |
||
| 161 | } |
||
| 162 | |||
| 163 | if ( isset( $url['query'] ) ) { |
||
| 164 | $normalized_url .= "?{$url['query']}"; |
||
| 165 | } |
||
| 166 | |||
| 167 | return $normalized_url; |
||
| 168 | } |
||
| 169 | |||
| 170 | public static function get_plugins() { |
||
| 171 | if ( ! function_exists( 'get_plugins' ) ) { |
||
| 172 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
||
| 176 | return apply_filters( 'all_plugins', get_plugins() ); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Returns items inserted to wp-admin admin page menu by custom plugins and themes. |
||
| 181 | * They usually do that bu hooking in admin_menu and calling add_menu_page and add_submenu_page. |
||
| 182 | * @return array |
||
| 183 | **/ |
||
| 184 | public static function get_custom_admin_menu_items() { |
||
| 185 | global $menu, $submenu; |
||
| 186 | |||
| 187 | /** Since some of the menu items are displayed only for certain capability, we need user switcharoo */ |
||
| 188 | $current_user_id = get_current_user_id(); |
||
| 189 | wp_set_current_user( Jetpack_Options::get_option( 'master_user' ) ); |
||
| 190 | /** add_menu_page and add_submenu_page hook into admin_menu. Documented in wp-admin/includes/menu.php */ |
||
| 191 | do_action( 'admin_menu', '' ); |
||
| 192 | /** Lets clean up user switch */ |
||
| 193 | wp_set_current_user( $current_user_id ); |
||
| 194 | return array( menu => $menu, submenu => $submenu ); |
||
| 195 | } |
||
| 196 | |||
| 197 | public static function wp_version() { |
||
| 202 | |||
| 203 | public static function site_icon_url() { |
||
| 210 | } |
||
| 211 |