admin-scripts.php
2 years ago
class-wpcode-admin-page-loader-lite.php
9 months ago
class-wpcode-connect.php
2 years ago
class-wpcode-metabox-snippets-lite.php
1 year ago
class-wpcode-usage-tracking-lite.php
3 years ago
notices.php
1 year ago
class-wpcode-connect.php
268 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * WPCode Connect. |
| 9 | * |
| 10 | * WPCode Connect is our service that makes it easy to upgrade to WPCode Pro |
| 11 | * without having to manually install the WPCode Pro plugin. |
| 12 | * |
| 13 | * @since 2.0.9 |
| 14 | */ |
| 15 | class WPCode_Connect { |
| 16 | |
| 17 | /** |
| 18 | * Constructor. |
| 19 | * |
| 20 | * @since 2.0.9 |
| 21 | */ |
| 22 | public function __construct() { |
| 23 | $this->hooks(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Hooks. |
| 28 | * |
| 29 | * @since 2.0.9 |
| 30 | */ |
| 31 | public function hooks() { |
| 32 | add_action( 'wpcode_admin_page_content_wpcode-settings', array( $this, 'settings_enqueues' ) ); |
| 33 | add_action( 'wp_ajax_wpcode_connect_url', array( $this, 'generate_url' ) ); |
| 34 | add_action( 'wp_ajax_nopriv_wpcode_connect_process', array( $this, 'process' ) ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Settings page enqueues. |
| 39 | * |
| 40 | * @since 2.0.9 |
| 41 | */ |
| 42 | public function settings_enqueues() { |
| 43 | |
| 44 | $admin_asset_file = WPCODE_PLUGIN_PATH . 'build/connect.asset.php'; |
| 45 | |
| 46 | if ( ! file_exists( $admin_asset_file ) ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | $asset = require $admin_asset_file; |
| 51 | |
| 52 | wp_enqueue_script( 'wpcode-connect-js', WPCODE_PLUGIN_URL . 'build/connect.js', $asset['dependencies'], $asset['version'], true ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Generate and return the WPCode Connect URL. |
| 57 | * |
| 58 | * @since 2.0.9 |
| 59 | */ |
| 60 | public function generate_url() { |
| 61 | |
| 62 | // Run a security check. |
| 63 | check_ajax_referer( 'wpcode_admin' ); |
| 64 | |
| 65 | // Check for permissions. |
| 66 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 67 | wp_send_json_error( array( 'message' => esc_html__( 'You are not allowed to install plugins.', 'insert-headers-and-footers' ) ) ); |
| 68 | } |
| 69 | |
| 70 | $key = ! empty( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : ''; |
| 71 | |
| 72 | if ( empty( $key ) ) { |
| 73 | wp_send_json_error( array( 'message' => esc_html__( 'Please enter your license key to connect.', 'insert-headers-and-footers' ) ) ); |
| 74 | } |
| 75 | |
| 76 | if ( class_exists( 'WPCode_Premium' ) ) { |
| 77 | wp_send_json_error( array( 'message' => esc_html__( 'Only the Lite version can be upgraded.', 'insert-headers-and-footers' ) ) ); |
| 78 | } |
| 79 | |
| 80 | // Verify pro version is not installed. |
| 81 | $active = activate_plugin( 'wpcode-premium/wpcode.php', false, false, true ); |
| 82 | |
| 83 | if ( ! is_wp_error( $active ) ) { |
| 84 | |
| 85 | update_option( 'wpcode_install', 1 ); // Run install routines. |
| 86 | // Deactivate Lite. |
| 87 | $plugin = plugin_basename( WPCODE_FILE ); |
| 88 | |
| 89 | deactivate_plugins( $plugin ); |
| 90 | |
| 91 | do_action( 'wpcode_plugin_deactivated', $plugin ); |
| 92 | |
| 93 | wp_send_json_success( |
| 94 | array( |
| 95 | 'message' => esc_html__( 'WPCode Pro is installed but not activated.', 'insert-headers-and-footers' ), |
| 96 | 'reload' => true, |
| 97 | ) |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | // Generate URL. |
| 102 | $oth = hash( 'sha512', wp_rand() ); |
| 103 | $hashed_oth = hash_hmac( 'sha512', $oth, wp_salt() ); |
| 104 | |
| 105 | update_option( 'wpcode_connect_token', $oth ); |
| 106 | update_option( 'wpcode_connect', $key ); |
| 107 | |
| 108 | $version = WPCODE_VERSION; |
| 109 | $endpoint = admin_url( 'admin-ajax.php' ); |
| 110 | $redirect = admin_url( 'admin.php?page=wpcode-settings' ); |
| 111 | $url = add_query_arg( |
| 112 | array( |
| 113 | 'key' => $key, |
| 114 | 'oth' => $hashed_oth, |
| 115 | 'endpoint' => $endpoint, |
| 116 | 'version' => $version, |
| 117 | 'siteurl' => admin_url(), |
| 118 | 'homeurl' => home_url(), |
| 119 | 'redirect' => rawurldecode( base64_encode( $redirect ) ), // phpcs:ignore |
| 120 | 'v' => 2, |
| 121 | 'php' => phpversion(), |
| 122 | 'wp' => get_bloginfo( 'version' ), |
| 123 | ), |
| 124 | 'https://upgrade.wpcode.com/' |
| 125 | ); |
| 126 | |
| 127 | wp_send_json_success( |
| 128 | array( |
| 129 | 'url' => $url, |
| 130 | 'back_url' => add_query_arg( |
| 131 | array( |
| 132 | 'action' => 'wpcode_connect', |
| 133 | 'oth' => $oth, |
| 134 | ), |
| 135 | $endpoint |
| 136 | ), |
| 137 | ) |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Process WPCode Connect. |
| 143 | * |
| 144 | * @since 2.0.9 |
| 145 | */ |
| 146 | public function process() { |
| 147 | |
| 148 | $error = esc_html__( 'There was an error while installing an upgrade. Please download the plugin from wpcode.com and install it manually.', 'insert-headers-and-footers' ); |
| 149 | |
| 150 | // Verify params present (oth & download link). |
| 151 | $post_oth = ! empty( $_REQUEST['oth'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['oth'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
| 152 | $post_url = ! empty( $_REQUEST['file'] ) ? esc_url_raw( wp_unslash( $_REQUEST['file'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification |
| 153 | |
| 154 | if ( empty( $post_oth ) || empty( $post_url ) ) { |
| 155 | wp_send_json_error( $error ); |
| 156 | } |
| 157 | |
| 158 | // Verify oth. |
| 159 | $oth = get_option( 'wpcode_connect_token' ); |
| 160 | |
| 161 | if ( hash_hmac( 'sha512', $oth, wp_salt() ) !== $post_oth ) { |
| 162 | wp_send_json_error( $error ); |
| 163 | } |
| 164 | |
| 165 | // Delete so cannot replay. |
| 166 | delete_option( 'wpcode_connect_token' ); |
| 167 | |
| 168 | // Set the current screen to avoid undefined notices. |
| 169 | set_current_screen( 'wpcode_page_wpcode-settings' ); |
| 170 | |
| 171 | // Prepare variables. |
| 172 | $url = esc_url_raw( |
| 173 | add_query_arg( |
| 174 | array( 'page' => 'wpcode-settings' ), |
| 175 | admin_url( 'admin.php' ) |
| 176 | ) |
| 177 | ); |
| 178 | |
| 179 | // Verify pro not activated. |
| 180 | if ( class_exists( 'WPCode_Premium' ) ) { |
| 181 | wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'insert-headers-and-footers' ) ); |
| 182 | } |
| 183 | |
| 184 | // Verify pro not installed. |
| 185 | $active = activate_plugin( 'wpcode-premium/wpcode.php', $url, false, true ); |
| 186 | |
| 187 | if ( ! is_wp_error( $active ) ) { |
| 188 | $plugin = plugin_basename( WPCODE_FILE ); |
| 189 | |
| 190 | deactivate_plugins( $plugin ); |
| 191 | |
| 192 | do_action( 'wpcode_plugin_deactivated', $plugin ); |
| 193 | |
| 194 | wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'insert-headers-and-footers' ) ); |
| 195 | } |
| 196 | |
| 197 | $creds = request_filesystem_credentials( $url, '', false, false, null ); |
| 198 | |
| 199 | // Check for file system permissions. |
| 200 | if ( false === $creds || ! WP_Filesystem( $creds ) ) { |
| 201 | wp_send_json_error( |
| 202 | esc_html__( 'There was an error while installing an upgrade. Please check file system permissions and try again. Also, you can download the plugin from wpcode.com and install it manually.', 'insert-headers-and-footers' ) |
| 203 | ); |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * We do not need any extra credentials if we have gotten this far, so let's install the plugin. |
| 208 | */ |
| 209 | // Do not allow WordPress to search/download translations, as this will break JS output. |
| 210 | remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); |
| 211 | |
| 212 | wpcode_require_upgrader(); |
| 213 | |
| 214 | // Create the plugin upgrader with our custom skin. |
| 215 | $installer = new Plugin_Upgrader( new WPCode_Skin() ); |
| 216 | |
| 217 | // Error check. |
| 218 | if ( ! method_exists( $installer, 'install' ) ) { |
| 219 | wp_send_json_error( $error ); |
| 220 | } |
| 221 | |
| 222 | // Check license key. |
| 223 | $key = get_option( 'wpcode_connect', false ); |
| 224 | |
| 225 | if ( empty( $key ) ) { |
| 226 | wp_send_json_error( |
| 227 | new WP_Error( |
| 228 | '403', |
| 229 | esc_html__( 'No key provided.', 'insert-headers-and-footers' ) |
| 230 | ) |
| 231 | ); |
| 232 | } |
| 233 | |
| 234 | $installer->install( $post_url ); // phpcs:ignore |
| 235 | |
| 236 | // Flush the cache and return the newly installed plugin basename. |
| 237 | wp_cache_flush(); |
| 238 | |
| 239 | $plugin_basename = $installer->plugin_info(); |
| 240 | |
| 241 | if ( $plugin_basename ) { |
| 242 | |
| 243 | // Deactivate the lite version first. |
| 244 | $plugin = plugin_basename( WPCODE_FILE ); |
| 245 | |
| 246 | deactivate_plugins( $plugin ); |
| 247 | |
| 248 | do_action( 'wpcode_plugin_deactivated', $plugin ); |
| 249 | |
| 250 | // Activate the plugin silently. |
| 251 | $activated = activate_plugin( $plugin_basename, '', false, true ); |
| 252 | |
| 253 | if ( ! is_wp_error( $activated ) ) { |
| 254 | add_option( 'wpcode_install', 1 ); |
| 255 | wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'insert-headers-and-footers' ) ); |
| 256 | } else { |
| 257 | // Reactivate the lite plugin if pro activation failed. |
| 258 | activate_plugin( plugin_basename( WPCODE_FILE ), '', false, true ); |
| 259 | wp_send_json_error( esc_html__( 'Pro version installed but needs to be activated on the Plugins page inside your WordPress admin.', 'insert-headers-and-footers' ) ); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | wp_send_json_error( $error ); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | new WPCode_Connect(); |
| 268 |