| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadpages; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('No script kiddies please!'); // Avoid direct file request |
| 6 |
|
| 7 |
use Leadpages\providers\Utils; |
| 8 |
use Leadpages\PopupScope; |
| 9 |
|
| 10 |
/** |
| 11 |
* Injects a selected Nova (new Leadpages) pop-up embed script on the front end. |
| 12 |
* |
| 13 |
* The embed script is public and loads asynchronously. It is enqueued on normal WordPress front-end |
| 14 |
* requests via wp_enqueue_scripts, gated by the configured page scope (all pages, or only the |
| 15 |
* selected pages/posts). Proxied landing pages exit() on init (long before the enqueue hook runs), |
| 16 |
* so the Proxy injects the same embed for those pages, reading the same scope from PopupScope. |
| 17 |
*/ |
| 18 |
class Popups { |
| 19 |
|
| 20 |
use Utils; |
| 21 |
|
| 22 |
/** @var string script handle for the pop-up embed */ |
| 23 |
private const HANDLE = 'leadpages-nova-popup'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Enqueue the selected pop-up's embed script when one is chosen and the current front-end page is |
| 27 |
* within the configured scope. No output otherwise. Enqueued (not printed directly) so it goes |
| 28 |
* through the standard WordPress script pipeline and passes plugin-check's NonEnqueuedScript rule. |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public function inject_embed() { |
| 33 |
$popup_id = PopupScope::selected_popup_id(); |
| 34 |
if (empty($popup_id)) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
if (! PopupScope::allows_current_request()) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
$src = PopupScope::embed_src($popup_id); |
| 43 |
|
| 44 |
// The embed URL is versioned by Nova, so no WordPress ?ver should be appended (null version). |
| 45 |
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Nova versions the URL. |
| 46 |
wp_enqueue_script(self::HANDLE, $src, [], null, true); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Add the `async` attribute to the pop-up embed <script> tag (wp_enqueue_script does not emit it |
| 51 |
* natively on WordPress 6.0). Filters script_loader_tag and only touches our handle. |
| 52 |
* |
| 53 |
* @param string $tag |
| 54 |
* @param string $handle |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
public function add_async_attribute( $tag, $handle ) { |
| 58 |
if (self::HANDLE === $handle && false === strpos($tag, ' async')) { |
| 59 |
$tag = str_replace(' src=', ' async src=', $tag); |
| 60 |
} |
| 61 |
return $tag; |
| 62 |
} |
| 63 |
} |
| 64 |
|