Нет описания

class-jetpack-recommendations.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * Utilities related to the Jetpack Recommendations
  4. *
  5. * @package automattic/jetpack
  6. */
  7. use Automattic\Jetpack\Connection\Client;
  8. use Automattic\Jetpack\Connection\Manager as Connection_Manager;
  9. use Automattic\Jetpack\Status;
  10. /**
  11. * Contains utilities related to the Jetpack Recommendations.
  12. *
  13. * @package automattic/jetpack
  14. */
  15. /**
  16. * Jetpack_Recommendations class
  17. */
  18. class Jetpack_Recommendations {
  19. /**
  20. * Returns a boolean indicating if the Jetpack Recommendations are enabled.
  21. *
  22. * @since 9.3.0
  23. *
  24. * @return bool
  25. */
  26. public static function is_enabled() {
  27. // Shortcircuit early if Jetpack is not active or we are in offline mode.
  28. if ( ! Jetpack::is_connection_ready() || ( new Status() )->is_offline_mode() ) {
  29. return false;
  30. }
  31. // No recommendations for Atomic sites, they already get onboarded in Calypso.
  32. if ( jetpack_is_atomic_site() ) {
  33. return false;
  34. }
  35. self::initialize_jetpack_recommendations();
  36. return true;
  37. }
  38. /**
  39. * Returns a boolean indicating if the Jetpack Banner is enabled.
  40. *
  41. * @since 9.3.0
  42. *
  43. * @return bool
  44. */
  45. public static function is_banner_enabled() {
  46. // Shortcircuit early if the recommendations are not enabled at all.
  47. if ( ! self::is_enabled() ) {
  48. return false;
  49. }
  50. $recommendations_banner_enabled = Jetpack_Options::get_option( 'recommendations_banner_enabled', null );
  51. // If the option is already set, just return the cached value.
  52. // Otherwise calculate it and store it before returning it.
  53. if ( null !== $recommendations_banner_enabled ) {
  54. return $recommendations_banner_enabled;
  55. }
  56. if ( ! Jetpack::connection()->is_connected() ) {
  57. return new WP_Error( 'site_not_connected', esc_html__( 'Site not connected.', 'jetpack' ) );
  58. }
  59. $blog_id = Jetpack_Options::get_option( 'id' );
  60. $request_path = sprintf( '/sites/%s/jetpack-recommendations/site-registered-date', $blog_id );
  61. $result = Client::wpcom_json_api_request_as_blog(
  62. $request_path,
  63. 2,
  64. array(
  65. 'headers' => array( 'content-type' => 'application/json' ),
  66. ),
  67. null,
  68. 'wpcom'
  69. );
  70. $body = json_decode( wp_remote_retrieve_body( $result ) );
  71. if ( 200 === wp_remote_retrieve_response_code( $result ) ) {
  72. $site_registered_date = $body->site_registered_date;
  73. } else {
  74. $connection = new Connection_Manager( 'jetpack' );
  75. $site_registered_date = $connection->get_assumed_site_creation_date();
  76. }
  77. $recommendations_start_date = gmdate( 'Y-m-d H:i:s', strtotime( '2020-12-01 00:00:00' ) );
  78. $recommendations_banner_enabled = $site_registered_date > $recommendations_start_date;
  79. Jetpack_Options::update_option( 'recommendations_banner_enabled', $recommendations_banner_enabled );
  80. return $recommendations_banner_enabled;
  81. }
  82. /**
  83. * Initializes the Recommendations step according to the Setup Wizard state.
  84. */
  85. private static function initialize_jetpack_recommendations() {
  86. if ( Jetpack_Options::get_option( 'recommendations_step' ) ) {
  87. return;
  88. }
  89. $setup_wizard_status = Jetpack_Options::get_option( 'setup_wizard_status' );
  90. if ( 'completed' === $setup_wizard_status ) {
  91. Jetpack_Options::update_option( 'recommendations_banner_enabled', false );
  92. Jetpack_Options::update_option( 'recommendations_step', 'setup-wizard-completed' );
  93. }
  94. }
  95. /**
  96. * Get the data for the recommendations
  97. *
  98. * @return array Recommendations data
  99. */
  100. public static function get_recommendations_data() {
  101. self::initialize_jetpack_recommendations();
  102. return Jetpack_Options::get_option( 'recommendations_data', array() );
  103. }
  104. /**
  105. * Update the data for the recommendations
  106. *
  107. * @param WP_REST_Request $data The data.
  108. */
  109. public static function update_recommendations_data( $data ) {
  110. if ( ! empty( $data ) ) {
  111. Jetpack_Options::update_option( 'recommendations_data', $data );
  112. }
  113. }
  114. /**
  115. * Get the data for the recommendations
  116. *
  117. * @return array Recommendations data
  118. */
  119. public static function get_recommendations_step() {
  120. self::initialize_jetpack_recommendations();
  121. return array(
  122. 'step' => Jetpack_Options::get_option( 'recommendations_step', 'not-started' ),
  123. );
  124. }
  125. /**
  126. * Update the step for the recommendations
  127. *
  128. * @param WP_REST_Request $step The step.
  129. */
  130. public static function update_recommendations_step( $step ) {
  131. if ( ! empty( $step ) ) {
  132. Jetpack_Options::update_option( 'recommendations_step', $step );
  133. }
  134. }
  135. }