Bez popisu

class-jetpack-redux-state-helper.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. /**
  3. * A utility class that generates the initial state for Redux in wp-admin.
  4. * Modularized from `class.jetpack-react-page.php`.
  5. *
  6. * @package automattic/jetpack
  7. */
  8. use Automattic\Jetpack\Connection\Manager as Connection_Manager;
  9. use Automattic\Jetpack\Connection\REST_Connector;
  10. use Automattic\Jetpack\Constants;
  11. use Automattic\Jetpack\Device_Detection\User_Agent_Info;
  12. use Automattic\Jetpack\Identity_Crisis;
  13. use Automattic\Jetpack\Licensing;
  14. use Automattic\Jetpack\Partner;
  15. use Automattic\Jetpack\Status;
  16. /**
  17. * Responsible for populating the initial Redux state.
  18. */
  19. class Jetpack_Redux_State_Helper {
  20. /**
  21. * Generate the initial state array to be used by the Redux store.
  22. */
  23. public static function get_initial_state() {
  24. global $is_safari;
  25. // Load API endpoint base classes and endpoints for getting the module list fed into the JS Admin Page.
  26. require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-xmlrpc-consumer-endpoint.php';
  27. require_once JETPACK__PLUGIN_DIR . '_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php';
  28. $module_list_endpoint = new Jetpack_Core_API_Module_List_Endpoint();
  29. $modules = $module_list_endpoint->get_modules();
  30. // Preparing translated fields for JSON encoding by transforming all HTML entities to
  31. // respective characters.
  32. foreach ( $modules as $slug => $data ) {
  33. $modules[ $slug ]['name'] = html_entity_decode( $data['name'] );
  34. $modules[ $slug ]['description'] = html_entity_decode( $data['description'] );
  35. $modules[ $slug ]['short_description'] = html_entity_decode( $data['short_description'] );
  36. $modules[ $slug ]['long_description'] = html_entity_decode( $data['long_description'] );
  37. }
  38. // Collecting roles that can view site stats.
  39. $stats_roles = array();
  40. $enabled_roles = function_exists( 'stats_get_option' ) ? stats_get_option( 'roles' ) : array( 'administrator' );
  41. if ( ! function_exists( 'get_editable_roles' ) ) {
  42. require_once ABSPATH . 'wp-admin/includes/user.php';
  43. }
  44. foreach ( get_editable_roles() as $slug => $role ) {
  45. $stats_roles[ $slug ] = array(
  46. 'name' => translate_user_role( $role['name'] ),
  47. 'canView' => is_array( $enabled_roles ) ? in_array( $slug, $enabled_roles, true ) : false,
  48. );
  49. }
  50. // Get information about current theme.
  51. $current_theme = wp_get_theme();
  52. // Get all themes that Infinite Scroll provides support for natively.
  53. $inf_scr_support_themes = array();
  54. foreach ( Jetpack::glob_php( JETPACK__PLUGIN_DIR . 'modules/infinite-scroll/themes' ) as $path ) {
  55. if ( is_readable( $path ) ) {
  56. $inf_scr_support_themes[] = basename( $path, '.php' );
  57. }
  58. }
  59. // Get last post, to build the link to Customizer in the Related Posts module.
  60. $last_post = get_posts( array( 'posts_per_page' => 1 ) );
  61. $last_post = isset( $last_post[0] ) && $last_post[0] instanceof WP_Post
  62. ? get_permalink( $last_post[0]->ID )
  63. : get_home_url();
  64. $current_user_data = jetpack_current_user_data();
  65. /**
  66. * Adds information to the `connectionStatus` API field that is unique to the Jetpack React dashboard.
  67. */
  68. $connection_status = array(
  69. 'isInIdentityCrisis' => Identity_Crisis::validate_sync_error_idc_option(),
  70. 'sandboxDomain' => JETPACK__SANDBOX_DOMAIN,
  71. /**
  72. * Filter to add connection errors
  73. * Format: array( array( 'code' => '...', 'message' => '...', 'action' => '...' ), ... )
  74. *
  75. * @since 8.7.0
  76. *
  77. * @param array $errors Connection errors.
  78. */
  79. 'errors' => apply_filters( 'react_connection_errors_initial_state', array() ),
  80. );
  81. $connection_status = array_merge( REST_Connector::connection_status( false ), $connection_status );
  82. return array(
  83. 'WP_API_root' => esc_url_raw( rest_url() ),
  84. 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
  85. 'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
  86. 'purchaseToken' => self::get_purchase_token(),
  87. 'pluginBaseUrl' => plugins_url( '', JETPACK__PLUGIN_FILE ),
  88. 'connectionStatus' => $connection_status,
  89. 'connectUrl' => false == $current_user_data['isConnected'] // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
  90. ? Jetpack::init()->build_connect_url( true, false, false )
  91. : '',
  92. 'dismissedNotices' => self::get_dismissed_jetpack_notices(),
  93. 'isDevVersion' => Jetpack::is_development_version(),
  94. 'currentVersion' => JETPACK__VERSION,
  95. 'is_gutenberg_available' => true,
  96. 'getModules' => $modules,
  97. 'rawUrl' => ( new Status() )->get_site_suffix(),
  98. 'adminUrl' => esc_url( admin_url() ),
  99. 'siteTitle' => (string) htmlspecialchars_decode( get_option( 'blogname' ), ENT_QUOTES ),
  100. 'stats' => array(
  101. // data is populated asynchronously on page load.
  102. 'data' => array(
  103. 'general' => false,
  104. 'day' => false,
  105. 'week' => false,
  106. 'month' => false,
  107. ),
  108. 'roles' => $stats_roles,
  109. ),
  110. 'aff' => Partner::init()->get_partner_code( Partner::AFFILIATE_CODE ),
  111. 'partnerSubsidiaryId' => Partner::init()->get_partner_code( Partner::SUBSIDIARY_CODE ),
  112. 'settings' => self::get_flattened_settings( $modules ),
  113. 'userData' => array(
  114. 'currentUser' => $current_user_data,
  115. ),
  116. 'siteData' => array(
  117. 'icon' => has_site_icon()
  118. ? apply_filters( 'jetpack_photon_url', get_site_icon_url(), array( 'w' => 64 ) )
  119. : '',
  120. 'siteVisibleToSearchEngines' => '1' == get_option( 'blog_public' ), // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
  121. /**
  122. * Whether promotions are visible or not.
  123. *
  124. * @since 4.8.0
  125. *
  126. * @param bool $are_promotions_active Status of promotions visibility. True by default.
  127. */
  128. 'showPromotions' => apply_filters( 'jetpack_show_promotions', true ),
  129. 'isAtomicSite' => jetpack_is_atomic_site(),
  130. 'plan' => Jetpack_Plan::get(),
  131. 'showBackups' => Jetpack::show_backups_ui(),
  132. 'showRecommendations' => Jetpack_Recommendations::is_enabled(),
  133. 'isMultisite' => is_multisite(),
  134. 'dateFormat' => get_option( 'date_format' ),
  135. ),
  136. 'themeData' => array(
  137. 'name' => $current_theme->get( 'Name' ),
  138. 'hasUpdate' => (bool) get_theme_update_available( $current_theme ),
  139. 'support' => array(
  140. 'infinite-scroll' => current_theme_supports( 'infinite-scroll' ) || in_array( $current_theme->get_stylesheet(), $inf_scr_support_themes, true ),
  141. ),
  142. ),
  143. 'jetpackStateNotices' => array(
  144. 'messageCode' => Jetpack::state( 'message' ),
  145. 'errorCode' => Jetpack::state( 'error' ),
  146. 'errorDescription' => Jetpack::state( 'error_description' ),
  147. 'messageContent' => Jetpack::state( 'display_update_modal' ) ? self::get_update_modal_data() : null,
  148. ),
  149. 'tracksUserData' => Jetpack_Tracks_Client::get_connected_user_tracks_identity(),
  150. 'currentIp' => function_exists( 'jetpack_protect_get_ip' ) ? jetpack_protect_get_ip() : false,
  151. 'lastPostUrl' => esc_url( $last_post ),
  152. 'externalServicesConnectUrls' => self::get_external_services_connect_urls(),
  153. 'calypsoEnv' => Jetpack::get_calypso_env(),
  154. 'products' => Jetpack::get_products_for_purchase(),
  155. 'recommendationsStep' => Jetpack_Core_Json_Api_Endpoints::get_recommendations_step()['step'],
  156. 'isSafari' => $is_safari || User_Agent_Info::is_opera_desktop(), // @todo Rename isSafari everywhere.
  157. 'doNotUseConnectionIframe' => Constants::is_true( 'JETPACK_SHOULD_NOT_USE_CONNECTION_IFRAME' ),
  158. 'licensing' => array(
  159. 'error' => Licensing::instance()->last_error(),
  160. 'showLicensingUi' => Licensing::instance()->is_licensing_input_enabled(),
  161. ),
  162. );
  163. }
  164. /**
  165. * Gets array of any Jetpack notices that have been dismissed.
  166. *
  167. * @return mixed|void
  168. */
  169. public static function get_dismissed_jetpack_notices() {
  170. $jetpack_dismissed_notices = get_option( 'jetpack_dismissed_notices', array() );
  171. /**
  172. * Array of notices that have been dismissed.
  173. *
  174. * @param array $jetpack_dismissed_notices If empty, will not show any Jetpack notices.
  175. */
  176. $dismissed_notices = apply_filters( 'jetpack_dismissed_notices', $jetpack_dismissed_notices );
  177. return $dismissed_notices;
  178. }
  179. /**
  180. * Returns an array of modules and settings both as first class members of the object.
  181. *
  182. * @return array flattened settings with modules.
  183. */
  184. public static function get_flattened_settings() {
  185. $core_api_endpoint = new Jetpack_Core_API_Data();
  186. $settings = $core_api_endpoint->get_all_options();
  187. return $settings->data;
  188. }
  189. /**
  190. * Returns the release post content and image data as an associative array.
  191. * This data is used to create the update modal.
  192. */
  193. public static function get_update_modal_data() {
  194. $post_data = self::get_release_post_data();
  195. if ( ! isset( $post_data['posts'][0] ) ) {
  196. return;
  197. }
  198. $post = $post_data['posts'][0];
  199. if ( empty( $post['content'] ) ) {
  200. return;
  201. }
  202. // This allows us to embed videopress videos into the release post.
  203. add_filter( 'wp_kses_allowed_html', array( __CLASS__, 'allow_post_embed_iframe' ), 10, 2 );
  204. $content = wp_kses_post( $post['content'] );
  205. remove_filter( 'wp_kses_allowed_html', array( __CLASS__, 'allow_post_embed_iframe' ), 10, 2 );
  206. $post_title = isset( $post['title'] ) ? $post['title'] : null;
  207. $title = wp_kses( $post_title, array() );
  208. $post_thumbnail = isset( $post['post_thumbnail'] ) ? $post['post_thumbnail'] : null;
  209. if ( ! empty( $post_thumbnail ) ) {
  210. jetpack_require_lib( 'class.jetpack-photon-image' );
  211. $photon_image = new Jetpack_Photon_Image(
  212. array(
  213. 'file' => jetpack_photon_url( $post_thumbnail['URL'] ),
  214. 'width' => $post_thumbnail['width'],
  215. 'height' => $post_thumbnail['height'],
  216. ),
  217. $post_thumbnail['mime_type']
  218. );
  219. $photon_image->resize(
  220. array(
  221. 'width' => 600,
  222. 'height' => null,
  223. 'crop' => false,
  224. )
  225. );
  226. $post_thumbnail_url = $photon_image->get_raw_filename();
  227. } else {
  228. $post_thumbnail_url = null;
  229. }
  230. $post_array = array(
  231. 'release_post_content' => $content,
  232. 'release_post_featured_image' => $post_thumbnail_url,
  233. 'release_post_title' => $title,
  234. );
  235. return $post_array;
  236. }
  237. /**
  238. * Temporarily allow post content to contain iframes, e.g. for videopress.
  239. *
  240. * @param string $tags The tags.
  241. * @param string $context The context.
  242. */
  243. public static function allow_post_embed_iframe( $tags, $context ) {
  244. if ( 'post' === $context ) {
  245. $tags['iframe'] = array(
  246. 'src' => true,
  247. 'height' => true,
  248. 'width' => true,
  249. 'frameborder' => true,
  250. 'allowfullscreen' => true,
  251. );
  252. }
  253. return $tags;
  254. }
  255. /**
  256. * Obtains the release post from the Jetpack release post blog. A release post will be displayed in the
  257. * update modal when a post has a tag equal to the Jetpack version number.
  258. *
  259. * The response parameters for the post array can be found here:
  260. * https://developer.wordpress.com/docs/api/1.1/get/sites/%24site/posts/%24post_ID/#apidoc-response
  261. *
  262. * @return array|null Returns an associative array containing the release post data at index ['posts'][0].
  263. * Returns null if the release post data is not available.
  264. */
  265. public static function get_release_post_data() {
  266. if ( Constants::is_defined( 'TESTING_IN_JETPACK' ) && Constants::get_constant( 'TESTING_IN_JETPACK' ) ) {
  267. return null;
  268. }
  269. $release_post_src = add_query_arg(
  270. array(
  271. 'order_by' => 'date',
  272. 'tag' => JETPACK__VERSION,
  273. 'number' => '1',
  274. ),
  275. 'https://public-api.wordpress.com/rest/v1/sites/' . JETPACK__RELEASE_POST_BLOG_SLUG . '/posts'
  276. );
  277. $response = wp_remote_get( $release_post_src );
  278. if ( ! is_array( $response ) ) {
  279. return null;
  280. }
  281. return json_decode( wp_remote_retrieve_body( $response ), true );
  282. }
  283. /**
  284. * Get external services connect URLs.
  285. */
  286. public static function get_external_services_connect_urls() {
  287. $connect_urls = array();
  288. jetpack_require_lib( 'class.jetpack-keyring-service-helper' );
  289. // phpcs:disable
  290. foreach ( Jetpack_Keyring_Service_Helper::$SERVICES as $service_name => $service_info ) {
  291. // phpcs:enable
  292. $connect_urls[ $service_name ] = Jetpack_Keyring_Service_Helper::connect_url( $service_name, $service_info['for'] );
  293. }
  294. return $connect_urls;
  295. }
  296. /**
  297. * Gets a purchase token that is used for Jetpack logged out visitor checkout.
  298. * The purchase token should be appended to all CTA url's that lead to checkout.
  299. *
  300. * @since 9.8.0
  301. * @return string|boolean
  302. */
  303. public static function get_purchase_token() {
  304. if ( ! Jetpack::current_user_can_purchase() ) {
  305. return false;
  306. }
  307. $purchase_token = Jetpack_Options::get_option( 'purchase_token', false );
  308. if ( $purchase_token ) {
  309. return $purchase_token;
  310. }
  311. // If the purchase token is not saved in the options table yet, then add it.
  312. Jetpack_Options::update_option( 'purchase_token', self::generate_purchase_token(), true );
  313. return Jetpack_Options::get_option( 'purchase_token', false );
  314. }
  315. /**
  316. * Generates a purchase token that is used for Jetpack logged out visitor checkout.
  317. *
  318. * @since 9.8.0
  319. * @return string
  320. */
  321. public static function generate_purchase_token() {
  322. return wp_generate_password( 12, false );
  323. }
  324. }
  325. /**
  326. * Gather data about the current user.
  327. *
  328. * @since 4.1.0
  329. *
  330. * @return array
  331. */
  332. function jetpack_current_user_data() {
  333. $jetpack_connection = new Connection_Manager( 'jetpack' );
  334. $current_user = wp_get_current_user();
  335. $is_user_connected = $jetpack_connection->is_user_connected( $current_user->ID );
  336. $is_master_user = $is_user_connected && (int) $current_user->ID && (int) Jetpack_Options::get_option( 'master_user' ) === (int) $current_user->ID;
  337. $dotcom_data = $jetpack_connection->get_connected_user_data();
  338. // Add connected user gravatar to the returned dotcom_data.
  339. $dotcom_data['avatar'] = ( ! empty( $dotcom_data['email'] ) ?
  340. get_avatar_url(
  341. $dotcom_data['email'],
  342. array(
  343. 'size' => 64,
  344. 'default' => 'mysteryman',
  345. )
  346. )
  347. : false );
  348. $current_user_data = array(
  349. 'isConnected' => $is_user_connected,
  350. 'isMaster' => $is_master_user,
  351. 'username' => $current_user->user_login,
  352. 'id' => $current_user->ID,
  353. 'wpcomUser' => $dotcom_data,
  354. 'gravatar' => get_avatar_url( $current_user->ID, 64, 'mm', '', array( 'force_display' => true ) ),
  355. 'permissions' => array(
  356. 'admin_page' => current_user_can( 'jetpack_admin_page' ),
  357. 'connect' => current_user_can( 'jetpack_connect' ),
  358. 'connect_user' => current_user_can( 'jetpack_connect_user' ),
  359. 'disconnect' => current_user_can( 'jetpack_disconnect' ),
  360. 'manage_modules' => current_user_can( 'jetpack_manage_modules' ),
  361. 'network_admin' => current_user_can( 'jetpack_network_admin_page' ),
  362. 'network_sites_page' => current_user_can( 'jetpack_network_sites_page' ),
  363. 'edit_posts' => current_user_can( 'edit_posts' ),
  364. 'publish_posts' => current_user_can( 'publish_posts' ),
  365. 'manage_options' => current_user_can( 'manage_options' ),
  366. 'view_stats' => current_user_can( 'view_stats' ),
  367. 'manage_plugins' => current_user_can( 'install_plugins' )
  368. && current_user_can( 'activate_plugins' )
  369. && current_user_can( 'update_plugins' )
  370. && current_user_can( 'delete_plugins' ),
  371. ),
  372. );
  373. return $current_user_data;
  374. }