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 / GoogleAnalyticsModule.php

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

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