-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add health check paths to scheduled update endpoint (#36990)
* Add health paths to scheduled updates * changelog * Fix: typo * Minor refactoring * Add unit tests * Set paths as required false * Add option deletion * Add limit to 5 paths * Add unit tests fix * Add items type check * Fix: variable typo * Move health paths code to a separate class * Move health checks unit tests to separate class * Fix phan static errors * Add new unit tests * Add esc_url_raw * Minor change * Add warning suppress
- Loading branch information
Showing
11 changed files
with
556 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/packages/scheduled-updates/changelog/add-scheduled-updates-health-checks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Add health paths to scheduled updates. |
138 changes: 138 additions & 0 deletions
138
projects/packages/scheduled-updates/src/class-scheduled-updates-health-paths.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
/** | ||
* Scheduled Updates Health Paths class | ||
* | ||
* @package automattic/scheduled-updates | ||
*/ | ||
|
||
namespace Automattic\Jetpack; | ||
|
||
use WP_Error; | ||
|
||
/** | ||
* Scheduled_Updates_Health_Paths class | ||
* | ||
* This class provides static methods to get/save health paths for scheduled updates. | ||
*/ | ||
class Scheduled_Updates_Health_Paths { | ||
|
||
/** | ||
* The name of the WordPress option where the health check paths are stored. | ||
*/ | ||
const OPTION_NAME = 'jetpack_scheduled_update_health_check_paths'; | ||
|
||
/** | ||
* Get the health check paths for a scheduled update. | ||
* | ||
* @param string $schedule_id Request ID. | ||
* @return array List of health check paths. | ||
*/ | ||
public static function get( $schedule_id ) { | ||
$option = get_option( self::OPTION_NAME, array() ); | ||
|
||
return $option[ $schedule_id ] ?? array(); | ||
} | ||
|
||
/** | ||
* Update the health check paths for a scheduled update. | ||
* | ||
* @param string $schedule_id Request ID. | ||
* @param array $paths List of paths to save. | ||
* @return bool | ||
*/ | ||
public static function update( $schedule_id, $paths ) { | ||
$option = get_option( self::OPTION_NAME, array() ); | ||
$parsed_paths = array(); | ||
|
||
foreach ( $paths as $path ) { | ||
$parsed = self::validate( $path ); | ||
|
||
if ( is_string( $parsed ) ) { | ||
$parsed_paths[] = $parsed; | ||
} | ||
} | ||
|
||
$parsed_paths = array_values( array_unique( $parsed_paths ) ); | ||
|
||
if ( count( $parsed_paths ) ) { | ||
$option[ $schedule_id ] = $parsed_paths; | ||
} | ||
|
||
return update_option( self::OPTION_NAME, $option ); | ||
} | ||
|
||
/** | ||
* Clear the health check paths for a scheduled update. | ||
* | ||
* @param string|null $schedule_id Request ID. | ||
* @return bool | ||
*/ | ||
public static function clear( $schedule_id ) { | ||
$option = get_option( self::OPTION_NAME, array() ); | ||
|
||
if ( isset( $option[ $schedule_id ] ) ) { | ||
unset( $option[ $schedule_id ] ); | ||
} | ||
|
||
if ( count( $option ) ) { | ||
return update_option( self::OPTION_NAME, $option ); | ||
} else { | ||
return delete_option( self::OPTION_NAME ); | ||
} | ||
} | ||
|
||
/** | ||
* Validate a path. | ||
* | ||
* @param string $path An health path. | ||
* @return string|WP_Error | ||
*/ | ||
public static function validate( $path ) { | ||
if ( ! is_string( $path ) ) { | ||
return new WP_Error( 'rest_invalid_path', __( 'The path must be a string.', 'jetpack-scheduled-updates' ) ); | ||
} | ||
|
||
$site_url = wp_parse_url( get_site_url() ); | ||
$path = trim( $path ); | ||
|
||
if ( | ||
! str_starts_with( $path, $site_url['host'] ) && | ||
! str_starts_with( $path, $site_url['scheme'] . '://' . $site_url['host'] ) | ||
) { | ||
// The user sent 'test/test.php' instead of '/test/test.php' and not | ||
// 'http://example.com/test/test.php' or 'example.com/test/test.php'. | ||
$path = '/' . ltrim( $path, '/\\' ); | ||
} | ||
|
||
$path = esc_url_raw( trim( $path ) ); | ||
$parsed = wp_parse_url( $path ); | ||
|
||
if ( false === $parsed ) { | ||
return new WP_Error( 'rest_invalid_path', __( 'The path must be a valid URL.', 'jetpack-scheduled-updates' ) ); | ||
} | ||
|
||
if ( array_key_exists( 'host', $parsed ) ) { | ||
if ( $site_url['host'] !== $parsed['host'] ) { | ||
return new WP_Error( 'rest_invalid_path', __( 'The URL is not from the current site.', 'jetpack-scheduled-updates' ) ); | ||
} | ||
|
||
if ( array_key_exists( 'scheme', $parsed ) && $site_url['scheme'] !== $parsed['scheme'] ) { | ||
return new WP_Error( 'rest_invalid_path', __( 'The URL scheme must match the current site.', 'jetpack-scheduled-updates' ) ); | ||
} | ||
} | ||
|
||
if ( ! array_key_exists( 'path', $parsed ) ) { | ||
$parsed['path'] = ''; | ||
} else { | ||
$parsed['path'] = trim( $parsed['path'] ); | ||
} | ||
|
||
$ret = '/' . ltrim( $parsed['path'], '/\\' ); | ||
|
||
if ( array_key_exists( 'query', $parsed ) ) { | ||
$ret .= '?' . trim( $parsed['query'] ); | ||
} | ||
|
||
return $ret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.