| 1 |
<?php |
| 2 |
|
| 3 |
namespace cookiebot_addons\controller\addons\jetpack\widget; |
| 4 |
|
| 5 |
use cookiebot_addons\lib\buffer\Buffer_Output_Interface; |
| 6 |
use cookiebot_addons\lib\Cookie_Consent_Interface; |
| 7 |
use cookiebot_addons\lib\script_loader_tag\Script_Loader_Tag_Interface; |
| 8 |
use cookiebot_addons\lib\Settings_Service_Interface; |
| 9 |
|
| 10 |
class Google_Maps_Widget implements Jetpack_Widget_Interface { |
| 11 |
|
| 12 |
/** |
| 13 |
* @var array list of supported cookie types |
| 14 |
* |
| 15 |
* @since 1.3.0 |
| 16 |
*/ |
| 17 |
protected $cookie_types; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var Settings_Service_Interface |
| 21 |
* |
| 22 |
* @since 1.3.0 |
| 23 |
*/ |
| 24 |
protected $settings; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var Script_Loader_Tag_Interface |
| 28 |
* |
| 29 |
* @since 1.3.0 |
| 30 |
*/ |
| 31 |
protected $script_loader_tag; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var Cookie_Consent_Interface |
| 35 |
* |
| 36 |
* @since 1.3.0 |
| 37 |
*/ |
| 38 |
protected $cookie_consent; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var Buffer_Output_Interface |
| 42 |
* |
| 43 |
* @since 1.3.0 |
| 44 |
*/ |
| 45 |
protected $buffer_output; |
| 46 |
|
| 47 |
/** |
| 48 |
* Option name for jetpack addon |
| 49 |
* |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
public $widget_option; |
| 53 |
|
| 54 |
/** |
| 55 |
* This class is used to support facebook page widget in jetpack |
| 56 |
* |
| 57 |
* Google_Maps_Widget constructor. |
| 58 |
* |
| 59 |
* @param Settings_Service_Interface $settings |
| 60 |
* @param Script_Loader_Tag_Interface $script_loader_tag |
| 61 |
* @param Cookie_Consent_Interface $cookie_consent |
| 62 |
* @param Buffer_Output_Interface $buffer_output |
| 63 |
* @param string $widget_option |
| 64 |
* |
| 65 |
* @version 1.8.0 |
| 66 |
* @since 1.2.0 |
| 67 |
*/ |
| 68 |
public function __construct( Settings_Service_Interface $settings, Script_Loader_Tag_Interface $script_loader_tag, Cookie_Consent_Interface $cookie_consent, Buffer_Output_Interface $buffer_output, $widget_option ) { |
| 69 |
$this->settings = $settings; |
| 70 |
$this->script_loader_tag = $script_loader_tag; |
| 71 |
$this->cookie_consent = $cookie_consent; |
| 72 |
$this->buffer_output = $buffer_output; |
| 73 |
$this->widget_option = $widget_option; |
| 74 |
} |
| 75 |
|
| 76 |
public function load_configuration() { |
| 77 |
/** |
| 78 |
* The widget is active |
| 79 |
*/ |
| 80 |
if ( is_active_widget( false, false, 'widget_contact_info', true ) ) { |
| 81 |
/** |
| 82 |
* The widget is enabled in Prior consent |
| 83 |
*/ |
| 84 |
if ( $this->is_widget_enabled() ) { |
| 85 |
/** |
| 86 |
* Cookie types are not selected |
| 87 |
*/ |
| 88 |
if ( ! $this->cookie_consent->are_cookie_states_accepted( $this->get_widget_cookie_types() ) ) { |
| 89 |
$this->cookie_types = $this->get_widget_cookie_types(); |
| 90 |
|
| 91 |
/** |
| 92 |
* Replace attributes of the google maps widget iframe |
| 93 |
*/ |
| 94 |
add_action( 'jetpack_contact_info_widget_start', array( $this, 'start_buffer' ) ); |
| 95 |
add_action( 'jetpack_contact_info_widget_end', array( $this, 'stop_buffer' ) ); |
| 96 |
|
| 97 |
if ( $this->is_widget_placeholder_enabled() ) { |
| 98 |
add_action( 'jetpack_stats_extra', array( $this, 'cookie_consent_div' ), 10, 2 ); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public function get_label() { |
| 106 |
return 'Google Maps'; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns widget option name |
| 111 |
* |
| 112 |
* @return string |
| 113 |
* |
| 114 |
* @since 1.8.0 |
| 115 |
*/ |
| 116 |
public function get_widget_option_name() { |
| 117 |
return 'google_maps'; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Returns cookie types for a widget |
| 122 |
* |
| 123 |
* @return mixed |
| 124 |
* |
| 125 |
* @since 1.8.0 |
| 126 |
*/ |
| 127 |
public function get_widget_cookie_types() { |
| 128 |
return $this->settings->get_widget_cookie_types( $this->widget_option, $this->get_widget_option_name() ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Checks if a widget is enabled |
| 133 |
* |
| 134 |
* @return mixed |
| 135 |
* |
| 136 |
* @since 1.8.0 |
| 137 |
*/ |
| 138 |
public function is_widget_enabled() { |
| 139 |
return $this->settings->is_widget_enabled( $this->widget_option, $this->get_widget_option_name() ); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @return string |
| 144 |
*/ |
| 145 |
public function get_default_placeholder() { |
| 146 |
return 'Please accept [renew_consent]%cookie_types[/renew_consent] cookies to enable google maps.'; |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Checks if a widget placeholder is enabled |
| 151 |
* |
| 152 |
* @return boolean true If widget placeholder is checked |
| 153 |
* false If widget placeholder is not checked |
| 154 |
* |
| 155 |
* @since 1.8.0 |
| 156 |
*/ |
| 157 |
public function is_widget_placeholder_enabled() { |
| 158 |
return $this->settings->is_widget_placeholder_enabled( $this->widget_option, $this->get_widget_option_name() ); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Checks if widget has existing placeholders |
| 163 |
* |
| 164 |
* @return mixed |
| 165 |
* |
| 166 |
* @since 1.8.0 |
| 167 |
*/ |
| 168 |
public function widget_has_placeholder() { |
| 169 |
return $this->settings->widget_has_placeholder( $this->widget_option, $this->get_widget_option_name() ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Returns all widget placeholders |
| 174 |
* |
| 175 |
* @return mixed |
| 176 |
* |
| 177 |
* @since 1.8.0 |
| 178 |
*/ |
| 179 |
public function get_widget_placeholders() { |
| 180 |
return $this->settings->get_widget_placeholders( $this->widget_option, $this->get_widget_option_name() ); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* returns widget placeholder |
| 185 |
* |
| 186 |
* @return mixed |
| 187 |
* |
| 188 |
* @since 1.8.0 |
| 189 |
*/ |
| 190 |
public function get_widget_placeholder() { |
| 191 |
return $this->settings->get_widget_placeholder( $this->widget_option, $this->get_widget_option_name(), $this->get_default_placeholder(), cookiebot_addons_output_cookie_types( $this->get_widget_cookie_types() ) ); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Start catching the output |
| 196 |
* |
| 197 |
* @since 1.2.0 |
| 198 |
*/ |
| 199 |
public function start_buffer() { |
| 200 |
ob_start( array( $this, 'manipulate_iframe' ) ); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Clear the buffer |
| 205 |
* |
| 206 |
* @since 1.2.0 |
| 207 |
*/ |
| 208 |
public function stop_buffer() { |
| 209 |
ob_end_flush(); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Return manipulated output with cookieconsent attribute |
| 214 |
* |
| 215 |
* @param $buffer |
| 216 |
* |
| 217 |
* @return mixed|null|string|string[] |
| 218 |
* |
| 219 |
* @since 1.2.0 |
| 220 |
*/ |
| 221 |
public function manipulate_iframe( $buffer ) { |
| 222 |
/** |
| 223 |
* Get wp head scripts from the cache |
| 224 |
*/ |
| 225 |
$updated_scripts = get_transient( 'jetpack_google_maps_widget' ); |
| 226 |
|
| 227 |
/** |
| 228 |
* If cache is not set then build it |
| 229 |
*/ |
| 230 |
if ( $updated_scripts === false ) { |
| 231 |
/** |
| 232 |
* Pattern to get all iframes |
| 233 |
*/ |
| 234 |
$pattern = "/\<iframe(.*?)?\>(.|\s)*?\<\/iframe\>/i"; |
| 235 |
|
| 236 |
/** |
| 237 |
* Get all scripts and add cookieconsent if it does match with the criterion |
| 238 |
*/ |
| 239 |
$updated_scripts = preg_replace_callback( $pattern, function ( $matches ) { |
| 240 |
|
| 241 |
$data = ( isset( $matches[0] ) ) ? $matches[0] : ''; |
| 242 |
|
| 243 |
$data = str_replace( 'src=', 'data-cookieconsent="' . cookiebot_addons_output_cookie_types( $this->cookie_types ) . '" data-src=', $data ); |
| 244 |
|
| 245 |
/** |
| 246 |
* Return updated iframe tag |
| 247 |
*/ |
| 248 |
return $data; |
| 249 |
}, $buffer ); |
| 250 |
|
| 251 |
/** |
| 252 |
* Set cache for 15 minutes |
| 253 |
*/ |
| 254 |
set_transient( 'jetpack_google_maps_widget', $updated_scripts, 60 * 15 ); |
| 255 |
} |
| 256 |
|
| 257 |
return $updated_scripts; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Show consent message when the consent is not given. |
| 262 |
* |
| 263 |
* @param $view string |
| 264 |
* @param $widget string |
| 265 |
* |
| 266 |
* @since 1.6.0 |
| 267 |
*/ |
| 268 |
public function cookie_consent_div( $view, $widget ) { |
| 269 |
if ( $widget == 'contact_info' && $view == 'widget_view' ) { |
| 270 |
if ( is_array( $this->get_widget_cookie_types() ) && count( $this->get_widget_cookie_types() ) > 0 ) { |
| 271 |
echo '<div class="cookieconsent-optout-' . cookiebot_addons_get_one_cookie_type( $this->get_widget_cookie_types() ) . '"> |
| 272 |
' . $this->get_widget_placeholder() . ' |
| 273 |
</div>'; |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Adds extra information under the label |
| 280 |
* |
| 281 |
* @return string |
| 282 |
* |
| 283 |
* @since 1.8.0 |
| 284 |
*/ |
| 285 |
public function get_extra_information() { |
| 286 |
return false; |
| 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 |
|