| 1 |
<?php |
| 2 |
/** |
| 3 |
* Headline Testing feature class |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.21.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely; |
| 12 |
|
| 13 |
/** |
| 14 |
* Handles the Headline Testing feature functionality. |
| 15 |
* |
| 16 |
* @since 3.21.0 |
| 17 |
* |
| 18 |
* @phpstan-import-type Parsely_Options_Headline_Testing from Parsely |
| 19 |
*/ |
| 20 |
class Headline_Testing { |
| 21 |
/** |
| 22 |
* Instance of Parsely class. |
| 23 |
* |
| 24 |
* @var Parsely |
| 25 |
*/ |
| 26 |
protected $parsely; |
| 27 |
|
| 28 |
/** |
| 29 |
* Data attributes to be added to the one-line script tag. |
| 30 |
* |
| 31 |
* @var array<string> |
| 32 |
*/ |
| 33 |
private $data_attributes = array(); |
| 34 |
|
| 35 |
/** |
| 36 |
* Constructor. |
| 37 |
* |
| 38 |
* @since 3.21.0 |
| 39 |
* |
| 40 |
* @param Parsely $parsely Instance of Parsely class. |
| 41 |
*/ |
| 42 |
public function __construct( Parsely $parsely ) { |
| 43 |
$this->parsely = $parsely; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Registers the Headline Testing feature. |
| 48 |
* |
| 49 |
* @since 3.21.0 |
| 50 |
*/ |
| 51 |
public function run(): void { |
| 52 |
if ( false === $this->can_enable_feature() ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_headline_testing_script' ) ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns whether the Headline Testing feature can be enabled. |
| 61 |
* |
| 62 |
* @since 3.21.0 |
| 63 |
* |
| 64 |
* @return bool True if the feature can be enabled, false otherwise. |
| 65 |
*/ |
| 66 |
public function can_enable_feature(): bool { |
| 67 |
$options = $this->parsely->get_options(); |
| 68 |
|
| 69 |
return true === $options['headline_testing']['enabled'] && |
| 70 |
'' !== $this->parsely->get_site_id(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Enqueues the Headline Testing script, in accordance to the |
| 75 |
* installation_method option. |
| 76 |
* |
| 77 |
* @since 3.21.0 |
| 78 |
*/ |
| 79 |
public function enqueue_headline_testing_script(): void { |
| 80 |
$options = $this->parsely->get_options(); |
| 81 |
$site_id = $this->parsely->get_site_id(); |
| 82 |
|
| 83 |
$headline_testing_options = $options['headline_testing']; |
| 84 |
$installation_method = $headline_testing_options['installation_method']; |
| 85 |
|
| 86 |
if ( 'one_line' === $installation_method ) { |
| 87 |
$this->enqueue_one_line_script( $headline_testing_options, $site_id ); |
| 88 |
} else { |
| 89 |
$this->enqueue_advanced_script( $headline_testing_options, $site_id ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Enqueues the one-line snippet script. |
| 95 |
* |
| 96 |
* @since 3.21.0 |
| 97 |
* |
| 98 |
* @param Parsely_Options_Headline_Testing $options The headline testing options. |
| 99 |
* @param string $site_id The Parse.ly site ID. |
| 100 |
*/ |
| 101 |
private function enqueue_one_line_script( $options, string $site_id ): void { |
| 102 |
$script_url = 'https://experiments.parsely.com/vip-experiments.js?apiKey=' . rawurlencode( $site_id ); |
| 103 |
|
| 104 |
// Build data attributes string. |
| 105 |
$data_attributes = array(); |
| 106 |
|
| 107 |
if ( $options['enable_live_updates'] ) { |
| 108 |
$data_attributes[] = 'data-enable-live-updates="true"'; |
| 109 |
|
| 110 |
$timeout = absint( $options['live_update_timeout'] ); |
| 111 |
if ( 30000 !== $timeout ) { |
| 112 |
$data_attributes[] = 'data-live-update-timeout="' . esc_attr( (string) $timeout ) . '"'; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if ( $options['allow_after_content_load'] ) { |
| 117 |
$data_attributes[] = 'data-allow-after-content-load="true"'; |
| 118 |
} |
| 119 |
|
| 120 |
// Store data attributes and add filter to modify the script tag. |
| 121 |
if ( count( $data_attributes ) > 0 ) { |
| 122 |
$this->data_attributes = $data_attributes; |
| 123 |
|
| 124 |
if ( false === has_filter( |
| 125 |
'script_loader_tag', |
| 126 |
array( $this, 'add_data_attributes_to_script_tag' ) |
| 127 |
) ) { |
| 128 |
add_filter( |
| 129 |
'script_loader_tag', |
| 130 |
array( $this, 'add_data_attributes_to_script_tag' ), |
| 131 |
10, |
| 132 |
2 |
| 133 |
); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
// Register and enqueue the script. |
| 138 |
wp_register_script( |
| 139 |
'parsely-headline-testing-one-line', |
| 140 |
$script_url, |
| 141 |
array(), |
| 142 |
PARSELY_VERSION, |
| 143 |
false |
| 144 |
); |
| 145 |
|
| 146 |
wp_enqueue_script( 'parsely-headline-testing-one-line' ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Adds data attributes to the one-line script tag. |
| 151 |
* |
| 152 |
* @since 3.21.0 |
| 153 |
* |
| 154 |
* @param string $tag The script tag. |
| 155 |
* @param string $handle The script handle. |
| 156 |
* @return string The modified script tag. |
| 157 |
*/ |
| 158 |
public function add_data_attributes_to_script_tag( string $tag, string $handle ): string { |
| 159 |
if ( 'parsely-headline-testing-one-line' === $handle ) { |
| 160 |
// Insert data attributes before the closing > of the script tag. |
| 161 |
$tag = str_replace( '></script>', ' ' . implode( ' ', $this->data_attributes ) . '></script>', $tag ); |
| 162 |
} |
| 163 |
|
| 164 |
return $tag; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Enqueues the advanced installation script. |
| 169 |
* |
| 170 |
* @since 3.21.0 |
| 171 |
* |
| 172 |
* @param Parsely_Options_Headline_Testing $options The headline testing options. |
| 173 |
* @param string $site_id The Parse.ly site ID. |
| 174 |
*/ |
| 175 |
private function enqueue_advanced_script( $options, string $site_id ): void { |
| 176 |
$config_options = array(); |
| 177 |
|
| 178 |
if ( $options['enable_flicker_control'] ) { |
| 179 |
$config_options[] = 'enableFlickerControl: true'; |
| 180 |
} |
| 181 |
|
| 182 |
if ( $options['enable_live_updates'] ) { |
| 183 |
$config_options[] = 'enableLiveUpdates: true'; |
| 184 |
|
| 185 |
$timeout = absint( $options['live_update_timeout'] ); |
| 186 |
$config_options[] = 'liveUpdateTimeout: ' . $timeout; |
| 187 |
} |
| 188 |
|
| 189 |
if ( $options['allow_after_content_load'] ) { |
| 190 |
$config_options[] = 'allowAfterContentLoad: true'; |
| 191 |
} |
| 192 |
|
| 193 |
$config_str = count( $config_options ) > 0 ? ', {' . implode( ', ', $config_options ) . '}' : ''; |
| 194 |
|
| 195 |
$script_content = '!function(){"use strict";var e=window.VIP_EXP=window.VIP_EXP||{config:{}};e.loadVIPExp=function(t){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};t&&(e.config=n,e.config.apikey=t,function(e){if(!e)return;var t="https://experiments.parsely.com/vip-experiments.js"+"?apiKey=".concat(e),n=document.createElement("script");n.src=t,n.type="text/javascript",n.fetchPriority="high";var i=document.getElementsByTagName("script")[0];i&&i.parentNode&&i.parentNode.insertBefore(n,i)}(t),n.enableFlickerControl&&function(){var t,n;if(null!==(t=performance)&&void 0!==t&&null!==(n=t.getEntriesByName)&&void 0!==n&&null!==(n=n.call(t,"first-contentful-paint"))&&void 0!==n&&n[0])return;var i="vipexp-fooc-prevention";e.config.disableFlickerControl=function(){var e=document.getElementById(i);null!=e&&e.parentNode&&e.parentNode.removeChild(e)};var o=document.createElement("style");o.setAttribute("type","text/css"),o.appendChild(document.createTextNode("body { visibility: hidden; }")),o.id=i,document.head.appendChild(o),window.setTimeout(e.config.disableFlickerControl,500)}())},e.loadVIPExp("' . esc_js( $site_id ) . '"' . $config_str . ')}();'; |
| 196 |
|
| 197 |
// Register and enqueue the inline script. |
| 198 |
wp_register_script( |
| 199 |
'parsely-headline-testing-advanced', |
| 200 |
'', |
| 201 |
array(), |
| 202 |
PARSELY_VERSION, |
| 203 |
false |
| 204 |
); |
| 205 |
|
| 206 |
wp_add_inline_script( 'parsely-headline-testing-advanced', $script_content ); |
| 207 |
wp_enqueue_script( 'parsely-headline-testing-advanced' ); |
| 208 |
} |
| 209 |
} |
| 210 |
|