/** * Constructor */ public function __construct() { // Set the page $this->set_page('license'); // Set the title $this->set_title(__('License', 'related-posts-for-wp')); $is_activated = RP4WP_Updater_Key_API::is_activated(); // The fields $this->sections = array(self::PREFIX . 'misc' => array('id' => 'misc', 'fields' => array(array('id' => 'license_status', 'label' => __('License Status', 'related-posts-for-wp'), 'type' => 'license_status', 'default' => $is_activated), array('id' => 'licence_key', 'label' => __('License Key', 'related-posts-for-wp'), 'description' => sprintf(__('Your license key. You can find your license key in your %sMy Account%s page.', 'related-posts-for-wp'), '<a href="https://www.relatedpostsforwp.com/my-account/" target="_blank">', '</a>'), 'type' => 'text', 'default' => '', 'disabled' => $is_activated), array('id' => 'email', 'label' => __('Activation Email', 'related-posts-for-wp'), 'description' => sprintf(__('Your activation email address. You can find your activation email address in your %sMy Account%s page.', 'related-posts-for-wp'), '<a href="https://www.relatedpostsforwp.com/my-account/" target="_blank">', '</a>'), 'type' => 'text', 'default' => get_option('admin_email'), 'disabled' => $is_activated)))); if (false == $is_activated) { $this->set_button_title('Save and Activate License'); } else { $this->set_button_title('Deactivate License'); } // Parent constructor parent::__construct(); }
/** * Take over the Plugin info screen */ public function plugins_api($false, $action, $args) { global $wp_version; // Only take over plugin info screen if license is activated if (false == RP4WP_Updater_Key_API::is_activated()) { return $false; } if (!isset($args->slug) || $args->slug !== $this->plugin_slug) { return $false; } // Get the current version $plugin_info = get_site_transient('update_plugins'); $current_ver = isset($plugin_info->checked[$this->plugin_name]) ? $plugin_info->checked[$this->plugin_name] : ''; // only continue when the plugin version is set if ('' == $current_ver) { return $false; } $args = array('request' => 'plugininformation', 'plugin_name' => $this->plugin_name, 'version' => $current_ver, 'api_product_id' => $this->plugin_slug, 'license_key' => $this->api_key, 'email' => $this->activation_email, 'instance' => site_url()); // Check for a plugin update $response = $this->do_license_request($args); if (isset($response->errors)) { $this->handle_errors($response->errors); } // If everything is okay return the $response if (isset($response) && is_object($response) && $response !== false) { return $response; } }
/** * Sanitize the option value * * @param array $post_data * * @since 1.1.0 * @access public * * @return array */ public function sanitize_option($post_data) { /** * @todo When options are moved to a more OOP setup, each input class will have their own sanitization method. */ if (false !== strstr($this->page, 'rp4wp_general_')) { // Unset automatic_linking if not set in post if (!isset($post_data['automatic_linking'])) { $post_data['automatic_linking'] = 0; } // automatic_linking must be an integer if (isset($post_data['automatic_linking'])) { $post_data['automatic_linking'] = intval($post_data['automatic_linking']); } // automatic_linking_post_amount must be an integer if (isset($post_data['automatic_linking_post_amount'])) { $post_data['automatic_linking_post_amount'] = intval($post_data['automatic_linking_post_amount']); } // Excerpt length must be an integer if (isset($post_data['excerpt_length'])) { $post_data['excerpt_length'] = intval($post_data['excerpt_length']); } } else { if (false !== strstr($this->page, 'rp4wp_license')) { // License sanitize callback try { $plugin_slug = str_replace('.php', '', basename(RP4WP_PLUGIN_FILE)); // Try to activate the license if (false == RP4WP_Updater_Key_API::is_activated()) { $license_key = sanitize_text_field($post_data['licence_key']); $email = sanitize_text_field($post_data['email']); if (empty($license_key)) { throw new Exception('Please enter your license key.'); } if (empty($email)) { throw new Exception('Please enter the email address associated with your license.'); } $activate_results = json_decode(RP4WP_Updater_Key_API::activate(array('email' => $email, 'license_key' => $license_key, 'api_product_id' => $plugin_slug)), true); if (!empty($activate_results['activated'])) { // Set post data from API respond $post_data['licence_key'] = $license_key; $post_data['email'] = $email; // Add successful activation messages $message_handler = new RP4WP_Manager_Settings_Messages(); $message_handler->add_message('License successfully activated', 'updated'); // Set local activation status to true RP4WP_Updater_Key_API::set_activated(true); } elseif ($activate_results === false) { throw new Exception('Connection failed to the License Key API server. Try again later.'); } elseif (isset($activate_results['error_code'])) { throw new Exception($activate_results['error']); } } else { // Get license options $license_options = get_option('rp4wp_license', array()); // Throw error if there's no license key in the database if (isset($license_options['licence_key']) && '' != $license_options['licence_key']) { // Try to deactivate the license RP4WP_Updater_Key_API::deactivate(array('api_product_id' => $plugin_slug, 'license_key' => $license_options['licence_key'])); // Set local activation status to false RP4WP_Updater_Key_API::set_activated(false); // Set the correct license key as post data $post_data['licence_key'] = $license_options['licence_key']; } } } catch (Exception $e) { // Add exception messages as error message to message handler $message_handler = new RP4WP_Manager_Settings_Messages(); $message_handler->add_message($e->getMessage(), 'error'); // Set local activation status to false RP4WP_Updater_Key_API::set_activated(false); } } else { if (false !== strstr($this->page, 'rp4wp_configurator')) { // delete components transient delete_transient(RP4WP_Constants::TRANSIENT_COMPONENTS); // delete component css transient delete_transient(RP4WP_Constants::TRANSIENT_COMPONENT_CSS); // Set correct component order in configuration $config = json_decode($post_data['configuration']); if (null !== $config && count($config) > 0) { // sort components usort($config, array($this, 'sort_components')); $post_data['configuration'] = json_encode($config); } // Remove tags from custom CSS if ('' != $post_data['css']) { $post_data['css'] = strip_tags($post_data['css']); } } else { if (false !== strstr($this->page, 'rp4wp_words')) { // delete joined words transient delete_transient(RP4WP_Constants::TRANSIENT_JOINED_WORDS); // delete extra ignored words transient delete_transient(RP4WP_Constants::TRANSIENT_EXTRA_IGNORED_WORDS); } } } } return $post_data; }