← All changes
|
jetpack_vendor/automattic/jetpack-backup-helper-script-manager/src/class-helper-script-manager.php
+89
-0
16.2-beta
→
16.3-beta
View file →
| @@ -1,0 +1,89 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Jetpack Backup Helper Script Manager class (static wrapper). | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack-backup | |
| 6 | + */ | |
| 7 | + | |
| 8 | +// After changing this file, consider increasing the version number ("VXXX") in all the files using this namespace, in | |
| 9 | +// order to ensure that the specific version of this file always get loaded. Otherwise, Jetpack autoloader might decide | |
| 10 | +// to load an older/newer version of the class (if, for example, both the standalone and bundled versions of the plugin | |
| 11 | +// are installed, or in some other cases). | |
| 12 | +namespace Automattic\Jetpack\Backup\V0005; | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Manage installation, deletion and cleanup of Helper Scripts to assist with backing up Jetpack Sites. | |
| 16 | + * | |
| 17 | + * A static wrapper around an "implementation" class so that it gets autoloaded late, and we always get the latest | |
| 18 | + * version of the class instead of a random version of it: | |
| 19 | + * | |
| 20 | + * https://github.com/Automattic/jetpack/pull/34297#discussion_r1424227489 | |
| 21 | + */ | |
| 22 | +class Helper_Script_Manager { | |
| 23 | + | |
| 24 | + /** | |
| 25 | + * Instance of helper script manager implementation, or null if not initialized yet. | |
| 26 | + * | |
| 27 | + * @var Helper_Script_Manager_Impl|null | |
| 28 | + */ | |
| 29 | + protected static $impl = null; | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * Initialize an instance of helper script manager implementation (if needed). | |
| 33 | + * | |
| 34 | + * @return void | |
| 35 | + */ | |
| 36 | + protected static function initialize_impl_if_needed() { | |
| 37 | + if ( null === static::$impl ) { | |
| 38 | + static::$impl = new Helper_Script_Manager_Impl(); | |
| 39 | + } | |
| 40 | + } | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Install a Helper Script, and returns its filesystem path and access url. | |
| 44 | + * | |
| 45 | + * @param string $script_body Helper Script file contents. | |
| 46 | + * | |
| 47 | + * @return array|\WP_Error Either an array containing the filesystem path ("path"), the URL ("url") of the helper | |
| 48 | + * script, and the WordPress root ("abspath"), or an instance of WP_Error. | |
| 49 | + */ | |
| 50 | + public static function install_helper_script( $script_body ) { | |
| 51 | + static::initialize_impl_if_needed(); | |
| 52 | + return static::$impl->install_helper_script( $script_body ); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Ensure that the helper script is gone (by deleting it, if needed). | |
| 57 | + * | |
| 58 | + * @param string $path Path to the helper script to delete. | |
| 59 | + * | |
| 60 | + * @return true|\WP_Error True if the file helper script is gone (either it got deleted, or it was never there), or | |
| 61 | + * WP_Error instance on deletion failures. | |
| 62 | + */ | |
| 63 | + public static function delete_helper_script( $path ) { | |
| 64 | + static::initialize_impl_if_needed(); | |
| 65 | + return static::$impl->delete_helper_script( $path ); | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Search for Helper Scripts that are suspiciously old, and clean them out. | |
| 70 | + * | |
| 71 | + * @return true|\WP_Error True if all expired helper scripts got cleaned up successfully, or an instance of | |
| 72 | + * WP_Error if one or more expired helper scripts didn't manage to get cleaned up. | |
| 73 | + */ | |
| 74 | + public static function cleanup_expired_helper_scripts() { | |
| 75 | + static::initialize_impl_if_needed(); | |
| 76 | + return static::$impl->cleanup_expired_helper_scripts(); | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 80 | + * Search for and delete all Helper Scripts. Used during uninstallation. | |
| 81 | + * | |
| 82 | + * @return true|\WP_Error True if all helper scripts got deleted successfully, or an instance of WP_Error if one or | |
| 83 | + * more helper scripts didn't manage to get deleted. | |
| 84 | + */ | |
| 85 | + public static function delete_all_helper_scripts() { | |
| 86 | + static::initialize_impl_if_needed(); | |
| 87 | + return static::$impl->delete_all_helper_scripts(); | |
| 88 | + } | |
| 89 | +} | |