| @@ -1,0 +1,110 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | +/** | |
| 3 | + * Client = Plugin | |
| 4 | + * Client Server = API Methods the Plugin must respond to | |
| 5 | + * | |
| 6 | + * @package automattic/jetpack | |
| 7 | + */ | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Client = Plugin | |
| 11 | + * Client Server = API Methods the Plugin must respond to | |
| 12 | + */ | |
| 13 | +class Jetpack_Client_Server { | |
| 14 | + | |
| 15 | + /** | |
| 16 | + * Whether the class has been initialized. | |
| 17 | + * | |
| 18 | + * @var bool | |
| 19 | + */ | |
| 20 | + private static $did_init = false; | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * Initialize the hooks, but only once. | |
| 24 | + * | |
| 25 | + * @return void | |
| 26 | + */ | |
| 27 | + public static function init() { | |
| 28 | + if ( static::$did_init ) { | |
| 29 | + return; | |
| 30 | + } | |
| 31 | + | |
| 32 | + add_filter( 'jetpack_rest_connection_check_response', array( static::class, 'connection_check' ) ); | |
| 33 | + | |
| 34 | + static::$did_init = true; | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * Handle the client authorization error. | |
| 39 | + * | |
| 40 | + * @param WP_Error $error The error object. | |
| 41 | + */ | |
| 42 | + public static function client_authorize_error( $error ) { | |
| 43 | + if ( $error instanceof WP_Error ) { | |
| 44 | + Jetpack::state( 'error', $error->get_error_code() ); | |
| 45 | + } | |
| 46 | + } | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * The user is already authorized, we set the Jetpack state and adjust the redirect URL. | |
| 50 | + * | |
| 51 | + * @return string | |
| 52 | + */ | |
| 53 | + public static function client_authorize_already_authorized_url() { | |
| 54 | + Jetpack::state( 'message', 'already_authorized' ); | |
| 55 | + return Jetpack::admin_url(); | |
| 56 | + } | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * The authorization processing has started. | |
| 60 | + */ | |
| 61 | + public static function client_authorize_processing() { | |
| 62 | + Jetpack::log( 'authorize' ); | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * The authorization has completed (successfully or not), and the redirect URL is empty. | |
| 67 | + * We set the Jetpack Dashboard as the default URL. | |
| 68 | + * | |
| 69 | + * @return string | |
| 70 | + */ | |
| 71 | + public static function client_authorize_fallback_url() { | |
| 72 | + return Jetpack::admin_url(); | |
| 73 | + } | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * Deactivate a plugin. | |
| 77 | + * | |
| 78 | + * @param string $probable_file Expected plugin file. | |
| 79 | + * @param string $probable_title Expected plugin title. | |
| 80 | + * @return int 1 if a plugin was deactivated, 0 if not. | |
| 81 | + */ | |
| 82 | + public static function deactivate_plugin( $probable_file, $probable_title ) { | |
| 83 | + include_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 84 | + if ( is_plugin_active( $probable_file ) ) { | |
| 85 | + deactivate_plugins( $probable_file ); | |
| 86 | + return 1; | |
| 87 | + } else { | |
| 88 | + // If the plugin is not in the usual place, try looking through all active plugins. | |
| 89 | + $active_plugins = Jetpack::get_active_plugins(); | |
| 90 | + foreach ( $active_plugins as $plugin ) { | |
| 91 | + $data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); | |
| 92 | + if ( $data['Name'] === $probable_title ) { | |
| 93 | + deactivate_plugins( $plugin ); | |
| 94 | + return 1; | |
| 95 | + } | |
| 96 | + } | |
| 97 | + } | |
| 98 | + | |
| 99 | + return 0; | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * Filters the result of test_connection REST method | |
| 104 | + * | |
| 105 | + * @return string The current Jetpack version number | |
| 106 | + */ | |
| 107 | + public static function connection_check() { | |
| 108 | + return JETPACK__VERSION; | |
| 109 | + } | |
| 110 | +} | |