/** * Record a conversion * * @since 0.4.0 * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Request */ public function conversion($request) { if (!ingot_is_bot()) { $id = $request->get_param('id'); ingot_register_conversion($id, $request->get_param('ingot_session_ID')); } return ingot_rest_response(['message' => ':)'], 200); }
/** * Check if session has been used * * @since 0.3.0 * * @param \WP_REST_Request $request Full data about the request. * * @return \WP_Error|\WP_REST_Response */ public function session($request) { $session = $this->get_session_by_url_params($request); if (ingot_is_bot()) { $data['ingot_ID'] = $session['ingot_ID']; $data['tests'] = []; $data['session_ID'] = $session['ID']; return $data; } $tests = []; if (!is_array($session)) { return $session; } else { if (\ingot\testing\crud\session::is_used($session['ID'])) { $new_session_args = []; if (0 != $request->get_param('ingot_id')) { $new_session_args['ingot_ID'] = $request->get_param('ingot_id'); } elseif (0 < absint($session['ingot_ID'])) { $new_session_args['ingot_ID'] = $session['ingot_ID']; } if (0 != get_current_user_id()) { $new_session_args['uID'] = get_current_user_id(); } $data['session_ID'] = \ingot\testing\crud\session::create($new_session_args, true); if (is_numeric($data['session_ID'])) { $session = \ingot\testing\crud\session::read((int) $data['session_ID']); if (!empty($request->get_param('test_ids'))) { foreach ($request->get_param('test_ids') as $variant_id) { $html = ''; if (is_array($group = group::get_by_variant_id($variant_id))) { $html = ingot_click_test($group); } $tests[] = ['html' => $html, 'ID' => $variant_id]; } } } } else { \ingot\testing\crud\session::mark_used($session['ID']); $data['session_ID'] = $session['ID']; } $data['ingot_ID'] = $session['ingot_ID']; $data['tests'] = $tests; return rest_ensure_response($data); } }
/** * Add hooks * * @since 0.0.5 */ public function hooks() { add_action('rest_api_init', array(__CLASS__, 'boot_rest_api')); add_action('pre_update_option', array($this, 'presave_settings'), 10, 2); if (!ingot_is_bot()) { add_action('wp_enqueue_scripts', function () { $version = INGOT_VER; $min = '.min'; if (SCRIPT_DEBUG) { $min = ''; $version = rand(); } wp_enqueue_script('ingot', INGOT_URL . "/assets/front-end/js/ingot-click-test{$min}.js", array('jquery'), $version, true); wp_localize_script('ingot', 'INGOT_UI', ingot::js_vars()); }); add_action('parse_request', array($this, 'init_session'), 50); } }
/** * Get HTML for our test. Use this function to output the test. * * Note that because this function has the same name as the test sub_type we registered, it can be called by the Ingot shortcode. * * @return string */ function my_custom_banner() { //check if is bot $is_bot = ingot_is_bot(); $variant = []; //if cookie isset get variant without registering as a new test instance if (!$is_bot && isset($_COOKIE['my_ingot_custom_test_variant']) && 0 < absint($_COOKIE['my_ingot_custom_test_variant'])) { $variant_id = absint($_COOKIE['my_ingot_custom_test_variant']); $variant = \ingot\testing\crud\variant::read($variant_id); } elseif (!$is_bot && isset($my_custom_test_variant_id) && 0 < absint($my_custom_test_variant_id)) { //if is same session as cookie was set, we use the global variable to get the ID $variant_id = absint($my_custom_test_variant_id); $variant = \ingot\testing\crud\variant::read($variant_id); } else { //this should never be reached unless this is a bot, the cookie should have been set at template_redirect //We still need to show something since we want crawlers to index the site properly and false postives on bot checks to look right. $id = my_ingot_custom_test_group(); if (is_numeric($id)) { $group = \ingot\testing\crud\group::read($id); if (is_array($group) && isset($group['variants']) && !empty($group['variants'])) { $variant = array_rand($group['variants']); //set up test and select a variant $test = new \ingot\testing\bandit\content($id); $variant_id = $test->choose(); //a test instance is now registered $variant = \ingot\testing\crud\variant::read($variant_id); } } } //get chosen image URL and return it in an image tag if (isset($variant['content'])) { $content = $variant['content']; } else { //something is very wrong, let's show a picture of a cat in a flower pot $content = 'http://hellogiggles.com/wp-content/uploads/2014/07/22/you-popular-cute-cat-the-very-creative_113506.jpg'; } return sprintf('<img src="%s" />', esc_url($content)); }
/** * Choose a variant * * @since 0.4.0 * * @return mixed */ public function choose() { $record = !ingot_is_bot(); $val = $this->bandit->chooseLever($this->experiment, $record)->getValue(); return $val; }
/** * Check if in no testing mode * * If true, no iterations or conversions are recorded and a random variant is used. * * @since 1.1.0 * * @return bool */ function ingot_is_no_testing_mode() { $is_no_testing_mode = false; if (ingot_is_bot()) { $is_no_testing_mode = true; } /** * Turn no testing mode on or of * * @since 1.1.0 * * @param bool $is_no_testing_mode */ return (bool) apply_filters('ingot_is_no_testing_mode', $is_no_testing_mode); }