reduce number of checks when system cron is not used (#472)

* reduce number of checks when system cron is not used

* add health check
This commit is contained in:
Matthias Pfefferle 2023-09-27 11:14:52 +02:00 committed by GitHub
parent bcb88eb06f
commit b3e5bad89c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 12 deletions

View file

@ -1,6 +1,12 @@
<?php <?php
namespace Activitypub; namespace Activitypub;
use WP_Error;
use Activitypub\Webfinger;
use function Activitypub\get_plugin_version;
use function Activitypub\get_webfinger_resource;
/** /**
* ActivityPub Health_Check Class * ActivityPub Health_Check Class
* *
@ -29,6 +35,11 @@ class Health_Check {
'test' => array( self::class, 'test_webfinger' ), 'test' => 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; return $tests;
} }
@ -70,6 +81,42 @@ class Health_Check {
return $result; 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(
'<p>%s</p>',
\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(
'<p>%s</p>',
\__( 'It is highly recommended to use your Systems Task Scheduler instead of the default <code>WP_Cron</code> setup. For further informations, check the "<a href="https://developer.wordpress.org/plugins/cron/hooking-wp-cron-into-the-system-task-scheduler/" target="_blank">Hooking WP-Cron Into the System Task Scheduler</a>" from the Plugin Handbook.', 'activitypub' )
);
return $result;
}
/** /**
* WebFinger tests * WebFinger tests
* *
@ -111,7 +158,7 @@ class Health_Check {
/** /**
* Check if `author_posts_url` is accessible and that request returns correct JSON * 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() { public static function is_author_url_accessible() {
$user = \wp_get_current_user(); $user = \wp_get_current_user();
@ -120,7 +167,7 @@ class Health_Check {
// check for "author" in URL // check for "author" in URL
if ( $author_url !== $reference_author_url ) { if ( $author_url !== $reference_author_url ) {
return new \WP_Error( return new WP_Error(
'author_url_not_accessible', 'author_url_not_accessible',
\sprintf( \sprintf(
// translators: %s: Author URL // translators: %s: Author URL
@ -143,7 +190,7 @@ class Health_Check {
); );
if ( \is_wp_error( $response ) ) { if ( \is_wp_error( $response ) ) {
return new \WP_Error( return new WP_Error(
'author_url_not_accessible', 'author_url_not_accessible',
\sprintf( \sprintf(
// translators: %s: Author URL // translators: %s: Author URL
@ -160,7 +207,7 @@ class Health_Check {
// check for redirects // check for redirects
if ( \in_array( $response_code, array( 301, 302, 307, 308 ), true ) ) { if ( \in_array( $response_code, array( 301, 302, 307, 308 ), true ) ) {
return new \WP_Error( return new WP_Error(
'author_url_not_accessible', 'author_url_not_accessible',
\sprintf( \sprintf(
// translators: %s: Author URL // translators: %s: Author URL
@ -177,7 +224,7 @@ class Health_Check {
$body = \wp_remote_retrieve_body( $response ); $body = \wp_remote_retrieve_body( $response );
if ( ! \is_string( $body ) || ! \is_array( \json_decode( $body, true ) ) ) { if ( ! \is_string( $body ) || ! \is_array( \json_decode( $body, true ) ) ) {
return new \WP_Error( return new WP_Error(
'author_url_not_accessible', 'author_url_not_accessible',
\sprintf( \sprintf(
// translators: %s: Author URL // translators: %s: Author URL
@ -196,13 +243,13 @@ class Health_Check {
/** /**
* Check if WebFinger endpoint is accessible and profile request returns correct JSON * 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() { public static function is_webfinger_endpoint_accessible() {
$user = \wp_get_current_user(); $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 ) ) { if ( \is_wp_error( $url ) ) {
$allowed = array( 'code' => array() ); $allowed = array( 'code' => array() );
$not_accessible = wp_kses( $not_accessible = wp_kses(
@ -237,7 +284,7 @@ class Health_Check {
if ( isset( $health_messages[ $url->get_error_code() ] ) ) { if ( isset( $health_messages[ $url->get_error_code() ] ) ) {
$message = $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(), $url->get_error_code(),
$message, $message,
$url->get_error_data() $url->get_error_data()
@ -291,7 +338,7 @@ class Health_Check {
'fields' => array( 'fields' => array(
'webfinger' => array( 'webfinger' => array(
'label' => __( 'WebFinger Resource', 'activitypub' ), '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, 'private' => true,
), ),
'author_url' => array( 'author_url' => array(
@ -299,6 +346,11 @@ class Health_Check {
'value' => get_author_posts_url( wp_get_current_user()->ID ), 'value' => get_author_posts_url( wp_get_current_user()->ID ),
'private' => true, 'private' => true,
), ),
'plugin_version' => array(
'label' => __( 'Plugin Version', 'activitypub' ),
'value' => get_plugin_version(),
'private' => true,
),
), ),
); );

View file

@ -105,7 +105,13 @@ class Scheduler {
* @return void * @return void
*/ */
public static function update_followers() { 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 ) { foreach ( $followers as $follower ) {
$meta = get_remote_metadata_by_actor( $follower->get_url(), false ); $meta = get_remote_metadata_by_actor( $follower->get_url(), false );
@ -125,7 +131,13 @@ class Scheduler {
* @return void * @return void
*/ */
public static function cleanup_followers() { 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 ) { foreach ( $followers as $follower ) {
$meta = get_remote_metadata_by_actor( $follower->get_url(), false ); $meta = get_remote_metadata_by_actor( $follower->get_url(), false );