/**
  * 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();
 }
function rp4wp_premium_deactivate_plugin()
{
    include_once plugin_dir_path(RP4WP_PLUGIN_FILE) . 'classes/class-updater-key-api.php';
    // Plugin slug
    $plugin_slug = str_replace('.php', '', basename(RP4WP_PLUGIN_FILE_INSTALLER));
    // Get license options
    $license_options = get_option('rp4wp_license', array());
    // Only continue if there's a license key
    if (!isset($license_options['licence_key'])) {
        return;
    }
    // Deactivate license
    RP4WP_Updater_Key_API::deactivate(array('api_product_id' => $plugin_slug, 'licence_key' => $license_options['licence_key']));
    // Always delete license related options
    delete_option('rp4wp_license');
    delete_site_transient('update_plugins');
}
 /**
  * An update is required, do it
  *
  * @param $current_version
  */
 private function do_upgrade($current_version)
 {
     global $wpdb;
     // Upgrade to version 1.2.2
     if (version_compare($current_version, '1.2.2', '<')) {
         /**
          * Add a post type to currently existing links
          */
         // Get id's that need an upgrade
         $upgrade_ids = get_posts(array('post_type' => RP4WP_Constants::LINK_PT, 'fields' => 'ids', 'posts_per_page' => -1, 'meta_query' => array(array('key' => RP4WP_Constants::PM_PT_PARENT, 'value' => '1', 'compare' => 'NOT EXISTS'))));
         // Preparing the sql lines
         if (count($upgrade_ids) > 0) {
             $sql_lines = array();
             // Loop
             foreach ($upgrade_ids as $upgrade_id) {
                 $sql_lines[] = "(" . $upgrade_id . ", '" . RP4WP_Constants::PM_PT_PARENT . "', 'post')";
             }
             // Insert the rows
             $wpdb->query("INSERT INTO `{$wpdb->postmeta}` (`post_id`,`meta_key`,`meta_value`) VALUES" . implode(',', $sql_lines) . " ;");
         }
     }
     // Upgrade to 1.3.0
     if (version_compare($current_version, '1.3.0', '<')) {
         /**
          * Upgrade the 'rp4wp_installed_post_types' option
          */
         // Get old post types
         $old_installed_post_types = get_option(RP4WP_Constants::OPTION_INSTALLED_PT, array());
         // Check
         if (count($old_installed_post_types) > 0) {
             // New installed post types
             $installed_post_types = array();
             // Loop
             foreach ($old_installed_post_types as $old_installed_post_type => $old_installed_post_type_children) {
                 if (is_array($old_installed_post_type_children)) {
                     $installed_post_types[$old_installed_post_type] = $old_installed_post_type_children;
                 } else {
                     $installed_post_types[$old_installed_post_type_children] = array($old_installed_post_type_children);
                 }
             }
             // Set new option
             update_option(RP4WP_Constants::OPTION_INSTALLED_PT, $installed_post_types);
         }
         /**
          * Upgrade license key and email
          */
         // Fetch old license key and email
         $plugin_slug = str_replace('.php', '', basename(RP4WP_PLUGIN_FILE));
         $api_key = get_option($plugin_slug . '_licence_key', '');
         $activation_email = get_option($plugin_slug . '_email', '');
         // Check if values exist
         if ('' != $api_key && '' != $activation_email) {
             // Update option
             update_option('rp4wp_license', array('licence_key' => $api_key, 'email' => $activation_email));
             // Set local activation status to true
             RP4WP_Updater_Key_API::set_activated(true);
         }
     }
     // upgrade to 1.4.0, the configurator
     if (version_compare($current_version, '1.4.0', '<')) {
         // get installed post types
         $pt_manager = new RP4WP_Post_Type_Manager();
         $installed_post_types = $pt_manager->get_installed_post_types();
         // check
         if (count($installed_post_types) > 0) {
             // get first pt
             foreach ($installed_post_types as $settings_pt => $dump) {
                 break;
             }
             unset($dump);
             // get current theme option
             $theme_options = get_option('rp4wp_themes', array());
             // get settings
             $pt_settings = RP4WP::get()->settings['general_' . $settings_pt];
             // configurator options
             $configurator_options = array('posts_per_row' => isset($theme_options['theme']) && intval($theme_options['theme']) > 0 ? intval($theme_options['theme']) : 2, 'fixed_height' => 325, 'css' => isset($theme_options['css']) ? $theme_options['css'] : '');
             // get settings from first pt
             $display_image = intval($pt_settings->get_option('display_image'));
             $excerpt_length = intval($pt_settings->get_option('excerpt_length'));
             // set the right configuration
             if (1 == $display_image && $excerpt_length > 0) {
                 // image + title + excerpt
                 $configurator_options['configuration'] = '[{"type":"image","x":0,"y":0,"width":1,"height":5},{"type":"title","x":1,"y":0,"width":1,"height":2},{"type":"excerpt","x":1,"y":2,"width":1,"height":3}]';
             } else {
                 if (1 == $display_image) {
                     // title + image
                     $configurator_options['configuration'] = '[{"type":"title","x":0,"y":0,"width":2,"height":1},{"type":"image","x":0,"y":1,"width":2,"height":4}]';
                 } else {
                     if ($excerpt_length > 0) {
                         // title + excerpt
                         $configurator_options['configuration'] = '[{"type":"title","x":0,"y":0,"width":2,"height":1},{"type":"excerpt","x":0,"y":1,"width":2,"height":4}]';
                     } else {
                         // title only
                         $configurator_options['configuration'] = '[{"type":"title","x":0,"y":0,"width":2,"height":5}]';
                     }
                 }
             }
             // set configuration option
             update_option('rp4wp_configurator', $configurator_options);
             // delete old theme options
             delete_option('rp4wp_themes');
         }
     }
     // Upgrade to 1.5.0
     if (version_compare($current_version, '1.5.0', '<')) {
         /**
          * Add INDEX to word column in cache table
          */
         // check for index
         $wpdb->query("SHOW INDEX FROM  `" . $wpdb->prefix . "rp4wp_cache` WHERE KEY_NAME = 'word'");
         if (0 === intval($wpdb->num_rows)) {
             // add index
             $wpdb->query("ALTER TABLE `" . $wpdb->prefix . "rp4wp_cache` ADD INDEX(`word`);");
         }
         /**
          * Move excluded post meta ID's to option to improve related post query
          */
         // get all post ids that are excluded
         $excluded_ids = get_posts(array('fields' => 'ids', 'posts_per_page' => -1, 'meta_query' => array(array('key' => 'rp4wp_exclude', 'value' => '1'))));
         // check if there are any excluded posts
         if (count($excluded_ids) > 0) {
             // put excluded ids in comma separated string
             $excluded_ids = implode(',', $excluded_ids);
             // put in option
             update_option(RP4WP_Constants::OPTION_EXCLUDED, $excluded_ids);
         }
         /**
          * Delete CSS transient so the new frontend styling will apply after update
          */
         delete_transient(RP4WP_Constants::TRANSIENT_COMPONENT_CSS);
     }
     // Upgrade to 1.5.2
     if (version_compare($current_version, '1.5.2', '<')) {
         // fix the excludes
         $excludes = explode(',', get_option(RP4WP_Constants::OPTION_EXCLUDED, ''));
         if (count($excludes) > 0) {
             // trim values
             $excludes = array_map('trim', $excludes);
             // remove empty values
             $excludes = array_filter($excludes);
             // update option
             update_option(RP4WP_Constants::OPTION_EXCLUDED, implode(',', $excludes));
         }
     }
 }
 /**
  * Handle errors from the API
  *
  * @param  array $errors
  */
 public function handle_errors($errors)
 {
     // loop through errors
     foreach ($errors as $error_key => $error) {
         // add error to WP
         $this->add_error($error, $error_key);
         if ('no_activation' == $error_key) {
             // Deactivate license on server
             RP4WP_Updater_Key_API::deactivate(array('api_product_id' => $this->plugin_slug, 'license_key' => $this->api_key));
             // Set local activation status to false
             RP4WP_Updater_Key_API::set_activated(false);
         }
     }
 }
 /**
  * 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;
 }