Skip to content

Commit

Permalink
Run REST API health check on plugin activation, Display notice if che…
Browse files Browse the repository at this point in the history
…ck fails
  • Loading branch information
b1ink0 committed Dec 24, 2024
1 parent 2b2ca3e commit e7c1001
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
18 changes: 16 additions & 2 deletions plugins/optimization-detective/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ static function ( string $global_var_name, string $version, Closure $load ): voi
* action), so this is why it gets initialized at priority 9.
*/
add_action( 'init', $bootstrap, 9 );
register_activation_hook(
__FILE__,
static function () use ( $bootstrap ): void {
$bootstrap();
od_plugin_activation();
}
);
}

// Register this copy of the plugin.
Expand Down Expand Up @@ -143,7 +150,14 @@ function od_plugin_activation(): void {
if ( ! (bool) get_option( 'od_rest_api_info' ) ) {
add_option( 'od_rest_api_info', array() );
}
require_once __DIR__ . '/site-health/rest-api/helper.php';
od_schedule_rest_api_health_check();
// Run the check immediately after Optimization Detective is activated.
add_action(
'activated_plugin',
static function ( string $plugin ): void {
if ( 'optimization-detective/load.php' === $plugin ) {
od_optimization_detective_rest_api_test();
}
}
);
}
register_activation_hook( __FILE__, 'od_plugin_activation' );
29 changes: 29 additions & 0 deletions plugins/optimization-detective/site-health/rest-api/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ function od_optimization_detective_rest_api_test(): array {
esc_html__( 'The Optimization Detective endpoint could not be reached. This might mean the REST API is disabled or blocked.', 'optimization-detective' )
);
}
$info['error_message'] = $result['label'];
}

update_option( 'od_rest_api_info', $info );
Expand All @@ -118,3 +119,31 @@ function od_schedule_rest_api_health_check(): void {
function od_run_scheduled_rest_api_health_check(): void {
od_optimization_detective_rest_api_test();
}

/**
* Displays an admin notice if the REST API health check fails.
*
* @since n.e.x.t
*
* @param string $plugin_file Plugin file.
*/
function od_rest_api_health_check_admin_notice( string $plugin_file ): void {
if ( 'optimization-detective/load.php' !== $plugin_file ) {
return;
}

$od_rest_api_info = get_option( 'od_rest_api_info', array() );
if (
isset( $od_rest_api_info['available'] ) &&
! (bool) $od_rest_api_info['available'] &&
isset( $od_rest_api_info['error_message'] )
) {
wp_admin_notice(
esc_html( $od_rest_api_info['error_message'] ),
array(
'type' => 'warning',
'additional_classes' => array( 'inline', 'notice-alt' ),
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ function od_optimization_detective_add_rest_api_test( array $tests ): array {

// Hook for the scheduled REST API health check.
add_action( 'od_rest_api_health_check_event', 'od_run_scheduled_rest_api_health_check' );
add_action( 'after_plugin_row_meta', 'od_rest_api_health_check_admin_notice', 30 );

0 comments on commit e7c1001

Please sign in to comment.