| @@ -1,0 +1,132 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Compatibility functions for the Creative Mail plugin. | |
| 4 | + * https://wordpress.org/plugins/creative-mail-by-constant-contact/ | |
| 5 | + * | |
| 6 | + * @since 8.9.0 | |
| 7 | + * | |
| 8 | + * @package automattic/jetpack | |
| 9 | + */ | |
| 10 | + | |
| 11 | +namespace Automattic\Jetpack\Creative_Mail; | |
| 12 | + | |
| 13 | +use Automattic\Jetpack\Plugins_Installer; | |
| 14 | + | |
| 15 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 16 | + exit( 0 ); | |
| 17 | +} | |
| 18 | + | |
| 19 | +const PLUGIN_SLUG = 'creative-mail-by-constant-contact'; | |
| 20 | +const PLUGIN_FILE = 'creative-mail-by-constant-contact/creative-mail-plugin.php'; | |
| 21 | + | |
| 22 | +add_action( 'jetpack_activated_plugin', __NAMESPACE__ . '\configure_plugin', 10, 2 ); | |
| 23 | + | |
| 24 | +// Check for the JITM action. | |
| 25 | +if ( isset( $_GET['creative-mail-action'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 26 | + add_action( 'admin_init', __NAMESPACE__ . '\try_install' ); | |
| 27 | +} | |
| 28 | + | |
| 29 | +if ( ! empty( $_GET['creative-mail-install-error'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 30 | + add_action( 'admin_notices', __NAMESPACE__ . '\error_notice' ); | |
| 31 | +} | |
| 32 | + | |
| 33 | +/** | |
| 34 | + * Verify the intent to install Creative Mail, and kick off installation. | |
| 35 | + * | |
| 36 | + * This works in tandem with a JITM set up in the JITM package. | |
| 37 | + * | |
| 38 | + * @return never | |
| 39 | + */ | |
| 40 | +function try_install() { | |
| 41 | + check_admin_referer( 'creative-mail-install' ); | |
| 42 | + | |
| 43 | + $result = false; | |
| 44 | + $redirect = admin_url( 'admin.php?page=jetpack-forms-admin' ); | |
| 45 | + | |
| 46 | + // Attempt to install and activate the plugin. | |
| 47 | + if ( current_user_can( 'activate_plugins' ) ) { | |
| 48 | + switch ( $_GET['creative-mail-action'] ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated -- Function only hooked if set. | |
| 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=creativemail' ); | |
| 62 | + } else { | |
| 63 | + $redirect = add_query_arg( 'creative-mail-install-error', true, $redirect ); | |
| 64 | + } | |
| 65 | + | |
| 66 | + wp_safe_redirect( $redirect ); | |
| 67 | + | |
| 68 | + exit( 0 ); | |
| 69 | +} | |
| 70 | + | |
| 71 | +/** | |
| 72 | + * Install and activate the Creative Mail 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 Creative Mail 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 Creative Mail failed. | |
| 100 | + */ | |
| 101 | +function error_notice() { | |
| 102 | + wp_admin_notice( | |
| 103 | + esc_html__( 'There was an error installing Creative Mail.', 'jetpack' ), | |
| 104 | + array( | |
| 105 | + 'type' => 'error', | |
| 106 | + 'dismissible' => true, | |
| 107 | + ) | |
| 108 | + ); | |
| 109 | +} | |
| 110 | + | |
| 111 | +/** | |
| 112 | + * Set some options when first activating the plugin via Jetpack. | |
| 113 | + * | |
| 114 | + * @since 8.9.0 | |
| 115 | + * | |
| 116 | + * @param string $plugin_file Plugin file. | |
| 117 | + * @param string $source Where did the plugin installation originate. | |
| 118 | + */ | |
| 119 | +function configure_plugin( $plugin_file, $source ) { | |
| 120 | + if ( PLUGIN_FILE !== $plugin_file ) { | |
| 121 | + return; | |
| 122 | + } | |
| 123 | + | |
| 124 | + $plugin_info = array( | |
| 125 | + 'plugin' => 'jetpack', | |
| 126 | + 'version' => JETPACK__VERSION, | |
| 127 | + 'time' => time(), | |
| 128 | + 'source' => esc_attr( $source ), | |
| 129 | + ); | |
| 130 | + | |
| 131 | + update_option( 'ce4wp_referred_by', $plugin_info ); | |
| 132 | +} | |