class-ad-list-stats.php
1 week ago
class-cache.php
1 week ago
class-conditional.php
3 months ago
class-content-injection.php
1 year ago
class-data.php
1 week ago
class-sanitize.php
1 year ago
class-testing.php
1 year ago
class-validation.php
1 year ago
class-wordpress.php
1 week ago
index.php
2 years ago
class-testing.php
97 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The class is responsible to allow split testing websites based on their URL. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.50.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Utilities; |
| 11 | |
| 12 | use Advanced_Ads; |
| 13 | use AdvancedAds\Framework\Utilities\Params; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Utilities Testing. |
| 19 | */ |
| 20 | class Testing { |
| 21 | /** |
| 22 | * Show stuff to new users only. |
| 23 | * |
| 24 | * @param integer $timestamp time after which to show whatever. |
| 25 | * @param string $group optional group. |
| 26 | * |
| 27 | * @return bool true if user enabled after given timestamp. |
| 28 | */ |
| 29 | public static function show_to_new_users( $timestamp, $group = 'a' ) { |
| 30 | return self::get_group_by_url( null, $group ) && self::is_new_user( $timestamp ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Check if user started after a given date |
| 35 | * |
| 36 | * @param integer $timestamp time stamp. |
| 37 | * |
| 38 | * @return bool true if user is added after timestamp. |
| 39 | */ |
| 40 | public static function is_new_user( $timestamp = 0 ) { |
| 41 | // Allow admins to see version for new users in any case. |
| 42 | if ( Conditional::user_can( 'advanced_ads_manage_options' ) && Params::request( 'advads-ignore-timestamp' ) ) { |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | $timestamp = absint( $timestamp ); |
| 47 | |
| 48 | $options = Advanced_Ads::get_instance()->internal_options(); |
| 49 | $installed = isset( $options['installed'] ) ? $options['installed'] : 0; |
| 50 | |
| 51 | return $installed >= $timestamp; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Create a random group |
| 56 | * |
| 57 | * @param string $url optional parameter. |
| 58 | * @param string $ex group. |
| 59 | * |
| 60 | * @return bool |
| 61 | */ |
| 62 | public static function get_group_by_url( $url = '', $ex = 'a' ) { |
| 63 | $url = self::get_short_url( $url ); |
| 64 | $code = (int) substr( md5( $url ), - 1 ); |
| 65 | |
| 66 | switch ( $ex ) { |
| 67 | case 'b': |
| 68 | return ( $code & 2 ) >> 1; // returns 1 or 0. |
| 69 | case 'c': |
| 70 | return ( $code & 4 ) >> 2; // returns 1 or 0. |
| 71 | case 'd': |
| 72 | return ( $code & 8 ) >> 3; // returns 1 or 0. |
| 73 | default: |
| 74 | return $code & 1; // returns 1 or 0. |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get short version of home_url() by removing protocol, www and slash |
| 80 | * |
| 81 | * @param string $url URL to be shortened. |
| 82 | * |
| 83 | * @return string |
| 84 | */ |
| 85 | public static function get_short_url( $url = '' ) { |
| 86 | $url = empty( $url ) ? home_url() : $url; |
| 87 | |
| 88 | // Strip protocols. |
| 89 | if ( preg_match( '/^(\w[\w\d]*:\/\/)?(www\.)?(.*)$/', trim( $url ), $matches ) ) { |
| 90 | $url = $matches[3]; |
| 91 | } |
| 92 | |
| 93 | // Strip slashes. |
| 94 | return trim( $url, '/' ); |
| 95 | } |
| 96 | } |
| 97 |