ActivationForm.php
2 years ago
CheckForm.php
2 years ago
Endpoint.php
4 years ago
License.php
2 years ago
RequestResponse.php
4 years ago
Updater.php
4 years ago
ActivationForm.php
282 lines
| 1 | <?php |
| 2 | namespace Kubio\Core\License; |
| 3 | |
| 4 | use IlluminateAgnostic\Arr\Support\Arr; |
| 5 | use Kubio\Core\LodashBasic; |
| 6 | use Kubio\Core\License\License; |
| 7 | use Kubio\Core\License\Updater; |
| 8 | use Kubio\Flags; |
| 9 | use Plugin_Upgrader; |
| 10 | class ActivationForm { |
| 11 | const FEEDBACK_URL = 'https://kubiobuilder.com/upgrade-reason-results/'; |
| 12 | |
| 13 | public function __construct() { |
| 14 | add_action( 'wp_ajax_kubiowp-page-builder-activate', array( $this, 'callActivateLicenseEndpoint' ) ); |
| 15 | add_action( 'wp_ajax_kubiowp-page-builder-upgrade-feedback', array( $this, 'callUpgradeFeedback' ) ); |
| 16 | add_action( 'wp_ajax_kubiowp-page-builder-maybe-install-pro', array( $this, 'maybeInstallPRO' ) ); |
| 17 | } |
| 18 | |
| 19 | public function printForm() { |
| 20 | add_action( 'admin_notices', array( $this, 'makeActivateNotice' ) ); |
| 21 | $this->enqueue(); |
| 22 | } |
| 23 | |
| 24 | public function enqueue() { |
| 25 | wp_enqueue_script( 'wp-util' ); |
| 26 | } |
| 27 | |
| 28 | public function makeUpgradeView( $message = '' ) { |
| 29 | ?> |
| 30 | <div class="kubio-page-builder-upgade-view kubio-admin-panel"> |
| 31 | <div class="kubio-page-builder-license-notice kubio-page-builder-activate-license"> |
| 32 | <h3 class="notice_title"><?php esc_html_e( 'Enter a valid Kubio PRO license key to unlock all the PRO features', 'kubio' ); ?></h3> |
| 33 | <?php echo $this->formHtml( $message ); ?> |
| 34 | </div> |
| 35 | </div> |
| 36 | <?php echo $this->formUpgradeReasonPopup(); ?> |
| 37 | <?php |
| 38 | } |
| 39 | |
| 40 | public function makeActivateNotice( $formId = '', $classHhtml = array(), $message = '' ) { |
| 41 | |
| 42 | $screen = get_current_screen(); |
| 43 | global $post; |
| 44 | $action = ( $screen && $screen->action ) ? $screen->action : Arr::get( $_REQUEST, 'action', '' ); |
| 45 | $is_block_editor = ( $screen && $screen->is_block_editor ) || ( ! empty( $action ) && $post && use_block_editor_for_post( $post ) ); |
| 46 | $is_block_editor = $is_block_editor || did_filter( 'block_editor_settings_all' ); |
| 47 | |
| 48 | if ( $is_block_editor ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | if ( ! array( $classHhtml ) ) { |
| 53 | $classHhtml = array( $classHhtml ); |
| 54 | } |
| 55 | if ( $formId !== '' ) { |
| 56 | $formId = ' id="' . esc_attr( $formId ) . '"'; |
| 57 | } |
| 58 | $classHhtml = implode( ' ', $classHhtml ); |
| 59 | ?> |
| 60 | <div class="notice notice-error is-dismissible kubio-activation-wrapper <?php echo esc_attr( $classHhtml ); ?>"<?php echo $formId; ?>> |
| 61 | <div class="notification-logo-wrapper"> |
| 62 | <div class="notification-logo"> |
| 63 | <?php echo wp_kses_post( KUBIO_LOGO_SVG ); ?> |
| 64 | </div> |
| 65 | </div> |
| 66 | <div class="kubio-page-builder-license-notice kubio-page-builder-activate-license"> |
| 67 | <h1 class="notice_title"><?php esc_html_e( 'Activate Kubio PRO License', 'kubio' ); ?></h1> |
| 68 | <h3 class="notice_sub_title"><?php esc_html_e( 'If this is a testing site you can ignore this message. If this is your live site then please insert the license key below.', 'kubio' ); ?></h3> |
| 69 | <?php echo $this->formHtml( $message ); ?> |
| 70 | </div> |
| 71 | </div> |
| 72 | <?php |
| 73 | } |
| 74 | |
| 75 | public function formHtml( $message = '' ) { |
| 76 | $html = '<div class="kubio-page-builder-activate-license-form_wrapper"> |
| 77 | <form id="kubio-page-builder-activate-license-form" class="activate-form"> |
| 78 | <input placeholder="6F474380-5929B874-D2E0CB90-C7282097" type="text" |
| 79 | value="' . esc_attr( get_option( 'kubio_sync_data_source', '' ) ) . '" |
| 80 | class="regular-text"> |
| 81 | <button type="submit" class="button button-primary">' . esc_html__( 'Activate License', 'kubio' ) . '</button> |
| 82 | </form> |
| 83 | ' . $this->formMessage( $message ) . ' |
| 84 | </div>'; |
| 85 | |
| 86 | return $html; |
| 87 | } |
| 88 | |
| 89 | public function formMessage( $message = '' ) { |
| 90 | $html = ''; |
| 91 | if ( '' === $message ) { |
| 92 | $html .= '<p id="kubio-page-builder-activate-license-message" class="message" style="display: none;"></p>'; |
| 93 | } else { |
| 94 | $html .= '<p id="kubio-page-builder-activate-license-message" class="message">' . $message . '</p>'; |
| 95 | } |
| 96 | |
| 97 | $html .= '<p class="description">'; |
| 98 | // translators: placeholders are some urls like <a href="#">My Account</a> |
| 99 | $html .= sprintf( __( 'Your key was sent via email when the purchase was completed. Also you can find the key in the %1$s of your %2$s account', 'kubio' ), '<a href="' . esc_attr( License::getInstance()->getDashboardUrl() ) . '/#/my-plans" target="_blank">My plans</a>', '<a href="' . esc_attr( License::getInstance()->getDashboardUrl() ) . '" target="_blank">Kubio</a>' ); |
| 100 | $html .= '</p> |
| 101 | <div class="spinner-holder plugin-installer-spinner" style="display: none;"> |
| 102 | <span class="icon"> |
| 103 | <span class="loader">' . kubio_get_iframe_loader( |
| 104 | array( |
| 105 | 'size' => '19px', |
| 106 | 'color' => '#2271B1', |
| 107 | ) |
| 108 | ) . '</span> |
| 109 | <span class="ok"><span class="dashicons dashicons-before dashicons-yes"></span></span> |
| 110 | </span> |
| 111 | <span class="message"></span> |
| 112 | </div>'; |
| 113 | |
| 114 | return $html; |
| 115 | } |
| 116 | |
| 117 | public function callActivateLicenseEndpoint() { |
| 118 | check_ajax_referer('kubio_ajax_nonce'); |
| 119 | $key = isset( $_REQUEST['key'] ) ? sanitize_text_field( $_REQUEST['key'] ) : false; |
| 120 | |
| 121 | if ( ! $key ) { |
| 122 | wp_send_json_error( esc_html__( 'License key is empty', 'kubio' ), 403 ); |
| 123 | } |
| 124 | |
| 125 | License::getInstance()->setLicenseKey( $key ); |
| 126 | $response = Endpoint::activate(); |
| 127 | |
| 128 | if ( $response->isError() ) { |
| 129 | License::getInstance()->setLicenseKey( null ); |
| 130 | } |
| 131 | |
| 132 | wp_send_json( |
| 133 | array( |
| 134 | 'data' => $response->getMessage( true ), |
| 135 | 'success' => $response->isSuccess(), |
| 136 | ), |
| 137 | $response->getResponseCode() |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | public function maybeInstallPRO() { |
| 142 | check_ajax_referer('kubio_ajax_nonce'); |
| 143 | add_filter( |
| 144 | 'kubio/companion/update_remote_data', |
| 145 | function ( $data ) { |
| 146 | $data['args'] = array( |
| 147 | 'product' => 'kubio-pro', |
| 148 | 'key' => License::getInstance()->getLicenseKey(), |
| 149 | ); |
| 150 | |
| 151 | $data['plugin_path'] = 'kubio-pro/plugin.php'; |
| 152 | |
| 153 | return $data; |
| 154 | }, |
| 155 | PHP_INT_MAX |
| 156 | ); |
| 157 | |
| 158 | $status = (array) Updater::getInstance()->isUpdateAvailable(); |
| 159 | $url = LodashBasic::array_get_value( $status, 'package_url', false ); |
| 160 | |
| 161 | if ( $url ) { |
| 162 | |
| 163 | if ( ! function_exists( 'plugins_api' ) ) { |
| 164 | include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); //for plugins_api.. |
| 165 | } |
| 166 | |
| 167 | if ( ! class_exists( 'Plugin_Upgrader' ) ) { |
| 168 | /** Plugin_Upgrader class */ |
| 169 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 170 | } |
| 171 | |
| 172 | $upgrader = new Plugin_Upgrader( new \Automatic_Upgrader_Skin() ); |
| 173 | $result = $upgrader->install( $url ); |
| 174 | |
| 175 | if ( $result !== true && $result !== null ) { |
| 176 | wp_send_json_error(); |
| 177 | } |
| 178 | |
| 179 | $ac = get_option( 'active_plugins' ); |
| 180 | $ac = array_diff( $ac, array( 'kubio/plugin.php' ) ); |
| 181 | $ac[] = 'kubio-pro/plugin.php'; |
| 182 | update_option( 'active_plugins', $ac ); |
| 183 | |
| 184 | // set the kubio pro plugin activation time |
| 185 | if ( ! Flags::get( 'kubio_pro_activation_time', false ) ) { |
| 186 | Flags::set( 'kubio_pro_activation_time', time() ); |
| 187 | } |
| 188 | |
| 189 | wp_send_json_success( array( 'message' => esc_html__( 'No error', 'kubio' ) ) ); |
| 190 | } |
| 191 | |
| 192 | wp_send_json_success( $status ); |
| 193 | } |
| 194 | |
| 195 | public function formUpgradeReasonPopup() { |
| 196 | $upgrade_reasons = array( |
| 197 | 'ai-features' => __( 'AI Features', 'kubio' ), |
| 198 | 'more-sections' => __( 'More sections and blocks', 'kubio' ), |
| 199 | 'edit-footer' => __( 'Footer editing', 'kubio' ), |
| 200 | 'multiple-page-headers' => __( 'Multiple page headers', 'kubio' ), |
| 201 | 'other' => __( 'Other', 'kubio' ), |
| 202 | ); |
| 203 | ob_start(); |
| 204 | ?> |
| 205 | <div class="kubio--modal kubio--modal-hidden"> |
| 206 | <div class="kubio--modal__content"> |
| 207 | <div class="kubio--popup__wrapper"> |
| 208 | <div class="kubio--popup__close"> |
| 209 | <button type="button" class="button"></button> |
| 210 | </div> |
| 211 | <!-- /.kubio--popup__close --> |
| 212 | <div class="kubio--popup__content"> |
| 213 | <div class="kubio--popup__left"> |
| 214 | <img src="<?php echo kubio_url( 'static/admin-pages/upgrade-reason.png' ); ?>" alt=""> |
| 215 | </div> |
| 216 | <!-- /.kubio--popup__left --> |
| 217 | <div class="kubio--popup__right"> |
| 218 | <p><?php _e( 'Thank you for choosing Kubio PRO!', 'kubio' ); ?></p> |
| 219 | <h2> |
| 220 | <?php |
| 221 | _e( |
| 222 | 'Could you tell us what inspired your decision to upgrade?', |
| 223 | 'kubio' |
| 224 | ); |
| 225 | ?> |
| 226 | </h2> |
| 227 | |
| 228 | <div class="kubio--popup__form"> |
| 229 | <?php |
| 230 | foreach ( $upgrade_reasons as $reason => $label ) : |
| 231 | ?> |
| 232 | <label> |
| 233 | <input type="radio" name="upgrade__reason" value="<?php echo $reason; ?>"> |
| 234 | <em></em> |
| 235 | <span><?php echo $label; ?></span> |
| 236 | </label> |
| 237 | <?php endforeach; ?> |
| 238 | </div> |
| 239 | </div> |
| 240 | <!-- /.kubio--popup__right --> |
| 241 | </div> |
| 242 | </div> |
| 243 | <!-- /.kubio--popup__wrapper --> |
| 244 | </div> |
| 245 | <!-- /.kubio--modal__content --> |
| 246 | </div> |
| 247 | <!-- /.kubio--modal --> |
| 248 | |
| 249 | <?php |
| 250 | $str = ob_get_clean(); |
| 251 | return $str; |
| 252 | } |
| 253 | |
| 254 | public function callUpgradeFeedback() { |
| 255 | check_ajax_referer('kubio_ajax_nonce'); |
| 256 | $license = isset( $_REQUEST['license'] ) ? sanitize_text_field( $_REQUEST['license'] ) : ''; |
| 257 | $reason = isset( $_REQUEST['reason'] ) ? sanitize_text_field( $_REQUEST['reason'] ) : ''; |
| 258 | |
| 259 | $response = wp_remote_post( |
| 260 | self::FEEDBACK_URL, |
| 261 | array( |
| 262 | 'sslverify' => false, |
| 263 | 'body' => ( |
| 264 | array( |
| 265 | 'license' => $license, |
| 266 | 'reason' => $reason, |
| 267 | ) |
| 268 | ), |
| 269 | ) |
| 270 | ); |
| 271 | |
| 272 | $body = json_decode( wp_remote_retrieve_body( $response ) ); |
| 273 | |
| 274 | wp_send_json_success( |
| 275 | array( |
| 276 | 'data' => isset( $body->status ) ? $body->status : 'error', |
| 277 | ) |
| 278 | ); |
| 279 | |
| 280 | } |
| 281 | } |
| 282 |