| 1 |
<?php |
| 2 |
|
| 3 |
namespace FloatingButton\Publish; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
use FloatingButton\WOWP_Plugin; |
| 8 |
|
| 9 |
class EnqueueScript { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var mixed |
| 13 |
*/ |
| 14 |
private $id; |
| 15 |
private $param; |
| 16 |
|
| 17 |
public function __construct( $id, $param ) { |
| 18 |
$this->id = $id; |
| 19 |
$this->param = maybe_unserialize( $param ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* @throws \JsonException |
| 24 |
*/ |
| 25 |
public function init(): void { |
| 26 |
|
| 27 |
$slug = WOWP_Plugin::SLUG; |
| 28 |
$version = WOWP_Plugin::info( 'version' ); |
| 29 |
$assets = WOWP_Plugin::url() . 'public/assets/'; |
| 30 |
$assets = apply_filters( WOWP_Plugin::PREFIX . '_frontend_assets', $assets ); |
| 31 |
|
| 32 |
$pre_suffix = ! WOWP_Plugin::DEVMODE ? '.min' : ''; |
| 33 |
|
| 34 |
$url_script = $assets . 'js/script' . $pre_suffix . '.js'; |
| 35 |
wp_enqueue_script( $slug, $url_script, array( 'jquery' ), $version, true ); |
| 36 |
|
| 37 |
$inline_script = $this->inline(); |
| 38 |
wp_add_inline_script( $slug, $inline_script, 'before' ); |
| 39 |
|
| 40 |
do_action( WOWP_Plugin::PREFIX . '_enqueue_script' ); |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @throws \JsonException |
| 46 |
*/ |
| 47 |
public function inline(): string { |
| 48 |
$param = $this->param; |
| 49 |
|
| 50 |
$arg = [ |
| 51 |
'element' => 'floatBtn-' . absint( $this->id ), |
| 52 |
]; |
| 53 |
|
| 54 |
if ( ! empty( $param['showAfterPosition'] ) ) { |
| 55 |
$arg['showAfterPosition'] = (int) $param['showAfterPosition']; |
| 56 |
} |
| 57 |
|
| 58 |
if ( ! empty( $param['hideAfterPosition'] ) ) { |
| 59 |
$arg['hideAfterPosition'] = (int) $param['hideAfterPosition']; |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! empty( $param['showAfterTimer'] ) ) { |
| 63 |
$arg['showAfterTimer'] = (int) $param['showAfterTimer']; |
| 64 |
} |
| 65 |
|
| 66 |
if ( ! empty( $param['hideAfterTimer'] ) ) { |
| 67 |
$arg['hideAfterTimer'] = (int) $param['hideAfterTimer']; |
| 68 |
} |
| 69 |
|
| 70 |
if ( ! empty( $param['uncheckedBtn'] ) ) { |
| 71 |
$arg['uncheckedBtn'] = true; |
| 72 |
} |
| 73 |
|
| 74 |
if ( ! empty( $param['uncheckedSubBtn'] ) ) { |
| 75 |
$arg['uncheckedSubBtn'] = true; |
| 76 |
} |
| 77 |
|
| 78 |
if ( ! empty( $param['hideBtns'] ) ) { |
| 79 |
$arg['hideBtns'] = true; |
| 80 |
} |
| 81 |
|
| 82 |
if ( ! empty( $param['touch'] ) ) { |
| 83 |
$arg['touch'] = true; |
| 84 |
} |
| 85 |
|
| 86 |
if ( is_singular() ) { |
| 87 |
$arg['pageId'] = get_the_ID(); |
| 88 |
} |
| 89 |
|
| 90 |
return 'var FloatingButton_' . absint( $this->id ) . ' = ' . json_encode( $arg, JSON_THROW_ON_ERROR ); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
} |