Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Utility functions to generate data synced to wpcom |
||
| 4 | * |
||
| 5 | * @package automattic/jetpack-sync |
||
| 6 | */ |
||
| 7 | |||
| 8 | namespace Automattic\Jetpack\Sync; |
||
| 9 | |||
| 10 | use Automattic\Jetpack\Constants; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Utility functions to generate data synced to wpcom |
||
| 14 | */ |
||
| 15 | class Functions { |
||
| 16 | const HTTPS_CHECK_OPTION_PREFIX = 'jetpack_sync_https_history_'; |
||
| 17 | const HTTPS_CHECK_HISTORY = 5; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Return array of Jetpack modules. |
||
| 21 | * |
||
| 22 | * @return array |
||
| 23 | */ |
||
| 24 | public static function get_modules() { |
||
| 25 | require_once JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php'; |
||
| 26 | |||
| 27 | return \Jetpack_Admin::init()->get_modules(); |
||
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Return array of taxonomies registered on the site. |
||
| 32 | * |
||
| 33 | * @return array |
||
| 34 | */ |
||
| 35 | public static function get_taxonomies() { |
||
| 36 | global $wp_taxonomies; |
||
| 37 | $wp_taxonomies_without_callbacks = array(); |
||
| 38 | foreach ( $wp_taxonomies as $taxonomy_name => $taxonomy ) { |
||
| 39 | $sanitized_taxonomy = self::sanitize_taxonomy( $taxonomy ); |
||
| 40 | if ( ! empty( $sanitized_taxonomy ) ) { |
||
| 41 | $wp_taxonomies_without_callbacks[ $taxonomy_name ] = $sanitized_taxonomy; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | return $wp_taxonomies_without_callbacks; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Return array of registered shortcodes. |
||
| 49 | * |
||
| 50 | * @return array |
||
| 51 | */ |
||
| 52 | public static function get_shortcodes() { |
||
| 53 | global $shortcode_tags; |
||
| 54 | return array_keys( $shortcode_tags ); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Removes any callback data since we will not be able to process it on our side anyways. |
||
| 59 | * |
||
| 60 | * @param \WP_Taxonomy $taxonomy \WP_Taxonomy item. |
||
| 61 | * |
||
| 62 | * @return mixed|null |
||
| 63 | */ |
||
| 64 | public static function sanitize_taxonomy( $taxonomy ) { |
||
| 65 | |||
| 66 | // Lets clone the taxonomy object instead of modifing the global one. |
||
| 67 | $cloned_taxonomy = json_decode( wp_json_encode( $taxonomy ) ); |
||
| 68 | |||
| 69 | // recursive taxonomies are no fun. |
||
| 70 | if ( is_null( $cloned_taxonomy ) ) { |
||
| 71 | return null; |
||
| 72 | } |
||
| 73 | // Remove any meta_box_cb if they are not the default wp ones. |
||
| 74 | if ( isset( $cloned_taxonomy->meta_box_cb ) && |
||
| 75 | ! in_array( $cloned_taxonomy->meta_box_cb, array( 'post_tags_meta_box', 'post_categories_meta_box' ), true ) ) { |
||
| 76 | $cloned_taxonomy->meta_box_cb = null; |
||
| 77 | } |
||
| 78 | // Remove update call back. |
||
| 79 | if ( isset( $cloned_taxonomy->update_count_callback ) && |
||
| 80 | ! is_null( $cloned_taxonomy->update_count_callback ) ) { |
||
| 81 | $cloned_taxonomy->update_count_callback = null; |
||
| 82 | } |
||
| 83 | // Remove rest_controller_class if it something other then the default. |
||
| 84 | if ( isset( $cloned_taxonomy->rest_controller_class ) && |
||
| 85 | 'WP_REST_Terms_Controller' !== $cloned_taxonomy->rest_controller_class ) { |
||
| 86 | $cloned_taxonomy->rest_controller_class = null; |
||
| 87 | } |
||
| 88 | return $cloned_taxonomy; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Return array of registered post types. |
||
| 93 | * |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | public static function get_post_types() { |
||
| 97 | global $wp_post_types; |
||
| 98 | |||
| 99 | $post_types_without_callbacks = array(); |
||
| 100 | foreach ( $wp_post_types as $post_type_name => $post_type ) { |
||
| 101 | $sanitized_post_type = self::sanitize_post_type( $post_type ); |
||
| 102 | if ( ! empty( $sanitized_post_type ) ) { |
||
| 103 | $post_types_without_callbacks[ $post_type_name ] = $sanitized_post_type; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | return $post_types_without_callbacks; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Sanitizes by cloning post type object. |
||
| 111 | * |
||
| 112 | * @param object $post_type \WP_Post_Type. |
||
| 113 | * |
||
| 114 | * @return object |
||
| 115 | */ |
||
| 116 | public static function sanitize_post_type( $post_type ) { |
||
| 117 | // Lets clone the post type object instead of modifing the global one. |
||
| 118 | $sanitized_post_type = array(); |
||
| 119 | foreach ( Defaults::$default_post_type_attributes as $attribute_key => $default_value ) { |
||
| 120 | if ( isset( $post_type->{ $attribute_key } ) ) { |
||
| 121 | $sanitized_post_type[ $attribute_key ] = $post_type->{ $attribute_key }; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | return (object) $sanitized_post_type; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Return information about a synced post type. |
||
| 129 | * |
||
| 130 | * @param array $sanitized_post_type Array of args used in constructing \WP_Post_Type. |
||
| 131 | * @param string $post_type Post type name. |
||
| 132 | * |
||
| 133 | * @return object \WP_Post_Type |
||
| 134 | */ |
||
| 135 | public static function expand_synced_post_type( $sanitized_post_type, $post_type ) { |
||
| 136 | $post_type = sanitize_key( $post_type ); |
||
| 137 | $post_type_object = new \WP_Post_Type( $post_type, $sanitized_post_type ); |
||
| 138 | $post_type_object->add_supports(); |
||
| 139 | $post_type_object->add_rewrite_rules(); |
||
| 140 | $post_type_object->add_hooks(); |
||
| 141 | $post_type_object->register_taxonomies(); |
||
| 142 | return (object) $post_type_object; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns site's post_type_features. |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public static function get_post_type_features() { |
||
| 151 | global $_wp_post_type_features; |
||
| 152 | |||
| 153 | return $_wp_post_type_features; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Return hosting provider. |
||
| 158 | * |
||
| 159 | * Uses a set of known constants, classes, or functions to help determine the hosting platform. |
||
| 160 | * |
||
| 161 | * @return string Hosting provider. |
||
| 162 | */ |
||
| 163 | public static function get_hosting_provider() { |
||
| 164 | $hosting_provider_detection_methods = array( |
||
| 165 | 'get_hosting_provider_by_known_constant', |
||
| 166 | 'get_hosting_provider_by_known_class', |
||
| 167 | 'get_hosting_provider_by_known_function', |
||
| 168 | ); |
||
| 169 | |||
| 170 | $functions = new Functions(); |
||
| 171 | foreach ( $hosting_provider_detection_methods as $method ) { |
||
| 172 | $hosting_provider = call_user_func( array( $functions, $method ) ); |
||
| 173 | if ( false !== $hosting_provider ) { |
||
| 174 | return $hosting_provider; |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 178 | return 'unknown'; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Return a hosting provider using a set of known constants. |
||
| 183 | * |
||
| 184 | * @return mixed A host identifier string or false. |
||
| 185 | */ |
||
| 186 | public function get_hosting_provider_by_known_constant() { |
||
| 187 | $hosting_provider_constants = array( |
||
| 188 | 'GD_SYSTEM_PLUGIN_DIR' => 'gd-managed-wp', |
||
| 189 | 'MM_BASE_DIR' => 'bh', |
||
| 190 | 'PAGELYBIN' => 'pagely', |
||
| 191 | 'KINSTAMU_VERSION' => 'kinsta', |
||
| 192 | 'FLYWHEEL_CONFIG_DIR' => 'flywheel', |
||
| 193 | 'IS_PRESSABLE' => 'pressable', |
||
| 194 | 'VIP_GO_ENV' => 'vip-go', |
||
| 195 | ); |
||
| 196 | |||
| 197 | foreach ( $hosting_provider_constants as $constant => $constant_value ) { |
||
| 198 | if ( Constants::is_defined( $constant ) ) { |
||
| 199 | if ( 'VIP_GO_ENV' === $constant && false === Constants::get_constant( 'VIP_GO_ENV' ) ) { |
||
| 200 | continue; |
||
| 201 | } |
||
| 202 | return $constant_value; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | return false; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Return a hosting provider using a set of known classes. |
||
| 211 | * |
||
| 212 | * @return mixed A host identifier string or false. |
||
| 213 | */ |
||
| 214 | public function get_hosting_provider_by_known_class() { |
||
| 215 | $hosting_provider = false; |
||
| 216 | |||
| 217 | switch ( true ) { |
||
| 218 | case ( class_exists( '\\WPaaS\\Plugin' ) ): |
||
| 219 | $hosting_provider = 'gd-managed-wp'; |
||
| 220 | break; |
||
| 221 | } |
||
| 222 | |||
| 223 | return $hosting_provider; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Return a hosting provider using a set of known functions. |
||
| 228 | * |
||
| 229 | * @return mixed A host identifier string or false. |
||
| 230 | */ |
||
| 231 | public function get_hosting_provider_by_known_function() { |
||
| 232 | $hosting_provider = false; |
||
| 233 | |||
| 234 | switch ( true ) { |
||
| 235 | case ( function_exists( 'is_wpe' ) || function_exists( 'is_wpe_snapshot' ) ): |
||
| 236 | $hosting_provider = 'wpe'; |
||
| 237 | break; |
||
| 238 | } |
||
| 239 | |||
| 240 | return $hosting_provider; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Return array of allowed REST API post types. |
||
| 245 | * |
||
| 246 | * @return array Array of allowed post types. |
||
| 247 | */ |
||
| 248 | public static function rest_api_allowed_post_types() { |
||
| 249 | /** This filter is already documented in class.json-api-endpoints.php */ |
||
| 250 | return apply_filters( 'rest_api_allowed_post_types', array( 'post', 'page', 'revision' ) ); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Return array of allowed REST API public metadata. |
||
| 255 | * |
||
| 256 | * @return array Array of allowed metadata. |
||
| 257 | */ |
||
| 258 | public static function rest_api_allowed_public_metadata() { |
||
| 259 | /** This filter is documented in json-endpoints/class.wpcom-json-api-post-endpoint.php */ |
||
| 260 | return apply_filters( 'rest_api_allowed_public_metadata', array() ); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Finds out if a site is using a version control system. |
||
| 265 | * |
||
| 266 | * @return bool |
||
| 267 | **/ |
||
| 268 | public static function is_version_controlled() { |
||
| 269 | |||
| 270 | if ( ! class_exists( 'WP_Automatic_Updater' ) ) { |
||
| 271 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
||
| 272 | } |
||
| 273 | $updater = new \WP_Automatic_Updater(); |
||
| 274 | |||
| 275 | return (bool) (string) $updater->is_vcs_checkout( ABSPATH ); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Returns true if the site has file write access false otherwise. |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | **/ |
||
| 283 | public static function file_system_write_access() { |
||
| 284 | if ( ! function_exists( 'get_filesystem_method' ) ) { |
||
| 285 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
||
| 286 | } |
||
| 287 | |||
| 288 | require_once ABSPATH . 'wp-admin/includes/template.php'; |
||
| 289 | |||
| 290 | $filesystem_method = get_filesystem_method(); |
||
| 291 | if ( 'direct' === $filesystem_method ) { |
||
| 292 | return true; |
||
| 293 | } |
||
| 294 | |||
| 295 | ob_start(); |
||
| 296 | |||
| 297 | if ( ! function_exists( 'request_filesystem_credentials' ) ) { |
||
| 298 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
||
| 299 | } |
||
| 300 | |||
| 301 | $filesystem_credentials_are_stored = request_filesystem_credentials( self_admin_url() ); |
||
| 302 | ob_end_clean(); |
||
| 303 | if ( $filesystem_credentials_are_stored ) { |
||
| 304 | return true; |
||
| 305 | } |
||
| 306 | |||
| 307 | return false; |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Helper function that is used when getting home or siteurl values. Decides |
||
| 312 | * whether to get the raw or filtered value. |
||
| 313 | * |
||
| 314 | * @param string $url_type URL to get, home or siteurl. |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | public static function get_raw_or_filtered_url( $url_type ) { |
||
| 318 | $url_function = ( 'home' === $url_type ) |
||
| 319 | ? 'home_url' |
||
| 320 | : 'site_url'; |
||
| 321 | |||
| 322 | if ( |
||
| 323 | ! Constants::is_defined( 'JETPACK_SYNC_USE_RAW_URL' ) || |
||
| 324 | Constants::get_constant( 'JETPACK_SYNC_USE_RAW_URL' ) |
||
| 325 | ) { |
||
| 326 | $scheme = is_ssl() ? 'https' : 'http'; |
||
| 327 | $url = self::get_raw_url( $url_type ); |
||
| 328 | $url = set_url_scheme( $url, $scheme ); |
||
| 329 | } else { |
||
| 330 | $url = self::normalize_www_in_url( $url_type, $url_function ); |
||
| 331 | } |
||
| 332 | |||
| 333 | return self::get_protocol_normalized_url( $url_function, $url ); |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Return the escaped home_url. |
||
| 338 | * |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | public static function home_url() { |
||
| 342 | $url = self::get_raw_or_filtered_url( 'home' ); |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Allows overriding of the home_url value that is synced back to WordPress.com. |
||
| 346 | * |
||
| 347 | * @since 5.2.0 |
||
| 348 | * |
||
| 349 | * @param string $home_url |
||
| 350 | */ |
||
| 351 | return esc_url_raw( apply_filters( 'jetpack_sync_home_url', $url ) ); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Return the escaped siteurl. |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | public static function site_url() { |
||
| 360 | $url = self::get_raw_or_filtered_url( 'siteurl' ); |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Allows overriding of the site_url value that is synced back to WordPress.com. |
||
| 364 | * |
||
| 365 | * @since 5.2.0 |
||
| 366 | * |
||
| 367 | * @param string $site_url |
||
| 368 | */ |
||
| 369 | return esc_url_raw( apply_filters( 'jetpack_sync_site_url', $url ) ); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Return main site URL with a normalized protocol. |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public static function main_network_site_url() { |
||
| 378 | return self::get_protocol_normalized_url( 'main_network_site_url', network_site_url() ); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Return main site WordPress.com site ID. |
||
| 383 | * |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | public static function main_network_site_wpcom_id() { |
||
| 387 | /** |
||
| 388 | * Return the current site WPCOM ID for single site installs |
||
| 389 | */ |
||
| 390 | if ( ! is_multisite() ) { |
||
| 391 | return \Jetpack_Options::get_option( 'id' ); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Return the main network site WPCOM ID for multi-site installs |
||
| 396 | */ |
||
| 397 | $current_network = get_network(); |
||
| 398 | switch_to_blog( $current_network->blog_id ); |
||
| 399 | $wpcom_blog_id = \Jetpack_Options::get_option( 'id' ); |
||
| 400 | restore_current_blog(); |
||
| 401 | return $wpcom_blog_id; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Return URL with a normalized protocol. |
||
| 406 | * |
||
| 407 | * @param callable $callable Function to retrieve URL option. |
||
| 408 | * @param string $new_value URL Protocol to set URLs to. |
||
| 409 | * @return string Normalized URL. |
||
| 410 | */ |
||
| 411 | public static function get_protocol_normalized_url( $callable, $new_value ) { |
||
| 412 | $option_key = self::HTTPS_CHECK_OPTION_PREFIX . $callable; |
||
| 413 | |||
| 414 | $parsed_url = wp_parse_url( $new_value ); |
||
| 415 | if ( ! $parsed_url ) { |
||
|
0 ignored issues
–
show
|
|||
| 416 | return $new_value; |
||
| 417 | } |
||
| 418 | if ( array_key_exists( 'scheme', $parsed_url ) ) { |
||
| 419 | $scheme = $parsed_url['scheme']; |
||
| 420 | } else { |
||
| 421 | $scheme = ''; |
||
| 422 | } |
||
| 423 | $scheme_history = get_option( $option_key, array() ); |
||
| 424 | $scheme_history[] = $scheme; |
||
| 425 | |||
| 426 | // Limit length to self::HTTPS_CHECK_HISTORY. |
||
| 427 | $scheme_history = array_slice( $scheme_history, ( self::HTTPS_CHECK_HISTORY * -1 ) ); |
||
| 428 | |||
| 429 | update_option( $option_key, $scheme_history ); |
||
| 430 | |||
| 431 | $forced_scheme = in_array( 'https', $scheme_history, true ) ? 'https' : 'http'; |
||
| 432 | |||
| 433 | return set_url_scheme( $new_value, $forced_scheme ); |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Return URL from option or PHP constant. |
||
| 438 | * |
||
| 439 | * @param string $option_name (e.g. 'home'). |
||
| 440 | * |
||
| 441 | * @return mixed|null URL. |
||
| 442 | */ |
||
| 443 | public static function get_raw_url( $option_name ) { |
||
| 444 | $value = null; |
||
| 445 | $constant = ( 'home' === $option_name ) |
||
| 446 | ? 'WP_HOME' |
||
| 447 | : 'WP_SITEURL'; |
||
| 448 | |||
| 449 | // Since we disregard the constant for multisites in ms-default-filters.php, |
||
| 450 | // let's also use the db value if this is a multisite. |
||
| 451 | if ( ! is_multisite() && Constants::is_defined( $constant ) ) { |
||
| 452 | $value = Constants::get_constant( $constant ); |
||
| 453 | } else { |
||
| 454 | // Let's get the option from the database so that we can bypass filters. This will help |
||
| 455 | // ensure that we get more uniform values. |
||
| 456 | $value = \Jetpack_Options::get_raw_option( $option_name ); |
||
| 457 | } |
||
| 458 | |||
| 459 | return $value; |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Normalize domains by removing www unless declared in the site's option. |
||
| 464 | * |
||
| 465 | * @param string $option Option value from the site. |
||
| 466 | * @param callable $url_function Function retrieving the URL to normalize. |
||
| 467 | * @return mixed|string URL. |
||
| 468 | */ |
||
| 469 | public static function normalize_www_in_url( $option, $url_function ) { |
||
| 470 | $url = wp_parse_url( call_user_func( $url_function ) ); |
||
| 471 | $option_url = wp_parse_url( get_option( $option ) ); |
||
| 472 | |||
| 473 | if ( ! $option_url || ! $url ) { |
||
|
0 ignored issues
–
show
The expression
$option_url of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
Loading history...
The expression
$url of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
Loading history...
|
|||
| 474 | return $url; |
||
| 475 | } |
||
| 476 | |||
| 477 | View Code Duplication | if ( "www.{$option_url[ 'host' ]}" === $url['host'] ) { |
|
| 478 | // remove www if not present in option URL. |
||
| 479 | $url['host'] = $option_url['host']; |
||
| 480 | } |
||
| 481 | View Code Duplication | if ( "www.{$url[ 'host' ]}" === $option_url['host'] ) { |
|
| 482 | // add www if present in option URL. |
||
| 483 | $url['host'] = $option_url['host']; |
||
| 484 | } |
||
| 485 | |||
| 486 | $normalized_url = "{$url['scheme']}://{$url['host']}"; |
||
| 487 | if ( isset( $url['path'] ) ) { |
||
| 488 | $normalized_url .= "{$url['path']}"; |
||
| 489 | } |
||
| 490 | |||
| 491 | if ( isset( $url['query'] ) ) { |
||
| 492 | $normalized_url .= "?{$url['query']}"; |
||
| 493 | } |
||
| 494 | |||
| 495 | return $normalized_url; |
||
| 496 | } |
||
| 497 | |||
| 498 | /** |
||
| 499 | * Return filtered value of get_plugins. |
||
| 500 | * |
||
| 501 | * @return mixed|void |
||
| 502 | */ |
||
| 503 | public static function get_plugins() { |
||
| 504 | if ( ! function_exists( 'get_plugins' ) ) { |
||
| 505 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
||
| 506 | } |
||
| 507 | |||
| 508 | /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
||
| 509 | return apply_filters( 'all_plugins', get_plugins() ); |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Get custom action link tags that the plugin is using |
||
| 514 | * Ref: https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_action_links_(plugin_file_name) |
||
| 515 | * |
||
| 516 | * @param string $plugin_file_singular Particular plugin. |
||
| 517 | * @return array of plugin action links (key: link name value: url) |
||
| 518 | */ |
||
| 519 | public static function get_plugins_action_links( $plugin_file_singular = null ) { |
||
| 520 | // Some sites may have DOM disabled in PHP fail early. |
||
| 521 | if ( ! class_exists( 'DOMDocument' ) ) { |
||
| 522 | return array(); |
||
| 523 | } |
||
| 524 | $plugins_action_links = get_option( 'jetpack_plugin_api_action_links', array() ); |
||
| 525 | if ( ! empty( $plugins_action_links ) ) { |
||
| 526 | if ( is_null( $plugin_file_singular ) ) { |
||
| 527 | return $plugins_action_links; |
||
| 528 | } |
||
| 529 | return ( isset( $plugins_action_links[ $plugin_file_singular ] ) ? $plugins_action_links[ $plugin_file_singular ] : null ); |
||
| 530 | } |
||
| 531 | return array(); |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Return the WP version as defined in the $wp_version global. |
||
| 536 | * |
||
| 537 | * @return string |
||
| 538 | */ |
||
| 539 | public static function wp_version() { |
||
| 540 | global $wp_version; |
||
| 541 | return $wp_version; |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Return site icon url used on the site. |
||
| 546 | * |
||
| 547 | * @param int $size Size of requested icon in pixels. |
||
| 548 | * @return mixed|string|void |
||
| 549 | */ |
||
| 550 | public static function site_icon_url( $size = 512 ) { |
||
| 551 | $site_icon = get_site_icon_url( $size ); |
||
| 552 | return $site_icon ? $site_icon : get_option( 'jetpack_site_icon_url' ); |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Return roles registered on the site. |
||
| 557 | * |
||
| 558 | * @return array |
||
| 559 | */ |
||
| 560 | public static function roles() { |
||
| 561 | $wp_roles = wp_roles(); |
||
| 562 | return $wp_roles->roles; |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Determine time zone from WordPress' options "timezone_string" |
||
| 567 | * and "gmt_offset". |
||
| 568 | * |
||
| 569 | * 1. Check if `timezone_string` is set and return it. |
||
| 570 | * 2. Check if `gmt_offset` is set, formats UTC-offset from it and return it. |
||
| 571 | * 3. Default to "UTC+0" if nothing is set. |
||
| 572 | * |
||
| 573 | * Note: This function is specifically not using wp_timezone() to keep consistency with |
||
| 574 | * the existing formatting of the timezone string. |
||
| 575 | * |
||
| 576 | * @return string |
||
| 577 | */ |
||
| 578 | public static function get_timezone() { |
||
| 579 | $timezone_string = get_option( 'timezone_string' ); |
||
| 580 | |||
| 581 | if ( ! empty( $timezone_string ) ) { |
||
| 582 | return str_replace( '_', ' ', $timezone_string ); |
||
| 583 | } |
||
| 584 | |||
| 585 | $gmt_offset = get_option( 'gmt_offset', 0 ); |
||
| 586 | |||
| 587 | $formatted_gmt_offset = sprintf( '%+g', (float) $gmt_offset ); |
||
| 588 | |||
| 589 | $formatted_gmt_offset = str_replace( |
||
| 590 | array( '.25', '.5', '.75' ), |
||
| 591 | array( ':15', ':30', ':45' ), |
||
| 592 | (string) $formatted_gmt_offset |
||
| 593 | ); |
||
| 594 | |||
| 595 | /* translators: %s is UTC offset, e.g. "+1" */ |
||
| 596 | return sprintf( __( 'UTC%s', 'jetpack' ), $formatted_gmt_offset ); |
||
| 597 | } |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Return list of paused themes. |
||
| 601 | * |
||
| 602 | * @todo Remove function_exists check when WP 5.2 is the minimum. |
||
| 603 | * |
||
| 604 | * @return array|bool Array of paused themes or false if unsupported. |
||
| 605 | */ |
||
| 606 | public static function get_paused_themes() { |
||
| 607 | if ( function_exists( 'wp_paused_themes' ) ) { |
||
| 608 | $paused_themes = wp_paused_themes(); |
||
| 609 | return $paused_themes->get_all(); |
||
| 610 | } |
||
| 611 | return false; |
||
| 612 | } |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Return list of paused plugins. |
||
| 616 | * |
||
| 617 | * @todo Remove function_exists check when WP 5.2 is the minimum. |
||
| 618 | * |
||
| 619 | * @return array|bool Array of paused plugins or false if unsupported. |
||
| 620 | */ |
||
| 621 | public static function get_paused_plugins() { |
||
| 622 | if ( function_exists( 'wp_paused_plugins' ) ) { |
||
| 623 | $paused_plugins = wp_paused_plugins(); |
||
| 624 | return $paused_plugins->get_all(); |
||
| 625 | } |
||
| 626 | return false; |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Return the theme's supported features. |
||
| 631 | * Used for syncing the supported feature that we care about. |
||
| 632 | * |
||
| 633 | * @return array List of features that the theme supports. |
||
| 634 | */ |
||
| 635 | public static function get_theme_support() { |
||
| 636 | global $_wp_theme_features; |
||
| 637 | |||
| 638 | $theme_support = array(); |
||
| 639 | foreach ( Defaults::$default_theme_support_whitelist as $theme_feature ) { |
||
| 640 | $has_support = current_theme_supports( $theme_feature ); |
||
| 641 | if ( $has_support ) { |
||
| 642 | $theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ]; |
||
| 643 | } |
||
| 644 | } |
||
| 645 | |||
| 646 | return $theme_support; |
||
| 647 | } |
||
| 648 | } |
||
| 649 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: