From b3e5bad89c7f430fd6ff79f4d709726e473b5585 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Wed, 27 Sep 2023 11:14:52 +0200 Subject: [PATCH] reduce number of checks when system cron is not used (#472) * reduce number of checks when system cron is not used * add health check --- includes/class-health-check.php | 72 ++++++++++++++++++++++++++++----- includes/class-scheduler.php | 16 +++++++- 2 files changed, 76 insertions(+), 12 deletions(-) diff --git a/includes/class-health-check.php b/includes/class-health-check.php index 18cd48e..238455a 100644 --- a/includes/class-health-check.php +++ b/includes/class-health-check.php @@ -1,6 +1,12 @@ array( self::class, 'test_webfinger' ), ); + $tests['direct']['activitypub_test_system_cron'] = array( + 'label' => __( 'System Cron Test', 'activitypub' ), + 'test' => array( self::class, 'test_system_cron' ), + ); + return $tests; } @@ -70,6 +81,42 @@ class Health_Check { return $result; } + /** + * System Cron tests + * + * @return array + */ + public static function test_system_cron() { + $result = array( + 'label' => \__( 'System Task Scheduler configured', 'activitypub' ), + 'status' => 'good', + 'badge' => array( + 'label' => \__( 'ActivityPub', 'activitypub' ), + 'color' => 'green', + ), + 'description' => \sprintf( + '

%s

', + \esc_html__( 'You seem to use the System Task Scheduler to process WP_Cron tasks.', 'activitypub' ) + ), + 'actions' => '', + 'test' => 'test_system_cron', + ); + + if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { + return $result; + } + + $result['status'] = 'recommended'; + $result['label'] = \__( 'System Task Scheduler not configured', 'activitypub' ); + $result['badge']['color'] = 'orange'; + $result['description'] = \sprintf( + '

%s

', + \__( 'It is highly recommended to use your Systems Task Scheduler instead of the default WP_Cron setup. For further informations, check the "Hooking WP-Cron Into the System Task Scheduler" from the Plugin Handbook.', 'activitypub' ) + ); + + return $result; + } + /** * WebFinger tests * @@ -111,7 +158,7 @@ class Health_Check { /** * Check if `author_posts_url` is accessible and that request returns correct JSON * - * @return boolean|\WP_Error + * @return boolean|WP_Error */ public static function is_author_url_accessible() { $user = \wp_get_current_user(); @@ -120,7 +167,7 @@ class Health_Check { // check for "author" in URL if ( $author_url !== $reference_author_url ) { - return new \WP_Error( + return new WP_Error( 'author_url_not_accessible', \sprintf( // translators: %s: Author URL @@ -143,7 +190,7 @@ class Health_Check { ); if ( \is_wp_error( $response ) ) { - return new \WP_Error( + return new WP_Error( 'author_url_not_accessible', \sprintf( // translators: %s: Author URL @@ -160,7 +207,7 @@ class Health_Check { // check for redirects if ( \in_array( $response_code, array( 301, 302, 307, 308 ), true ) ) { - return new \WP_Error( + return new WP_Error( 'author_url_not_accessible', \sprintf( // translators: %s: Author URL @@ -177,7 +224,7 @@ class Health_Check { $body = \wp_remote_retrieve_body( $response ); if ( ! \is_string( $body ) || ! \is_array( \json_decode( $body, true ) ) ) { - return new \WP_Error( + return new WP_Error( 'author_url_not_accessible', \sprintf( // translators: %s: Author URL @@ -196,13 +243,13 @@ class Health_Check { /** * Check if WebFinger endpoint is accessible and profile request returns correct JSON * - * @return boolean|\WP_Error + * @return boolean|WP_Error */ public static function is_webfinger_endpoint_accessible() { $user = \wp_get_current_user(); - $account = \Activitypub\get_webfinger_resource( $user->ID ); + $account = get_webfinger_resource( $user->ID ); - $url = \Activitypub\Webfinger::resolve( $account ); + $url = Webfinger::resolve( $account ); if ( \is_wp_error( $url ) ) { $allowed = array( 'code' => array() ); $not_accessible = wp_kses( @@ -237,7 +284,7 @@ class Health_Check { if ( isset( $health_messages[ $url->get_error_code() ] ) ) { $message = $health_messages[ $url->get_error_code() ]; } - return new \WP_Error( + return new WP_Error( $url->get_error_code(), $message, $url->get_error_data() @@ -291,7 +338,7 @@ class Health_Check { 'fields' => array( 'webfinger' => array( 'label' => __( 'WebFinger Resource', 'activitypub' ), - 'value' => \Activitypub\Webfinger::get_user_resource( wp_get_current_user()->ID ), + 'value' => Webfinger::get_user_resource( wp_get_current_user()->ID ), 'private' => true, ), 'author_url' => array( @@ -299,6 +346,11 @@ class Health_Check { 'value' => get_author_posts_url( wp_get_current_user()->ID ), 'private' => true, ), + 'plugin_version' => array( + 'label' => __( 'Plugin Version', 'activitypub' ), + 'value' => get_plugin_version(), + 'private' => true, + ), ), ); diff --git a/includes/class-scheduler.php b/includes/class-scheduler.php index e7f2bd7..63f9273 100644 --- a/includes/class-scheduler.php +++ b/includes/class-scheduler.php @@ -105,7 +105,13 @@ class Scheduler { * @return void */ public static function update_followers() { - $followers = Followers::get_outdated_followers(); + $number = 5; + + if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { + $number = 50; + } + + $followers = Followers::get_outdated_followers( $number ); foreach ( $followers as $follower ) { $meta = get_remote_metadata_by_actor( $follower->get_url(), false ); @@ -125,7 +131,13 @@ class Scheduler { * @return void */ public static function cleanup_followers() { - $followers = Followers::get_faulty_followers(); + $number = 5; + + if ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) { + $number = 50; + } + + $followers = Followers::get_faulty_followers( $number ); foreach ( $followers as $follower ) { $meta = get_remote_metadata_by_actor( $follower->get_url(), false );