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_Functions 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_Functions, and based on these observations, apply Extract Interface, too.
| 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() { |
||
| 30 | |||
| 31 | public static function get_shortcodes() { |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Removes any callback data since we will not be able to process it on our side anyways. |
||
| 38 | */ |
||
| 39 | public static function sanitize_taxonomy( $taxonomy ) { |
||
| 65 | |||
| 66 | public static function get_post_types() { |
||
| 80 | |||
| 81 | public static function sanitize_post_type( $post_type ) { |
||
| 91 | |||
| 92 | public static function expand_synced_post_type( $sanitized_post_type, $post_type ) { |
||
| 101 | |||
| 102 | public static function get_post_type_features() { |
||
| 107 | |||
| 108 | public static function get_hosting_provider() { |
||
| 126 | |||
| 127 | public static function rest_api_allowed_post_types() { |
||
| 131 | |||
| 132 | public static function rest_api_allowed_public_metadata() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Finds out if a site is using a version control system. |
||
| 139 | * |
||
| 140 | * @return bool |
||
| 141 | **/ |
||
| 142 | public static function is_version_controlled() { |
||
| 143 | |||
| 144 | if ( ! class_exists( 'WP_Automatic_Updater' ) ) { |
||
| 145 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
||
| 146 | } |
||
| 147 | $updater = new WP_Automatic_Updater(); |
||
| 148 | |||
| 149 | return (bool) strval( $updater->is_vcs_checkout( $context = ABSPATH ) ); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns true if the site has file write access false otherwise. |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | **/ |
||
| 157 | public static function file_system_write_access() { |
||
| 158 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
| 159 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
||
| 160 | } |
||
| 161 | |||
| 162 | require_once ABSPATH . 'wp-admin/includes/template.php'; |
||
| 163 | |||
| 164 | $filesystem_method = get_filesystem_method(); |
||
| 165 | if ( 'direct' === $filesystem_method ) { |
||
| 166 | return true; |
||
| 167 | } |
||
| 168 | |||
| 169 | ob_start(); |
||
| 170 | |||
| 171 | if ( ! function_exists( 'request_filesystem_credentials' ) ) { |
||
| 172 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
||
| 173 | } |
||
| 174 | |||
| 175 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
| 176 | ob_end_clean(); |
||
| 177 | if ( $filesystem_credentials_are_stored ) { |
||
| 178 | return true; |
||
| 179 | } |
||
| 180 | |||
| 181 | return false; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Helper function that is used when getting home or siteurl values. Decides |
||
| 186 | * whether to get the raw or filtered value. |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public static function get_raw_or_filtered_url( $url_type ) { |
||
| 191 | $url_function = ( 'home' == $url_type ) |
||
| 192 | ? 'home_url' |
||
| 193 | : 'site_url'; |
||
| 194 | |||
| 195 | if ( |
||
| 196 | ! Jetpack_Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) || |
||
| 197 | Jetpack_Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) |
||
| 198 | ) { |
||
| 199 | $scheme = is_ssl() ? 'https' : 'http'; |
||
| 200 | $url = self::get_raw_url( $url_type ); |
||
| 201 | $url = set_url_scheme( $url, $scheme ); |
||
| 202 | } else { |
||
| 203 | $url = self::normalize_www_in_url( $url_type, $url_function ); |
||
| 204 | } |
||
| 205 | |||
| 206 | return self::get_protocol_normalized_url( $url_function, $url ); |
||
| 207 | } |
||
| 208 | |||
| 209 | public static function home_url() { |
||
| 210 | $url = self::get_raw_or_filtered_url( 'home' ); |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Allows overriding of the home_url value that is synced back to WordPress.com. |
||
| 214 | * |
||
| 215 | * @since 5.2.0 |
||
| 216 | * |
||
| 217 | * @param string $home_url |
||
| 218 | */ |
||
| 219 | return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) ); |
||
| 220 | } |
||
| 221 | |||
| 222 | public static function site_url() { |
||
| 223 | $url = self::get_raw_or_filtered_url( 'siteurl' ); |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Allows overriding of the site_url value that is synced back to WordPress.com. |
||
| 227 | * |
||
| 228 | * @since 5.2.0 |
||
| 229 | * |
||
| 230 | * @param string $site_url |
||
| 231 | */ |
||
| 232 | return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) ); |
||
| 233 | } |
||
| 234 | |||
| 235 | public static function main_network_site_url() { |
||
| 236 | return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() ); |
||
| 237 | } |
||
| 238 | |||
| 239 | public static function get_protocol_normalized_url( $callable, $new_value ) { |
||
| 240 | $option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable; |
||
| 241 | |||
| 242 | $parsed_url = wp_parse_url( $new_value ); |
||
| 243 | if ( ! $parsed_url ) { |
||
| 244 | return $new_value; |
||
| 245 | } |
||
| 246 | if ( array_key_exists( 'scheme', $parsed_url ) ) { |
||
| 247 | $scheme = $parsed_url['scheme']; |
||
| 248 | } else { |
||
| 249 | $scheme = ''; |
||
| 250 | } |
||
| 251 | $scheme_history = get_option( $option_key, array() ); |
||
| 252 | $scheme_history[] = $scheme; |
||
| 253 | |||
| 254 | // Limit length to self::HTTPS_CHECK_HISTORY |
||
| 255 | $scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) ); |
||
| 256 | |||
| 257 | update_option( $option_key, $scheme_history ); |
||
| 258 | |||
| 259 | $forced_scheme = in_array( 'https', $scheme_history ) ? 'https' : 'http'; |
||
| 260 | |||
| 261 | return set_url_scheme( $new_value, $forced_scheme ); |
||
| 262 | } |
||
| 263 | |||
| 264 | public static function get_raw_url( $option_name ) { |
||
| 265 | $value = null; |
||
| 266 | $constant = ( 'home' == $option_name ) |
||
| 267 | ? 'WP_HOME' |
||
| 268 | : 'WP_SITEURL'; |
||
| 269 | |||
| 270 | // Since we disregard the constant for multisites in ms-default-filters.php, |
||
| 271 | // let's also use the db value if this is a multisite. |
||
| 272 | if ( ! is_multisite() && Jetpack_Constants::is_defined( $constant ) ) { |
||
| 273 | $value = Jetpack_Constants::get_constant( $constant ); |
||
| 274 | } else { |
||
| 275 | // Let's get the option from the database so that we can bypass filters. This will help |
||
| 276 | // ensure that we get more uniform values. |
||
| 277 | $value = Jetpack_Options::get_raw_option( $option_name ); |
||
| 278 | } |
||
| 279 | |||
| 280 | return $value; |
||
| 281 | } |
||
| 282 | |||
| 283 | public static function normalize_www_in_url( $option, $url_function ) { |
||
| 284 | $url = wp_parse_url( call_user_func( $url_function ) ); |
||
| 285 | $option_url = wp_parse_url( get_option( $option ) ); |
||
| 286 | |||
| 287 | if ( ! $option_url || ! $url ) { |
||
| 288 | return $url; |
||
| 289 | } |
||
| 290 | |||
| 291 | View Code Duplication | if ( $url['host'] === "www.{$option_url[ 'host' ]}" ) { |
|
| 292 | // remove www if not present in option URL |
||
| 293 | $url['host'] = $option_url['host']; |
||
| 294 | } |
||
| 295 | View Code Duplication | if ( $option_url['host'] === "www.{$url[ 'host' ]}" ) { |
|
| 296 | // add www if present in option URL |
||
| 297 | $url['host'] = $option_url['host']; |
||
| 298 | } |
||
| 299 | |||
| 300 | $normalized_url = "{$url['scheme']}://{$url['host']}"; |
||
| 301 | if ( isset( $url['path'] ) ) { |
||
| 302 | $normalized_url .= "{$url['path']}"; |
||
| 303 | } |
||
| 304 | |||
| 305 | if ( isset( $url['query'] ) ) { |
||
| 306 | $normalized_url .= "?{$url['query']}"; |
||
| 307 | } |
||
| 308 | |||
| 309 | return $normalized_url; |
||
| 310 | } |
||
| 311 | |||
| 312 | public static function get_plugins() { |
||
| 313 | if ( ! function_exists( 'get_plugins' ) ) { |
||
| 314 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
||
| 318 | return apply_filters( 'all_plugins', get_plugins() ); |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get custom action link tags that the plugin is using |
||
| 323 | * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name) |
||
| 324 | * |
||
| 325 | * @return array of plugin action links (key: link name value: url) |
||
| 326 | */ |
||
| 327 | public static function get_plugins_action_links( $plugin_file_singular = null ) { |
||
| 328 | // Some sites may have DOM disabled in PHP fail early |
||
| 329 | if ( ! class_exists( 'DOMDocument' ) ) { |
||
| 330 | return array(); |
||
| 331 | } |
||
| 332 | $plugins_action_links = get_option( 'jetpack_plugin_api_action_links', array() ); |
||
| 333 | if ( ! empty( $plugins_action_links ) ) { |
||
| 334 | if ( is_null( $plugin_file_singular ) ) { |
||
| 335 | return $plugins_action_links; |
||
| 336 | } |
||
| 337 | return ( isset( $plugins_action_links[ $plugin_file_singular ] ) ? $plugins_action_links[ $plugin_file_singular ] : null ); |
||
| 338 | } |
||
| 339 | return array(); |
||
| 340 | } |
||
| 341 | |||
| 342 | public static function wp_version() { |
||
| 343 | global $wp_version; |
||
| 344 | return $wp_version; |
||
| 345 | } |
||
| 346 | |||
| 347 | public static function site_icon_url( $size = 512 ) { |
||
| 348 | $site_icon = get_site_icon_url( $size ); |
||
| 349 | return $site_icon ? $site_icon : get_option( 'jetpack_site_icon_url' ); |
||
| 350 | } |
||
| 351 | |||
| 352 | public static function roles() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Determine time zone from WordPress' options "timezone_string" |
||
| 359 | * and "gmt_offset". |
||
| 360 | * |
||
| 361 | * 1. Check if `timezone_string` is set and return it. |
||
| 362 | * 2. Check if `gmt_offset` is set, formats UTC-offset from it and return it. |
||
| 363 | * 3. Default to "UTC+0" if nothing is set. |
||
| 364 | * |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public static function get_timezone() { |
||
| 387 | // New in WP 5.1 |
||
| 388 | public static function get_paused_themes() { |
||
| 395 | // New in WP 5.1 |
||
| 396 | public static function get_paused_plugins() { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Determine the class that extends `WP_Importer` which is responsible for |
||
| 406 | * the current action. Designed to be used within an action handler. |
||
| 407 | * |
||
| 408 | * @return string The name of the calling class, or 'unknown'. |
||
| 409 | */ |
||
| 410 | public static function get_calling_importer_class() { |
||
| 411 | // If WP_Importer doesn't exist, neither will any importer that extends it |
||
| 412 | if ( ! class_exists( 'WP_Importer' ) ){ |
||
| 413 | return 'unknown'; |
||
| 414 | } |
||
| 415 | |||
| 416 | $action = current_filter(); |
||
| 452 | } |
||
| 453 |
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.