PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.2
Elementor Website Builder – more than just a page builder v4.2.2
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / core / utils / ab-test.php

ab-test.php in Elementor Website Builder – more than just a page builder 4.2.2, at core/utils/ab-test.php

87 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Core\Utils;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit; // Exit if accessed directly.
7 }
8
9 /**
10 * Core A/B Testing utility class
11 *
12 * Provides A/B testing functionality for Core features.
13 * Uses WordPress transients for caching and user-specific variation assignment.
14 */
15 class Ab_Test {
16
17 const PREFIX_CACHE_KEY = '_elementor_ab_test_';
18 const CACHE_TTL = 90 * DAY_IN_SECONDS;
19
20 /**
21 * Get variation for a specific test
22 *
23 * @param string $test_name The name of the A/B test
24 * @param int $user_id Optional user ID, defaults to current user
25 * @return int Returns 1 or 2 for variation assignment
26 */
27 public static function get_variation( $test_name, $user_id = null ): int {
28 if ( null === $user_id ) {
29 $user_id = get_current_user_id();
30 }
31
32 $variation_id = self::get_variation_id_from_cache( $test_name, $user_id );
33
34 if ( false === $variation_id ) {
35 $variation_id = self::get_random_variation();
36 self::set_variation_id_from_cache( $test_name, $user_id, $variation_id );
37 }
38
39 return absint( $variation_id );
40 }
41
42 /**
43 * Check if user should see the feature (50% probability)
44 *
45 * @param string $test_name The name of the A/B test
46 * @param int $user_id Optional user ID, defaults to current user
47 * @return bool True if user should see the feature
48 */
49 public static function should_show_feature( $test_name, $user_id = null ): bool {
50 $variation = self::get_variation( $test_name, $user_id );
51 return 1 === $variation; // Only variation 1 sees the feature
52 }
53
54 /**
55 * Get variation ID from cache
56 *
57 * @param string $test_name The name of the A/B test
58 * @param int $user_id User ID
59 * @return int|false Variation ID or false if not cached
60 */
61 private static function get_variation_id_from_cache( $test_name, $user_id ) {
62 $cache_key = self::PREFIX_CACHE_KEY . $test_name . '_' . $user_id;
63 return get_transient( $cache_key );
64 }
65
66 /**
67 * Set variation ID in cache
68 *
69 * @param string $test_name The name of the A/B test
70 * @param int $user_id User ID
71 * @param int $variation_id Variation ID to cache
72 */
73 private static function set_variation_id_from_cache( $test_name, $user_id, $variation_id ): void {
74 $cache_key = self::PREFIX_CACHE_KEY . $test_name . '_' . $user_id;
75 set_transient( $cache_key, $variation_id, self::CACHE_TTL );
76 }
77
78 /**
79 * Generate random variation (1 or 2)
80 *
81 * @return int Random variation ID
82 */
83 private static function get_random_variation(): int {
84 return mt_rand( 1, 2 );
85 }
86 }
87