functions.global.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. * Please write docblocks
  10. */
  11. /**
  12. * Disable direct access.
  13. */
  14. if ( ! defined( 'ABSPATH' ) ) {
  15. exit;
  16. }
  17. /**
  18. * Set the admin language, based on user language.
  19. *
  20. * @since 4.5.0
  21. *
  22. * @return string
  23. *
  24. * @todo Remove this function when WordPress 4.8 is released
  25. * and replace `jetpack_get_user_locale()` in this file with `get_user_locale()`.
  26. */
  27. function jetpack_get_user_locale() {
  28. $locale = get_locale();
  29. if ( function_exists( 'get_user_locale' ) ) {
  30. $locale = get_user_locale();
  31. }
  32. return $locale;
  33. }
  34. /**
  35. * Determine if this site is an Atomic site or not looking first at the 'at_options' option.
  36. * As a fallback, check for presence of wpcomsh plugin to determine if a current site has undergone AT.
  37. *
  38. * @since 4.8.1
  39. *
  40. * @return bool
  41. */
  42. function jetpack_is_atomic_site() {
  43. $at_options = get_option( 'at_options', array() );
  44. return ! empty( $at_options ) || defined( 'WPCOMSH__PLUGIN_FILE' );
  45. }
  46. /**
  47. * Register post type for migration.
  48. *
  49. * @since 5.2
  50. */
  51. function jetpack_register_migration_post_type() {
  52. register_post_type( 'jetpack_migration', array(
  53. 'supports' => array(),
  54. 'taxonomies' => array(),
  55. 'hierarchical' => false,
  56. 'public' => false,
  57. 'has_archive' => false,
  58. 'can_export' => true,
  59. ) );
  60. }
  61. /**
  62. * Stores migration data in the database.
  63. *
  64. * @since 5.2
  65. *
  66. * @param string $option_name
  67. * @param bool $option_value
  68. *
  69. * @return int|WP_Error
  70. */
  71. function jetpack_store_migration_data( $option_name, $option_value ) {
  72. jetpack_register_migration_post_type();
  73. $insert = array(
  74. 'post_title' => $option_name,
  75. 'post_content_filtered' => $option_value,
  76. 'post_type' => 'jetpack_migration',
  77. 'post_date' => date( 'Y-m-d H:i:s', time() ),
  78. );
  79. $post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' );
  80. if ( null !== $post ) {
  81. $insert['ID'] = $post->ID;
  82. }
  83. return wp_insert_post( $insert, true );
  84. }
  85. /**
  86. * Retrieves legacy image widget data.
  87. *
  88. * @since 5.2
  89. *
  90. * @param string $option_name
  91. *
  92. * @return mixed|null
  93. */
  94. function jetpack_get_migration_data( $option_name ) {
  95. $post = get_page_by_title( $option_name, 'OBJECT', 'jetpack_migration' );
  96. return null !== $post ? maybe_unserialize( $post->post_content_filtered ) : null;
  97. }
  98. /**
  99. * Prints a TOS blurb used throughout the connection prompts.
  100. *
  101. * @since 5.3
  102. *
  103. * @return string
  104. */
  105. function jetpack_render_tos_blurb() {
  106. printf(
  107. __( 'By clicking the <strong>Set up Jetpack</strong> button, you agree to our <a href="%s" target="_blank">Terms of Service</a> and to <a href="%s" target="_blank">share details</a> with WordPress.com.', 'jetpack' ),
  108. 'https://wordpress.com/tos',
  109. 'https://jetpack.com/support/what-data-does-jetpack-sync'
  110. );
  111. }
  112. /**
  113. * Intervene upgrade process so Jetpack themes are downloaded with credentials.
  114. *
  115. * @since 5.3
  116. *
  117. * @param bool $preempt Whether to preempt an HTTP request's return value. Default false.
  118. * @param array $r HTTP request arguments.
  119. * @param string $url The request URL.
  120. *
  121. * @return array|bool|WP_Error
  122. */
  123. function jetpack_theme_update( $preempt, $r, $url ) {
  124. if ( false !== stripos( $url, JETPACK__WPCOM_JSON_API_HOST . '/rest/v1/themes/download' ) ) {
  125. $file = $r['filename'];
  126. if ( ! $file ) {
  127. return new WP_Error( 'problem_creating_theme_file', esc_html__( 'Problem creating file for theme download', 'jetpack' ) );
  128. }
  129. $theme = pathinfo( parse_url( $url, PHP_URL_PATH ), PATHINFO_FILENAME );
  130. // Remove filter to avoid endless loop since wpcom_json_api_request_as_blog uses this too.
  131. remove_filter( 'pre_http_request', 'jetpack_theme_update' );
  132. $result = Jetpack_Client::wpcom_json_api_request_as_blog(
  133. "themes/download/$theme.zip", '1.1', array( 'stream' => true, 'filename' => $file )
  134. );
  135. if ( 200 !== wp_remote_retrieve_response_code( $result ) ) {
  136. return new WP_Error( 'problem_fetching_theme', esc_html__( 'Problem downloading theme', 'jetpack' ) );
  137. }
  138. return $result;
  139. }
  140. return $preempt;
  141. }
  142. /**
  143. * Add the filter when a upgrade is going to be downloaded.
  144. *
  145. * @since 5.3
  146. *
  147. * @param bool $reply Whether to bail without returning the package. Default false.
  148. *
  149. * @return bool
  150. */
  151. function jetpack_upgrader_pre_download( $reply ) {
  152. add_filter( 'pre_http_request', 'jetpack_theme_update', 10, 3 );
  153. return $reply;
  154. }
  155. add_filter( 'upgrader_pre_download', 'jetpack_upgrader_pre_download' );
  156. /**
  157. * Wraps data in a way so that we can distinguish between objects and array and also prevent object recursion.
  158. *
  159. * @since 6.1.0
  160. *
  161. * @param $any
  162. * @param array $seen_nodes
  163. *
  164. * @return array
  165. */
  166. function jetpack_json_wrap( &$any, $seen_nodes = array() ) {
  167. if ( is_object( $any ) ) {
  168. $input = get_object_vars( $any );
  169. $input['__o'] = 1;
  170. } else {
  171. $input = &$any;
  172. }
  173. if ( is_array( $input ) ) {
  174. $seen_nodes[] = &$any;
  175. $return = array();
  176. foreach ( $input as $k => &$v ) {
  177. if ( ( is_array( $v ) || is_object( $v ) ) ) {
  178. if ( in_array( $v, $seen_nodes, true ) ) {
  179. continue;
  180. }
  181. $return[ $k ] = jetpack_json_wrap( $v, $seen_nodes );
  182. } else {
  183. $return[ $k ] = $v;
  184. }
  185. }
  186. return $return;
  187. }
  188. return $any;
  189. }