init_updates( UR_PLUGIN_FILE ); } /** * Init the updater. * * @param string $_plugin_file Plugin File. */ public function init_updates( $_plugin_file ) { $this->plugin_file = $_plugin_file; $this->plugin_slug = str_replace( '.php', '', basename( $this->plugin_file ) ); $this->plugin_name = basename( dirname( $this->plugin_file ) ) . '/' . $this->plugin_slug . '.php'; register_activation_hook( $this->plugin_name, array( $this, 'plugin_activation' ), 10 ); register_deactivation_hook( $this->plugin_name, array( $this, 'plugin_deactivation' ), 10 ); add_filter( 'block_local_requests', '__return_false' ); add_action( 'admin_init', array( $this, 'admin_init' ) ); // Include required files. include_once __DIR__ . '/admin/updater/class-ur-plugin-updater-api.php'; } /** * Run on admin init. */ public function admin_init() { $this->load_errors(); add_action( 'shutdown', array( $this, 'store_errors' ) ); $this->api_key = get_option( $this->plugin_slug . '_license_key' ); $this->plugin_data = get_plugin_data( $this->plugin_file ); // Check if pro is activated to display license notices. if ( ( file_exists( WP_PLUGIN_DIR . '/user-registration-pro/user-registration.php' ) && is_plugin_active( 'user-registration-pro/user-registration.php' ) ) && current_user_can( 'update_plugins' ) ) { $this->plugin_requests(); add_action( 'in_admin_header', array( $this, 'user_registration_upgrade_to_pro_notice' ) ); $this->plugin_license_view(); } $message = get_option( 'user_registration_failed_installing_extensions_message', '' ); if ( $message ) { add_action( 'admin_notices', array( $this, 'user_registration_failed_extension_install' ) ); } } /** * Process plugin requests. */ private function plugin_requests() { if ( ! current_user_can( 'manage_options' ) ) { return false; } if ( isset( $_POST['ur_license_nonce'] ) ) { if ( ! wp_verify_nonce( $_POST['ur_license_nonce'], '_ur_license_nonce' ) ) { return; } if ( ! empty( $_POST[ $this->plugin_slug . '_license_key' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification $this->activate_license_request(); } elseif ( ! empty( $_POST['download_user_registration_pro'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification $this->install_extension(); wp_redirect( remove_query_arg( array( 'deactivated_license', $this->plugin_slug . '_deactivate_license' ), add_query_arg( 'activated_license', $this->plugin_slug ) ) ); exit; } } if ( isset( $_GET['_wpnonce'] ) ) { if ( ! wp_verify_nonce( $_GET['_wpnonce'], '_ur_license_nonce' ) ) { return; } if ( ! empty( $_GET[ $this->plugin_slug . '_deactivate_license' ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $this->deactivate_license_request(); } elseif ( ! empty( $_GET[ 'dismiss-' . sanitize_title( $this->plugin_slug ) ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended update_option( $this->plugin_slug . '_hide_key_notice', 1 ); } elseif ( ! empty( $_GET['activated_license'] ) && $_GET['activated_license'] === $this->plugin_slug ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $this->add_notice( array( $this, 'activated_key_notice' ) ); } elseif ( ! empty( $_GET['deactivated_license'] ) && $_GET['deactivated_license'] === $this->plugin_slug ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended $this->add_notice( array( $this, 'deactivated_key_notice' ) ); } } } /** * Activate a license request. */ private function activate_license_request() { $license_key = sanitize_text_field( $_POST[ $this->plugin_slug . '_license_key' ] ); // phpcs:ignore if ( $this->activate_license( $license_key ) ) { $this->install_extension(); wp_redirect( remove_query_arg( array( 'deactivated_license', $this->plugin_slug . '_deactivate_license' ), add_query_arg( 'activated_license', $this->plugin_slug ) ) ); exit; } else { wp_redirect( remove_query_arg( array( 'activated_license', 'deactivated_license', $this->plugin_slug . '_deactivate_license' ) ) ); exit; } } /** * Install Extensions Action. */ public function install_extension() { $status = ur_install_extensions( 'User Registration PRO', 'user-registration-pro' ); if ( $status['success'] ) { add_action( 'admin_notices', array( $this, 'user_registration_extension_download_success_notice' ) ); } else { add_option( 'user_registration_failed_installing_extensions_message', $status['message'] ); } } /** * Deactivate a license request */ private function deactivate_license_request() { $this->deactivate_license(); wp_redirect( remove_query_arg( array( 'activated_license', $this->plugin_slug . '_deactivate_license' ), add_query_arg( 'deactivated_license', $this->plugin_slug ) ) ); exit; } /** * Display plugin license view. */ private function plugin_license_view() { add_filter( 'plugin_action_links_' . $this->plugin_name, array( $this, 'plugin_action_links' ) ); add_action( 'admin_notices', array( $this, 'user_registration_error_notices' ) ); } /** * Add notices * * @param string $callback Callback function. */ private function add_notice( $callback ) { add_action( 'admin_notices', $callback ); add_action( 'network_admin_notices', $callback ); } /** * Add an error message * * @param string $message Your error message. * @param string $type Type of error message. */ public function add_error( $message, $type = '' ) { if ( $type ) { $this->errors[ $type ] = $message; } else { $this->errors[] = $message; } } /** * Load errors from option */ public function load_errors() { $this->errors = get_option( $this->plugin_slug . '_errors', array() ); } /** * Store errors in option */ public function store_errors() { if ( count( $this->errors ) > 0 ) { update_option( $this->plugin_slug . '_errors', $this->errors ); } else { delete_option( $this->plugin_slug . '_errors' ); } } /** * Deprecation function for error notices. * * @deprecated 2.0.6 */ public function error_notices() { ur_deprecated_function( 'UR_Admin_Profile::error_notices', '1.4.1', 'UR_Plugin_Updater::user_registration_error_notices' ); } /** * Output errors */ public function user_registration_error_notices() { if ( ! empty( $this->errors ) ) { foreach ( $this->errors as $key => $error ) { include __DIR__ . '/admin/notifications/views/html-notice-error.php'; if ( 'invalid_key' !== $key && did_action( 'all_admin_notices' ) ) { unset( $this->errors[ $key ] ); } } } } /** * Ran on plugin-activation. */ public function plugin_activation() { delete_option( $this->plugin_slug . '_hide_key_notice' ); } /** * Ran on plugin-deactivation. */ public function plugin_deactivation() { delete_option( 'user_registration_activated' ); $this->deactivate_license(); } /** * Show the input form for the license key. * * @param string $plugin_file Plugin File. */ public function plugin_license_form( $plugin_file ) { $slug = $this->plugin_slug; if ( is_plugin_active( 'user-registration-pro/user-registration.php' ) ) { $slug .= '-pro'; } if ( strtolower( basename( dirname( $plugin_file ) ) ) === strtolower( $slug ) ) { include_once __DIR__ . '/admin/views/html-license-form.php'; } } /** * Display action links in the Plugins list table. * * @param array $actions Actions. * @return array */ public function plugin_action_links( $actions ) { $new_actions = array(); if ( ! $this->api_key ) { $new_actions['activate_license_settings'] = '' . __( 'Activate License', 'user-registration' ) . ''; } else { $new_actions['deactivate_license'] = '' . __( 'Deactivate License', 'user-registration' ) . ''; } return array_merge( $actions, $new_actions ); } /** * Try to activate a license. * * @param string $license_key License Key. * * @throws Exception Throws license activation failed error message. */ public function activate_license( $license_key ) { try { if ( empty( $license_key ) ) { throw new Exception( 'Please enter your license key' ); } $activate_results = json_decode( UR_Updater_Key_API::activate( array( 'license' => $license_key, ) ) ); // Update activate results. update_option( $this->plugin_slug . '_license_active', $activate_results ); if ( ! empty( $activate_results ) && is_object( $activate_results ) ) { if ( isset( $activate_results->error_code ) ) { throw new Exception( $activate_results->error ); } elseif ( false === $activate_results->success ) { switch ( $activate_results->error ) { case 'expired': $error_msg = wp_kses_post( sprintf( __( 'The provided license key expired on %1$s. Please renew your license key.', 'user-registration' ), esc_html( date_i18n( get_option( 'date_format' ) ), esc_html( strtotime( $activate_results->expires, current_time( 'timestamp' ) ) ) ), esc_url( 'https://wpeverest.com/checkout/?edd_license_key=' . $license_key . '&utm_campaign=admin&utm_source=licenses&utm_medium=expired' ) ) ); // phpcs:ignore. break; case 'revoked': /* translators: %s: Contact Support URL */ $error_msg = wp_kses_post( sprintf( __( 'The provided license key has been disabled. Please contact support for more information.', 'user-registration' ), 'https://wpeverest.com/contact?utm_campaign=admin&utm_source=licenses&utm_medium=revoked' ) ); break; case 'missing': /* translators: %s: Account Page URL */ $error_msg = wp_kses_post( sprintf( __( 'The provided license is invalid. Please visit your account page and verify it.', 'user-registration' ), 'https://wpeverest.com/my-account?utm_campaign=admin&utm_source=licenses&utm_medium=missing' ) ); break; case 'invalid': case 'site_inactive': /* translators: %s: Account Page URL */ $error_msg = wp_kses_post( sprintf( __( 'The provided license is not active for this URL. Please visit your account page to manage your license key URLs.', 'user-registration' ), 'https://wpeverest.com/my-account?utm_campaign=admin&utm_source=licenses&utm_medium=missing' ) ); break; case 'invalid_item_id': case 'item_name_mismatch': /* translators: %s: Plugin Name */ $error_msg = wp_kses_post( sprintf( __( 'This appears to be an invalid license key for %s.', 'user-registration' ), esc_html( $this->plugin_data['Name'] ) ) ); break; case 'no_activations_left': /* translators: %s: Pricing Page URL */ $error_msg = wp_kses_post( sprintf( __( 'The provided license key has reached its activation limit. Please View possible upgrades now.', 'user-registration' ), 'https://wpeverest.com/my-account/' ) ); break; case 'license_not_activable': $error_msg = __( 'The key you entered belongs to a bundle, please use the product specific license key.', 'user-registration' ); break; default: /* translators: %s: Contact Support URL */ $error_msg = wp_kses_post( sprintf( __( 'The provided license key could not be found. Please contact support for more information.', 'user-registration' ), 'https://wpeverest.com/contact/' ) ); break; } /* translators: %s: Error Message */ throw new Exception( wp_kses_post( sprintf( __( 'Activation error: %s', 'user-registration' ), wp_kses_post( $error_msg ) ) ) ); } elseif ( 'valid' === $activate_results->license ) { $this->api_key = $license_key; $this->errors = array(); update_option( $this->plugin_slug . '_license_key', $this->api_key ); delete_option( $this->plugin_slug . '_errors' ); $license_data = json_decode( UR_Updater_Key_API::check( array( 'license' => $this->api_key, ) ) ); if ( ! empty( $license_data->item_name ) ) { $license_data->item_plan = trim( strtolower( str_replace( 'LifeTime', '', str_replace( 'User Registration', '', $license_data->item_name ) ) ) ); set_transient( 'ur_pro_license_plan', $license_data, WEEK_IN_SECONDS ); } return true; } throw new Exception( 'License could not activate. Please contact support.' ); } else { throw new Exception( 'Connection failed to the License Key API server - possible server issue.' ); } } catch ( Exception $e ) { $this->add_error( $e->getMessage() ); return false; } } /** * Deactivate a license. */ public function deactivate_license() { if ( get_transient( 'user_registration_pro_activated' ) ) { return; } $reset = UR_Updater_Key_API::deactivate( array( 'license' => $this->api_key, ) ); delete_option( $this->plugin_slug . '_errors' ); delete_option( $this->plugin_slug . '_license_key' ); delete_option( $this->plugin_slug . '_license_active' ); delete_transient( 'ur_pro_license_plan' ); // Reset huh? $this->errors = array(); $this->api_key = ''; } /** * Show a notice prompting the user to update. */ public function key_notice() { if ( count( $this->errors ) === 0 && ! get_option( $this->plugin_slug . '_hide_key_notice' ) ) { include __DIR__ . '/admin/notifications/views/html-notice-key-unvalidated.php'; } } /** * Activation success notice. */ public function activated_key_notice() { include __DIR__ . '/admin/notifications/views/html-notice-key-activated.php'; } /** * Dectivation success notice. */ public function deactivated_key_notice() { include __DIR__ . '/admin/notifications/views/html-notice-key-deactivated.php'; } /** * Display error message when extension installation fails. * * @since 2.0.6 */ public function user_registration_failed_extension_install() { $ur_pro_plugins_path = WP_PLUGIN_DIR . '/user-registration-pro/user-registration.php'; $message = get_option( 'user_registration_failed_installing_extensions_message', '' ); if ( ! file_exists( $ur_pro_plugins_path ) ) { $message = $message . esc_html__( ' Please manually download User Registration PRO.', 'user-registration' ); echo '
' . wp_kses_post( $message ) . '
' . wp_kses_post( $message ) . '
%1$s, %2$s
', __( 'If you already have an active license key.', 'user-registration' ), __( 'please activate the key.', 'user-registration' ) ); $content .= sprintf( '%1$s, %2$s
', __( 'If you do not have active premium license of User Registration', 'user-registration' ), __( 'please purchase premium license. Going forward active premium license will be vital for smooth running of premium addons of User Registration that you are currently using.', 'user-registration' ) ); $link .= 'Please update all the listed addons to the latest version.
->plugin_url() . '/assets/images/UR-Logo.png' ); ?>)