public static function current_sequence($group, $what = 'click') { if (!is_array($group)) { $group = group::read($group); } return helpers::v('current_sequence', $group, array()); }
/** * Constructors this class * * @since 0.2.0 */ public function __construct() { add_action('admin_menu', array($this, 'add_menu')); if ($this->menu_slug === helpers::v('page', $_GET, 0)) { add_action('admin_enqueue_scripts', array($this, 'scripts')); } }
/** * Get variants by group ID * * @since 1.1.0 * * @param array|int $params Array with the key 'group_ID' or the group ID * * @return array */ public static function get_items($params) { if (is_numeric($params)) { $group_id = $params; } else { $group_id = helpers::v('group_ID', $params, 0); } if (0 != absint($group_id)) { $table_name = self::get_table_name(); $sql = sprintf('SELECT * FROM `%s` WHERE `group_ID` = %d', $table_name, $group_id); return self::bulk_query($sql, true); } }
/** * Validate item config * * @since 0.4.0 * * @access protected * * @param array $data Item config * * @return \WP_Error|array Item config array if valid, WP_Error if not. */ protected static function validate_config($data) { $required = static::required(); foreach ($required as $key) { if (!isset($data[$key])) { return new \WP_Error('ingot-invalid-config', __(sprintf('Groups require the field %s', $key), 'ingot'), $data); } } if (!isset($data['IP'])) { $data['IP'] = ingot_get_ip(); } if (isset($data['UTM']) && $data['UTM']) { $data['UTM'] = helpers::sanitize($data['UTM']); } $data['time'] = self::date_validation($data['time']); return $data; }
/** * Get a variant's HTML * * @since 0.4.0 * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Request */ public function get_item($request) { return $this->not_yet_response(); $context = $request->get_param('context'); $url = $request->get_url_params(); $id = helpers::v('ID', $url, 0); if (0 == absint($id) || !is_array(\ingot\testing\crud\variant::read($id))) { return new \WP_Error('ingot-invalid-test'); } elseif ('context' != 'view') { return new \WP_Error('ingot-test-context-invalid'); } else { if ('view' == $context) { $test = \ingot\testing\crud\variantP::read($id); $html = ingot_click_test($test); return ingot_rest_response($html); } } }
/** * Prepare price test details needed for use in cookies/tracking * * @since 0.2.0 * * @param array $price_test Price test config * @param string $a_or_b a|b * @param int $sequence_id Sequence ID * @param int $group_id Group ID * * @return array */ public static function price_detail($price_test, $a_or_b, $sequence_id, $group_id) { if (is_numeric($price_test)) { $price_test = price_test::read($price_test); } if (is_numeric($group_id)) { $group = group::read($group_id); } elseif (is_array($group_id)) { $group = $group_id; $group_id = helpers::v('ID', $group, 0); } else { return array(); } if (!is_array($price_test) || !is_array($group)) { return array(); } $details = array('plugin' => $group['plugin'], 'product_ID' => $price_test['product_ID'], 'test_ID' => $price_test['ID'], 'sequence_ID' => $sequence_id, 'group_ID' => $group_id, 'a_or_b' => $a_or_b); return $details; }
/** * Process multiple rows from a SQL query * * Should be result of `$wpdb->get_results( $sql, ARRAY_A );` * * @since 1.1.0 * * @param array $results * * @return array */ public static function bulk_results($results, $key_by_id = false) { if (!empty($results)) { foreach ($results as $i => $result) { $item = self::unseralize($result); if ($key_by_id) { $k = helpers::v('ID', $item, $i); } else { $k = $i; } $results[$k] = $item; } } return $results; }
/** * Test that invalid word gives us default color * * @since 0.1.1 * * @covers \ingot\testing\utility\helpers::is_color_word() */ public function testInvalidWordToHex() { $color = 'lemongrab'; $this->assertSame(\ingot\testing\utility\defaults::color(), \ingot\testing\utility\helpers::prepare_color($color, false)); }
/** * @param \WP_REST_Request $request Full data about the request. * * @return array|mixed|null|object|void */ protected function get_session_by_url_params($request) { $url = $request->get_url_params(); $id = helpers::v('id', $url, 0); if (0 != absint($id) && is_array($session = \ingot\testing\crud\session::read($id))) { return $session; } else { return new \WP_Error('ingot-invalid-session'); } }
/** * Track that test ran * * @since 0.0.9 * * @access protected * * @param array $data */ protected function increase_total($data) { flow::increase_total(helpers::v('test_ID', $data, 0), helpers::v('sequence_ID', $data, 0)); }
/** * @TODO REMOVE? * * @since 1.1.0 * * @param $destination * @param $group * * @return mixed|void */ public static function get_hook($destination, $group) { switch ($destination) { case 'hook': $hook = helpers::v('hook', $group['meta'], false); break; case 'cart_edd': $hook = 'edd_post_add_to_cart'; break; case 'sale_edd': $hook = 'edd_complete_purchase'; break; case 'cart_woo': $hook = 'woocommerce_add_to_cart'; break; case 'sale_woo': $hook = 'woocommerce_payment_complete_order_status'; break; default: $hook = null; break; } return apply_filters('ingot_destination_tracking_hook', $hook, $destination, $group); }
/** * Determine if admin scripts should be loaded * * @since 1.1.0 * * @return bool */ protected function should_load_scripts() { /** * Should we load our admin scripts or not? * * @since 1.1.0 * * @param $load bool */ return (bool) apply_filters('ingot_admin_load_scripts', $this->menu_slug === helpers::v('page', $_GET, 0)); }
/** * Inititialize Ingot session * * @uses "parse_request" * * @since 0.3.0 */ public function init_session() { $id = null; if (isset($_GET['ingot_session_ID']) && ingot_verify_session_nonce(helpers::v('ingot_session_nonce', $_GET, ''))) { $id = helpers::v('ingot_session_ID', $_GET, null); } $session = \ingot\testing\object\session::instance($id); $session_data = $session->get_session_info(); do_action('ingot_session_initialized', $session_data); }
/** * @return array */ private function current_meta() { return helpers::make_array_values_numeric(get_post_meta($this->post->ID, $this->meta_key, false)); }
/** * Inititialize Ingot session * * @uses "parse_request" * * @since 0.3.0 */ public function init_session() { if (ingot_is_front_end() && !ingot_is_no_testing_mode() && !ingot_is_admin_ajax() && !is_admin() && !ingot_is_rest_api()) { $id = null; if (isset($_GET['ingot_session_ID']) && ingot_verify_session_nonce(helpers::v('ingot_session_nonce', $_GET, ''))) { $id = helpers::v('ingot_session_ID', $_GET, null); } $session = new \ingot\testing\object\session($id); $session_data = $session->get_session_info(); /** * Fired when Ingot session is setup at parse_request * * @since 0.3.0 * * @param array $session_data has ID (session ID) and ingot_ID */ do_action('ingot_session_initialized', $session_data); $this->current_session_data = $session_data; } }
/** * Prepare the meta in group config array * * @since 1.1.0 * * @param array $group Group config * * @return array|\WP_Error */ public static function prepare_meta(array $group) { if (!isset($group['meta']) || !is_array($group['meta'])) { $group['meta'] = []; } $meta = $group['meta']; if (!isset($meta['destination']) || !types::allowed_destination_type($meta['destination'])) { return new \WP_Error('ingot-invalid-destination-type', __('Invalid destination', 'ingot'), ['destination' => helpers::v('destination', $meta, false), 'meta' => $meta]); } if ('page' == $meta['destination']) { if (!isset($meta['page'])) { return new \WP_Error('ingot-invalid-destination-page', __('Page destination types need a page ID', 'ingot')); } else { $meta['page'] = absint($meta['page']); } } if ('hook' == $meta['destination']) { if (!isset($meta['hook'])) { return new \WP_Error('ingot-invalid-destination-hook', __('Hook destination types need a hook.', 'ingot')); } else { $meta['hook'] = trim($meta['hook']); } } if (!isset($meta['is_tagline'])) { $meta['is_tagline'] = false; } $meta['is_tagline'] = (bool) $meta['is_tagline']; $group['meta'] = $meta; return $group; }
/** * Save variants and return IDs * * @since 0.4.0 * * @param array $group Group config -- variants key should be an array of variant configs to save * * @return array||WP_Error */ protected function save_variants($group) { $variants_ids = []; $variants = helpers::v('variants', $group, []); if (isset($group['ID'])) { $group_id = $group['ID']; } elseif (isset($group['id'])) { $group_id = $group['id']; } else { return new \WP_Error('ingot-generalized-failure'); } if (!empty($variants)) { foreach ($variants as $variant) { if (is_numeric($variant)) { continue; } $variant['group_ID'] = $group_id; $variant['type'] = $group['type']; if ((!isset($variant['content']) || empty($variant['content'])) && 'button_color' == $group['sub_type']) { $variant['content'] = ' '; } if (!isset($variant['ID']) || 0 == abs($variant['ID'])) { unset($variant['ID']); $_variant_id = variant::create($variant); } else { $_variant_id = variant::update($variant, $variant['ID']); } if (is_wp_error($_variant_id)) { return $_variant_id; } $variants_ids[] = $_variant_id; } } return $variants_ids; }
/** * Update groups associated with a post * * @since 1.0.0 * * @param \WP_REST_Request $request Full data about the request. * @return \WP_Error|\WP_REST_Response */ public function update_posts($request) { $url = $request->get_url_params(); $post_id = (int) helpers::v('id', $url, 0); $post = get_post($post_id); if (!is_a($post, 'WP_POST')) { return ingot_rest_response(['message' => esc_html__('No group found', 'ingot')]); } $obj = new posts($post); $obj->add($request->get_param('group_ids')); return ingot_rest_response($obj->get_groups()); }
/** * Create style tags for buttons * * @since 0.1.1 * * @acess protected * * @param $config * * @return string */ protected function make_style($config) { $color = helpers::get_color_from_meta($config); $background_color = helpers::get_background_color_from_meta($config); return sprintf('style="background-color:%s;color:%s;"', $background_color, $color); }
/** * Get link from click group config * * @since 0.4.0 * * @return string */ protected function link() { return helpers::get_link_from_meta($this->group); }
/** * Utility function to make all keys of an array integers (recursively) * * @since 0.0.6 * * @param $array * * @return array */ public function make_array_values_numeric($array) { return \ingot\testing\utility\helpers::make_array_values_numeric($array); }
/** * Ensure all tests are not expired and refresh if needed * * @since 0.0.9 * * @access protected */ protected function check_sequence_lives() { $now = time(); foreach ($this->price_cookie as $sequence_id => $test) { $expires = helpers::v('expires', $test, 0); if ($now < $expires) { $this->refresh_test($sequence_id); } } }
protected function create_experiment() { $group = group::read($this->ID); if (!empty($group['variants'])) { $variants = helpers::make_array_values_numeric($group['variants'], true); $creator = new CreateExperiment($variants, $this->ID, $this->bandit); $this->experiment = $creator->get_experiment(); } }
/** * Get product ID from a group * * @param array|int $group * * @return int|null */ public static function get_product_ID($group) { if (is_numeric($group)) { $group = \ingot\testing\crud\group::read($group); } if (\ingot\testing\crud\group::valid($group)) { return (int) helpers::v('product_ID', $group['meta'], null); } }
/** * Get a group by variant ID * * @since 0.4.0 * * @param int $variant_id * * @return array|bool Group config if found or false if not found */ public static function get_by_variant_id($variant_id) { if (is_array($variant = variant::read($variant_id))) { $group_id = helpers::v('group_ID', $variant, 0); if (is_array($group = self::read($group_id))) { return $group; } } return false; }
/** * Generic save for read/update * * @since 0.0.4 * * @param array $data Item con * @param int $id Optional. Item ID. Not used or needed if using to create. * @param bool|false $bypass_cap * * @return int|bool||WP_Error Item ID if created, or false if not created, or error if not allowed to create. */ protected static function save($data, $id = null, $bypass_cap = false) { $data = static::prepare_data($data); if (is_wp_error($data) || !is_array($data)) { return $data; } $table_name = static::get_table_name(); foreach ($data as $key => $datum) { if (is_array($data[$key])) { if (empty($data[$key])) { $data[$key] = serialize([]); } else { $data[$key] = helpers::sanitize($data[$key]); $data[$key] = serialize($datum); } } } if (self::can($id, $bypass_cap)) { global $wpdb; if ($id) { $data['ID'] = $id; $wpdb->update($table_name, $data, array('ID' => (int) $id)); } else { unset($data['ID']); $wpdb->insert($table_name, $data); $id = $wpdb->insert_id; } return $id; } else { return false; } }