public static function build_error_page_on_invalid_settings()
 {
     // Check settings
     try {
         $aux = NelioABAccountSettings::check_user_settings();
     } catch (Exception $e) {
         switch ($e->getCode()) {
             case NelioABErrCodes::DEACTIVATED_USER:
             case NelioABErrCodes::INVALID_MAIL:
             case NelioABErrCodes::INVALID_PRODUCT_REG_NUM:
             case NelioABErrCodes::NON_ACCEPTED_TAC:
             case NelioABErrCodes::BACKEND_NO_SITE_CONFIGURED:
                 require_once NELIOAB_ADMIN_DIR . '/error-controller.php';
                 $view = NelioABErrorController::get_view($e);
                 $view->render();
                 return true;
             default:
                 return false;
         }
     }
     return false;
 }
 /**
  * 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 most relevant information of Nelio A/B Testing (to be displayed in the dashboard).
  *
  * @return array {
  *     Most relevant information of Nelio A/B Testing (to be displayed in the dashboard).
  *
  *     @type array $exps  List of running experiments.
  *     @type array $quota Two integers: the amount of `used` quota and the `total` quota available.
  * }
  *
  * @since 3.4.4
  */
 public static function get_dashboard_summary()
 {
     // This function is used in the Dashboard
     require_once NELIOAB_UTILS_DIR . '/backend.php';
     $json_data = NelioABBackend::remote_get(sprintf(NELIOAB_BACKEND_URL . '/site/%s/exp/summary', NelioABAccountSettings::get_site_id()));
     // Including types of experiments...
     require_once NELIOAB_MODELS_DIR . '/summaries/alt-exp-summary.php';
     require_once NELIOAB_MODELS_DIR . '/summaries/heatmap-exp-summary.php';
     $json_data = json_decode($json_data['body']);
     $result = array('exps' => array(), 'quota' => array('regular' => 5000, 'monthly' => 5000, 'extra' => 0));
     if (isset($json_data->quota)) {
         $result['quota']['regular'] = $json_data->quota + $json_data->quotaExtra;
     }
     if (isset($json_data->quotaPerMonth)) {
         $result['quota']['monthly'] = $json_data->quotaPerMonth;
     }
     if ($result['quota']['regular'] > $result['quota']['monthly']) {
         $diff = $result['quota']['regular'] - $result['quota']['monthly'];
         $result['quota']['extra'] += $diff;
         $result['quota']['regular'] = $result['quota']['monthly'];
     }
     if (isset($json_data->items)) {
         foreach ($json_data->items as $item) {
             /** @var NelioABExperimentSummary $exp */
             switch ($item->kind) {
                 case NelioABExperiment::HEATMAP_EXP_STR:
                     $exp = new NelioABHeatmapExpSummary($item->key->id);
                     break;
                 default:
                     $exp = new NelioABAltExpSummary($item->key->id);
             }
             if ($exp) {
                 $exp->load_json4ae($item);
                 array_push($result['exps'], $exp);
             }
         }
     }
     return $result;
 }
 /**
  * Removes this site from the account of our customer.
  *
  * @return void
  *
  * @throws Exception an exception triggered by AppEngine.
  *
  * @since 2.1.0
  */
 public static function deregister_this_site()
 {
     try {
         NelioABBackend::remote_post(sprintf(NELIOAB_BACKEND_URL . '/site/%s/deactivate', NelioABAccountSettings::get_site_id()), array(), true);
     } catch (Exception $e) {
         throw $e;
     }
     self::update_nelioab_option('has_a_configured_site', false);
 }
 public function save()
 {
     // 1. UPDATE OR CREATE THE EXPERIMENT
     // -------------------------------------------------------------------------
     if ($this->get_id() < 0) {
         $url = sprintf(NELIOAB_BACKEND_URL . '/site/%s/exp/post', NelioABAccountSettings::get_site_id());
     } else {
         $url = sprintf(NELIOAB_BACKEND_URL . '/exp/post/%s/update', $this->get_id());
     }
     if ($this->get_status() != NelioABExperiment::STATUS_PAUSED && $this->get_status() != NelioABExperiment::STATUS_RUNNING && $this->get_status() != NelioABExperiment::STATUS_FINISHED && $this->get_status() != NelioABExperiment::STATUS_TRASH) {
         $this->set_status($this->determine_proper_status());
     }
     $body = array('name' => $this->get_name(), 'description' => $this->get_description(), 'originalPost' => $this->get_originals_id(), 'status' => $this->get_status(), 'kind' => $this->get_textual_type(), 'showHeatmap' => $this->are_heatmaps_tracked(), 'finalizationMode' => $this->get_finalization_mode(), 'finalizationModeValue' => $this->get_finalization_value());
     $result = NelioABBackend::remote_post($url, $body);
     $exp_id = $this->get_id();
     if ($exp_id < 0) {
         if (is_wp_error($result)) {
             return;
         }
         $json = json_decode($result['body']);
         $exp_id = $json->key->id;
         $this->id = $exp_id;
     }
     // 1.1 SAVE GOALS
     // -------------------------------------------------------------------------
     $this->make_goals_persistent();
     // 2. UPDATE THE ALTERNATIVES
     // -------------------------------------------------------------------------
     // 2.1. UPDATE CHANGES ON ALREADY EXISTING APPSPOT ALTERNATIVES
     foreach ($this->get_appspot_alternatives() as $alt) {
         /** @var NelioABHeadlineAlternative $alt */
         if ($alt->was_removed() || !$alt->is_dirty()) {
             continue;
         }
         $body = array('name' => $alt->get_name(), 'value' => json_encode($this->fix_image_id_in_value($alt->get_value())));
         NelioABBackend::remote_post(sprintf(NELIOAB_BACKEND_URL . '/alternative/%s/update', $alt->get_id()), $body);
     }
     // 2.2. REMOVE FROM APPSPOT THE REMOVED ALTERNATIVES
     foreach ($this->get_appspot_alternatives() as $alt) {
         if (!$alt->was_removed()) {
             continue;
         }
         $url = sprintf(NELIOAB_BACKEND_URL . '/alternative/%s/delete', $alt->get_id());
         NelioABBackend::remote_post($url);
     }
     // 2.3. CREATE LOCAL ALTERNATIVES IN APPSPOT
     foreach ($this->get_local_alternatives() as $alt) {
         if ($alt->was_removed()) {
             continue;
         }
         $body = array('name' => $alt->get_name(), 'value' => json_encode($this->fix_image_id_in_value($alt->get_value())), 'kind' => $this->get_textual_type());
         try {
             /** @var int $result */
             $result = NelioABBackend::remote_post(sprintf(NELIOAB_BACKEND_URL . '/exp/post/%s/alternative', $exp_id), $body);
             $alt->set_id($result);
         } catch (Exception $e) {
         }
     }
     require_once NELIOAB_MODELS_DIR . '/experiments-manager.php';
     NelioABExperimentsManager::update_experiment($this);
 }
 function column_name($exp)
 {
     $url_fragment = admin_url('admin.php?page=nelioab-experiments&action=%1$s&id=%2$s&exp_type=%3$s');
     if (isset($_REQUEST['status'])) {
         $url_fragment .= '&status=' . $_REQUEST['status'];
     }
     $url = '<a href="' . $url_fragment . '">%4$s</a>';
     $url_dialog = '<a href="#" onclick="javascript:nelioabValidateClick(%5$s, \'' . $url_fragment . '\');return false;">%4$s</a>';
     $url_duplicate = '<a href="' . $url_fragment . '&_nonce=' . nelioab_onetime_nonce('duplicate-' . $exp->get_id()) . '">%4$s</a>';
     $progress_url = '<a href="?page=nelioab-experiments&action=progress&id=%1$s&exp_type=%2$s">%3$s</a>';
     if ($exp->get_type() == NelioABExperiment::HEATMAP_EXP) {
         include_once NELIOAB_UTILS_DIR . '/wp-helper.php';
         $progress_url = '<a href="' . str_replace('https://', 'http://', admin_url('admin.php?nelioab-page=heatmaps&id=%1$s&exp_type=%2$s')) . '">%3$s</a>';
     }
     $actions = array();
     switch ($exp->get_status()) {
         case NelioABExperiment::STATUS_DRAFT:
             $actions = array('edit' => sprintf($url, 'edit', $exp->get_id(), $exp->get_type(), __('Edit')), 'trash' => sprintf($url, 'trash', $exp->get_id(), $exp->get_type(), __('Trash')));
             break;
         case NelioABExperiment::STATUS_READY:
             $actions = array();
             $actions['edit'] = sprintf($url, 'edit', $exp->get_id(), $exp->get_type(), __('Edit'));
             $actions['start'] = sprintf($url_dialog, 'start', $exp->get_id(), $exp->get_type(), __('Start', 'nelioab'), 0);
             $actions['schedule'] = sprintf($url, 'schedule', $exp->get_id(), $exp->get_type(), __('Schedule'));
             $actions['duplicate'] = sprintf($url_duplicate, 'duplicate', $exp->get_id(), $exp->get_type(), __('Duplicate'), 'nelioab');
             $actions['trash'] = sprintf($url, 'trash', $exp->get_id(), $exp->get_type(), __('Trash'));
             break;
         case NelioABExperiment::STATUS_SCHEDULED:
             $actions = array('start' => sprintf($url_dialog, 'start', $exp->get_id(), $exp->get_type(), __('Start Now', 'nelioab'), 0), 'schedule' => sprintf($url, 'schedule', $exp->get_id(), $exp->get_type(), __('Reschedule')), 'cancel-schedule' => sprintf($url, 'cancel-schedule', $exp->get_id(), $exp->get_type(), __('Cancel Schedule', 'nelioab'), 1), 'duplicate' => sprintf($url_duplicate, 'duplicate', $exp->get_id(), $exp->get_type(), __('Duplicate', 'nelioab')));
             break;
         case NelioABExperiment::STATUS_RUNNING:
             $actions = array('theprogress' => sprintf($progress_url, $exp->get_id(), $exp->get_type(), __('View')), 'stop' => sprintf($url_dialog, 'stop', $exp->get_id(), $exp->get_type(), __('Stop', 'nelioab'), 1), 'duplicate' => sprintf($url_duplicate, 'duplicate', $exp->get_id(), $exp->get_type(), __('Duplicate', 'nelioab')));
             break;
         case NelioABExperiment::STATUS_FINISHED:
             $actions = array('theprogress' => sprintf($progress_url, $exp->get_id(), $exp->get_type(), __('View')), 'duplicate' => sprintf($url_duplicate, 'duplicate', $exp->get_id(), $exp->get_type(), __('Duplicate', 'nelioab')), 'delete' => sprintf($url, 'delete', $exp->get_id(), $exp->get_type(), __('Delete Permanently')));
             break;
         case NelioABExperiment::STATUS_TRASH:
         default:
             $actions = array('restore' => sprintf($url, 'restore', $exp->get_id(), $exp->get_type(), __('Restore')), 'delete' => sprintf($url, 'delete', $exp->get_id(), $exp->get_type(), __('Delete Permanently')));
             break;
     }
     $related_post = $exp->get_related_post_id();
     if (isset($actions['start']) && $related_post && $related_post > 0 && get_post_status($related_post) !== 'publish') {
         $label = sprintf('<span style="cursor:default;" title="%s">%s</span>', esc_html(__('The experiment cannot be started because the tested element has not been published yet', 'nelioab')), __('Start'));
         $actions['start'] = $label;
     }
     if (!NelioABAccountSettings::is_plan_at_least(NelioABAccountSettings::PROFESSIONAL_SUBSCRIPTION_PLAN)) {
         $expl = __('Feature only available in the Professional Plan', 'nelioab');
         // No actions available to Professional Plans only
     }
     if (!NelioABAccountSettings::is_plan_at_least(NelioABAccountSettings::ENTERPRISE_SUBSCRIPTION_PLAN)) {
         $expl = __('Feature only available in the Enterprise Plan', 'nelioab');
         if (isset($actions['schedule'])) {
             $actions['schedule'] = sprintf('<span title="%s">%s</span>', $expl, __('Schedule', 'nelioab'));
         }
     }
     //Build row actions
     return sprintf('<span class="row-title">%2$s</span>%3$s', $exp->get_id(), $exp->get_name(), $this->row_actions($actions));
 }
        public static function print_notification_email_field()
        {
            $field_name = 'notification_email';
            printf('<div class="nelio-sect"><input type="text" id="%1$s" name="nelioab_settings[%1$s]" style="max-width:400px;width:100%%;" value="%3$s" placeholder="%4$s" %2$s></div>', $field_name, self::get_basic_details(), esc_html(NelioABSettings::get_notification_email()), sprintf(__('Default: %s', 'nelioab'), esc_html(NelioABAccountSettings::get_email())));
            ?>
			<div class="the-descr" style="display:none;"><span class="description"><?php 
            printf(__('If you type an e-mail address, all Nelio A/B Testing notifications will be sent to both the new address and «%s».', 'nelioab'), NelioABAccountSettings::get_email());
            ?>
</span></div>
			<script type="text/javascript">
				(function($) {
					var mail = $('#<?php 
            echo $field_name;
            ?>
');
					var form = $('#nelioab-settings');
					var save;
					function validateMail() {
						var x = mail.attr('value');
						if ( x.length == 0 )
							return true;
						var atpos = x.indexOf('@');
						var dotpos = x.lastIndexOf('.');
						if (atpos< 1 || dotpos<atpos+2 || dotpos+2>=x.length)
							return false;
						return true;
					}
					function control() {
						if ( validateMail() ) {
							mail.removeClass('error');
							save.removeClass('disabled');
							form.unbind('submit', returnFalse);
						}
						else {
							mail.addClass('error');
							save.addClass('disabled');
							form.on('submit', returnFalse);
						}
					}
					function returnFalse() { return false; }
					mail.on('keyup focusout', control);
					$(document).ready(function() {
						save = $('#submit');
						control();
					});
				})(jQuery);
			</script>
			<?php 
        }
 /**
  * Returns whether the upgrade notice is visible or not.
  *
  * @return boolean whether the upgrade notice is visible or not.
  *
  * @since 2.0.11
  */
 public static function is_upgrade_message_visible()
 {
     if (NelioABAccountSettings::get_subscription_plan() != NelioABAccountSettings::BASIC_SUBSCRIPTION_PLAN) {
         return false;
     }
     $result = get_option('nelioab_hide_upgrade_message', false);
     if (!$result) {
         return true;
     } else {
         return false;
     }
 }
 /**
  * PHPDOC
  *
  * @param int $order_id PHPDOC
  *
  * @return void
  *
  * @since PHPDOC
  */
 public function sync_order_completed($order_id)
 {
     $order = wc_get_order($order_id);
     $items = $order->get_items();
     $user = get_post_meta($order_id, '_nelioab_userid', true);
     $env = get_post_meta($order_id, '_nelioab_form_env', true);
     if (!$user) {
         return;
     }
     require_once NELIOAB_MODELS_DIR . '/experiments-manager.php';
     $running_experiments = NelioABExperimentsManager::get_running_experiments_from_cache();
     // Let's start by selecting the relevant experiments.
     // A relevant experiment is a running experiment that was in the environment
     // that was active during the purchase AND that has at least one conversion
     // action in its goals that tracks an "Order Completed" event.
     $relevant_experiments = array();
     $relevant_conversion_actions = array();
     foreach ($env as $id => $alt) {
         foreach ($running_experiments as $exp) {
             if ($exp->get_id() != $id) {
                 continue;
             }
             foreach ($exp->get_goals() as $goal) {
                 if ($goal->get_kind() != NelioABGoal::ALTERNATIVE_EXPERIMENT_GOAL) {
                     continue;
                 }
                 foreach ($goal->get_actions() as $action) {
                     echo "Considering {$action->get_id()}... ";
                     if ($action->get_type() != NelioABAction::WC_ORDER_COMPLETED) {
                         echo "NO<br>";
                         continue;
                     }
                     echo "YES<br>";
                     // Once we've found a Order Complete conversion action, we save:
                     //  a) the experiment
                     //  b) the product controlled by that conversion action
                     if (!in_array($exp, $relevant_experiments)) {
                         array_push($relevant_experiments, $exp);
                     }
                     array_push($relevant_conversion_actions, $action);
                 }
             }
         }
     }
     // Now we need to see if products are being tested and, if they are,
     // the IDs of the concrete alternatives the visitor saw.
     $relevant_products = array();
     foreach ($relevant_experiments as $exp) {
         if ($exp->get_type() != NelioABExperiment::WC_PRODUCT_SUMMARY_ALT_EXP) {
             continue;
         }
         $alt_num = false;
         foreach ($env as $id => $alt) {
             if ($exp->get_id() == $id) {
                 $alt_num = $alt;
                 break;
             }
         }
         if ($alt_num !== false) {
             if (0 == $alt_num) {
                 // We use "-1" because it's the ID we use for the "original alternative" object
                 // (which doesn't exist in AE).
                 $alt = -1;
             } else {
                 $alts = $exp->get_alternatives();
                 if ($alt_num > count($alts)) {
                     continue;
                 }
                 $alt = $alts[$alt_num - 1];
                 $alt = $alt->get_id();
             }
             $relevant_products[$exp->get_originals_id()] = $alt;
             break;
         }
     }
     // Once we have the list of PRODUCT_ID => ACTUAL_PRODUCT_ID, we need to
     // include all the other relevant product IDs:
     foreach ($relevant_conversion_actions as $action) {
         $product_id = $action->get_product_id();
         if (!isset($relevant_products[$product_id])) {
             $relevant_products[$product_id] = $product_id;
         }
     }
     // Now we need to get information about the global experiments:
     $css_alt = false;
     $menu_alt = false;
     $theme_alt = false;
     $widget_alt = false;
     foreach ($relevant_experiments as $exp) {
         foreach ($env as $id => $alt_num) {
             if ($exp->get_id() != $id) {
                 continue;
             }
             $alt = false;
             switch ($exp->get_type()) {
                 case NelioABExperiment::CSS_ALT_EXP:
                 case NelioABExperiment::MENU_ALT_EXP:
                 case NelioABExperiment::THEME_ALT_EXP:
                 case NelioABExperiment::WIDGET_ALT_EXP:
                     if (0 == $alt_num) {
                         $alt = $exp->get_original();
                     } else {
                         $alts = $exp->get_alternatives();
                         if ($alt_num > count($alts)) {
                             continue;
                         }
                         $alt = $alts[$alt_num - 1];
                     }
                     $alt = $alt->get_id();
                     break;
             }
             if ($alt) {
                 switch ($exp->get_type()) {
                     case NelioABExperiment::CSS_ALT_EXP:
                         $css_alt = $alt;
                         break;
                     case NelioABExperiment::MENU_ALT_EXP:
                         $menu_alt = $alt;
                         break;
                     case NelioABExperiment::THEME_ALT_EXP:
                         $theme_alt = $alt;
                         break;
                     case NelioABExperiment::WIDGET_ALT_EXP:
                         $widget_alt = $alt;
                         break;
                 }
             }
         }
     }
     // Finally, we can create the result object
     $result = array('products' => array());
     foreach ($relevant_products as $product_id => $actual_product_id) {
         $product_found = false;
         foreach ($items as $item) {
             if (isset($item['product_id']) && $item['product_id'] == $product_id) {
                 $product_found = true;
                 break;
             }
         }
         if ($product_found) {
             array_push($result['products'], $product_id . ':' . $actual_product_id);
         }
     }
     if (count($result['products']) > 0) {
         $result['kind'] = 'OrderComplete';
         $result['products'] = implode(',', $result['products']);
         $result['user'] = $user;
         if ($css_alt) {
             $result['activeCSS'] = $css_alt;
         }
         if ($menu_alt) {
             $result['activeMenu'] = $menu_alt;
         }
         if ($theme_alt) {
             $result['activeTheme'] = $theme_alt;
         }
         if ($widget_alt) {
             $result['activeWidget'] = $widget_alt;
         }
         $url = sprintf(NELIOAB_BACKEND_URL . '/site/%s/productevent', NelioABAccountSettings::get_site_id());
         $data = NelioABBackend::build_json_object_with_credentials($result);
         $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.
             }
         }
     }
 }
        protected function do_render()
        {
            echo "<div class='nelio-message'>";
            printf('<img class="animated flipInY" src="%s" alt="%s" />', nelioab_admin_asset_link('/images/settings-icon.png'), __('Information Notice', 'nelioab'));
            $tac_text = '';
            if (NelioABAccountSettings::can_free_trial_be_started()) {
                echo '<h2>' . __('Welcome!', 'nelioab') . '</h2>';
                printf("<p class=\"nelio-admin-explanation\">%s</p>\n", __('Thank you very much for installing <strong>Nelio A/B Testing</strong> by <em>Nelio Software</em>. You\'re just one step away from optimizing your WordPress site.', 'nelioab'));
                printf("<p class=\"nelio-admin-explanation\"><strong>%s</strong></p>\n", __('Let\'s get started!', 'nelioab'));
                $account_url = admin_url('admin.php?page=nelioab-account&nabmode=my-account');
                $my_account_button = $this->make_button(__('Use Nelio Account', 'nelioab'), $account_url, false);
                $free_trial_button = $this->make_button(__('Start Free Trial', 'nelioab'), '#', true);
                $tac_text = sprintf(__('By starting the free trial you agree to be legally bound by these <a href="%s" target="_blank">terms</a>.', 'nelioab'), 'https://nelioabtesting.com/terms-and-conditions/');
            } else {
                if (!NelioABAccountSettings::is_email_valid() || !NelioABAccountSettings::is_reg_num_valid() || !NelioABAccountSettings::are_terms_and_conditions_accepted()) {
                    echo '<h2>' . __('Welcome!', 'nelioab') . '</h2>';
                    printf("<p class=\"nelio-admin-explanation\">%s</p>\n", __('Thank you very much for installing <strong>Nelio A/B Testing</strong> by <em>Nelio Software</em>. You\'re just one step away from optimizing your WordPress site.', 'nelioab'));
                    printf("<p class=\"nelio-admin-explanation\"><strong>%s</strong></p>\n", __('Let\'s get started!', 'nelioab'));
                    $account_url = admin_url('admin.php?page=nelioab-account&nabmode=my-account');
                    $my_account_button = $this->make_button(__('Use Nelio Account', 'nelioab'), $account_url, true);
                    $free_trial_button = '';
                } else {
                    echo '<h2>' . __('Setup', 'nelioab') . '</h2>';
                    printf("<p class=\"nelio-admin-explanation\">%s</p>\n", __('You\'re just one step away from optimizing WordPress with <strong style="white-space:nowrap;">Nelio A/B Testing</strong> by <em>Nelio Software</em>. Are you ready?', 'nelioab'));
                    printf("<p class=\"nelio-admin-explanation\"><strong>%s</strong></p>\n", __('Activate this site in your account.', 'nelioab'));
                    $account_url = admin_url('admin.php?page=nelioab-account&nabmode=my-account');
                    $my_account_button = $this->make_button(__('Open My Account', 'nelioab'), $account_url, true);
                    $free_trial_button = '';
                }
            }
            printf("<p id=\"nelio-cta-buttons\" class=\"nelio-admin-explanation\">%s %s</p>\n", $my_account_button, $free_trial_button);
            if (strlen($tac_text) > 0) {
                echo '<p style="padding-top:3em;font-size:95%;color:gray;">' . $tac_text . '</p>';
            }
            if (NelioABAccountSettings::can_free_trial_be_started()) {
                ?>
				<script type="text/javascript">
				(function($) {
					$('#nelio-cta-buttons .button-primary').click(function() {
						smoothTransitions();
						$.ajax({
							url: ajaxurl,
							data: {
								action: 'nelioab_start_free_trial'
							},
							type: 'post',
							success: function(res) {
								if ( "OK" === res ) {
									window.location = "<?php 
                echo admin_url('admin.php?page=nelioab-account&nabmode=free-trial');
                ?>
";
								} else {
									window.location.reload();
								}
							},
						});
					});
				})(jQuery);
				</script>
				<?php 
            }
            echo '</div>';
        }
        protected function print_experiment_information($exp, $descr, $res)
        {
            ?>
			<div id="exp-info-header">
				<?php 
            echo $this->get_experiment_icon($exp);
            ?>
				<span class="exp-title"><?php 
            echo $exp->get_name();
            ?>
</span>
			</div>

			<?php 
            $startDate = NelioABFormatter::format_date($exp->get_start_date());
            $end = $exp->get_end_date();
            if (empty($end)) {
                $running = __('Started on', 'nelioab') . ' ' . $startDate;
            } else {
                $endDate = NelioABFormatter::format_date($end);
                $running = $startDate . '&mdash;' . $endDate;
            }
            if ($res == null && $exp->get_status() == NelioABExperiment::STATUS_FINISHED) {
                $duration = NelioABFormatter::get_timelapse($exp->get_start_date(), $exp->get_end_date());
            } else {
                if ($res == null) {
                    $duration = __('Not available', 'nelioab');
                } else {
                    $duration = NelioABFormatter::get_timelapse($exp->get_start_date(), $res->get_last_update());
                }
            }
            ?>

			<div id="exp-info-running-time">
				<span class="exp-info-header"><?php 
            _e('Duration', 'nelioab');
            ?>
</span>
				<span class="exp-info-duration"><?php 
            echo $duration;
            ?>
</span>
				<span class="exp-info-running-values"><?php 
            echo $running;
            ?>
</span>
			</div>

			<?php 
            $end_mode = __('The experiment can only be stopped manually', 'nelioab');
            if ($exp->get_status() == NelioABExperiment::STATUS_RUNNING && NelioABAccountSettings::get_subscription_plan() >= NelioABAccountSettings::ENTERPRISE_SUBSCRIPTION_PLAN) {
                switch ($exp->get_finalization_mode()) {
                    case NelioABExperiment::FINALIZATION_MANUAL:
                        $end_mode = __('The experiment can only be stopped manually', 'nelioab');
                        break;
                    case NelioABExperiment::FINALIZATION_AFTER_DATE:
                        $raw_fin_value = $exp->get_finalization_value();
                        $fin_value = __('24 hours', 'nelioab');
                        if ($raw_fin_value >= 2) {
                            $fin_value = __('48 hours', 'nelioab');
                        }
                        if ($raw_fin_value >= 5) {
                            $fin_value = __('5 days', 'nelioab');
                        }
                        if ($raw_fin_value >= 7) {
                            $fin_value = __('1 week', 'nelioab');
                        }
                        if ($raw_fin_value >= 14) {
                            $fin_value = __('2 weeks', 'nelioab');
                        }
                        if ($raw_fin_value >= 30) {
                            $fin_value = __('1 month', 'nelioab');
                        }
                        if ($raw_fin_value >= 60) {
                            $fin_value = __('2 months', 'nelioab');
                        }
                        $end_mode = sprintf(__('The experiment will be automatically stopped %s after it was started.', 'nelioab'), $fin_value);
                        break;
                    case NelioABExperiment::FINALIZATION_AFTER_VIEWS:
                        $end_mode = sprintf(__('The experiment will be automatically stopped when the tested page (along with its alternatives) has been seen over %s times.', 'nelioab'), $exp->get_finalization_value());
                        break;
                    case NelioABExperiment::FINALIZATION_AFTER_CONFIDENCE:
                        $end_mode = sprintf(__('The experiment will be automatically stopped when confidence reaches %s%%.', 'nelioab'), $exp->get_finalization_value());
                        break;
                }
            }
            ?>

			<div id="exp-info-end-mode">
				<span><?php 
            _e('Finalization Mode', 'nelioab');
            ?>
</span>
				<span class="exp-end-mode"><?php 
            echo $end_mode;
            ?>
</span>
			</div>

			<?php 
            if (empty($descr)) {
                $descr = __('No description provided', 'nelioab');
            }
            ?>

			<div id="exp-info-descr">
				<span><?php 
            _e('Description', 'nelioab');
            ?>
</span>
				<span><?php 
            echo $descr;
            ?>
</span>
			</div>

		<?php 
        }
        /**
         * PHPDOC
         *
         * @param array $field PHPDOC
         *
         * @return void
         *
         * @since PHPDOC
         */
        protected function make_field($field)
        {
            $field_name = $field['label'];
            $field_id = $field['id'];
            $callback = $field['callback'];
            $is_mandatory = false;
            if (isset($field['mandatory']) && $field['mandatory']) {
                $is_mandatory = true;
            }
            $can_be_used = true;
            $explanation = false;
            if (isset($field['min_plan'])) {
                if (NelioABAccountSettings::get_subscription_plan() < $field['min_plan']) {
                    $can_be_used = false;
                }
                switch ($field['min_plan']) {
                    case NelioABAccountSettings::PROFESSIONAL_SUBSCRIPTION_PLAN:
                        $explanation = __('This option is only available for users subscribed to our Professional Plan.', 'nelioab');
                        break;
                    case NelioABAccountSettings::ENTERPRISE_SUBSCRIPTION_PLAN:
                        $explanation = __('This option is only available for users subscribed to our Enterprise Plan.', 'nelioab');
                        break;
                }
            }
            $error = '';
            if ($this->is_invalid($field_id)) {
                $error = ' class="error"';
            }
            ?>
			<table <?php 
            if ($can_be_used) {
                echo 'class="form-table"';
            } else {
                echo 'class="form-table setting-disabled"';
                if ($explanation) {
                    echo ' title="' . $explanation . '"';
                }
            }
            ?>
>
				<tr valign="top">
					<th scope="row"<?php 
            echo $error;
            ?>
>
						<?php 
            if ($is_mandatory) {
                ?>
							<label class="mandatory" for="<?php 
                echo $field_id;
                ?>
"><?php 
                echo $field_name;
                ?>
</label>
						<?php 
            } else {
                ?>
							<label for="<?php 
                echo $field_id;
                ?>
"><?php 
                echo $field_name;
                ?>
</label>
						<?php 
            }
            ?>
					</th>
					<td class="input_for_<?php 
            echo $field_id;
            ?>
">
					<?php 
            call_user_func($callback);
            ?>
					</td>
					<?php 
            if (!$can_be_used) {
                ?>
					<script type="text/javascript">(function($) {
						var selector = "" +
						  "td.input_for_<?php 
                echo $field_id;
                ?>
 input,"+
						  "td.input_for_<?php 
                echo $field_id;
                ?>
 select,"+
						  "td.input_for_<?php 
                echo $field_id;
                ?>
 textarea";
						$(selector).attr('disabled','disabled');
					})(jQuery);</script><?php 
            }
            ?>
				</tr>
			</table>
		<?php 
        }
/**
 * Remind users that they have to clean their cache after an update
 *
 * @return void
 *
 * @since PHPDOC
 */
function nelioab_add_cache_notice()
{
    global $pagenow;
    if ('plugins.php' == $pagenow || 'update.php' == $pagenow) {
        return;
    }
    try {
        NelioABAccountSettings::check_user_settings();
    } catch (Exception $e) {
        return;
    }
    $message = sprintf(__('You\'ve recently upgraded to <strong>Nelio A/B Testing %s</strong>. <strong>If you\'re running a cache system</strong> (such as <em>W3 Total Cache</em> or <em>WP Super Cache</em>) <strong>or if your server is behind a CDN</strong>, please <strong>clean all your caches</strong>. Otherwise, you may serve old versions of our tracking scripts and, therefore, the plugin may not work properly.', 'nelioab'), NELIOAB_PLUGIN_VERSION);
    ?>
	<div class="updated">
		<p>
			<?php 
    echo $message;
    ?>
			<a id="dismiss-nelioab-cache-notice" style="font-size:80%;" href="#"><?php 
    _e('Dismiss');
    ?>
</a>
		</p>
		<script style="display:none;" type="text/javascript">
		(function($) {
			$('a#dismiss-nelioab-cache-notice').on('click', function() {
				$.post( ajaxurl, {action:'nelioab_dismiss_cache_notice'} );
				$(this).parent().parent().fadeOut();
			});
		})(jQuery);
		</script>
	</div>
	<?php 
}
Exemple #14
0
 /**
  * PHPDOC
  *
  * @return array PHPDOC
  *
  * @since PHPDOC
  */
 public static function make_credential()
 {
     // Creating the credential
     $result = array();
     $aux = NelioABAccountSettings::get_customer_id();
     if ($aux) {
         $result['customerId'] = $aux;
     }
     $aux = NelioABAccountSettings::get_reg_num();
     if ($aux) {
         $result['registrationNumber'] = $aux;
     }
     $aux = NelioABAccountSettings::get_site_id();
     if ($aux) {
         $result['siteId'] = $aux;
     }
     $result['siteUrl'] = get_option('siteurl');
     return $result;
 }
 public function save()
 {
     // 1. UPDATE OR CREATE THE EXPERIMENT
     if ($this->get_id() < 0) {
         $url = sprintf(NELIOAB_BACKEND_URL . '/site/%s/exp/global', NelioABAccountSettings::get_site_id());
     } else {
         $url = sprintf(NELIOAB_BACKEND_URL . '/exp/global/%s/update', $this->get_id());
     }
     if ($this->get_status() != NelioABExperiment::STATUS_PAUSED && $this->get_status() != NelioABExperiment::STATUS_RUNNING && $this->get_status() != NelioABExperiment::STATUS_FINISHED && $this->get_status() != NelioABExperiment::STATUS_TRASH) {
         $this->set_status($this->determine_proper_status());
     }
     $body = array('name' => $this->get_name(), 'description' => $this->get_description(), 'origin' => $this->get_origins(), 'status' => $this->get_status(), 'kind' => $this->get_textual_type(), 'finalizationMode' => $this->get_finalization_mode(), 'finalizationModeValue' => $this->get_finalization_value());
     /** @var array $result */
     $result = NelioABBackend::remote_post($url, $body);
     $exp_id = $this->get_id();
     if ($exp_id < 0) {
         if (is_wp_error($result)) {
             return 0;
         }
         $json = json_decode($result['body']);
         $exp_id = $json->key->id;
         $this->id = $exp_id;
     }
     // 1.1 SAVE GOALS
     // -------------------------------------------------------------------------
     $this->make_goals_persistent();
     return $this->get_id();
 }
        /**
         * Returns the heading of a certain box type.
         *
         * This heading contains the title of the box and a beautiful icon.
         *
         * @param string $type the type of the heading we want to create.
         *
         * @return string the heading of a certain box type.
         *
         * @since 4.1.3
         */
        protected function get_action_heading($type)
        {
            switch ($type) {
                case 'basic-info':
                    $icon = nelioab_admin_asset_link('/images/freetrial-mail.png');
                    $aux = __('Your Basic Information (%s)', 'nelioab');
                    $alt = __('You and Your E-Mail', 'nelioab');
                    $num = 100;
                    break;
                case 'tweet':
                    $icon = nelioab_admin_asset_link('/images/freetrial-twitter.png');
                    $aux = __('Tweet About Us (%s)', 'nelioab');
                    $alt = __('Twitter', 'nelioab');
                    $num = 200;
                    break;
                case 'site-info':
                    $icon = nelioab_admin_asset_link('/images/freetrial-site.png');
                    $aux = __('Information About Your Site (%s)', 'nelioab');
                    $alt = __('Tiny form about your site', 'nelioab');
                    $num = 100;
                    break;
                case 'connect':
                    $icon = nelioab_admin_asset_link('/images/freetrial-facebook.png');
                    $aux = __('Like Our Facebook Profile (%s)', 'nelioab');
                    $alt = __('Facebook', 'nelioab');
                    $num = 200;
                    break;
                case 'recommend':
                    $icon = nelioab_admin_asset_link('/images/freetrial-friends.png');
                    $aux = __('Recommend Us to Your Friends (%s)', 'nelioab');
                    $alt = __('Friends', 'nelioab');
                    $num = 100;
                    break;
                case 'goals':
                    $icon = nelioab_admin_asset_link('/images/freetrial-life-goals.png');
                    $aux = __('The Goals You Pursue (%s)', 'nelioab');
                    $alt = __('Goals', 'nelioab');
                    $num = 200;
                    break;
                case 'subscribe':
                    $icon = nelioab_admin_asset_link('/images/freetrial-subscribe.png');
                    $aux = __('Subscribe to Nelio A/B Testing (%s)', 'nelioab');
                    $alt = __('Subscribe', 'nelioab');
                    $num = 5000;
                    break;
            }
            $num = '+' . number_format_i18n($num);
            $title = sprintf($aux, $num);
            $completed_title = sprintf($aux, __('Done!', 'nelioab'));
            $completed_icon = nelioab_admin_asset_link('/images/action-completed.png');
            // Workaround for BASIC INFO (which might use Confirmation...)
            if ('basic-info' === $type) {
                if (NelioABAccountSettings::is_promo_completed('basic-info')) {
                    $icon = nelioab_admin_asset_link('/images/action-pending.png');
                } else {
                    $completed_icon = nelioab_admin_asset_link('/images/action-pending.png');
                }
                $type = 'basic-info-check';
                $completed_title = sprintf($aux, __('Awaiting Confirmation...', 'nelioab'));
            }
            // End of the workaround
            if (NelioABAccountSettings::is_promo_completed($type)) {
                $animation = '';
            } else {
                $animation = 'animated flipInY';
            }
            $html = <<<HTML
\t\t\t<span class="nelio-freetrial-heading regular">
\t\t\t\t<img width="32" height="32" src="{$icon}" alt="{$alt}"/>
\t\t\t\t<span>{$title}</span>
\t\t\t</span>
\t\t\t<span class="nelio-freetrial-heading completed">
\t\t\t\t<img class="{$animation}" width="32" height="32" src="{$completed_icon}" alt="{$alt}"/>
\t\t\t\t<span>{$completed_title}</span>
\t\t\t</span>
HTML;
            return $html;
        }
 public function save()
 {
     // 1. UPDATE OR CREATE THE EXPERIMENT
     // -------------------------------------------------------------------------
     if ($this->get_id() < 0) {
         $url = sprintf(NELIOAB_BACKEND_URL . '/site/%s/exp/post', NelioABAccountSettings::get_site_id());
     } else {
         $url = sprintf(NELIOAB_BACKEND_URL . '/exp/post/%s/update', $this->get_id());
     }
     if ($this->get_status() != NelioABExperiment::STATUS_PAUSED && $this->get_status() != NelioABExperiment::STATUS_RUNNING && $this->get_status() != NelioABExperiment::STATUS_FINISHED && $this->get_status() != NelioABExperiment::STATUS_TRASH) {
         $this->set_status($this->determine_proper_status());
     }
     $body = array('name' => $this->get_name(), 'description' => $this->get_description(), 'originalPost' => $this->get_originals_id(), 'postType' => $this->get_post_type(), 'status' => $this->get_status(), 'kind' => $this->get_textual_type(), 'showHeatmap' => $this->are_heatmaps_tracked(), 'finalizationMode' => $this->get_finalization_mode(), 'finalizationModeValue' => $this->get_finalization_value());
     $result = NelioABBackend::remote_post($url, $body);
     $exp_id = $this->get_id();
     if ($exp_id < 0) {
         if (is_wp_error($result)) {
             return;
         }
         $json = json_decode($result['body']);
         $exp_id = $json->key->id;
         $this->id = $exp_id;
     }
     // 1.1 SAVE GOALS
     // -------------------------------------------------------------------------
     $this->make_goals_persistent();
     // 2. UPDATE THE ALTERNATIVES
     // -------------------------------------------------------------------------
     // 2.1. UPDATE CHANGES ON ALREADY EXISTING APPSPOT ALTERNATIVES
     foreach ($this->get_appspot_alternatives() as $alt) {
         /** @var NelioABAlternative $alt */
         if ($alt->was_removed() || !$alt->is_dirty()) {
             continue;
         }
         $body = array('name' => $alt->get_name(), 'value' => $alt->get_value());
         NelioABBackend::remote_post(sprintf(NELIOAB_BACKEND_URL . '/alternative/%s/update', $alt->get_id()), $body);
     }
     // 2.2. REMOVE FROM APPSPOT THE REMOVED ALTERNATIVES
     foreach ($this->get_appspot_alternatives() as $alt) {
         if (!$alt->was_removed()) {
             continue;
         }
         $url = sprintf(NELIOAB_BACKEND_URL . '/alternative/%s/delete', $alt->get_id());
         NelioABBackend::remote_post($url);
     }
     // 2.3. CREATE LOCAL ALTERNATIVES IN APPSPOT
     /** @var array $ori_post */
     $ori_post = get_post($this->get_originals_id(), ARRAY_A);
     foreach ($this->get_local_alternatives() as $alt) {
         if ($alt->was_removed()) {
             continue;
         }
         if ($this->get_type() != NelioABExperiment::HEADLINE_ALT_EXP) {
             if ($alt->is_based_on_another_element()) {
                 $new_id = $this->create_alternative_copying_content($alt->get_name(), $alt->get_base_element(), $ori_post);
                 if ($new_id) {
                     $alt->set_value($new_id);
                 } else {
                     continue;
                 }
             } else {
                 $new_id = $this->create_empty_alternative($alt->get_name(), $this->get_type(), $this->get_post_type());
                 if ($new_id) {
                     $alt->set_value($new_id);
                 } else {
                     continue;
                 }
             }
         }
         $body = array('name' => $alt->get_name(), 'value' => $alt->get_value(), 'kind' => NelioABExperiment::get_textual_type());
         try {
             /** @var int $result */
             $result = NelioABBackend::remote_post(sprintf(NELIOAB_BACKEND_URL . '/exp/post/%s/alternative', $exp_id), $body);
             $alt->set_id($result);
         } catch (Exception $e) {
             // If I could not add an alternative... remove the associated page
             wp_delete_post($alt->get_value());
         }
     }
     // 2.3 REMOVE THE PAGES THAT BELONGED TO AN ALTERNATIVE THAT HAS BEEN DELETED
     $all_alternatives = array_merge($this->get_appspot_alternatives(), $this->get_local_alternatives());
     foreach ($all_alternatives as $alt) {
         if ($alt->was_removed()) {
             // Delete permanently (skipping Trash)
             if ($alt->get_value()) {
                 wp_delete_post($alt->get_value(), true);
             }
         }
     }
     // 2.4 SET META "_is_nelioab_alternative" WITH THE ID OF THE EXPERIMENT
     foreach ($this->get_alternatives() as $alt) {
         $pid = $alt->get_value();
         if (is_int($pid) && $pid > 0) {
             $value = $this->get_id() . ',' . $this->get_status();
             update_post_meta($pid, "_is_nelioab_alternative", $value);
         }
     }
     require_once NELIOAB_MODELS_DIR . '/experiments-manager.php';
     NelioABExperimentsManager::update_experiment($this);
 }
 public static function manage_site_registration()
 {
     global $nelioab_admin_controller;
     if (isset($_POST['nelioab_registration_action'])) {
         $action = $_POST['nelioab_registration_action'];
         try {
             switch ($action) {
                 case 'register':
                     $type = 'unknown';
                     if (isset($_POST['nelioab_registration_type'])) {
                         $type = $_POST['nelioab_registration_type'];
                     }
                     $sector = 'unknown';
                     if (isset($_POST['nelioab_registration_sector'])) {
                         $sector = $_POST['nelioab_registration_sector'];
                     }
                     NelioABAccountSettings::register_this_site($type, $sector);
                     $nelioab_admin_controller->message = __('This site has been successfully activated in your account.', 'nelioab');
                     break;
                 case 'deregister':
                     NelioABAccountSettings::deregister_this_site();
                     $nelioab_admin_controller->message = __('This site is no longer active in your account.', 'nelioab');
                 case 'unlink':
                     NelioABAccountSettings::unlink_this_site();
                     $nelioab_admin_controller->message = __('The site is no longer linked to any of your other active sites. If you have free slots, you may now activate it as a completely different and new site.', 'nelioab');
             }
         } catch (Exception $e) {
             require_once NELIOAB_ADMIN_DIR . '/error-controller.php';
             NelioABErrorController::build($e);
         }
     }
 }
 private function print_table_of_registered_sites()
 {
     $sites = array();
     $other_sites = array();
     $this_url = get_option('siteurl');
     $this_id = NelioABAccountSettings::get_site_id();
     $reg_name = false;
     foreach ($this->sites as $site) {
         if ($site->get_id() != $this_id || $this->current_site_status != NelioABSite::ACTIVE) {
             array_push($other_sites, $site);
         } else {
             $reg_name = $site->get_url();
         }
     }
     if ($this->current_site_status == NelioABSite::ACTIVE) {
         $live = false === $reg_name ? 'live' : 'staging';
         array_push($sites, array('this_site' => true, 'name' => $this_url, 'reg_name' => $reg_name, 'live' => $live));
     }
     foreach ($other_sites as $site) {
         array_push($sites, array('this_site' => false, 'name' => $site->get_url(), 'goto_link' => $site->get_url()));
     }
     for ($i = count($sites); $i < $this->max_sites; ++$i) {
         array_push($sites, array('this_site' => false, 'name' => __('Empty Slot', 'nelioab')));
     }
     echo '<div id="registered-sites-table" style="margin-left:2em;">';
     $aux = new NelioABRegisteredSitesTable($sites);
     $aux->prepare_items();
     $aux->display();
     echo '</div>';
 }
 public function save()
 {
     // 1. UPDATE OR CREATE THE EXPERIMENT
     // -------------------------------------------------------------------------
     /** @var string $url */
     if ($this->get_id() < 0) {
         $url = sprintf(NELIOAB_BACKEND_URL . '/site/%s/exp/hm', NelioABAccountSettings::get_site_id());
     } else {
         $url = sprintf(NELIOAB_BACKEND_URL . '/exp/hm/%s/update', $this->get_id());
     }
     if ($this->get_status() != NelioABExperiment::STATUS_PAUSED && $this->get_status() != NelioABExperiment::STATUS_RUNNING && $this->get_status() != NelioABExperiment::STATUS_FINISHED && $this->get_status() != NelioABExperiment::STATUS_TRASH) {
         $this->set_status($this->determine_proper_status());
     }
     $body = array('name' => $this->get_name(), 'description' => $this->get_description(), 'post' => $this->get_post_id(), 'status' => $this->get_status(), 'kind' => $this->get_textual_type(), 'finalizationMode' => $this->get_finalization_mode(), 'finalizationModeValue' => $this->get_finalization_value());
     $result = NelioABBackend::remote_post($url, $body);
     $exp_id = $this->get_id();
     if ($exp_id < 0) {
         if (is_wp_error($result)) {
             return;
         }
         $json = json_decode($result['body']);
         $exp_id = $json->key->id;
         $this->id = $exp_id;
     }
     require_once NELIOAB_MODELS_DIR . '/experiments-manager.php';
     NelioABExperimentsManager::update_experiment($this);
 }
 /**
  * This function creates all the relevant pages for our plugin.
  * These pages appear in the Dashboard.
  *
  * @since 0.1
  */
 public function create_nelioab_admin_pages()
 {
     $nelioab_menu = 'nelioab-dashboard';
     // Main menu
     // ----------------------------------------------------------------------
     add_menu_page(__('Nelio A/B Testing', 'nelioab'), __('Nelio A/B Testing', 'nelioab'), 'manage_options', $nelioab_menu, null, null, NelioABSettings::get_menu_location() . '.000023510');
     // Dashboard page
     // ----------------------------------------------------------------------
     require_once NELIOAB_ADMIN_DIR . '/dashboard-page-controller.php';
     add_submenu_page($nelioab_menu, __('Dashboard', 'nelioab'), __('Dashboard', 'nelioab'), 'manage_options', 'nelioab-dashboard', array('NelioABDashboardPageController', 'build'));
     // Experiments pages (depending on the action, we show one or another)
     // ----------------------------------------------------------------------
     $the_action = NULL;
     if (isset($_GET['action'])) {
         $the_action = $_GET['action'];
     }
     switch ($the_action) {
         case 'edit':
             require_once NELIOAB_ADMIN_DIR . '/select-exp-edition-page-controller.php';
             $page_to_build = array('NelioABSelectExpEditionPageController', 'build');
             break;
         case 'progress':
             require_once NELIOAB_ADMIN_DIR . '/select-exp-progress-page-controller.php';
             $page_to_build = array('NelioABSelectExpProgressPageController', 'build');
             break;
         default:
             require_once NELIOAB_ADMIN_DIR . '/experiments-page-controller.php';
             $page_to_build = array('NelioABExperimentsPageController', 'build');
             break;
     }
     add_submenu_page($nelioab_menu, __('Experiments', 'nelioab'), __('Experiments', 'nelioab'), 'manage_options', 'nelioab-experiments', $page_to_build);
     // Creating Experiment; (depending on the type, we show one form or another)
     // ----------------------------------------------------------------------
     require_once NELIOAB_ADMIN_DIR . '/select-exp-creation-page-controller.php';
     add_action('admin_head', array($this, 'add_css_for_creation_page'));
     add_action('admin_head', array($this, 'add_css_for_themes'));
     $page_to_build = array('NelioABSelectExpCreationPageController', 'build');
     add_submenu_page($nelioab_menu, __('Add Experiment', 'nelioab'), __('Add Experiment', 'nelioab'), 'manage_options', 'nelioab-add-experiment', $page_to_build);
     // Either Free Trial or My Account page
     // ----------------------------------------------------------------------
     if (NelioABAccountSettings::is_using_free_trial()) {
         $label = __('Free Trial', 'nelioab');
     } else {
         $label = __('My Account', 'nelioab');
     }
     require_once NELIOAB_ADMIN_DIR . '/account-page-controller.php';
     add_submenu_page($nelioab_menu, $label, $label, 'manage_options', 'nelioab-account', array('NelioABAccountPageController', 'build'));
     // Settings page
     // ----------------------------------------------------------------------
     require_once NELIOAB_ADMIN_DIR . '/settings-page-controller.php';
     add_submenu_page($nelioab_menu, __('Settings', 'nelioab'), __('Settings', 'nelioab'), 'manage_options', 'nelioab-settings', array('NelioABSettingsPageController', 'build'));
     // Help
     // ----------------------------------------------------------------------
     add_submenu_page($nelioab_menu, __('Help', 'nelioab'), __('Help', 'nelioab'), 'manage_options', 'nelioab-help');
     global $submenu;
     for ($i = 0; $i < count($submenu['nelioab-dashboard']); ++$i) {
         if ('nelioab-help' == $submenu['nelioab-dashboard'][$i][2]) {
             $submenu['nelioab-dashboard'][$i][2] = 'http://support.nelioabtesting.com/support/home';
             break;
         }
     }
     // OTHER PAGES (not included in the menu)
     // CSS Editing
     // ----------------------------------------------------------------------
     require_once NELIOAB_ADMIN_DIR . '/views/content/css-edit.php';
     add_submenu_page(NULL, __('CSS Edit', 'nelioab'), __('CSS Edit', 'nelioab'), 'manage_options', 'nelioab-css-edit', array('NelioABCssEditPage', 'build'));
 }
 /**
  * This function registers Nelio tracking scripts.
  *
  * Our tracking scripts are:
  *
  * * `nelioab_appengine_script`: a script pointing to Google Cloud Storage,
  * with all the information about running experiments.
  * * `nelioab_tracking_script`: our tracking script.
  *
  * It also initializes some params that will be used by our tracking
  * script. They'll be available by means of the object `NelioABParams`.
  *
  * @return void
  *
  * @see self::load_tracking_script
  *
  * @since 3.3.2
  */
 public function register_tracking_script()
 {
     wp_register_script('nelioab_appengine_script', '//storage.googleapis.com/' . NELIOAB_BACKEND_NAME . '/' . NelioABAccountSettings::get_site_id() . '.js', array(), NELIOAB_PLUGIN_VERSION);
     wp_register_script('nelioab_tracking_script', nelioab_asset_link('/js/tracking.min.js', false), array('jquery', 'nelioab_appengine_script'), NELIOAB_PLUGIN_VERSION);
     // Prepare some information for our tracking script (such as the page we're in)
     /** @var $aux NelioABAlternativeExperimentController */
     $aux = $this->controllers['alt-exp'];
     $current_id = $this->get_queried_post_id();
     if ($aux->is_post_in_a_post_alt_exp($current_id)) {
         $current_actual_id = intval($aux->get_post_alternative($current_id));
     } elseif ($aux->is_post_in_a_headline_alt_exp($current_id)) {
         $headline_data = $aux->get_headline_experiment_and_alternative($current_id);
         /** @var NelioABAlternative $alternative */
         $alternative = $headline_data['alt'];
         $val = $alternative->get_value();
         $current_actual_id = $val['id'];
     } else {
         $current_actual_id = $current_id;
     }
     $current_page_ids = array('currentId' => $current_id, 'currentActualId' => $current_actual_id);
     // OUTWARDS NAVIGATIONS USING TARGET="_BLANK"
     $misc['useOutwardsNavigationsBlank'] = NelioABSettings::use_outwards_navigations_blank();
     $this->tracking_script_params = array('ajaxurl' => admin_url('admin-ajax.php', is_ssl() ? 'https' : 'http'), 'version' => NELIOAB_PLUGIN_VERSION, 'customer' => NelioABAccountSettings::get_customer_id(), 'site' => NelioABAccountSettings::get_site_id(), 'backend' => array('domain' => NELIOAB_BACKEND_DOMAIN, 'version' => NELIOAB_BACKEND_VERSION), 'misc' => $misc, 'sync' => array('headlines' => array()), 'info' => $current_page_ids, 'ieUrl' => preg_replace('/^https?:/', '', NELIOAB_URL . '/ajax/iesupport.php'), 'wasPostRequest' => 'POST' === $_SERVER['REQUEST_METHOD']);
 }