google-analyticator.php
339 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cookiebot_addons\controller\addons\google_analyticator; |
| 4 | |
| 5 | use cookiebot_addons\controller\addons\Cookiebot_Addons_Interface; |
| 6 | use cookiebot_addons\lib\buffer\Buffer_Output_Interface; |
| 7 | use cookiebot_addons\lib\Cookie_Consent_Interface; |
| 8 | use cookiebot_addons\lib\script_loader_tag\Script_Loader_Tag_Interface; |
| 9 | use cookiebot_addons\lib\Settings_Service_Interface; |
| 10 | |
| 11 | class Google_Analyticator implements Cookiebot_Addons_Interface { |
| 12 | |
| 13 | /** |
| 14 | * @var Settings_Service_Interface |
| 15 | * |
| 16 | * @since 1.3.0 |
| 17 | */ |
| 18 | protected $settings; |
| 19 | |
| 20 | /** |
| 21 | * @var Script_Loader_Tag_Interface |
| 22 | * |
| 23 | * @since 1.3.0 |
| 24 | */ |
| 25 | protected $script_loader_tag; |
| 26 | |
| 27 | /** |
| 28 | * @var Cookie_Consent_Interface |
| 29 | * |
| 30 | * @since 1.3.0 |
| 31 | */ |
| 32 | public $cookie_consent; |
| 33 | |
| 34 | /** |
| 35 | * @var Buffer_Output_Interface |
| 36 | * |
| 37 | * @since 1.3.0 |
| 38 | */ |
| 39 | protected $buffer_output; |
| 40 | |
| 41 | /** |
| 42 | * Jetpack constructor. |
| 43 | * |
| 44 | * @param $settings Settings_Service_Interface |
| 45 | * @param $script_loader_tag Script_Loader_Tag_Interface |
| 46 | * @param $cookie_consent Cookie_Consent_Interface |
| 47 | * @param $buffer_output Buffer_Output_Interface |
| 48 | * |
| 49 | * @since 1.3.0 |
| 50 | */ |
| 51 | public function __construct( Settings_Service_Interface $settings, Script_Loader_Tag_Interface $script_loader_tag, Cookie_Consent_Interface $cookie_consent, Buffer_Output_Interface $buffer_output ) { |
| 52 | $this->settings = $settings; |
| 53 | $this->script_loader_tag = $script_loader_tag; |
| 54 | $this->cookie_consent = $cookie_consent; |
| 55 | $this->buffer_output = $buffer_output; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Loads addon configuration |
| 60 | * |
| 61 | * @since 1.3.0 |
| 62 | */ |
| 63 | public function load_configuration() { |
| 64 | add_action( 'wp_loaded', array( $this, 'cookiebot_addon_google_analyticator' ), 5 ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Check for google analyticator action hooks |
| 69 | * |
| 70 | * @since 1.3.0 |
| 71 | */ |
| 72 | public function cookiebot_addon_google_analyticator() { |
| 73 | /** |
| 74 | * ga scripts are loaded in wp_head priority set to 99 |
| 75 | */ |
| 76 | if ( has_action( 'wp_head', 'add_google_analytics' ) ) { |
| 77 | /** |
| 78 | * Consent not given - no cache |
| 79 | */ |
| 80 | $this->buffer_output->add_tag( 'wp_head', 99, array( |
| 81 | 'gtag' => $this->get_cookie_types(), |
| 82 | 'google-analytics' => $this->get_cookie_types(), |
| 83 | '_gaq' => $this->get_cookie_types(), |
| 84 | 'www.googletagmanager.com/gtag/js?id=' => $this->get_cookie_types() |
| 85 | ), false ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * ga scripts are loaded in login_head priority set to 99 |
| 90 | */ |
| 91 | if ( has_action( 'login_head', 'add_google_analytics' ) ) { |
| 92 | /** |
| 93 | * Consent not given - no cache |
| 94 | */ |
| 95 | $this->buffer_output->add_tag( 'login_head', 99, array( |
| 96 | 'gtag' => $this->get_cookie_types(), |
| 97 | 'google-analytics' => $this->get_cookie_types(), |
| 98 | '_gaq' => $this->get_cookie_types(), |
| 99 | 'www.googletagmanager.com/gtag/js?id=' => $this->get_cookie_types() |
| 100 | ) ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * External js, so manipulate attributes |
| 105 | */ |
| 106 | if ( has_action( 'wp_print_scripts', 'ga_external_tracking_js' ) ) { |
| 107 | /** |
| 108 | * Catch external js file and add cookiebot attributes to it |
| 109 | * |
| 110 | * @since 1.1.0 |
| 111 | */ |
| 112 | $this->script_loader_tag->add_tag( 'ga-external-tracking', $this->get_cookie_types() ); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Return addon/plugin name |
| 118 | * |
| 119 | * @return string |
| 120 | * |
| 121 | * @since 1.3.0 |
| 122 | */ |
| 123 | public function get_addon_name() { |
| 124 | return 'Google Analyticator'; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Option name in the database |
| 129 | * |
| 130 | * @return string |
| 131 | * |
| 132 | * @since 1.3.0 |
| 133 | */ |
| 134 | public function get_option_name() { |
| 135 | return 'google_analyticator'; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * plugin file name |
| 140 | * |
| 141 | * @return string |
| 142 | * |
| 143 | * @since 1.3.0 |
| 144 | */ |
| 145 | public function get_plugin_file() { |
| 146 | return 'google-analyticator/google-analyticator.php'; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Returns checked cookie types |
| 151 | * @return array |
| 152 | * |
| 153 | * @since 1.3.0 |
| 154 | */ |
| 155 | public function get_cookie_types() { |
| 156 | return $this->settings->get_cookie_types( $this->get_option_name(), $this->get_default_cookie_types() ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns default cookie types |
| 161 | * @return array |
| 162 | * |
| 163 | * @since 1.5.0 |
| 164 | */ |
| 165 | public function get_default_cookie_types() { |
| 166 | return array( 'statistics' ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Check if plugin is activated and checked in the backend |
| 171 | * |
| 172 | * @since 1.3.0 |
| 173 | */ |
| 174 | public function is_addon_enabled() { |
| 175 | return $this->settings->is_addon_enabled( $this->get_option_name() ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Checks if addon is installed |
| 180 | * |
| 181 | * @since 1.3.0 |
| 182 | */ |
| 183 | public function is_addon_installed() { |
| 184 | return $this->settings->is_addon_installed( $this->get_plugin_file() ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Checks if addon is activated |
| 189 | * |
| 190 | * @since 1.3.0 |
| 191 | */ |
| 192 | public function is_addon_activated() { |
| 193 | return $this->settings->is_addon_activated( $this->get_plugin_file() ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Retrieves current installed version of the addon |
| 198 | * |
| 199 | * @return bool |
| 200 | * |
| 201 | * @since 2.2.1 |
| 202 | */ |
| 203 | public function get_addon_version() { |
| 204 | return $this->settings->get_addon_version( $this->get_plugin_file() ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Default placeholder content |
| 209 | * |
| 210 | * @return string |
| 211 | * |
| 212 | * @since 1.8.0 |
| 213 | */ |
| 214 | public function get_default_placeholder() { |
| 215 | return 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to watch this video.'; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Get placeholder content |
| 220 | * |
| 221 | * This function will check following features: |
| 222 | * - Current language |
| 223 | * |
| 224 | * @param $src |
| 225 | * |
| 226 | * @return bool|mixed |
| 227 | * |
| 228 | * @since 1.8.0 |
| 229 | */ |
| 230 | public function get_placeholder( $src = '' ) { |
| 231 | return $this->settings->get_placeholder( $this->get_option_name(), $this->get_default_placeholder(), cookiebot_addons_output_cookie_types( $this->get_cookie_types() ), $src ); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Checks if it does have custom placeholder content |
| 236 | * |
| 237 | * @return mixed |
| 238 | * |
| 239 | * @since 1.8.0 |
| 240 | */ |
| 241 | public function has_placeholder() { |
| 242 | return $this->settings->has_placeholder( $this->get_option_name() ); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * returns all placeholder contents |
| 247 | * |
| 248 | * @return mixed |
| 249 | * |
| 250 | * @since 1.8.0 |
| 251 | */ |
| 252 | public function get_placeholders() { |
| 253 | return $this->settings->get_placeholders( $this->get_option_name() ); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Return true if the placeholder is enabled |
| 258 | * |
| 259 | * @return mixed |
| 260 | * |
| 261 | * @since 1.8.0 |
| 262 | */ |
| 263 | public function is_placeholder_enabled() { |
| 264 | return $this->settings->is_placeholder_enabled( $this->get_option_name() ); |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Adds extra information under the label |
| 269 | * |
| 270 | * @return string |
| 271 | * |
| 272 | * @since 1.8.0 |
| 273 | */ |
| 274 | public function get_extra_information() { |
| 275 | |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Returns the url of WordPress SVN repository or another link where we can verify the plugin file. |
| 280 | * |
| 281 | * @return boolean |
| 282 | * |
| 283 | * @since 1.8.0 |
| 284 | */ |
| 285 | public function get_svn_url() { |
| 286 | return 'http://plugins.svn.wordpress.org/google-analyticator/trunk/google-analyticator.php'; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Placeholder helper overlay in the settings page. |
| 291 | * |
| 292 | * @return string |
| 293 | * |
| 294 | * @since 1.8.0 |
| 295 | */ |
| 296 | public function get_placeholder_helper() { |
| 297 | return '<p>Merge tags you can use in the placeholder text:</p><ul><li>%cookie_types - Lists required cookie types</li><li>[renew_consent]text[/renew_consent] - link to display cookie settings in frontend</li></ul>'; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | /** |
| 302 | * Returns parent class or false |
| 303 | * |
| 304 | * @return string|bool |
| 305 | * |
| 306 | * @since 2.1.3 |
| 307 | */ |
| 308 | public function get_parent_class() { |
| 309 | return get_parent_class( $this ); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Action after enabling the addon on the settings page |
| 314 | * |
| 315 | * @since 2.2.0 |
| 316 | */ |
| 317 | public function post_hook_after_enabling() { |
| 318 | //do nothing |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Cookiebot plugin is deactivated |
| 323 | * |
| 324 | * @since 2.2.0 |
| 325 | */ |
| 326 | public function plugin_deactivated() { |
| 327 | //do nothing |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * @return mixed |
| 332 | * |
| 333 | * @since 2.4.5 |
| 334 | */ |
| 335 | public function extra_available_addon_option() { |
| 336 | //do nothing |
| 337 | } |
| 338 | } |
| 339 |