/** * 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. } } }
/** * 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. } } } }
/** * This function starts the free trial period. * * It's an AJAX callback for the action `nelioab_start_free_trial`. * * @return void * * @since 4.1.3 */ public static function start_free_trial() { if (self::can_free_trial_be_started()) { $url = NELIOAB_BACKEND_URL . '/customer/trial/activate'; try { $params = array('body' => array('url' => get_option('siteurl'), 'version' => self::get_plugin_version_for_sync())); $json_data = NelioABBackend::remote_post_raw($url, $params, true); $json_data = json_decode($json_data['body']); self::update_nelioab_option('email', $json_data->mail); self::update_nelioab_option('reg_num', $json_data->registrationNumber); self::update_nelioab_option('customer_id', $json_data->key->id); self::update_nelioab_option('is_email_valid', true); self::update_nelioab_option('is_reg_num_valid', true); self::update_nelioab_option('uses_free_trial', true); self::update_nelioab_option('free_trial_code', $json_data->key->id); $site = $json_data->sites[0]; self::set_site_id($site->id); self::update_nelioab_option('has_a_configured_site', true); update_option('__nelio_ab_used_free_trial', true); echo 'OK'; } catch (Exception $e) { self::update_nelioab_option('customer_id', 'unknown'); self::update_nelioab_option('is_email_valid', false); self::update_nelioab_option('is_reg_num_valid', false); self::update_nelioab_option('has_a_configured_site', false); self::set_site_id('0'); } } die; }
/** * PHPDOC * * @param string $url PHPDOC * @param array $params PHPDOC * @param boolean $skip_status_check PHPDOC * * @return WP_Error|array The response or WP_Error on failure. * * @throws Exception with the appropriate error code. * * @since PHPDOC */ public static function remote_post($url, $params = array(), $skip_status_check = false) { $json_params = NelioABBackend::build_json_object_with_credentials($params); return NelioABBackend::remote_post_raw($url, $json_params, $skip_status_check); }