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 a collection of items * * @since 0.4.0 * * @param array $params { * $group_id int ID of group to get all * $ids array Optional. Array of ids to get. * $current bool Optional. Used with $ids or $group_id, if true, will return the first non-completed sequence for that group_id or set of ids. Default is false. * $limit int Optional. Limit results, default is -1 which gets all. * $page int Optional. Page of results, used with $limit. Default is 1 * $return string Optional. What to return all|IDs Return all fields or just IDs * $price_test Optional bool if true and $current current sequences for all prices tests are returned * } * * @return array */ public static function get_items($params) { $args = wp_parse_args($params, array('group_ID' => null, 'ids' => array(), 'current' => false, 'limit' => -1, 'page' => 1, 'return' => '*', 'price_test' => false)); if (-1 == $args['limit']) { $args['limit'] = 999999999; } if (strtolower('ids') !== $args['return']) { $fields = '*'; } else { $fields = '`ID`'; } global $wpdb; $table_name = self::get_table_name(); if (helpers::v('group_ID', $args, null)) { $sql = sprintf('SELECT %s FROM `%s` WHERE `group_ID` = %d', $fields, $table_name, helpers::v('group_ID', $params)); } elseif (!empty(helpers::v('ids', $args, array()))) { $in = implode(',', helpers::v('ids', $params, array())); $sql = sprintf('SELECT %s FROM `%s` WHERE `ID` IN( %s)', $fields, $table_name, $in); } else { $sql = sprintf('SELECT %s FROM `%s`', $fields, $table_name); } if (helpers::v('current', $args, false)) { $sql .= ' AND `completed` != 1'; } $sql .= sprintf(' ORDER BY `ID` ASC LIMIT %d OFFSET %d', $args['limit'], self::calculate_offset($args['limit'], $args['page'])); $results = $wpdb->get_results($sql, ARRAY_A); if (!empty($results)) { foreach ($results as $i => $result) { $results[$i] = self::unseralize($result); } } return $results; }
/** * 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); } }
/** * 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; }
/** * 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; }
/** * 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; } }
/** * 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()); }
/** * 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; }
/** * @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); }
/** * @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'); } }
/** * 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); } } }
/** * 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)); }
/** * 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); }
/** * 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; }
/** * 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); } }
/** * 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; }