function rp4wp_premium_activate_plugin()
{
    global $wpdb;
    if (!defined('RP4WP_PLUGIN_FILE_INSTALLER')) {
        return;
    }
    // Remove hide license key notice
    $plugin_slug = str_replace('.php', '', basename(RP4WP_PLUGIN_FILE_INSTALLER));
    delete_option($plugin_slug . '_hide_key_notice');
    // Create database table
    rp4wp_premium_create_db_table();
    // Add option that will redirect user to installation wizard
    add_option('rp4wp_do_install', true);
    // Check if we need to migrate data from free plugin
    $post_options = get_option('rp4wp', array());
    if (count($post_options) > 0) {
        // Move options
        update_option('rp4wp_general_post', $post_options);
        delete_option('rp4wp');
        // Set the post type 'post' as installed
        require_once plugin_dir_path(RP4WP_PLUGIN_FILE_INSTALLER) . 'classes/class-post-type-manager.php';
        require_once plugin_dir_path(RP4WP_PLUGIN_FILE_INSTALLER) . 'classes/class-constants.php';
        $post_type_manager = new RP4WP_Post_Type_Manager();
        $post_type_manager->add_post_type('post', array('post'));
        // add the post type meta to 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) . " ;");
        }
    }
    // Set plugin version
    update_option('rp4wp_current_version', RP4WP::VERSION);
    // add licensing option to prevent the first update_option calling add_option
    add_option('rp4wp_license', array());
}
 /**
  * Hook into admin AJAX to delete a link
  *
  * @access public
  * @return void
  */
 public function run()
 {
     // Check nonce
     check_ajax_referer(RP4WP_Constants::NONCE_AJAX, 'nonce');
     try {
         // Parent
         if (!isset($_POST['parent'])) {
             throw new Exception('Parent not set');
         }
         // Parent and post types
         $parent = sanitize_text_field($_POST['parent']);
         $post_types = array_map('sanitize_text_field', isset($_POST['post_types']) ? $_POST['post_types'] : array());
         // Check if user is allowed to do this
         if (!current_user_can('edit_posts')) {
             return;
         }
         // Related post manager
         $related_post_manager = new RP4WP_Related_Post_Manager();
         // Remove linked related posts
         $related_post_manager->remove_linked_posts($parent);
         // Add the post types
         $ptm = new RP4WP_Post_Type_Manager();
         // Check if we're adding or removing
         if (count($post_types) > 0) {
             // Add the post type
             $ptm->add_post_type($parent, $post_types);
             // Let's go0oo
             $redirect = true;
         } else {
             // get children of parent
             $children = $ptm->get_installed_post_type($parent);
             // Remove post type
             $ptm->remove_post_type($parent);
             // Related word manager
             $word_manager = new RP4WP_Related_Word_Manager();
             // check if parent is used as child in any other relations
             if (false === $ptm->is_post_type_used($parent)) {
                 // delete word cache of parent
                 $word_manager->delete_words_by_post_type($parent);
             }
             // check if there are children
             if (count($children) > 0) {
                 // loop
                 foreach ($children as $child) {
                     // check if this child post type is used in any other relation
                     if (false === $ptm->is_post_type_used($child)) {
                         // delete words
                         $word_manager->delete_words_by_post_type($child);
                     }
                 }
             }
             // No redirect needed
             $redirect = false;
         }
         // Success response
         $response = array('result' => 'success', 'redirect' => $redirect);
     } catch (Exception $e) {
         // Failure response
         $response = array('result' => 'failure', 'redirect' => false, 'error' => $e->getMessage());
     }
     // Send response
     wp_send_json($response);
 }
 /**
  * Install a post type. This includes caching and linking post types.
  *
  * ## OPTIONS
  *
  * <post_type>
  * : The post type where related posts are linked to
  *
  * <linked_post_types>
  * : The post types (comma separated) which are linked to the first argument
  *
  * <rel_amount>
  * : The amount of related posts per post
  *
  * [<max_post_age>]
  * : The maximum age of a related post in days
  *
  * ## EXAMPLE
  *
  *     wp rp4wp remove_related post
  *
  * @synopsis <post_type> <linked_post_types> <rel_amount> [<max_post_age>]
  */
 public function install($args, $assoc_args)
 {
     // args
     $post_type = trim($args[0]);
     $linked_post_types = explode(',', trim($args[1]));
     $rel_amount = intval($args[2]);
     $max_post_age = intval(isset($args[3]) ? $args[3] : 0);
     // we need at least one linked post type to continue
     if (count($linked_post_types) > 0) {
         /**
          * SETTINGS
          */
         // set the linked post types
         $ptm = new RP4WP_Post_Type_Manager();
         $ptm->add_post_type($post_type, $linked_post_types);
         // set the post type options
         if (isset(RP4WP::get()->settings['general_' . $post_type])) {
             $options = RP4WP::get()->settings['general_' . $post_type]->get_options();
             $options['automatic_linking_post_amount'] = $rel_amount;
             $options['max_post_age'] = $max_post_age;
             update_option('rp4wp_general_' . $post_type, $options);
         }
         /**
          * CACHE POSTS
          */
         WP_CLI::line('Caching Words');
         // related word manage
         $related_word_manager = new RP4WP_Related_Word_Manager();
         // cache words of parent post type
         $related_word_manager->save_all_words($post_type);
         // cache words of linked post types
         foreach ($linked_post_types as $linked_post_type) {
             $related_word_manager->save_all_words($linked_post_type);
         }
         /**
          * LINK POSTS
          */
         WP_CLI::line('Linking Posts');
         $related_post_manager = new RP4WP_Related_Post_Manager();
         $related_post_manager->link_related_posts($rel_amount, $post_type);
         // output success
         WP_CLI::success(sprintf('Successfully installed %s', $post_type));
     }
 }
 /**
  * Dev test flow
  *
  * ## OPTIONS
  *
  * ## EXAMPLES
  *
  *     wp rp4wp-dev test
  */
 public function test($args, $assoc_args)
 {
     $limit = 274;
     //		$limit = 10;
     WP_CLI::line('Start rp4wp-dev test');
     WP_CLI::line('---------------------');
     // args
     $post_type = 'post';
     $linked_post_types = array('post');
     $rel_amount = 3;
     $max_post_age = 0;
     /**
      * UNINSTALL
      */
     // Add the post types
     $ptm = new RP4WP_Post_Type_Manager();
     // Remove post type
     $ptm->remove_post_type($post_type);
     // Related post manager
     $related_post_manager = new RP4WP_Related_Post_Manager();
     // Remove linked related posts
     $related_post_manager->remove_linked_posts($post_type);
     // delete words
     $word_manager = new RP4WP_Related_Word_Manager();
     $word_manager->delete_words_by_post_type($post_type);
     /**
      * SETTINGS
      */
     // set the linked post types
     $ptm->add_post_type($post_type, $linked_post_types);
     // set the post type options
     if (isset(RP4WP::get()->settings['general_' . $post_type])) {
         $options = RP4WP::get()->settings['general_' . $post_type]->get_options();
         $options['automatic_linking_post_amount'] = $rel_amount;
         $options['max_post_age'] = $max_post_age;
         update_option('rp4wp_general_' . $post_type, $options);
     }
     /**
      * CACHE POSTS
      */
     WP_CLI::line('Start building word cache');
     // related word manage
     $related_word_manager = new RP4WP_Related_Word_Manager();
     $start = time();
     // cache words of parent post type
     $related_word_manager->save_all_words($post_type, $limit);
     // cache words of linked post types
     foreach ($linked_post_types as $linked_post_type) {
         $related_word_manager->save_all_words($linked_post_type, $limit);
     }
     $runtime = time() - $start;
     $avg = $runtime / $limit;
     WP_CLI::line(sprintf('Words cached - Runtime: %d - Average: %f', $runtime, $avg));
     $start = time();
     /**
      * LINK POSTS
      */
     WP_CLI::line('Start linking posts');
     $related_post_manager->link_related_posts($rel_amount, $post_type, $limit);
     $runtime = time() - $start;
     $avg = $runtime / $limit;
     WP_CLI::line(sprintf('Posts linked - Runtime: %d - Average: %f', $runtime, $avg));
     // output success
     WP_CLI::success(sprintf('Successfully installed %s', $post_type));
 }