public function __construct($title, $alt_type = NelioABExperiment::CPT_ALT_EXP)
 {
     parent::__construct($title);
     $this->set_icon('icon-nelioab');
     $this->set_form_name('nelioab_edit_ab_cpt_exp_form');
     $this->alt_type = $alt_type;
     $this->original_id = -1;
     $this->post_type_name = false;
     require_once NELIOAB_UTILS_DIR . '/wp-helper.php';
     $this->post_types = NelioABWpHelper::get_custom_post_types();
     // Prepare tabs
     $this->add_tab('info', __('General', 'nelioab'), array($this, 'print_basic_info'));
     $this->add_tab('alts', __('Alternatives', 'nelioab'), array($this, 'print_alternatives'));
     $this->add_tab('goals', __('Goals', 'nelioab'), array($this, 'print_goals'));
 }
 public function set_experiment($exp)
 {
     parent::set_experiment($exp);
     switch ($exp->get_post_type()) {
         case 'page':
             $this->post_type = array('name' => 'page', 'singular' => __('Page', 'nelioab'), 'plural' => __('Pages', 'nelioab'));
             break;
         case 'post':
             $this->post_type = array('name' => 'post', 'singular' => __('Post', 'nelioab'), 'plural' => __('Posts', 'nelioab'));
             break;
         default:
             require_once NELIOAB_UTILS_DIR . '/wp-helper.php';
             $ptn = $exp->get_post_type();
             $pt = NelioABWpHelper::get_custom_post_types($ptn);
             $this->post_type = array('name' => $pt->name, 'singular' => __($pt->labels->singular_name, 'nelioab'), 'plural' => __($pt->labels->name, 'nelioab'));
     }
 }
 public function configure_edit_nelioab_alternative()
 {
     // 0. Check whether there is a post_id set. If there is not any,
     // it is a new post, and so we can quit.
     if (!isset($_REQUEST['post'])) {
         return;
     }
     $post_id = $_REQUEST['post'];
     // 1. Determine whether the current post is a nelioab_alternative
     // If it is not, quit
     $post = get_post($post_id, ARRAY_A);
     if (isset($post) && count(get_post_meta($post_id, '_is_nelioab_alternative')) == 0) {
         return;
     }
     // ... but if it is ...
     // recover post type names
     require_once NELIOAB_UTILS_DIR . '/wp-helper.php';
     $cpts = NelioABWpHelper::get_custom_post_types();
     $post_types = array('post', 'page');
     foreach ($cpts as $post_type) {
         array_push($post_types, $post_type->name);
     }
     foreach ($post_types as $post_type) {
         // a) Hide some metaboxes whose contents are managed by the plugin
         remove_meta_box('submitdiv', $post_type, 'side');
         // Publish options
         remove_meta_box('commentstatusdiv', $post_type, 'side');
         // Comments
         remove_meta_box('slugdiv', $post_type, 'normal');
         // Comments
         // b) Create a custom box for saving the alternative post
         add_meta_box('save_nelioab_alternative_box', __('Edition of Alternative\'s Content', 'nelioab'), array($this, 'print_alternative_box'), $post_type, 'side', 'high');
     }
 }
 protected function do_build()
 {
     // Check settings
     require_once NELIOAB_ADMIN_DIR . '/error-controller.php';
     $error = NelioABErrorController::build_error_page_on_invalid_settings();
     if ($error) {
         return;
     }
     // Preparing labels for CUSTOM POST alternatives
     // ----------------------------------------------
     $alt_type = NelioABExperiment::CPT_ALT_EXP;
     $title = __('Edit Custom Post Type Experiment', 'nelioab');
     // We recover the experiment (if any)
     // ----------------------------------------------
     global $nelioab_admin_controller;
     $experiment = NULL;
     $other_names = array();
     if (!empty($nelioab_admin_controller->data)) {
         $experiment = $nelioab_admin_controller->data;
         $alt_type = $experiment->get_type();
     } else {
         $experiment = new NelioABPostAlternativeExperiment(-time());
         $experiment->clear();
     }
     // ...and we also recover other experiment names (if any)
     if (isset($_POST['other_names'])) {
         $other_names = json_decode(urldecode($_POST['other_names']));
     } else {
         foreach (NelioABExperimentsManager::get_experiments() as $aux) {
             if ($aux->get_id() != $experiment->get_id()) {
                 array_push($other_names, $aux->get_name());
             }
         }
     }
     // Get id of Original custom post type
     // ----------------------------------------------
     if (isset($_GET['post-id']) && $_GET['experiment-type'] == NelioABExperiment::CPT_ALT_EXP) {
         $experiment->set_original($_GET['post-id']);
     }
     // Checking whether there are custom post types available
     // ---------------------------------------------------
     require_once NELIOAB_UTILS_DIR . '/wp-helper.php';
     $post_types = NelioABWpHelper::get_custom_post_types();
     if ($alt_type == NelioABExperiment::CPT_ALT_EXP && count($post_types) == 0) {
         require_once NELIOAB_ADMIN_DIR . '/views/errors/message-page.php';
         $view = new NelioABMessagePage(__('There are no custom post types available.', 'nelioab'), __('You have to create a public custom post type and publish some custom posts first to use this type of experiment.', 'nelioab'));
         return $view;
     }
     $found = false;
     foreach ($post_types as $post_type) {
         $options_for_posts = array('posts_per_page' => 1, 'post_type' => $post_type->name, 'post_status' => 'publish');
         $list_of_posts = get_posts($options_for_posts);
         require_once NELIOAB_UTILS_DIR . '/data-manager.php';
         NelioABArrays::sort_posts($list_of_posts);
         if (count($list_of_posts) > 0) {
             $found = true;
             break;
         }
     }
     if ($alt_type == NelioABExperiment::CPT_ALT_EXP && !$found) {
         require_once NELIOAB_ADMIN_DIR . '/views/errors/message-page.php';
         $view = new NelioABMessagePage(__('There are no custom posts available.', 'nelioab'), __('Please, create one custom post and try again.', 'nelioab'));
         return $view;
     }
     // If everything is OK, we keep going!
     // ---------------------------------------------------
     // Creating the view
     $view = $this->create_view($alt_type);
     foreach ($other_names as $name) {
         $view->add_another_experiment_name($name);
     }
     // Experiment information
     $view->set_basic_info($experiment->get_id(), $experiment->get_name(), $experiment->get_description(), $experiment->get_finalization_mode(), $experiment->get_finalization_value());
     // Experiment specific variables and alternatives
     $view->set_custom_post_type($experiment->get_post_type());
     $view->set_original_id($experiment->get_originals_id());
     $view->set_alternatives($experiment->get_json4js_alternatives());
     // Goals
     $goals = $experiment->get_goals();
     foreach ($goals as $goal) {
         $view->add_goal($goal->json4js());
     }
     return $view;
 }
/**
 * Deactivates our plugin.
 *
 * This function is called by the "registed_deactivation_hook". Alternatives
 * are regular pages or posts (draft status) with a special metaoption that
 * is used to hide them from the admin menu. When the plugin is deactivated,
 * no one hides the alternatives... In order to prevent them from appearing,
 * we change their post_type to a fake type.
 *
 * @return void
 *
 * @since PHPDOC
 */
function nelioab_deactivate_plugin()
{
    /** @var wpdb $wpdb */
    global $wpdb;
    require_once NELIOAB_EXP_CONTROLLERS_DIR . '/widget-experiment-controller.php';
    require_once NELIOAB_EXP_CONTROLLERS_DIR . '/menu-experiment-controller.php';
    if (isset($_GET['action']) && 'clean-and-deactivate' == $_GET['action']) {
        // Remove all alternative widgets
        NelioABWidgetExpAdminController::clean_all_alternative_widgets();
        // Remove all alternative menus
        NelioABMenuExpAdminController::clean_all_alternative_menus();
        // Remove all alternative pages and posts
        $query = '' . 'DELETE FROM ' . $wpdb->posts . ' WHERE ' . 'id IN (' . 'SELECT post_id FROM ' . $wpdb->postmeta . ' WHERE ' . 'meta_key = \'_is_nelioab_alternative\' ' . ')';
        $wpdb->query($query);
        // Clean all experiments in AE
        require_once NELIOAB_UTILS_DIR . '/backend.php';
        for ($i = 0; $i < 5; ++$i) {
            try {
                NelioABBackend::remote_get(sprintf(NELIOAB_BACKEND_URL . '/site/%s/clean', NelioABAccountSettings::get_site_id()));
                break;
            } catch (Exception $e) {
            }
        }
        // Remove all Nelio options
        $query = 'DELETE FROM ' . $wpdb->postmeta . ' WHERE meta_key LIKE \'%nelioab%\'';
        $wpdb->query($query);
        $query = 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE \'%nelioab%\'';
        $wpdb->query($query);
    } else {
        // Hiding alternative posts
        $query = '' . 'UPDATE ' . $wpdb->posts . ' SET post_type = %s WHERE ' . 'id IN (' . 'SELECT post_id FROM ' . $wpdb->postmeta . ' WHERE ' . 'meta_key = \'_is_nelioab_alternative\' ' . ') AND ' . 'post_type = %s';
        // recover post type names
        require_once NELIOAB_UTILS_DIR . '/wp-helper.php';
        $cpts = NelioABWpHelper::get_custom_post_types();
        $post_types = array('post', 'page');
        foreach ($cpts as $post_type) {
            array_push($post_types, $post_type->name);
        }
        // execute the query
        foreach ($post_types as $post_type) {
            $wpdb->query($wpdb->prepare($query, 'nelioab_alt_' . $post_type, $post_type));
        }
        // Hiding widget alternatives
        NelioABWidgetExpAdminController::backup_alternative_widgets();
        // Hiding menu alternatives
        NelioABMenuExpAdminController::backup_alternative_menus();
    }
}
 /**
  * This function is an AJAX callback that looks for posts.
  *
  * It returns a list of up to 20 posts (or pages). It is used by the
  * select2 widget (an item selector that looks more beautiful than regular
  * the "select>option" combo.
  *
  * Accepted POST params are:
  *
  * * term: {string} the (part of the) string used to look for items.
  * * type: {array}  array containing the types of element are we looking.
  * * default_id: {int} (optional) if set, the item with that ID will be
  * returned. If that item is not found, then we'll perform a regular search
  * (as if the param had not been set).
  *
  * @return array a list of matching posts.
  *
  * @since PHPDOC
  */
 public static function search_posts()
 {
     $term = false;
     if (isset($_POST['term']) & !empty($_POST['term'])) {
         $term = $_POST['term'];
     }
     $types = array();
     if (isset($_POST['type'])) {
         $types = $_POST['type'];
     }
     if (!is_array($types)) {
         $types = array($types);
     }
     $status = 'publish';
     if (isset($_POST['drafts']) && 'show-drafts' == $_POST['drafts']) {
         $status = array('publish', 'draft');
     }
     $default_id = false;
     if (isset($_POST['default_id'])) {
         $default_id = $_POST['default_id'];
     }
     if ($default_id == -1) {
         $default_id = false;
     }
     $default_thumbnail = sprintf('<img src="data:image/gif;%s" class="%s" alt="%s" />', 'base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', 'attachment-thumbnail wp-post-image nelioab-no-thumbnail', __('No featured image available', 'nelioab'));
     $args = array('post_title_like' => $term, 'posts_per_page' => 20, 'meta_key' => '_is_nelioab_alternative', 'meta_compare' => 'NOT EXISTS', 'post_status' => $status);
     if (count($types) > 0) {
         $aux = array();
         foreach ($types as $type) {
             if (strpos($type, 'nelioab-') === false) {
                 array_push($aux, $type);
             }
         }
         foreach ($types as $type) {
             if ('nelioab-all-post-types' == $type) {
                 array_push($aux, 'page');
                 array_push($aux, 'post');
                 foreach (NelioABWpHelper::get_custom_post_types() as $cpt) {
                     array_push($aux, $cpt->name);
                 }
             }
         }
         if (count($aux) > 0) {
             $args['post_type'] = $aux;
         }
     }
     if ($types && count($types) == 1 && 'page' === $types[0] && !$term) {
         $args['order'] = 'asc';
         $args['orderby'] = 'title';
     }
     $latest_post_item = false;
     if (in_array('nelioab-latest-posts', $types) || false !== $default_id) {
         $lp_title = __('Your latest posts', 'nelioab');
         if (!$term || strpos(strtolower($lp_title), strtolower($term)) !== false) {
             $latest_post_item = array('id' => NelioABController::FRONT_PAGE__YOUR_LATEST_POSTS, 'type' => '<i>latest-posts</i>', 'title' => self::fix_title($lp_title), 'status' => '<i>dynamic</i>', 'date' => '', 'author' => 'WordPress', 'thumbnail' => $default_thumbnail);
         }
     }
     $theme_based_landing_page = false;
     if (in_array('nelioab-theme-landing-page', $types) && NelioABSettings::does_theme_use_a_custom_landing_page() || NelioABController::FRONT_PAGE__THEME_BASED_LANDING == $default_id) {
         $lp_title = __('Landing Page (Theme-based)', 'nelioab');
         if (!$term || strpos(strtolower($lp_title), strtolower($term)) !== false) {
             $theme_based_landing_page = array('id' => NelioABController::FRONT_PAGE__THEME_BASED_LANDING, 'type' => '<i>landing-page</i>', 'title' => self::fix_title($lp_title), 'status' => '<i>dynamic</i>', 'date' => '', 'author' => 'WordPress', 'thumbnail' => $default_thumbnail);
         }
     }
     // If there's a default_id set, it means that the user is interested
     // in one post only; I'm going to return that post to him
     if ($default_id !== false) {
         $id = $default_id;
         $post = false;
         if ($id == NelioABController::FRONT_PAGE__YOUR_LATEST_POSTS) {
             header('Content-Type: application/json');
             echo json_encode(array($latest_post_item));
             die;
         }
         if ($id == NelioABController::FRONT_PAGE__THEME_BASED_LANDING) {
             header('Content-Type: application/json');
             echo json_encode(array($theme_based_landing_page));
             die;
         }
         if ($id > 0) {
             $post = get_post($id);
         }
         if ($post) {
             $thumbnail = get_the_post_thumbnail($post->ID, 'thumbnail');
             if ($thumbnail === '') {
                 $thumbnail = $default_thumbnail;
             }
             $item = array('id' => $post->ID, 'title' => self::fix_title($post->post_title), 'thumbnail' => $thumbnail, 'excerpt' => $post->post_excerpt);
             header('Content-Type: application/json');
             echo json_encode(array($item));
             die;
         }
     }
     $result = array();
     add_filter('posts_where', array('NelioABWpHelper', 'add_title_filter_to_wpquery'), 10, 2);
     $my_query = new WP_Query($args);
     remove_filter('posts_where', array('NelioABWpHelper', 'add_title_filter_to_wpquery'), 10, 2);
     if ($my_query->have_posts()) {
         global $post;
         while ($my_query->have_posts()) {
             $my_query->the_post();
             $thumbnail = get_the_post_thumbnail($post->ID, 'thumbnail');
             if ($thumbnail === '') {
                 $thumbnail = $default_thumbnail;
             }
             $item = array('id' => $post->ID, 'type' => $post->post_type, 'title' => self::fix_title($post->post_title), 'status' => $post->post_status, 'date' => $post->post_date, 'excerpt' => $post->post_excerpt, 'author' => get_the_author(), 'thumbnail' => $thumbnail);
             array_push($result, $item);
         }
     }
     if ($latest_post_item) {
         array_unshift($result, $latest_post_item);
     }
     if ($theme_based_landing_page) {
         array_unshift($result, $theme_based_landing_page);
     }
     header('Content-Type: application/json');
     echo json_encode($result);
     die;
 }