PluginProbe ʕ •ᴥ•ʔ
Email Encoder – Protect Email Addresses and Phone Numbers / 2.5.1
Email Encoder – Protect Email Addresses and Phone Numbers v2.5.1
2.5.2 2.5.1 2.5.0 2.4.8 trunk 0.10 0.11 0.12 0.20 0.21 0.22 0.30 0.31 0.32 0.40 0.41 0.42 0.50 0.60 0.70 0.71 0.80 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.5 1.5.2 1.51 1.53 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.10 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7
email-encoder-bundle / core / includes / classes / class-email-encoder-bundle-settings.php
email-encoder-bundle / core / includes / classes Last commit date
class-email-encoder-bundle-ajax.php 2 months ago class-email-encoder-bundle-helpers.php 3 months ago class-email-encoder-bundle-settings.php 2 months ago index.php 6 years ago
class-email-encoder-bundle-settings.php
578 lines
1 <?php
2
3 namespace Legacy\EmailEncoderBundle;
4
5 if ( ! defined( 'ABSPATH' ) ) exit;
6
7 class Email_Encoder_Settings {
8
9 public const PROTECT_FULL_PAGE = 1;
10 public const PROTECT_FILTERS_ONLY = 2;
11 public const PROTECT_DISABLED = 3;
12
13 private string $admin_cap = 'manage_options';
14 private string $page_name = 'email-encoder-bundle-option-page';
15 private string $page_title;
16 private string $final_output_buffer_hook = 'final_output';
17 private string $widget_callback_hook = 'widget_output';
18 private string $settings_key= 'WP_Email_Encoder_Bundle_options';
19 private string $version_key= 'email-encoder-bundle-version';
20 private string $image_secret_key = 'email-encoder-bundle-img-key';
21 private string $at_identifier = '##eebAddIdent##';
22 private ?string $previous_version = null;
23
24 /** @var array< string, int > */
25 private array $hook_priorities = [ // deprecated!
26 'buffer_final_output' => 1000,
27 'setup_single_filter_hooks' => 100,
28 'add_custom_template_tags' => 10,
29 'load_frontend_header_styling' => 10,
30 'filter_rss' => 100,
31 'filter_page' => 100,
32 'filter_content' => 100,
33 'first_version_init' => 100,
34 'version_update' => 100,
35 'display_email_image' => 999,
36 'callback_rss_remove_shortcodes' => 10,
37 'load_ajax_scripts_styles' => 10,
38 'load_ajax_scripts_styles_admin' => 10,
39 'reload_settings_for_integrations' => 5,
40 // 'eeb_dynamic_sidebar_params' => 100, //deprecated but kept for compatibility
41 ];
42
43 /** @var array< string, array< string, mixed > > */
44 private array $safe_attr_html;
45
46 private string $email_regex = '([_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,}))';
47
48 /** @var array< string, string > > */
49 private array $soft_attribute_regex = [
50 'woocommerce_variation_attribute_tag' => '/data-product_variations="([^"]*)"/i',
51 'jetpack_carousel_image_attribute_tag' => '/data-image-meta="([^"]*)"/i',
52 'html_placeholder_tag' => '/placeholder="([^"]*)"/i',
53 ];
54
55 /** @var array< string, array< string, mixed > > */
56 private array $settings = [];
57
58 /** @var array< string, mixed > */
59 private array $values = [];
60
61 private string $version;
62 private string $email_image_secret;
63
64 /** @var array< string, string > > */
65 private array $template_tags= [
66 'eeb_filter' => 'template_tag_eeb_filter',
67 'eeb_mailto' => 'template_tag_eeb_mailto'
68 ];
69
70 /** @var array< string, mixed > > */
71 private array $default_values = [
72 'protect' => self::PROTECT_FULL_PAGE,
73 'filter_rss' => 1,
74 'protect_using' => 'with_javascript',
75 'class_name' => 'mail-link',
76 'protection_text' => '*protected email*',
77 'image_color' => '0,0,0',
78 'image_background_color' => '0,0,0',
79 'image_text_opacity' => '0',
80 'image_underline' => '0',
81 'image_background_opacity' => '127',
82 'image_font_size' => '4',
83 ];
84
85 /**
86 * Email_Encoder_Settings constructor.
87 *
88 * We define all of our necessary settings in here.
89 * If you need to do plugin related changes, everything will
90 * be available in this file.
91 */
92 function __construct() {
93 $this->page_title = EEB_NAME;
94 $this->safe_attr_html = require EEB_PLUGIN_DIR . '/config/SafeHtmlConfig.php';
95
96 add_action( 'init', [ $this, 'load_values' ], 1 );
97 add_action( 'init', [ $this, 'load_settings' ] );
98 add_action( 'init', [ $this, 'load_version' ] );
99 add_action( 'init', [ $this, 'load_email_image_secret' ] );
100 }
101
102 /**
103 * @return array< string, mixed >
104 */
105 public function get_saved(): array {
106 return get_option( $this->settings_key, [] );
107 }
108
109 /**
110 * @return array< string, mixed >
111 */
112 public function get_values(): array {
113 if ( $this->values === [] ) {
114 $this->load_values();
115 }
116 return $this->values;
117 }
118 /**
119 * ######################
120 * ###
121 * #### MAIN SETTINGS
122 * ###
123 * ######################
124 */
125
126 /**
127 * Load setting values from the database without triggering translations.
128 *
129 * This runs early (init:1) so that get_setting() can return values
130 * before load_settings() populates the full field definitions.
131 * Prevents _load_textdomain_just_in_time warnings on WP 6.7+.
132 *
133 * @return void
134 */
135 public function load_values(): void {
136 $saved_values = get_option( $this->settings_key, [] );
137
138 // Fresh install: opt new sites into the top-level admin menu.
139 // Existing installs (non-empty saved options) keep whatever they had
140 // — historically the absence of this key meant "submenu under Settings",
141 // and silently flipping that on update would surprise active users.
142 $is_fresh_install = empty( $saved_values );
143
144 $this->values = array_replace_recursive( $this->default_values, $saved_values );
145
146 if ( $is_fresh_install ) {
147 $this->values['own_admin_menu'] = 1;
148 }
149
150 if ( $this->values != $saved_values ) {
151 update_option( $this->settings_key, $this->values );
152 }
153 }
154
155 /**
156 * Load the settings for our admin settings page
157 *
158 * @return void
159 */
160 public function load_settings() {
161
162 if ( $this->values === [] ) {
163 $this->load_values();
164 }
165
166 $fields = require EEB_PLUGIN_DIR . '/config/SettingsConfig.php';
167 $fields = apply_filters( 'eeb/settings/pre_filter_fields', $fields );
168
169 $values = $this->values;
170
171 foreach ( $fields as $key => $field ) {
172 if ( $field['type'] === 'multi-input' ) {
173 foreach ( $field['inputs'] as $smi_key => $smi_data ) {
174
175 if ( $field['input-type'] === 'radio' ) {
176 if ( isset( $values[ $key ] ) && (string) $values[ $key ] === (string) $smi_key ) {
177 $fields[ $key ]['value'] = $values[ $key ];
178 }
179 }
180 else {
181 if ( isset( $values[ $smi_key ] ) ) {
182 $fields[ $key ]['inputs'][ $smi_key ]['value'] = $values[ $smi_key ];
183 }
184 }
185
186 }
187 }
188 else {
189 if ( isset( $values[ $key ] ) ) {
190 $fields[ $key ]['value'] = $values[ $key ];
191 }
192 }
193 }
194
195 $this->settings = apply_filters( 'eeb/settings/fields', $fields );
196 }
197
198 /**
199 * ######################
200 * ###
201 * #### VERSIONING
202 * ###
203 * ######################
204 */
205
206 /**
207 * @return string
208 */
209 public function load_version() {
210
211 $current_version = get_option( $this->get_version_key() );
212
213 if ( empty( $current_version ) ) {
214 $current_version = EEB_VERSION;
215 update_option( $this->get_version_key(), $current_version );
216
217 add_action( 'init', array( $this, 'first_version_init' ), $this->get_hook_priorities( 'first_version_init' ) );
218 }
219 else {
220 if ( $current_version !== EEB_VERSION ) {
221 $this->previous_version = $current_version;
222 $current_version = EEB_VERSION;
223 update_option( $this->get_version_key(), $current_version );
224
225 add_action( 'init', array( $this, 'version_update' ), $this->get_hook_priorities( 'version_update' ) );
226 }
227 }
228
229 $this->version = $current_version;
230 return $current_version;
231 }
232
233
234 public function load_email_image_secret(): void {
235
236 if ( ! (bool) $this->get_setting( 'convert_plain_to_image', true, 'filter_body' ) ) {
237 return;
238 }
239
240 $image_descret = get_option( $this->get_image_secret_key() );
241
242 if ( ! empty( $image_descret ) ) {
243 $this->email_image_secret = $image_descret;
244 return;
245 }
246
247 $key = '';
248
249 for ( $i = 0; $i < 265; $i++ ) {
250 $key .= chr( wp_rand( 33, 126 ) );
251 }
252
253 update_option( $this->get_image_secret_key(), $key );
254
255 $this->email_image_secret = $key;
256 // return $key;
257 }
258
259 /**
260 * Fires an action after our settings key was initially set
261 * the very first time.
262 *
263 * @return void
264 */
265 public function first_version_init(): void {
266 do_action( 'eeb/settings/first_version_init', EEB_VERSION );
267 }
268
269 /**
270 * Fires after the version of the plugin is initially updated
271 *
272 * @return void
273 */
274 public function version_update(): void {
275 do_action( 'eeb/settings/version_update', EEB_VERSION, $this->previous_version );
276 }
277
278 /**
279 * ######################
280 * ###
281 * #### CALLABLE FUNCTIONS
282 * ###
283 * ######################
284 */
285
286 /**
287 * Our admin cap handler function
288 *
289 * This function handles the admin capability throughout
290 * the whole plugin.
291 *
292 * $target - With the target function you can make a more precised filtering
293 * by changing it for specific actions.
294 *
295 * @param string $target - A identifier where the call comes from
296 * @return mixed
297 */
298 public function get_admin_cap( $target = 'main' ) {
299 /**
300 * Customize the globally used capability for this plugin
301 *
302 * This filter is called every time the capability is needed.
303 */
304 return apply_filters( 'eeb/settings/capability', $this->admin_cap, $target );
305 }
306
307 /**
308 * Return the page name for our admin page
309 *
310 * @return string - the page name
311 */
312 public function get_page_name() {
313 return apply_filters( 'eeb/settings/page_name', $this->page_name );
314 }
315
316 /**
317 * Return the page title for our admin page
318 *
319 * @return string - the page title
320 */
321 public function get_page_title() {
322 return apply_filters( 'eeb/settings/page_title', $this->page_title );
323 }
324
325 /**
326 * Return the settings_key
327 *
328 * @return string - the settings key
329 */
330 public function get_settings_key() {
331 return $this->settings_key;
332 }
333
334 /**
335 * Return the version_key
336 *
337 * @return string - the version_key
338 */
339 public function get_version_key() {
340 return $this->version_key;
341 }
342
343 /**
344 * Return the image_secret_key
345 *
346 * @return string - the image_secret_key
347 */
348 public function get_image_secret_key() {
349 return $this->image_secret_key;
350 }
351
352 /**
353 * Return the email_image_secret
354 *
355 * @return string - the email_image_secret
356 */
357 public function get_email_image_secret() {
358 return $this->email_image_secret;
359 }
360
361 /**
362 * Return the version
363 *
364 * @return string - the version
365 */
366 public function get_version() {
367 return apply_filters( 'eeb/settings/get_version', $this->version );
368 }
369
370 /**
371 * Return the default template tags
372 *
373 * @return array< string, string > - the template tags
374 */
375 public function get_template_tags() {
376 return apply_filters( 'eeb/settings/get_template_tags', $this->template_tags );
377 }
378
379 /**
380 * Return the widget callback hook name
381 *
382 * @return string - the final widget callback hook name
383 */
384 public function get_widget_callback_hook() {
385 return apply_filters( 'eeb/settings/widget_callback_hook', $this->widget_callback_hook );
386 }
387
388 /**
389 * Return the final output buffer hook name
390 *
391 * @return string - the final output buffer hook name
392 */
393 public function get_final_output_buffer_hook() {
394 return apply_filters( 'eeb/settings/final_output_buffer_hook', $this->final_output_buffer_hook );
395 }
396
397 /**
398 * Return the @ symbol identifier
399 *
400 * @return string - the @ symbol identifier
401 */
402 public function get_at_identifier() {
403 return apply_filters( 'eeb/settings/at_identifier', $this->at_identifier );
404 }
405
406 /**
407 * @link http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/
408 * @param boolean $include
409 * @return string
410 */
411 public function get_email_regex( $include = false ) {
412
413 if ( $include === true ) {
414 $return = $this->email_regex;
415 } else {
416 $return = '/' . $this->email_regex . '/i';
417 }
418
419 return apply_filters( 'eeb/settings/get_email_regex', $return, $include );
420 }
421
422 /**
423 * Get Woocommerce variation attribute regex
424 *
425 * @param string $single
426 * @return string
427 */
428 public function get_soft_attribute_regex( $single = null ) {
429
430 $return = $this->soft_attribute_regex;
431
432 if ( $single !== null ) {
433 if ( isset( $this->soft_attribute_regex[ $single ] ) ) {
434 $return = $this->soft_attribute_regex[ $single ];
435 } else {
436 $return = false;
437 }
438 }
439
440 return apply_filters( 'eeb/settings/get_soft_attribute_regex', $return, $single );
441 }
442
443 /**
444 * Get hook priorities
445 *
446 * @param string $single - wether you want to return only a single hook priority or not
447 * @return mixed - An array or string of hook priority(-ies)
448 */
449 public function get_hook_priorities( ?string $single = null ) {
450
451 $is_single = $single && isset( $this->hook_priorities[ $single ] );
452
453 $return = $is_single
454 ? $this->hook_priorities[ $single ]
455 : ( $single ? 10 : $this->hook_priorities )
456 ;
457 $default = $is_single ? true : false;
458
459 // if ( $single ) {
460 // if ( isset( $this->hook_priorities[ $single ] ) ) {
461 // $return = $this->hook_priorities[ $single ];
462 // $default = false;
463 // } else {
464 // $return = 10;
465 // $default = true;
466 // }
467 // }
468
469 return apply_filters( 'eeb/settings/get_hook_priorities', $return, $default, $single );
470 }
471
472 /**
473 * Get a collection of safe HTML attributes
474 *
475 * @return array< string, array< string, mixed > >
476 */
477 public function get_safe_html_attr() {
478 return apply_filters( 'eeb/settings/get_safe_html_attr', $this->safe_attr_html );
479 }
480
481 /**
482 * ######################
483 * ###
484 * #### Settings helper
485 * ###
486 * ######################
487 */
488
489 /**
490 * Get the admin page url
491 *
492 * @return string - The admin page url
493 */
494 public function get_admin_page_url() {
495
496 $url = admin_url( "options-general.php?page=" . $this->get_page_name() );
497
498 return apply_filters( 'eeb/settings/get_admin_page_url', $url );
499 }
500
501 /**
502 * Helper function to reload the settings
503 *
504 * @return void
505 */
506 public function reload_settings() {
507 $this->load_values();
508 $this->load_settings();
509 }
510
511 /**
512 * Return the default strings that are available for this plugin.
513 *
514 * @param string $slug - the identifier for your specified setting
515 * @param bool $single - wether you only want to return the value or the whole settings element
516 * @param string $group - in case you call a multi-input that contains multiple values (e.g. checkbox), you can set a sub-slug to grab the sub value
517 * @return mixed - the default string
518 */
519 public function get_setting( $slug = '', $single = false, $group = '' ) {
520
521 // When only the value is needed and full field definitions haven't loaded yet,
522 // return directly from the lightweight values array to avoid triggering
523 // translation loading (SettingsConfig.php) too early.
524 if ( $this->settings === [] && $single && $slug !== '' ) {
525 if ( $this->values === [] ) {
526 $this->load_values();
527 }
528
529 return $this->values[ $slug ] ?? false;
530 }
531
532 // Full field structure requested — ensure fields are loaded
533 if ( $this->settings === [] ) {
534 $this->load_settings();
535 }
536
537 $return = $this->settings;
538
539 if ( empty( $slug ) ) {
540 return $return;
541 }
542
543 if ( isset( $this->settings[ $slug ] ) || ( ! empty( $group ) && isset( $this->settings[ $group ] ) ) ) {
544 if ( $single ) {
545 $return = false; // Default false
546
547 //Set default to the main valie if available given with radio buttons)
548 if ( isset( $this->settings[ $slug ]['value'] ) ) {
549 $return = $this->settings[ $slug ]['value'];
550 }
551
552 if (
553 ! empty( $group )
554 && isset( $this->settings[ $group ]['type'] )
555 && $this->settings[ $group ]['type'] === 'multi-input'
556 )
557 {
558 if ( isset( $this->settings[ $group ]['inputs'][ $slug ] ) && isset( $this->settings[ $group ]['inputs'][ $slug ]['value'] ) ) {
559 $return = $this->settings[ $group ]['inputs'][ $slug ]['value'];
560 }
561 }
562
563 } else {
564
565 if ( ! empty( $group ) && isset( $this->settings[ $group ] ) ) {
566 $return = $this->settings[ $group ];
567 } else {
568 $return = $this->settings[ $slug ];
569 }
570
571 }
572
573 }
574
575 return $return;
576 }
577
578 }