/wp-content/plugins/jetpack/jetpack_vendor/automattic/jetpack-search/src/dashboard/class-initial-state.php

https://gitlab.com/chernushov881/charity-fund · PHP · 166 lines · 82 code · 17 blank · 67 comment · 6 complexity · 6aab3a86e935474ef99eb8f6182a591a MD5 · raw file

  1. <?php
  2. /**
  3. * The React initial state.
  4. *
  5. * @package automattic/jetpack-search
  6. */
  7. namespace Automattic\Jetpack\Search;
  8. use Automattic\Jetpack\Connection\Manager as Connection_Manager;
  9. use Automattic\Jetpack\Status;
  10. use Jetpack_Options;
  11. /**
  12. * The React initial state.
  13. */
  14. class Initial_State {
  15. /**
  16. * Connection Manager
  17. *
  18. * @var Connection_Manager
  19. */
  20. protected $connection_manager;
  21. /**
  22. * Search Module Control
  23. *
  24. * @var Module_Control
  25. */
  26. protected $module_control;
  27. /**
  28. * Constructor
  29. *
  30. * @param Connection_Manager $connection_manager - Connection mananger instance.
  31. * @param Module_Control $module_control - Module control instance.
  32. */
  33. public function __construct( $connection_manager = null, $module_control = null ) {
  34. // TODO: 'jetpack-search' better to be the current plugin where the package is running.
  35. $this->connection_manager = $connection_manager ? $connection_manager : new Connection_Manager( 'jetpack-search' );
  36. $this->module_control = $module_control ? $module_control : new Module_Control();
  37. }
  38. /**
  39. * Render JS for the initial state
  40. *
  41. * @return string - JS string.
  42. */
  43. public function render() {
  44. return 'var JETPACK_SEARCH_DASHBOARD_INITIAL_STATE=JSON.parse(decodeURIComponent("' . rawurlencode( wp_json_encode( $this->get_initial_state() ) ) . '"));';
  45. }
  46. /**
  47. * Get the initial state data.
  48. *
  49. * @return array
  50. */
  51. public function get_initial_state() {
  52. return array(
  53. 'siteData' => array(
  54. 'WP_API_root' => esc_url_raw( rest_url() ),
  55. 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
  56. 'registrationNonce' => wp_create_nonce( 'jetpack-registration-nonce' ),
  57. 'purchaseToken' => $this->get_purchase_token(),
  58. /**
  59. * Whether promotions are visible or not.
  60. *
  61. * @param bool $are_promotions_active Status of promotions visibility. True by default.
  62. */
  63. 'showPromotions' => apply_filters( 'jetpack_show_promotions', true ),
  64. 'adminUrl' => esc_url( admin_url() ),
  65. 'blogId' => Jetpack_Options::get_option( 'id', 0 ),
  66. // TODO: add JETPACK_SEARCH_PACKAGE_VERSION to a proper place after major PRs merged.
  67. 'version' => defined( 'JETPACK_SEARCH_PACKAGE_VERSION' ) ? JETPACK_SEARCH_PACKAGE_VERSION : 'dev',
  68. 'calypsoSlug' => ( new Status() )->get_site_suffix(),
  69. ),
  70. 'userData' => array(
  71. 'currentUser' => $this->current_user_data(),
  72. ),
  73. 'jetpackSettings' => array(
  74. 'search' => $this->module_control->is_active(),
  75. 'instant_search_enabled' => $this->module_control->is_instant_search_enabled(),
  76. ),
  77. 'features' => array_map(
  78. 'sanitize_text_field',
  79. // phpcs:ignore WordPress.Security.NonceVerification.Recommended
  80. isset( $_GET['features'] ) ? explode( ',', $_GET['features'] ) : array()
  81. ),
  82. );
  83. }
  84. /**
  85. * Gather data about the current user.
  86. *
  87. * @return array
  88. */
  89. protected function current_user_data() {
  90. $current_user = wp_get_current_user();
  91. $is_user_connected = $this->connection_manager->is_user_connected( $current_user->ID );
  92. $is_master_user = $is_user_connected && (int) $current_user->ID && (int) Jetpack_Options::get_option( 'master_user' ) === (int) $current_user->ID;
  93. $dotcom_data = $this->connection_manager->get_connected_user_data();
  94. $current_user_data = array(
  95. 'isConnected' => $is_user_connected,
  96. 'isMaster' => $is_master_user,
  97. 'username' => $current_user->user_login,
  98. 'id' => $current_user->ID,
  99. 'wpcomUser' => $dotcom_data,
  100. 'permissions' => array(
  101. 'manage_options' => current_user_can( 'manage_options' ),
  102. ),
  103. );
  104. return $current_user_data;
  105. }
  106. /**
  107. * Gets a purchase token that is used for Jetpack logged out visitor checkout.
  108. * The purchase token should be appended to all CTA url's that lead to checkout.
  109. *
  110. * @return string|boolean
  111. */
  112. protected function get_purchase_token() {
  113. if ( ! $this->current_user_can_purchase() ) {
  114. return false;
  115. }
  116. $purchase_token = Jetpack_Options::get_option( 'purchase_token', false );
  117. if ( $purchase_token ) {
  118. return $purchase_token;
  119. }
  120. // If the purchase token is not saved in the options table yet, then add it.
  121. Jetpack_Options::update_option( 'purchase_token', $this->generate_purchase_token(), true );
  122. return Jetpack_Options::get_option( 'purchase_token', false );
  123. }
  124. /**
  125. * Generates a purchase token that is used for Jetpack logged out visitor checkout.
  126. *
  127. * @return string
  128. */
  129. protected function generate_purchase_token() {
  130. return wp_generate_password( 12, false );
  131. }
  132. /**
  133. * Determine if the current user is allowed to make Jetpack purchases without
  134. * a WordPress.com account
  135. *
  136. * @return boolean True if the user can make purchases, false if not
  137. */
  138. public function current_user_can_purchase() {
  139. // The site must be site-connected to Jetpack (no users connected).
  140. if ( ! $this->connection_manager->is_site_connection() ) {
  141. return false;
  142. }
  143. // Make sure only administrators can make purchases.
  144. if ( ! current_user_can( 'manage_options' ) ) {
  145. return false;
  146. }
  147. return true;
  148. }
  149. }