Bez popisu

functions.global.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. /**
  3. * This file is meant to be the home for any generic & reusable functions
  4. * that can be accessed anywhere within Jetpack.
  5. *
  6. * This file is loaded whether or not Jetpack is active.
  7. *
  8. * Please namespace with jetpack_
  9. *
  10. * @package automattic/jetpack
  11. */
  12. use Automattic\Jetpack\Connection\Client;
  13. use Automattic\Jetpack\Device_Detection;
  14. use Automattic\Jetpack\Redirect;
  15. use Automattic\Jetpack\Sync\Functions;
  16. /**
  17. * Disable direct access.
  18. */
  19. if ( ! defined( 'ABSPATH' ) ) {
  20. exit;
  21. }
  22. /**
  23. * Hook into Core's _deprecated_function
  24. * Add more details about when a deprecated function will be removed.
  25. *
  26. * @since 8.8.0
  27. *
  28. * @param string $function The function that was called.
  29. * @param string $replacement Optional. The function that should have been called. Default null.
  30. * @param string $version The version of Jetpack that deprecated the function.
  31. */
  32. function jetpack_deprecated_function( $function, $replacement, $version ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
  33. // Bail early for non-Jetpack deprecations.
  34. if ( 0 !== strpos( $version, 'jetpack-' ) ) {
  35. return;
  36. }
  37. // Look for when a function will be removed based on when it was deprecated.
  38. $removed_version = jetpack_get_future_removed_version( $version );
  39. // If we could find a version, let's log a message about when removal will happen.
  40. if (
  41. ! empty( $removed_version )
  42. && ( defined( 'WP_DEBUG' ) && WP_DEBUG )
  43. /** This filter is documented in core/src/wp-includes/functions.php */
  44. && apply_filters( 'deprecated_function_trigger_error', true )
  45. ) {
  46. error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
  47. sprintf(
  48. /* Translators: 1. Function name. 2. Jetpack version number. */
  49. __( 'The %1$s function will be removed from the Jetpack plugin in version %2$s.', 'jetpack' ),
  50. $function,
  51. $removed_version
  52. )
  53. );
  54. }
  55. }
  56. add_action( 'deprecated_function_run', 'jetpack_deprecated_function', 10, 3 );
  57. /**
  58. * Hook into Core's _deprecated_file
  59. * Add more details about when a deprecated file will be removed.
  60. *
  61. * @since 8.8.0
  62. *
  63. * @param string $file The file that was called.
  64. * @param string $replacement The file that should have been included based on ABSPATH.
  65. * @param string $version The version of WordPress that deprecated the file.
  66. * @param string $message A message regarding the change.
  67. */
  68. function jetpack_deprecated_file( $file, $replacement, $version, $message ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
  69. // Bail early for non-Jetpack deprecations.
  70. if ( 0 !== strpos( $version, 'jetpack-' ) ) {
  71. return;
  72. }
  73. // Look for when a file will be removed based on when it was deprecated.
  74. $removed_version = jetpack_get_future_removed_version( $version );
  75. // If we could find a version, let's log a message about when removal will happen.
  76. if (
  77. ! empty( $removed_version )
  78. && ( defined( 'WP_DEBUG' ) && WP_DEBUG )
  79. /** This filter is documented in core/src/wp-includes/functions.php */
  80. && apply_filters( 'deprecated_file_trigger_error', true )
  81. ) {
  82. error_log( // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
  83. sprintf(
  84. /* Translators: 1. File name. 2. Jetpack version number. */
  85. __( 'The %1$s file will be removed from the Jetpack plugin in version %2$s.', 'jetpack' ),
  86. $file,
  87. $removed_version
  88. )
  89. );
  90. }
  91. }
  92. add_action( 'deprecated_file_included', 'jetpack_deprecated_file', 10, 4 );
  93. /**
  94. * Get the major version number of Jetpack 6 months after provided version.
  95. * Useful to indicate when a deprecated function will be removed from Jetpack.
  96. *
  97. * @since 8.8.0
  98. *
  99. * @param string $version The version of WordPress that deprecated the function.
  100. *
  101. * @return bool|float Return a Jetpack Major version number, or false.
  102. */
  103. function jetpack_get_future_removed_version( $version ) {
  104. /*
  105. * Extract the version number from a deprecation notice.
  106. * (let's only keep the first decimal, e.g. 8.8 and not 8.8.0)
  107. */
  108. preg_match( '#(([0-9]+\.([0-9]+))(?:\.[0-9]+)*)#', $version, $matches );
  109. if ( isset( $matches[2], $matches[3] ) ) {
  110. $deprecated_version = (float) $matches[2];
  111. $deprecated_minor = (float) $matches[3];
  112. /*
  113. * If the detected minor version number
  114. * (e.g. "7" in "8.7")
  115. * is higher than 9, we know the version number is malformed.
  116. * Jetpack does not use semver yet.
  117. * Bail.
  118. */
  119. if ( 10 <= $deprecated_minor ) {
  120. return false;
  121. }
  122. // We'll remove the function from the code 6 months later, thus 6 major versions later.
  123. $removed_version = $deprecated_version + 0.6;
  124. return (float) $removed_version;
  125. }
  126. return false;
  127. }
  128. /**
  129. * Set the admin language, based on user language.
  130. *
  131. * @since 4.5.0
  132. * @deprecated 6.6.0 Use Core function instead.
  133. *
  134. * @return string
  135. */
  136. function jetpack_get_user_locale() {
  137. _deprecated_function( __FUNCTION__, 'jetpack-6.6.0', 'get_user_locale' );
  138. return get_user_locale();
  139. }
  140. /**
  141. * Determine if this site is an Atomic site or not looking first at the 'at_options' option.
  142. * As a fallback, check for presence of wpcomsh plugin to determine if a current site has undergone AT.
  143. *
  144. * @since 4.8.1
  145. *
  146. * @return bool
  147. */
  148. function jetpack_is_atomic_site() {
  149. $at_options = get_option( 'at_options', array() );
  150. return ! empty( $at_options ) || defined( 'WPCOMSH__PLUGIN_FILE' );
  151. }
  152. /**
  153. * Register post type for migration.
  154. *
  155. * @since 5.2
  156. */
  157. function jetpack_register_migration_post_type() {
  158. register_post_type(
  159. 'jetpack_migration',
  160. array(
  161. 'supports' => array(),
  162. 'taxonomies' => array(),
  163. 'hierarchical' => false,
  164. 'public' => false,
  165. 'has_archive' => false,
  166. 'can_export' => true,
  167. )
  168. );
  169. }
  170. /**
  171. * Stores migration data in the database.
  172. *
  173. * @since 5.2
  174. *
  175. * @param string $option_name Option name.
  176. * @param bool $option_value Option value.
  177. *
  178. * @return int|WP_Error
  179. */
  180. function jetpack_store_migration_data( $option_name, $option_value ) {
  181. jetpack_register_migration_post_type();
  182. $insert = array(
  183. 'post_title' => $option_name,
  184. 'post_content_filtered' => $option_value,
  185. 'post_type' => 'jetpack_migration',
  186. 'post_date' => gmdate( 'Y-m-d H:i:s', time() ),
  187. );
  188. $post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' );
  189. if ( null !== $post ) {
  190. $insert['ID'] = $post->ID;
  191. }
  192. return wp_insert_post( $insert, true );
  193. }
  194. /**
  195. * Retrieves legacy image widget data.
  196. *
  197. * @since 5.2
  198. *
  199. * @param string $option_name Option name.
  200. *
  201. * @return mixed|null
  202. */
  203. function jetpack_get_migration_data( $option_name ) {
  204. $post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' );
  205. return null !== $post ? maybe_unserialize( $post->post_content_filtered ) : null;
  206. }
  207. /**
  208. * Prints a TOS blurb used throughout the connection prompts.
  209. *
  210. * Note: custom ToS messages are also defined in Jetpack_Pre_Connection_JITMs->get_raw_messages()
  211. *
  212. * @since 5.3
  213. *
  214. * @echo string
  215. */
  216. function jetpack_render_tos_blurb() {
  217. printf(
  218. wp_kses(
  219. /* Translators: placeholders are links. */
  220. __( 'By clicking the <strong>Set up Jetpack</strong> button, you agree to our <a href="%1$s" target="_blank" rel="noopener noreferrer">Terms of Service</a> and to <a href="%2$s" target="_blank" rel="noopener noreferrer">share details</a> with WordPress.com.', 'jetpack' ),
  221. array(
  222. 'a' => array(
  223. 'href' => array(),
  224. 'target' => array(),
  225. 'rel' => array(),
  226. ),
  227. 'strong' => true,
  228. )
  229. ),
  230. esc_url( Redirect::get_url( 'wpcom-tos' ) ),
  231. esc_url( Redirect::get_url( 'jetpack-support-what-data-does-jetpack-sync' ) )
  232. );
  233. }
  234. /**
  235. * Intervene upgrade process so Jetpack themes are downloaded with credentials.
  236. *
  237. * @since 5.3
  238. *
  239. * @param bool $preempt Whether to preempt an HTTP request's return value. Default false.
  240. * @param array $r HTTP request arguments.
  241. * @param string $url The request URL.
  242. *
  243. * @return array|bool|WP_Error
  244. */
  245. function jetpack_theme_update( $preempt, $r, $url ) {
  246. if ( 0 === stripos( $url, JETPACK__WPCOM_JSON_API_BASE . '/rest/v1/themes/download' ) ) {
  247. $file = $r['filename'];
  248. if ( ! $file ) {
  249. return new WP_Error( 'problem_creating_theme_file', esc_html__( 'Problem creating file for theme download', 'jetpack' ) );
  250. }
  251. $theme = pathinfo( wp_parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME );
  252. // Remove filter to avoid endless loop since wpcom_json_api_request_as_blog uses this too.
  253. remove_filter( 'pre_http_request', 'jetpack_theme_update' );
  254. $result = Client::wpcom_json_api_request_as_blog(
  255. "themes/download/$theme.zip",
  256. '1.1',
  257. array(
  258. 'stream' => true,
  259. 'filename' => $file,
  260. )
  261. );
  262. if ( 200 !== wp_remote_retrieve_response_code( $result ) ) {
  263. return new WP_Error( 'problem_fetching_theme', esc_html__( 'Problem downloading theme', 'jetpack' ) );
  264. }
  265. return $result;
  266. }
  267. return $preempt;
  268. }
  269. /**
  270. * Add the filter when a upgrade is going to be downloaded.
  271. *
  272. * @since 5.3
  273. *
  274. * @param bool $reply Whether to bail without returning the package. Default false.
  275. *
  276. * @return bool
  277. */
  278. function jetpack_upgrader_pre_download( $reply ) {
  279. add_filter( 'pre_http_request', 'jetpack_theme_update', 10, 3 );
  280. return $reply;
  281. }
  282. add_filter( 'upgrader_pre_download', 'jetpack_upgrader_pre_download' );
  283. /**
  284. * Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion.
  285. *
  286. * @since 6.1.0
  287. * @deprecated Automattic\Jetpack\Sync\Functions::json_wrap
  288. *
  289. * @param array|obj $any Source data to be cleaned up.
  290. * @param array $seen_nodes Built array of nodes.
  291. *
  292. * @return array
  293. */
  294. function jetpack_json_wrap( &$any, $seen_nodes = array() ) {
  295. _deprecated_function( __METHOD__, 'jetpack-9.5', 'Automattic\Jetpack\Sync\Functions' );
  296. return Functions::json_wrap( $any, $seen_nodes );
  297. }
  298. /**
  299. * Checks if the mime_content_type function is available and return it if so.
  300. *
  301. * The function mime_content_type is enabled by default in PHP, but can be disabled. We attempt to
  302. * enforce this via composer.json, but that won't be checked in majority of cases where
  303. * this would be happening.
  304. *
  305. * @since 7.8.0
  306. *
  307. * @param string $file File location.
  308. *
  309. * @return string|false MIME type or false if functionality is not available.
  310. */
  311. function jetpack_mime_content_type( $file ) {
  312. if ( function_exists( 'mime_content_type' ) ) {
  313. return mime_content_type( $file );
  314. }
  315. return false;
  316. }
  317. /**
  318. * Checks that the mime type of the specified file is among those in a filterable list of mime types.
  319. *
  320. * @since 7.8.0
  321. *
  322. * @param string $file Path to file to get its mime type.
  323. *
  324. * @return bool
  325. */
  326. function jetpack_is_file_supported_for_sideloading( $file ) {
  327. $type = jetpack_mime_content_type( $file );
  328. if ( ! $type ) {
  329. return false;
  330. }
  331. /**
  332. * Filter the list of supported mime types for media sideloading.
  333. *
  334. * @since 4.0.0
  335. *
  336. * @module json-api
  337. *
  338. * @param array $supported_mime_types Array of the supported mime types for media sideloading.
  339. */
  340. $supported_mime_types = apply_filters(
  341. 'jetpack_supported_media_sideload_types',
  342. array(
  343. 'image/png',
  344. 'image/jpeg',
  345. 'image/gif',
  346. 'image/bmp',
  347. 'image/webp',
  348. 'video/quicktime',
  349. 'video/mp4',
  350. 'video/mpeg',
  351. 'video/ogg',
  352. 'video/3gpp',
  353. 'video/3gpp2',
  354. 'video/h261',
  355. 'video/h262',
  356. 'video/h264',
  357. 'video/x-msvideo',
  358. 'video/x-ms-wmv',
  359. 'video/x-ms-asf',
  360. )
  361. );
  362. // If the type returned was not an array as expected, then we know we don't have a match.
  363. if ( ! is_array( $supported_mime_types ) ) {
  364. return false;
  365. }
  366. return in_array( $type, $supported_mime_types, true );
  367. }
  368. /**
  369. * Determine if the current User Agent matches the passed $kind
  370. *
  371. * @param string $kind Category of mobile device to check for.
  372. * Either: any, dumb, smart.
  373. * @param bool $return_matched_agent Boolean indicating if the UA should be returned.
  374. *
  375. * @return bool|string Boolean indicating if current UA matches $kind. If
  376. * $return_matched_agent is true, returns the UA string
  377. */
  378. function jetpack_is_mobile( $kind = 'any', $return_matched_agent = false ) {
  379. /**
  380. * Filter the value of jetpack_is_mobile before it is calculated.
  381. *
  382. * Passing a truthy value to the filter will short-circuit determining the
  383. * mobile type, returning the passed value instead.
  384. *
  385. * @since 4.2.0
  386. *
  387. * @param bool|string $matches Boolean if current UA matches $kind or not. If
  388. * $return_matched_agent is true, should return the UA string
  389. * @param string $kind Category of mobile device being checked
  390. * @param bool $return_matched_agent Boolean indicating if the UA should be returned
  391. */
  392. $pre = apply_filters( 'pre_jetpack_is_mobile', null, $kind, $return_matched_agent );
  393. if ( $pre ) {
  394. return $pre;
  395. }
  396. $return = false;
  397. $device_info = Device_Detection::get_info();
  398. if ( 'any' === $kind ) {
  399. $return = $device_info['is_phone'];
  400. } elseif ( 'smart' === $kind ) {
  401. $return = $device_info['is_smartphone'];
  402. } elseif ( 'dumb' === $kind ) {
  403. $return = $device_info['is_phone'] && ! $device_info['is_smartphone'];
  404. }
  405. if ( $return_matched_agent && true === $return ) {
  406. $return = $device_info['is_phone_matched_ua'];
  407. }
  408. /**
  409. * Filter the value of jetpack_is_mobile
  410. *
  411. * @since 4.2.0
  412. *
  413. * @param bool|string $matches Boolean if current UA matches $kind or not. If
  414. * $return_matched_agent is true, should return the UA string
  415. * @param string $kind Category of mobile device being checked
  416. * @param bool $return_matched_agent Boolean indicating if the UA should be returned
  417. */
  418. return apply_filters( 'jetpack_is_mobile', $return, $kind, $return_matched_agent );
  419. }
  420. /**
  421. * Determine whether the current request is for accessing the frontend.
  422. *
  423. * @return bool True if it's a frontend request, false otherwise.
  424. */
  425. function jetpack_is_frontend() {
  426. $is_frontend = true;
  427. if (
  428. is_admin() ||
  429. wp_doing_ajax() ||
  430. wp_doing_cron() ||
  431. wp_is_json_request() ||
  432. wp_is_jsonp_request() ||
  433. wp_is_xml_request() ||
  434. is_feed() ||
  435. ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ||
  436. ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) ||
  437. ( defined( 'WP_CLI' ) && WP_CLI )
  438. ) {
  439. $is_frontend = false;
  440. }
  441. /**
  442. * Filter whether the current request is for accessing the frontend.
  443. *
  444. * @since 9.0.0
  445. *
  446. * @param bool $is_frontend Whether the current request is for accessing the frontend.
  447. */
  448. return (bool) apply_filters( 'jetpack_is_frontend', $is_frontend );
  449. }