PluginProbe
Pronamic Client / trunk
Pronamic Client vtrunk
2.4.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.4.0 1.5.0 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 All 54 releases
pronamic-client / classes / GoogleTagManagerModule.php

GoogleTagManagerModule.php in Pronamic Client trunk, at classes/GoogleTagManagerModule.php

196 lines 4.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Pronamic\WordPress\PronamicClient;
4
5 class GoogleTagManagerModule {
6 /**
7 * Instance of this class.
8 *
9 * @since 1.4.0
10 *
11 * @var GoogleTagManagerModule
12 */
13 protected static $instance = null;
14
15 /**
16 * Plugin
17 *
18 * @var Plugin
19 */
20 private $plugin;
21
22 /**
23 * Container ID.
24 *
25 * @var string
26 */
27 private $container_id;
28
29 /**
30 * Constructs and initialize Google Tag Manager Module.
31 *
32 * @param Plugin $plugin
33 */
34 private function __construct( Plugin $plugin ) {
35 $this->plugin = $plugin;
36
37 // Init.
38 \add_action( 'init', [ $this, 'init' ] );
39
40 // Admin.
41 if ( \is_admin() ) {
42 \add_action( 'admin_init', [ $this, 'admin_init' ], 40 );
43 }
44
45 // Google Tag Manager Container ID.
46 $this->container_id = \get_option( 'pronamic_client_google_tag_manager_container_id' );
47
48 if ( ! empty( $this->container_id ) ) {
49 \add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] );
50
51 \add_action( 'wp_head', [ $this, 'head' ], 1 );
52 \add_action( 'wp_body_open', [ $this, 'body_open' ], 1 );
53 }
54 }
55
56 /**
57 * Initialize.
58 */
59 public function init() {
60 \register_setting(
61 'pronamic_client',
62 'pronamic_client_google_tag_manager_container_id',
63 [
64 'type' => 'string',
65 ]
66 );
67 }
68
69 /**
70 * Admin initialize.
71 */
72 public function admin_init() {
73 // Section
74 \add_settings_section(
75 'pronamic_client_google_tag_manager',
76 \__( 'Google Tag Manager', 'pronamic-client' ),
77 '__return_null',
78 'pronamic_client'
79 );
80
81 // GTM Code
82 \add_settings_field(
83 'pronamic_client_google_tag_manager_container_id',
84 \__( 'Google Tag Manager Container ID', 'pronamic-client' ),
85 function ( $args ) {
86 Admin::input_text( $args );
87 },
88 'pronamic_client',
89 'pronamic_client_google_tag_manager',
90 [
91 'label_for' => 'pronamic_client_google_tag_manager_container_id',
92 'classes' => 'regular-text',
93 'description' => sprintf(
94 /* translators: 1: hook */
95 \esc_html__( 'Your theme needs support for the %1$s hook.', 'pronamic-client' ),
96 '<code>wp_body_open</code>'
97 ),
98 ]
99 );
100 }
101
102 /**
103 * Plugins loaded.
104 */
105 public function plugins_loaded() {
106 $conflicts = [];
107
108 /**
109 * Google Tag Manager for WordPress (By Thomas Geiger).
110 *
111 * @link https://wordpress.org/plugins/duracelltomi-google-tag-manager/
112 * @link https://plugins.trac.wordpress.org/browser/duracelltomi-google-tag-manager/tags/1.11.2/duracelltomi-google-tag-manager-for-wordpress.php#L16
113 */
114 if ( \defined( 'GTM4WP_VERSION' ) ) {
115 $conflicts[] = 'Google Tag Manager for WordPress (By Thomas Geiger)';
116 }
117
118 /**
119 * Notices.
120 *
121 * @link https://github.com/Yoast/wordpress-seo/blob/13.1/admin/class-yoast-plugin-conflict.php#L202
122 * @link https://plugins.trac.wordpress.org/browser/wk-google-analytics/tags/1.8.0/wk-ga.php#L15
123 */
124 $this->admin_notices = [];
125
126 foreach ( $conflicts as $plugin ) {
127 $this->admin_notices[] = \sprintf(
128 /* translators: %1: <em>conflicting plugin name</em>, 2: Pronamic Client */
129 \__( 'The %1$s plugin might cause issues when used in conjunction with %2$s.', 'pronamic-client' ),
130 '<em>' . $plugin . '</em>',
131 'Pronamic Client'
132 );
133 }
134
135 \add_action( 'admin_notices', [ $this, 'admin_notices' ] );
136 }
137
138 /**
139 * Admin notices.
140 */
141 public function admin_notices() {
142 if ( empty( $this->admin_notices ) ) {
143 return;
144 }
145
146 foreach ( $this->admin_notices as $notice ) {
147 \printf(
148 '<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>',
149 $notice
150 );
151 }
152 }
153
154 /**
155 * Head.
156 */
157 public function head() {
158 if ( empty( $this->container_id ) ) {
159 return;
160 }
161
162 $container_id = $this->container_id;
163
164 include __DIR__ . '/../templates/google-tag-manager-head.php';
165 }
166
167 /**
168 * Body open.
169 */
170 public function body_open() {
171 if ( empty( $this->container_id ) ) {
172 return;
173 }
174
175 $url = \add_query_arg( 'id', $this->container_id, 'https://www.googletagmanager.com/ns.html' );
176
177 include __DIR__ . '/../templates/google-tag-manager-body.php';
178 }
179
180 /**
181 * Return an instance of this class.
182 *
183 * @since 1.1.0
184 *
185 * @return object A single instance of this class.
186 */
187 public static function get_instance( $plugin = false ) {
188 // If the single instance hasn't been set, set it now.
189 if ( null === self::$instance ) {
190 self::$instance = new self( $plugin );
191 }
192
193 return self::$instance;
194 }
195 }
196