| 1 |
<?php |
| 2 |
|
| 3 |
namespace cookiebot_addons\controller\addons\google_analytics; |
| 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_Analytics 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 |
$this->buffer_output->add_tag( 'wp_footer', 10, array( |
| 75 |
'googleanalytics_get_script' => $this->get_cookie_types(), |
| 76 |
), false ); |
| 77 |
|
| 78 |
if(has_action( 'wp_enqueue_scripts', 'Ga_Frontend::platform_sharethis' )) { |
| 79 |
$this->script_loader_tag->add_tag( GA_NAME . '-platform-sharethis', $this->get_cookie_types() ); |
| 80 |
} |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Return addon/plugin name |
| 86 |
* |
| 87 |
* @return string |
| 88 |
* |
| 89 |
* @since 1.3.0 |
| 90 |
*/ |
| 91 |
public function get_addon_name() { |
| 92 |
return 'Google Analytics'; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Option name in the database |
| 97 |
* |
| 98 |
* @return string |
| 99 |
* |
| 100 |
* @since 1.3.0 |
| 101 |
*/ |
| 102 |
public function get_option_name() { |
| 103 |
return 'google_analytics'; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* plugin file name |
| 108 |
* |
| 109 |
* @return string |
| 110 |
* |
| 111 |
* @since 1.3.0 |
| 112 |
*/ |
| 113 |
public function get_plugin_file() { |
| 114 |
return 'googleanalytics/googleanalytics.php'; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Returns checked cookie types |
| 119 |
* @return array |
| 120 |
* |
| 121 |
* @since 1.3.0 |
| 122 |
*/ |
| 123 |
public function get_cookie_types() { |
| 124 |
return $this->settings->get_cookie_types( $this->get_option_name(), $this->get_default_cookie_types() ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Returns default cookie types |
| 129 |
* @return array |
| 130 |
* |
| 131 |
* @since 1.5.0 |
| 132 |
*/ |
| 133 |
public function get_default_cookie_types() { |
| 134 |
return array( 'statistics' ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Check if plugin is activated and checked in the backend |
| 139 |
* |
| 140 |
* @since 1.3.0 |
| 141 |
*/ |
| 142 |
public function is_addon_enabled() { |
| 143 |
return $this->settings->is_addon_enabled( $this->get_option_name() ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Checks if addon is installed |
| 148 |
* |
| 149 |
* @since 1.3.0 |
| 150 |
*/ |
| 151 |
public function is_addon_installed() { |
| 152 |
return $this->settings->is_addon_installed( $this->get_plugin_file() ); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Checks if addon is activated |
| 157 |
* |
| 158 |
* @since 1.3.0 |
| 159 |
*/ |
| 160 |
public function is_addon_activated() { |
| 161 |
return $this->settings->is_addon_activated( $this->get_plugin_file() ); |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Retrieves current installed version of the addon |
| 166 |
* |
| 167 |
* @return bool |
| 168 |
* |
| 169 |
* @since 2.2.1 |
| 170 |
*/ |
| 171 |
public function get_addon_version() { |
| 172 |
return $this->settings->get_addon_version( $this->get_plugin_file() ); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Default placeholder content |
| 177 |
* |
| 178 |
* @return string |
| 179 |
* |
| 180 |
* @since 1.8.0 |
| 181 |
*/ |
| 182 |
public function get_default_placeholder() { |
| 183 |
return 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to track for google analytics.'; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Get placeholder content |
| 188 |
* |
| 189 |
* This function will check following features: |
| 190 |
* - Current language |
| 191 |
* |
| 192 |
* @param $src |
| 193 |
* |
| 194 |
* @return bool|mixed |
| 195 |
* |
| 196 |
* @since 1.8.0 |
| 197 |
*/ |
| 198 |
public function get_placeholder( $src = '' ) { |
| 199 |
return $this->settings->get_placeholder( $this->get_option_name(), $this->get_default_placeholder(), cookiebot_addons_output_cookie_types( $this->get_cookie_types() ), $src ); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Checks if it does have custom placeholder content |
| 204 |
* |
| 205 |
* @return mixed |
| 206 |
* |
| 207 |
* @since 1.8.0 |
| 208 |
*/ |
| 209 |
public function has_placeholder() { |
| 210 |
return $this->settings->has_placeholder( $this->get_option_name() ); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* returns all placeholder contents |
| 215 |
* |
| 216 |
* @return mixed |
| 217 |
* |
| 218 |
* @since 1.8.0 |
| 219 |
*/ |
| 220 |
public function get_placeholders() { |
| 221 |
return $this->settings->get_placeholders( $this->get_option_name() ); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Return true if the placeholder is enabled |
| 226 |
* |
| 227 |
* @return mixed |
| 228 |
* |
| 229 |
* @since 1.8.0 |
| 230 |
*/ |
| 231 |
public function is_placeholder_enabled() { |
| 232 |
return $this->settings->is_placeholder_enabled( $this->get_option_name() ); |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Adds extra information under the label |
| 237 |
* |
| 238 |
* @return string |
| 239 |
* |
| 240 |
* @since 1.8.0 |
| 241 |
*/ |
| 242 |
public function get_extra_information() { |
| 243 |
return '<p>' . __( 'Google Analytics is used to track how visitor interact with website content.', 'cookiebot-addons' ) . '</p>'; |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Returns the url of WordPress SVN repository or another link where we can verify the plugin file. |
| 248 |
* |
| 249 |
* @return boolean |
| 250 |
* |
| 251 |
* @since 1.8.0 |
| 252 |
*/ |
| 253 |
public function get_svn_url() { |
| 254 |
return 'http://plugins.svn.wordpress.org/googleanalytics/trunk/googleanalytics.php'; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Placeholder helper overlay in the settings page. |
| 259 |
* |
| 260 |
* @return string |
| 261 |
* |
| 262 |
* @since 1.8.0 |
| 263 |
*/ |
| 264 |
public function get_placeholder_helper() { |
| 265 |
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>'; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
/** |
| 270 |
* Returns parent class or false |
| 271 |
* |
| 272 |
* @return string|bool |
| 273 |
* |
| 274 |
* @since 2.1.3 |
| 275 |
*/ |
| 276 |
public function get_parent_class() { |
| 277 |
return get_parent_class( $this ); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Action after enabling the addon on the settings page |
| 282 |
* |
| 283 |
* @since 2.2.0 |
| 284 |
*/ |
| 285 |
public function post_hook_after_enabling() { |
| 286 |
//do nothing |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Cookiebot plugin is deactivated |
| 291 |
* |
| 292 |
* @since 2.2.0 |
| 293 |
*/ |
| 294 |
public function plugin_deactivated() { |
| 295 |
//do nothing |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* @return mixed |
| 300 |
* |
| 301 |
* @since 2.4.5 |
| 302 |
*/ |
| 303 |
public function extra_available_addon_option() { |
| 304 |
//do nothing |
| 305 |
} |
| 306 |
} |
| 307 |
|