| @@ -1,0 +1,112 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Compatibility functions for the Jetpack Boost plugin. | |
| 4 | + * https://wordpress.org/plugins/jetpack-boost/ | |
| 5 | + * | |
| 6 | + * @since 10.4 | |
| 7 | + * | |
| 8 | + * @package automattic/jetpack | |
| 9 | + */ | |
| 10 | + | |
| 11 | +namespace Automattic\Jetpack\Jetpack_Boost; | |
| 12 | + | |
| 13 | +use Automattic\Jetpack\Plugins_Installer; | |
| 14 | + | |
| 15 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 16 | + exit( 0 ); | |
| 17 | +} | |
| 18 | + | |
| 19 | +const PLUGIN_SLUG = 'jetpack-boost'; | |
| 20 | +const PLUGIN_FILE = 'jetpack-boost/jetpack-boost.php'; | |
| 21 | + | |
| 22 | +if ( isset( $_GET['jetpack-boost-install-error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 23 | + add_action( 'admin_notices', __NAMESPACE__ . '\error_notice' ); | |
| 24 | +} | |
| 25 | + | |
| 26 | +if ( isset( $_GET['jetpack-boost-action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 27 | + add_action( 'admin_init', __NAMESPACE__ . '\try_install' ); | |
| 28 | +} | |
| 29 | + | |
| 30 | +/** | |
| 31 | + * Verify the intent to install Jetpack Boost, and kick off installation. | |
| 32 | + * | |
| 33 | + * This works in tandem with a JITM set up in the JITM package. | |
| 34 | + */ | |
| 35 | +function try_install() { | |
| 36 | + if ( ! isset( $_GET['jetpack-boost-action'] ) ) { | |
| 37 | + return; | |
| 38 | + } | |
| 39 | + | |
| 40 | + check_admin_referer( 'jetpack-boost-install' ); | |
| 41 | + | |
| 42 | + $result = false; | |
| 43 | + // If the plugin install fails, redirect to plugin install page pre-populated with jetpack-boost search term. | |
| 44 | + $redirect_on_error = admin_url( 'plugin-install.php?s=jetpack-boost&tab=search&type=term' ); | |
| 45 | + | |
| 46 | + // Attempt to install and activate the plugin. | |
| 47 | + if ( current_user_can( 'activate_plugins' ) ) { | |
| 48 | + switch ( $_GET['jetpack-boost-action'] ) { | |
| 49 | + case 'install': | |
| 50 | + $result = install_and_activate(); | |
| 51 | + break; | |
| 52 | + case 'activate': | |
| 53 | + $result = activate(); | |
| 54 | + break; | |
| 55 | + } | |
| 56 | + } | |
| 57 | + | |
| 58 | + if ( $result ) { | |
| 59 | + /** This action is already documented in _inc/lib/class.core-rest-api-endpoints.php */ | |
| 60 | + do_action( 'jetpack_activated_plugin', PLUGIN_FILE, 'jitm' ); | |
| 61 | + $redirect = admin_url( 'admin.php?page=jetpack-boost' ); | |
| 62 | + } else { | |
| 63 | + $redirect = add_query_arg( 'jetpack-boost-install-error', true, $redirect_on_error ); | |
| 64 | + } | |
| 65 | + | |
| 66 | + wp_safe_redirect( $redirect ); | |
| 67 | + | |
| 68 | + exit( 0 ); | |
| 69 | +} | |
| 70 | + | |
| 71 | +/** | |
| 72 | + * Install and activate the Jetpack Boost plugin. | |
| 73 | + * | |
| 74 | + * @return bool result of installation | |
| 75 | + */ | |
| 76 | +function install_and_activate() { | |
| 77 | + $result = Plugins_Installer::install_and_activate_plugin( PLUGIN_SLUG ); | |
| 78 | + | |
| 79 | + if ( is_wp_error( $result ) ) { | |
| 80 | + return false; | |
| 81 | + } else { | |
| 82 | + return true; | |
| 83 | + } | |
| 84 | +} | |
| 85 | + | |
| 86 | +/** | |
| 87 | + * Activate the Jetpack Boost plugin. | |
| 88 | + * | |
| 89 | + * @return bool result of activation | |
| 90 | + */ | |
| 91 | +function activate() { | |
| 92 | + $result = activate_plugin( PLUGIN_FILE ); | |
| 93 | + | |
| 94 | + // Activate_plugin() returns null on success. | |
| 95 | + return $result === null; | |
| 96 | +} | |
| 97 | + | |
| 98 | +/** | |
| 99 | + * Notify the user that the installation of Jetpack Boost failed. | |
| 100 | + */ | |
| 101 | +function error_notice() { | |
| 102 | + if ( empty( $_GET['jetpack-boost-install-error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 103 | + return; | |
| 104 | + } | |
| 105 | + wp_admin_notice( | |
| 106 | + esc_html__( 'There was an error installing Jetpack Boost. Please try again.', 'jetpack' ), | |
| 107 | + array( | |
| 108 | + 'type' => 'error', | |
| 109 | + 'dismissible' => true, | |
| 110 | + ) | |
| 111 | + ); | |
| 112 | +} | |