PluginProbe
Pushly / trunk
Pushly vtrunk
2.4.0 2.3.0 trunk 1.0 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 2.0.0 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1
pushly / src / php / Frontend / SDK.php

SDK.php in Pushly trunk, at src/php/Frontend/SDK.php

55 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Pushly\Frontend;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 class SDK {
10 public function register_hooks(): void {
11 add_action( 'wp_head', [ $this, 'insert_header' ], 10 );
12 add_filter( 'script_loader_tag', [ $this, 'async_enqueue' ], 10, 2 );
13 }
14
15 public function insert_header(): void {
16 $sdk_key = null;
17 $initialization_disabled = false;
18
19 $options = get_option( 'pushly' );
20 if ( ! empty( $options['sdk_key'] ) ) {
21 $sdk_key = $options['sdk_key'];
22 $initialization_disabled = ! empty( $options['initialization_disabled'] );
23 } else {
24 // v1 backwards compatibility
25 $legacy = get_option( 'pushly_options' );
26 if ( ! empty( $legacy['domain_key'] ) ) {
27 $sdk_key = $legacy['domain_key'];
28 }
29 }
30
31 if ( ! $sdk_key || $initialization_disabled ) {
32 return;
33 }
34
35 wp_enqueue_script(
36 'pushly-sdk',
37 'https://' . PUSHLY__SDK_DOMAIN . '/pushly-sdk.min.js?domain_key=' . rawurlencode( $sdk_key ),
38 [],
39 PUSHLY__PLUGIN_VERSION,
40 true
41 );
42
43 require_once PUSHLY__DIR . '/includes/public/views/sdk.php';
44 pushly_render_sdk_snippet( $sdk_key, PUSHLY__PLUGIN_DIR );
45 }
46
47 public function async_enqueue( string $tag, string $handle ): string {
48 if ( 'pushly-sdk' === $handle && false === strpos( $tag, 'async' ) ) {
49 return str_replace( '<script ', '<script async ', $tag );
50 }
51
52 return $tag;
53 }
54 }
55