PluginSilentUpgrader.php
5 years ago
PluginSilentUpgraderSkin.php
6 years ago
addon-functions.php
6 years ago
class-cff-about.php
6 years ago
class-cff-new-user.php
5 years ago
class-cff-notifications.php
5 years ago
class-cff-tracking.php
5 years ago
class-install-skin.php
6 years ago
addon-functions.php
176 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 | |
| 4 | /** |
| 5 | * Deactivate addon. |
| 6 | * |
| 7 | * @since 1.0.0 |
| 8 | */ |
| 9 | function cff_deactivate_addon() { |
| 10 | |
| 11 | // Run a security check. |
| 12 | check_ajax_referer( 'cff-admin', 'nonce' ); |
| 13 | $cap = current_user_can( 'manage_custom_facebook_feed_options' ) ? 'manage_custom_facebook_feed_options' : 'manage_options'; |
| 14 | $cap = apply_filters( 'cff_settings_pages_capability', $cap ); |
| 15 | // Check for permissions. |
| 16 | if ( ! current_user_can( $cap ) ) { |
| 17 | wp_send_json_error(); |
| 18 | } |
| 19 | |
| 20 | $type = 'addon'; |
| 21 | if ( ! empty( $_POST['type'] ) ) { |
| 22 | $type = sanitize_key( $_POST['type'] ); |
| 23 | } |
| 24 | |
| 25 | if ( isset( $_POST['plugin'] ) ) { |
| 26 | deactivate_plugins( $_POST['plugin'] ); |
| 27 | |
| 28 | if ( 'plugin' === $type ) { |
| 29 | wp_send_json_success( esc_html__( 'Plugin deactivated.', 'custom-facebook-feed' ) ); |
| 30 | } else { |
| 31 | wp_send_json_success( esc_html__( 'Addon deactivated.', 'custom-facebook-feed' ) ); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | wp_send_json_error( esc_html__( 'Could not deactivate the addon. Please deactivate from the Plugins page.', 'custom-facebook-feed' ) ); |
| 36 | } |
| 37 | add_action( 'wp_ajax_cff_deactivate_addon', 'cff_deactivate_addon' ); |
| 38 | |
| 39 | /** |
| 40 | * Activate addon. |
| 41 | * |
| 42 | * @since 1.0.0 |
| 43 | */ |
| 44 | function cff_activate_addon() { |
| 45 | |
| 46 | // Run a security check. |
| 47 | check_ajax_referer( 'cff-admin', 'nonce' ); |
| 48 | |
| 49 | // Check for permissions. |
| 50 | if ( ! current_user_can( 'manage_options' ) ) { |
| 51 | wp_send_json_error(); |
| 52 | } |
| 53 | |
| 54 | if ( isset( $_POST['plugin'] ) ) { |
| 55 | |
| 56 | $type = 'addon'; |
| 57 | if ( ! empty( $_POST['type'] ) ) { |
| 58 | $type = sanitize_key( $_POST['type'] ); |
| 59 | } |
| 60 | |
| 61 | $activate = activate_plugins( $_POST['plugin'] ); |
| 62 | |
| 63 | if ( ! is_wp_error( $activate ) ) { |
| 64 | if ( 'plugin' === $type ) { |
| 65 | wp_send_json_success( esc_html__( 'Plugin activated.', 'custom-facebook-feed' ) ); |
| 66 | } else { |
| 67 | wp_send_json_success( esc_html__( 'Addon activated.', 'custom-facebook-feed' ) ); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | wp_send_json_error( esc_html__( 'Could not activate addon. Please activate from the Plugins page.', 'custom-facebook-feed' ) ); |
| 73 | } |
| 74 | add_action( 'wp_ajax_cff_activate_addon', 'cff_activate_addon' ); |
| 75 | |
| 76 | /** |
| 77 | * Install addon. |
| 78 | * |
| 79 | * @since 1.0.0 |
| 80 | */ |
| 81 | function cff_install_addon() { |
| 82 | |
| 83 | // Run a security check. |
| 84 | check_ajax_referer( 'cff-admin', 'nonce' ); |
| 85 | |
| 86 | // Check for permissions. |
| 87 | if ( ! current_user_can( 'manage_options' ) ) { |
| 88 | wp_send_json_error(); |
| 89 | } |
| 90 | |
| 91 | $error = esc_html__( 'Could not install addon. Please download from smashballoon.com and install manually.', 'custom-facebook-feed' ); |
| 92 | |
| 93 | if ( empty( $_POST['plugin'] ) ) { |
| 94 | wp_send_json_error( $error ); |
| 95 | } |
| 96 | |
| 97 | // Set the current screen to avoid undefined notices. |
| 98 | set_current_screen( 'cff-about' ); |
| 99 | |
| 100 | // Prepare variables. |
| 101 | $url = esc_url_raw( |
| 102 | add_query_arg( |
| 103 | array( |
| 104 | 'page' => 'cff-about', |
| 105 | ), |
| 106 | admin_url( 'admin.php' ) |
| 107 | ) |
| 108 | ); |
| 109 | |
| 110 | $creds = request_filesystem_credentials( $url, '', false, false, null ); |
| 111 | |
| 112 | // Check for file system permissions. |
| 113 | if ( false === $creds ) { |
| 114 | wp_send_json_error( $error ); |
| 115 | } |
| 116 | |
| 117 | if ( ! WP_Filesystem( $creds ) ) { |
| 118 | wp_send_json_error( $error ); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * We do not need any extra credentials if we have gotten this far, so let's install the plugin. |
| 123 | */ |
| 124 | |
| 125 | require_once CFF_PLUGIN_DIR . 'admin/class-install-skin.php'; |
| 126 | |
| 127 | // Do not allow WordPress to search/download translations, as this will break JS output. |
| 128 | remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); |
| 129 | |
| 130 | // Create the plugin upgrader with our custom skin. |
| 131 | $installer = new CFF\Helpers\PluginSilentUpgrader( new CFF_Install_Skin() ); |
| 132 | |
| 133 | // Error check. |
| 134 | if ( ! method_exists( $installer, 'install' ) || empty( $_POST['plugin'] ) ) { |
| 135 | wp_send_json_error( $error ); |
| 136 | } |
| 137 | |
| 138 | $installer->install( $_POST['plugin'] ); // phpcs:ignore |
| 139 | |
| 140 | // Flush the cache and return the newly installed plugin basename. |
| 141 | wp_cache_flush(); |
| 142 | |
| 143 | $plugin_basename = $installer->plugin_info(); |
| 144 | |
| 145 | if ( $plugin_basename ) { |
| 146 | |
| 147 | $type = 'addon'; |
| 148 | if ( ! empty( $_POST['type'] ) ) { |
| 149 | $type = sanitize_key( $_POST['type'] ); |
| 150 | } |
| 151 | |
| 152 | // Activate the plugin silently. |
| 153 | $activated = activate_plugin( $plugin_basename ); |
| 154 | |
| 155 | if ( ! is_wp_error( $activated ) ) { |
| 156 | wp_send_json_success( |
| 157 | array( |
| 158 | 'msg' => 'plugin' === $type ? esc_html__( 'Plugin installed & activated.', 'custom-facebook-feed' ) : esc_html__( 'Addon installed & activated.', 'custom-facebook-feed' ), |
| 159 | 'is_activated' => true, |
| 160 | 'basename' => $plugin_basename, |
| 161 | ) |
| 162 | ); |
| 163 | } else { |
| 164 | wp_send_json_success( |
| 165 | array( |
| 166 | 'msg' => 'plugin' === $type ? esc_html__( 'Plugin installed.', 'custom-facebook-feed' ) : esc_html__( 'Addon installed.', 'custom-facebook-feed' ), |
| 167 | 'is_activated' => false, |
| 168 | 'basename' => $plugin_basename, |
| 169 | ) |
| 170 | ); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | wp_send_json_error( $error ); |
| 175 | } |
| 176 | add_action( 'wp_ajax_cff_install_addon', 'cff_install_addon' ); |