class-module.php
214 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PEF module |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | */ |
| 8 | |
| 9 | namespace AdvancedAds\Modules\ProductExperimentationFramework; |
| 10 | |
| 11 | /** |
| 12 | * Module main class |
| 13 | */ |
| 14 | class Module { |
| 15 | /** |
| 16 | * User meta key where the dismiss flag is stored. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | const USER_META = 'advanced_ads_pef_dismiss'; |
| 21 | |
| 22 | /** |
| 23 | * Sum of all weights |
| 24 | * |
| 25 | * @var int |
| 26 | */ |
| 27 | private $weight_sum = 0; |
| 28 | |
| 29 | /** |
| 30 | * ID => weight association |
| 31 | * |
| 32 | * @var int[] |
| 33 | */ |
| 34 | private $weights = []; |
| 35 | |
| 36 | /** |
| 37 | * Whether the PEF can be displayed based on user meta |
| 38 | * |
| 39 | * @var bool |
| 40 | */ |
| 41 | private $can_display = true; |
| 42 | |
| 43 | /** |
| 44 | * Return the singleton. Create it if needed |
| 45 | * |
| 46 | * @return Module |
| 47 | */ |
| 48 | public static function get_instance(): Module { |
| 49 | static $instance; |
| 50 | |
| 51 | if ( null === $instance ) { |
| 52 | $instance = new self(); |
| 53 | } |
| 54 | |
| 55 | return $instance; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Singleton design |
| 60 | */ |
| 61 | private function __construct() { |
| 62 | add_action( 'admin_init', [ $this, 'admin_init' ] ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Initialization |
| 67 | * |
| 68 | * @return void |
| 69 | */ |
| 70 | public function admin_init(): void { |
| 71 | $meta = get_user_meta( get_current_user_id(), self::USER_META, true ); |
| 72 | if ( $this->get_minor_version( ADVADS_VERSION ) === $this->get_minor_version( $meta ) ) { |
| 73 | $this->can_display = false; |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | $this->collect_weights(); |
| 78 | add_action( 'wp_ajax_advanced_ads_pef', [ $this, 'dismiss' ] ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Ajax action to hie PEF for the current user until next plugin update |
| 83 | * |
| 84 | * @return void |
| 85 | */ |
| 86 | public function dismiss(): void { |
| 87 | if ( ! check_ajax_referer( 'advanced_ads_pef' ) ) { |
| 88 | wp_send_json_error( 'Unauthorized', 401 ); |
| 89 | } |
| 90 | |
| 91 | update_user_meta( get_current_user_id(), self::USER_META, ADVADS_VERSION ); |
| 92 | wp_send_json_success( 'OK', 200 ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get a random feature based on weights and a random number |
| 97 | * |
| 98 | * @return array |
| 99 | */ |
| 100 | public function get_winner_feature(): array { |
| 101 | $random_weight = wp_rand( 1, $this->weight_sum ); |
| 102 | $current_weight = 0; |
| 103 | foreach ( $this->get_features() as $id => $feature ) { |
| 104 | $current_weight += $this->weights[ $id ]; |
| 105 | if ( $random_weight <= $current_weight ) { |
| 106 | return array_merge( |
| 107 | [ |
| 108 | 'id' => $id, |
| 109 | 'weight' => $this->weights[ $id ], |
| 110 | ], |
| 111 | $this->get_features()[ $id ] |
| 112 | ); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Render PEF |
| 119 | * |
| 120 | * @param string $screen the screen on which PEF is displayed, used in the utm_campaign parameter. |
| 121 | * |
| 122 | * @return void |
| 123 | */ |
| 124 | public function render( $screen ): void { // phpcs:ignore |
| 125 | // Early bail!! |
| 126 | if ( ! $this->can_display ) { |
| 127 | return; |
| 128 | } |
| 129 | $winner = $this->get_winner_feature(); |
| 130 | |
| 131 | require_once DIR . '/views/template.php'; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get minor part of a version |
| 136 | * |
| 137 | * @param string $version version to get the minor part from. |
| 138 | * |
| 139 | * @return string |
| 140 | */ |
| 141 | public function get_minor_version( $version ): string { |
| 142 | return explode( '.', $version )[1] ?? '0'; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Build the link for the winner feature with all its utm parameters |
| 147 | * |
| 148 | * @param array $winner the winner feature. |
| 149 | * @param string $screen the screen on which it was displayed. |
| 150 | * |
| 151 | * @return string |
| 152 | */ |
| 153 | public function build_link( $winner, $screen ): string { |
| 154 | $utm_source = 'advanced-ads'; |
| 155 | $utm_medium = 'link'; |
| 156 | $utm_campaign = sprintf( '%s-aa-labs', $screen ); |
| 157 | $utm_term = sprintf( |
| 158 | 'b%s-w%d-%d', |
| 159 | str_replace( '.', '-', ADVADS_VERSION ), |
| 160 | $winner['weight'], |
| 161 | $this->weight_sum |
| 162 | ); |
| 163 | $utm_content = $winner['id']; |
| 164 | |
| 165 | return sprintf( |
| 166 | 'https://wpadvancedads.com/advanced-ads-labs/?utm_source=%s&utm_medium=%s&utm_campaign=%s&utm_term=%s&utm_content=%s', |
| 167 | $utm_source, |
| 168 | $utm_medium, |
| 169 | $utm_campaign, |
| 170 | $utm_term, |
| 171 | $utm_content |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Set the features/banners |
| 177 | * |
| 178 | * @return array |
| 179 | */ |
| 180 | public function get_features(): array { |
| 181 | return [ |
| 182 | 'labs-campaign-manager-ay' => [ |
| 183 | 'subheading' => __( 'FROM THE ADVANCED ADS LABS:', 'advanced-ads' ), |
| 184 | 'heading' => __( 'The Campaign Manager', 'advanced-ads' ), |
| 185 | 'weight' => 1, |
| 186 | 'text' => __( 'Advanced Ads’ upcoming new product, the Campaign Manager, will shake up how you sell ad space to clients directly. It bundles a decade of users’ requests and ideas into one standalone product. The core feature set includes a brilliant advertisement schedule screen, grouping ads and reports by client, brushed-up email notifications for timed ads, and more.<br><br>Our team is in the early stages of development, and we would like to see if this product resonates with you.', 'advanced-ads' ), |
| 187 | 'cta' => __( 'Are you interested in this product concept?', 'advanced-ads' ), |
| 188 | 'cta_button' => __( 'Yes, I want to know more!', 'advanced-ads' ), |
| 189 | ], |
| 190 | 'labs-campaign-manager-be' => [ |
| 191 | 'subheading' => __( 'FROM THE ADVANCED ADS LABS:', 'advanced-ads' ), |
| 192 | 'heading' => __( 'The Campaign Manager', 'advanced-ads' ), |
| 193 | 'weight' => 1, |
| 194 | 'text' => __( 'Advanced Ads’ upcoming new product, the Campaign Manager, will shake up how you sell ad space to clients directly. It bundles a decade of users’ requests and ideas into one standalone product. The core feature set includes a brilliant advertisement schedule screen, grouping ads and reports by client, brushed-up email notifications for timed ads, and more.<br><br>Our team is in the early stages of development, and we would like to see if this product resonates with you.', 'advanced-ads' ), |
| 195 | 'cta' => __( 'Are you interested in this product concept?', 'advanced-ads' ), |
| 196 | 'cta_button' => __( 'Yes, I want to know more!', 'advanced-ads' ), |
| 197 | ], |
| 198 | ]; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Collect feature ID with their weight as recorded in the class constant. Also calculate the weight sum |
| 203 | */ |
| 204 | private function collect_weights() { |
| 205 | if ( 0 !== $this->weight_sum ) { |
| 206 | return; |
| 207 | } |
| 208 | foreach ( $this->get_features() as $id => $feature ) { |
| 209 | $this->weights[ $id ] = (int) $feature['weight']; |
| 210 | $this->weight_sum += $this->weights[ $id ]; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 |