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 | |||
| 9 | public static function get_modules() { |
||
| 10 | require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php' ); |
||
| 11 | |||
| 12 | return Jetpack_Admin::init()->get_modules(); |
||
| 13 | } |
||
| 14 | |||
| 15 | public static function get_taxonomies() { |
||
| 16 | global $wp_taxonomies; |
||
| 17 | |||
| 18 | return $wp_taxonomies; |
||
| 19 | } |
||
| 20 | |||
| 21 | public static function get_post_types() { |
||
| 26 | |||
| 27 | public static function get_post_type_features() { |
||
| 28 | global $_wp_post_type_features; |
||
| 32 | |||
| 33 | public static function rest_api_allowed_post_types() { |
||
| 37 | |||
| 38 | public static function rest_api_allowed_public_metadata() { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Finds out if a site is using a version control system. |
||
| 45 | * @return bool |
||
| 46 | **/ |
||
| 47 | public static function is_version_controlled() { |
||
| 48 | |||
| 49 | if ( ! class_exists( 'WP_Automatic_Updater' ) ) { |
||
| 50 | require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' ); |
||
| 51 | } |
||
| 52 | $updater = new WP_Automatic_Updater(); |
||
| 53 | |||
| 54 | return (bool) strval( $updater->is_vcs_checkout( $context = ABSPATH ) ); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Returns true if the site has file write access false otherwise. |
||
| 59 | * @return bool |
||
| 60 | **/ |
||
| 61 | public static function file_system_write_access() { |
||
| 82 | |||
| 83 | public static function home_url() { |
||
| 86 | |||
| 87 | public static function site_url() { |
||
| 90 | |||
| 91 | public static function main_network_site_url() { |
||
| 94 | |||
| 95 | public static function preserve_scheme( $option, $url_function, $normalize_www = false ) { |
||
| 96 | $previous_https_value = isset( $_SERVER['HTTPS'] ) ? $_SERVER['HTTPS'] : null; |
||
| 97 | $_SERVER['HTTPS'] = 'off'; |
||
| 98 | $url = call_user_func( $url_function ); |
||
| 99 | $option_url = get_option( $option ); |
||
| 100 | if ( $previous_https_value ) { |
||
| 101 | $_SERVER['HTTPS'] = $previous_https_value; |
||
| 102 | } else { |
||
| 103 | unset( $_SERVER['HTTPS'] ); |
||
| 104 | } |
||
| 105 | |||
| 106 | if ( $option_url === $url ) { |
||
| 107 | return $url; |
||
| 108 | } |
||
| 109 | |||
| 110 | // turn them both into parsed format |
||
| 111 | $option_url = parse_url( $option_url ); |
||
| 112 | $url = parse_url( $url ); |
||
| 113 | |||
| 114 | if ( $normalize_www ) { |
||
| 115 | View Code Duplication | if ( $url['host'] === "www.{$option_url[ 'host' ]}" ) { |
|
| 116 | // remove www if not present in option URL |
||
| 117 | $url['host'] = $option_url['host']; |
||
| 118 | } |
||
| 119 | View Code Duplication | if ( $option_url['host'] === "www.{$url[ 'host' ]}" ) { |
|
| 120 | // add www if present in option URL |
||
| 121 | $url['host'] = $option_url['host']; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | if ( $url['host'] === $option_url['host'] ) { |
||
| 126 | $url['scheme'] = $option_url['scheme']; |
||
| 127 | // return set_url_scheme( $current_url, $option_url['scheme'] ); |
||
| 128 | } |
||
| 129 | |||
| 130 | $normalized_url = "{$url['scheme']}://{$url['host']}"; |
||
| 131 | |||
| 132 | if ( isset( $url['path'] ) ) { |
||
| 133 | $normalized_url .= "{$url['path']}"; |
||
| 134 | } |
||
| 135 | |||
| 136 | if ( isset( $url['query'] ) ) { |
||
| 137 | $normalized_url .= "?{$url['query']}"; |
||
| 138 | } |
||
| 139 | |||
| 140 | return $normalized_url; |
||
| 141 | } |
||
| 142 | |||
| 143 | public static function get_plugins() { |
||
| 151 | |||
| 152 | public static function wp_version() { |
||
| 157 | } |
||
| 158 |