Açıklama Yok

debug-functions.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * WP Site Health functionality temporarily stored in this file until all of Jetpack is PHP 5.3+
  4. *
  5. * @package automattic/jetpack
  6. */
  7. use Automattic\Jetpack\Sync\Modules;
  8. /**
  9. * Test runner for Core's Site Health module.
  10. *
  11. * @since 7.3.0
  12. */
  13. function jetpack_debugger_ajax_local_testing_suite() {
  14. check_ajax_referer( 'health-check-site-status' );
  15. if ( ! current_user_can( 'jetpack_manage_modules' ) ) {
  16. wp_send_json_error();
  17. }
  18. $tests = new Jetpack_Cxn_Tests();
  19. wp_send_json_success( $tests->output_results_for_core_async_site_health() );
  20. }
  21. /**
  22. * Adds the Jetpack Local Testing Suite to the Core Site Health system.
  23. *
  24. * @since 7.3.0
  25. *
  26. * @param array $core_tests Array of tests from Core's Site Health.
  27. *
  28. * @return array $core_tests Array of tests for Core's Site Health.
  29. */
  30. function jetpack_debugger_site_status_tests( $core_tests ) {
  31. $cxn_tests = new Jetpack_Cxn_Tests();
  32. $tests = $cxn_tests->list_tests( 'direct' );
  33. foreach ( $tests as $test ) {
  34. $core_tests['direct'][ $test['name'] ] = array(
  35. 'label' => __( 'Jetpack: ', 'jetpack' ) . $test['name'],
  36. /**
  37. * Callable for Core's Site Health system to execute.
  38. *
  39. * @param array $test A Jetpack Testing Suite test array.
  40. * @param Jetpack_Cxn_Tests $cxn_tests An instance of the Jetpack Test Suite.
  41. *
  42. * @return array {
  43. * A results array to match the format expected by WordPress Core.
  44. *
  45. * @type string $label Name for the test.
  46. * @type string $status 'critical', 'recommended', or 'good'.
  47. * @type array $badge Array for Site Health status. Keys label and color.
  48. * @type string $description Description of the test result.
  49. * @type string $action HTML to a link to resolve issue.
  50. * @type string $test Unique test identifier.
  51. * }
  52. */
  53. 'test' => function () use ( $test, $cxn_tests ) {
  54. $results = $cxn_tests->run_test( $test['name'] );
  55. if ( is_wp_error( $results ) ) {
  56. return;
  57. }
  58. $label = $results['label'] ?
  59. $results['label'] :
  60. ucwords(
  61. str_replace(
  62. '_',
  63. ' ',
  64. str_replace( 'test__', '', $test['name'] )
  65. )
  66. );
  67. if ( $results['long_description'] ) {
  68. $description = $results['long_description'];
  69. } elseif ( $results['short_description'] ) {
  70. $description = sprintf(
  71. '<p>%s</p>',
  72. $results['short_description']
  73. );
  74. } else {
  75. $description = sprintf(
  76. '<p>%s</p>',
  77. __( 'This test successfully passed!', 'jetpack' )
  78. );
  79. }
  80. $return = array(
  81. 'label' => $label,
  82. 'status' => 'good',
  83. 'badge' => array(
  84. 'label' => __( 'Jetpack', 'jetpack' ),
  85. 'color' => 'green',
  86. ),
  87. 'description' => $description,
  88. 'actions' => '',
  89. 'test' => 'jetpack_' . $test['name'],
  90. );
  91. if ( false === $results['pass'] ) {
  92. $return['status'] = $results['severity'];
  93. if ( ! empty( $results['action'] ) ) {
  94. $return['actions'] = sprintf(
  95. '<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s <span class="screen-reader-text">%3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>',
  96. esc_url( $results['action'] ),
  97. $results['action_label'],
  98. /* translators: accessibility text */
  99. __( '(opens in a new tab)', 'jetpack' )
  100. );
  101. }
  102. }
  103. return $return;
  104. },
  105. );
  106. }
  107. $core_tests['async']['jetpack_test_suite'] = array(
  108. 'label' => __( 'Jetpack Tests', 'jetpack' ),
  109. 'test' => 'jetpack_local_testing_suite',
  110. );
  111. return $core_tests;
  112. }
  113. /**
  114. * Loads site health scripts if we are on the site health page.
  115. *
  116. * @param string $hook The current admin page hook.
  117. */
  118. function jetpack_debugger_enqueue_site_health_scripts( $hook ) {
  119. $full_sync_module = Modules::get_module( 'full-sync' );
  120. $progress_percent = $full_sync_module ? $full_sync_module->get_sync_progress_percentage() : false;
  121. $ajax_nonce = wp_create_nonce( 'jetpack-site-health' );
  122. if ( 'site-health.php' === $hook ) {
  123. $wp_scripts = wp_scripts();
  124. wp_enqueue_script( 'jquery-ui-progressbar' );
  125. wp_enqueue_script(
  126. 'jetpack_debug_site_health_script',
  127. plugins_url( 'jetpack-debugger-site-health.js', __FILE__ ),
  128. array( 'jquery-ui-progressbar' ),
  129. JETPACK__VERSION,
  130. false
  131. );
  132. wp_enqueue_style(
  133. 'jetpack_debug_site_health_styles',
  134. plugins_url( 'jetpack-debugger-site-health.css', __FILE__ ),
  135. false,
  136. JETPACK__VERSION,
  137. false
  138. );
  139. /* WordPress is not bundled with jquery UI styles - we need to grab them from the Google API. */
  140. wp_enqueue_style(
  141. 'jetpack-jquery-ui-styles',
  142. 'https://code.jquery.com/ui/' . $wp_scripts->registered['jquery-ui-core']->ver . '/themes/smoothness/jquery-ui.min.css',
  143. false,
  144. JETPACK__VERSION,
  145. false
  146. );
  147. wp_localize_script(
  148. 'jetpack_debug_site_health_script',
  149. 'jetpackSiteHealth',
  150. array(
  151. 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
  152. 'syncProgressHeading' => __( 'Jetpack is performing a sync of your site', 'jetpack' ),
  153. 'progressPercent' => $progress_percent,
  154. 'fullSyncNonce' => $ajax_nonce,
  155. )
  156. );
  157. }
  158. }
  159. /**
  160. * Responds to ajax calls from the site health page. Echos a full sync percantage to update progress bar.
  161. */
  162. function jetpack_debugger_sync_progress_ajax() {
  163. $full_sync_module = Modules::get_module( 'full-sync' );
  164. $progress_percent = $full_sync_module ? $full_sync_module->get_sync_progress_percentage() : null;
  165. if ( ! $progress_percent ) {
  166. echo 'done';
  167. wp_die();
  168. }
  169. echo (int) $progress_percent;
  170. wp_die();
  171. }
  172. /**
  173. * Responds to ajax calls from the site health page. Triggers a Full Sync
  174. */
  175. function jetpack_debugger_full_sync_start() {
  176. check_ajax_referer( 'jetpack-site-health', 'site-health-nonce' );
  177. $full_sync_module = Modules::get_module( 'full-sync' );
  178. $full_sync_module->start();
  179. echo 'requested';
  180. wp_die();
  181. }