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