/**
  * PHPDOC
  *
  * @param string $type    PHPDOC
  * @param int    $form_id PHPDOC
  *
  * @return void
  *
  * @since PHPDOC
  */
 private function send_form_action_if_required($type, $form_id)
 {
     require_once NELIOAB_MODELS_DIR . '/goals/actions/form-submission-action.php';
     require_once NELIOAB_MODELS_DIR . '/goals/actions/action.php';
     require_once NELIOAB_UTILS_DIR . '/backend.php';
     $kap = NelioABFormSubmissionAction::type_to_kind_and_plugin($type);
     if (!$kap) {
         return;
     }
     $f = 'nelioab_current_id';
     if (!isset($_POST[$f])) {
         return;
     }
     $f = 'nelioab_current_actual_id';
     if (!isset($_POST[$f])) {
         return;
     }
     $f = 'nelioab_userid';
     if (!isset($_POST[$f]) || strlen(trim($_POST[$f])) === 0) {
         return;
     }
     // Constructing FORM EVENT object:
     $page = $_POST['nelioab_current_id'];
     $actual_page = $_POST['nelioab_current_actual_id'];
     $ev = array('kind' => $kap['kind'], 'form' => $form_id, 'plugin' => $kap['plugin'], 'page' => $page, 'actualPage' => $actual_page, 'user' => $_POST['nelioab_userid']);
     $the_theme = NelioABVisitor::get_alternative_for_global_alt_exp(NelioABExperiment::THEME_ALT_EXP);
     if ($the_theme) {
         $ev['activeTheme'] = $the_theme->get_id();
     }
     $the_css = NelioABVisitor::get_alternative_for_global_alt_exp(NelioABExperiment::CSS_ALT_EXP);
     if ($the_css) {
         $ev['activeCSS'] = $the_css->get_id();
     }
     $the_widget = NelioABVisitor::get_alternative_for_global_alt_exp(NelioABExperiment::WIDGET_ALT_EXP);
     if ($the_widget) {
         $ev['activeWidget'] = '' . $the_widget->get_id();
     }
     $the_menu = NelioABVisitor::get_alternative_for_global_alt_exp(NelioABExperiment::MENU_ALT_EXP);
     if ($the_menu) {
         $ev['activeMenu'] = $the_menu->get_id();
     }
     // Check if there's one experiment at least with a form submission action,
     // which corresponds to the given form, then send the event. Otherwise,
     // get out!
     require_once NELIOAB_MODELS_DIR . '/experiments-manager.php';
     $send_form_event = false;
     $running_exps = NelioABExperimentsManager::get_relevant_running_experiments_from_cache();
     foreach ($running_exps as $exp) {
         /** @var NelioABExperiment $exp */
         foreach ($exp->get_goals() as $goal) {
             /** @var NelioABAltExpGoal $goal */
             foreach ($goal->get_actions() as $action) {
                 /** @var NelioABFormSubmissionAction $action */
                 if ($action->get_type() == $type && $action->get_form_id() == $ev['form']) {
                     // If this action uses the form, then we have to check whether
                     // it accepts submissions from anywhere or only from the tested
                     // page.
                     if ($action->accepts_submissions_from_any_page()) {
                         $send_form_event = true;
                         break;
                     }
                     if ($exp->get_originals_id() == $ev['page']) {
                         $send_form_event = true;
                         break;
                     }
                 }
             }
             if ($send_form_event) {
                 break;
             }
         }
         if ($send_form_event) {
             break;
         }
     }
     if (!$send_form_event) {
         return;
     }
     $url = sprintf(NELIOAB_BACKEND_URL . '/site/%s/form', NelioABAccountSettings::get_site_id());
     $data = NelioABBackend::build_json_object_with_credentials($ev);
     $data['timeout'] = 50;
     for ($attemp = 0; $attemp < 5; ++$attemp) {
         try {
             NelioABBackend::remote_post_raw($url, $data);
             break;
         } catch (Exception $e) {
             // If the form submission event could not be sent, it may be that's
             // because there is no more quota available
             if ($e->getCode() == NelioABErrCodes::NO_MORE_QUOTA) {
                 // If that was the case, simply leave
                 break;
             }
             // If there was another error... we just keep trying (attemp) up to 5
             // times.
         }
     }
 }
 /**
  * Returns the list of running experiments for which the current user has one alternative assigned.
  *
  * @return array the list of running experiments for which the current user has one alternative assigned.
  *
  * @see NelioABVisitor::get_experiment_ids_in_request
  *
  * @since 4.0.0
  */
 public static function get_relevant_running_experiments_from_cache()
 {
     if (self::$relevant_running_experiments) {
         return self::$relevant_running_experiments;
     }
     $env_ids = NelioABVisitor::get_experiment_ids_in_request();
     $running_experiments = self::get_running_experiments_from_cache();
     $relevant_running_experiments = array();
     foreach ($running_experiments as $exp) {
         /** @var NelioABExperiment $exp */
         $is_relevant = false;
         for ($i = 0; $i < count($env_ids) && !$is_relevant; ++$i) {
             if ($exp->get_id() == $env_ids[$i]) {
                 $is_relevant = true;
             }
         }
         if ($is_relevant) {
             $already_in_array = false;
             foreach ($relevant_running_experiments as $relevant_exp) {
                 /** @var NelioABExperiment $relevant_exp */
                 if ($relevant_exp->get_id() == $exp->get_id()) {
                     $already_in_array = true;
                 }
             }
             if (!$already_in_array) {
                 array_push($relevant_running_experiments, $exp);
             }
         }
     }
     if (NelioABVisitor::is_fully_loaded()) {
         self::$relevant_running_experiments = $relevant_running_experiments;
     } else {
         self::$relevant_running_experiments = false;
     }
     return $relevant_running_experiments;
 }
 /**
  * Adds a Global Alternative Experiment in the appropriate property.
  *
  * @param NelioABGlobalAlternativeExperiment $exp The Alternative Experiment to be added.
  * @param int $alt_index The index of the alternative that this visitor is supposed to see.
  *            If 0, the original version is added; if 1, the first
  *            alternative; if 2, the second; and so on.
  *
  * @since 4.0.0
  */
 private static function add_global_exp($exp, $alt_index)
 {
     $alt = $exp->get_original();
     if ($alt_index > 0) {
         --$alt_index;
         $alts = $exp->get_alternatives();
         if ($alt_index < count($alts)) {
             $alt = $alts[$alt_index];
         }
     }
     switch ($exp->get_type()) {
         case NelioABExperiment::CSS_ALT_EXP:
             self::$css_info = array('exp' => $exp, 'alt' => $alt);
             break;
         case NelioABExperiment::MENU_ALT_EXP:
             self::$menu_info = array('exp' => $exp, 'alt' => $alt);
             break;
         case NelioABExperiment::THEME_ALT_EXP:
             self::$theme_info = array('exp' => $exp, 'alt' => $alt);
             break;
         case NelioABExperiment::WIDGET_ALT_EXP:
             self::$widget_info = array('exp' => $exp, 'alt' => $alt);
             break;
     }
 }
 /**
  * PHPDOC
  *
  * @param string          $single_title PHPDOC
  * @param WP_Post|boolean $post         PHPDOC
  *
  * @return string PHPDOC
  *
  * @since PHPDOC
  */
 public static function fix_the_single_title($single_title, $post = false)
 {
     require_once NELIOAB_MODELS_DIR . '/visitor.php';
     if ($post) {
         $alt_id = NelioABVisitor::get_alternative_for_post_alt_exp($post->ID);
         $alt_post = get_post($alt_id);
         if ($alt_post) {
             return $alt_post->post_title;
         }
     }
     return $single_title;
 }
 /**
  * PHPDOC
  *
  * @param string  $value     Type of object metadata is for (e.g., comment, post, or user)
  * @param int     $object_id ID of the object metadata is for
  * @param string  $meta_key  Optional. Metadata key.
  * @param boolean $single    Optional.
  *
  * @return mixed Single metadata value, or array of values
  *
  * @since PHPDOC
  */
 public function fix_product_summary_featured_image($value, $object_id, $meta_key, $single)
 {
     if ('_thumbnail_id' == $meta_key) {
         $summary_data = NelioABVisitor::get_alternative_for_wc_product_summary_alt_exp($object_id);
         if ($summary_data) {
             $this->add_active_product_summary_experiment($summary_data['exp'], $summary_data['alt']);
             /** @var NelioABAlternative $alt */
             $alt = $summary_data['alt'];
             $value = $alt->get_value();
             // This first IF is a safeguard...
             if (is_array($value) && isset($value['image_id'])) {
                 if ($single) {
                     return $value['image_id'];
                 } else {
                     return $value['image_id'];
                 }
             }
         }
     }
     return $value;
 }
 /**
  * It initializes the controller.
  *
  * If Nelio's subscription has been deactivated, the plugin will do
  * nothing at all.
  *
  * @return void
  */
 public function init()
 {
     // If the user has been disabled... get out of here
     try {
         NelioABAccountSettings::check_user_settings();
     } catch (Exception $e) {
         // It is important we add the check here: if the user was deactivated, but it no
         // longer is, then it's important his settings are rechecked so that we can
         // re-enable it.
         if ($e->getCode() == NelioABErrCodes::DEACTIVATED_USER) {
             return;
         }
     }
     // Load the Visitor class so that all assigned alternatives are
     require_once NELIOAB_MODELS_DIR . '/visitor.php';
     NelioABVisitor::load();
     // Trick for proper THEME ALT EXP testing
     require_once NELIOAB_UTILS_DIR . '/wp-helper.php';
     // Theme alt exp related
     if (NelioABWpHelper::is_at_least_version(3.4)) {
         $aux = $this->controllers['alt-exp'];
         add_filter('stylesheet', array(&$aux, 'modify_stylesheet'));
         add_filter('template', array(&$aux, 'modify_template'));
         add_filter('sidebars_widgets', array(&$aux, 'show_the_appropriate_widgets'));
         add_filter('option_stylesheet', array(&$aux, 'modify_option_stylesheet'));
         add_filter('option_current_theme', array(&$aux, 'modify_option_current_theme'));
         require_once NELIOAB_UTILS_DIR . '/theme-compatibility-layer.php';
         NelioABThemeCompatibilityLayer::make_compat();
     }
     add_action('init', array(&$this, 'do_init'));
     add_action('init', array(&$this, 'init_admin_stuff'));
 }