class-email-encoder-bundle-ajax.php
3 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
568 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 | 'powered_by' => 1, |
| 75 | 'protect_using' => 'with_javascript', |
| 76 | 'class_name' => 'mail-link', |
| 77 | 'protection_text' => '*protected email*', |
| 78 | 'image_color' => '0,0,0', |
| 79 | 'image_background_color' => '0,0,0', |
| 80 | 'image_text_opacity' => '0', |
| 81 | 'image_underline' => '0', |
| 82 | 'image_background_opacity' => '127', |
| 83 | 'image_font_size' => '4', |
| 84 | ]; |
| 85 | |
| 86 | /** |
| 87 | * Email_Encoder_Settings constructor. |
| 88 | * |
| 89 | * We define all of our necessary settings in here. |
| 90 | * If you need to do plugin related changes, everything will |
| 91 | * be available in this file. |
| 92 | */ |
| 93 | function __construct() { |
| 94 | $this->page_title = EEB_NAME; |
| 95 | $this->safe_attr_html = require EEB_PLUGIN_DIR . '/config/SafeHtmlConfig.php'; |
| 96 | |
| 97 | add_action( 'init', [ $this, 'load_values' ], 1 ); |
| 98 | add_action( 'init', [ $this, 'load_settings' ] ); |
| 99 | add_action( 'init', [ $this, 'load_version' ] ); |
| 100 | add_action( 'init', [ $this, 'load_email_image_secret' ] ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @return array< string, mixed > |
| 105 | */ |
| 106 | public function get_saved(): array { |
| 107 | return get_option( $this->settings_key, [] ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @return array< string, mixed > |
| 112 | */ |
| 113 | public function get_values(): array { |
| 114 | if ( $this->values === [] ) { |
| 115 | $this->load_values(); |
| 116 | } |
| 117 | return $this->values; |
| 118 | } |
| 119 | /** |
| 120 | * ###################### |
| 121 | * ### |
| 122 | * #### MAIN SETTINGS |
| 123 | * ### |
| 124 | * ###################### |
| 125 | */ |
| 126 | |
| 127 | /** |
| 128 | * Load setting values from the database without triggering translations. |
| 129 | * |
| 130 | * This runs early (init:1) so that get_setting() can return values |
| 131 | * before load_settings() populates the full field definitions. |
| 132 | * Prevents _load_textdomain_just_in_time warnings on WP 6.7+. |
| 133 | * |
| 134 | * @return void |
| 135 | */ |
| 136 | public function load_values(): void { |
| 137 | $saved_values = get_option( $this->settings_key, [] ); |
| 138 | $this->values = array_replace_recursive( $this->default_values, $saved_values ); |
| 139 | |
| 140 | if ( $this->values != $saved_values ) { |
| 141 | update_option( $this->settings_key, $this->values ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Load the settings for our admin settings page |
| 147 | * |
| 148 | * @return void |
| 149 | */ |
| 150 | public function load_settings() { |
| 151 | |
| 152 | if ( $this->values === [] ) { |
| 153 | $this->load_values(); |
| 154 | } |
| 155 | |
| 156 | $fields = require EEB_PLUGIN_DIR . '/config/SettingsConfig.php'; |
| 157 | $fields = apply_filters( 'eeb/settings/pre_filter_fields', $fields ); |
| 158 | |
| 159 | $values = $this->values; |
| 160 | |
| 161 | foreach ( $fields as $key => $field ) { |
| 162 | if ( $field['type'] === 'multi-input' ) { |
| 163 | foreach ( $field['inputs'] as $smi_key => $smi_data ) { |
| 164 | |
| 165 | if ( $field['input-type'] === 'radio' ) { |
| 166 | if ( isset( $values[ $key ] ) && (string) $values[ $key ] === (string) $smi_key ) { |
| 167 | $fields[ $key ]['value'] = $values[ $key ]; |
| 168 | } |
| 169 | } |
| 170 | else { |
| 171 | if ( isset( $values[ $smi_key ] ) ) { |
| 172 | $fields[ $key ]['inputs'][ $smi_key ]['value'] = $values[ $smi_key ]; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | } |
| 178 | else { |
| 179 | if ( isset( $values[ $key ] ) ) { |
| 180 | $fields[ $key ]['value'] = $values[ $key ]; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | $this->settings = apply_filters( 'eeb/settings/fields', $fields ); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * ###################### |
| 190 | * ### |
| 191 | * #### VERSIONING |
| 192 | * ### |
| 193 | * ###################### |
| 194 | */ |
| 195 | |
| 196 | /** |
| 197 | * @return string |
| 198 | */ |
| 199 | public function load_version() { |
| 200 | |
| 201 | $current_version = get_option( $this->get_version_key() ); |
| 202 | |
| 203 | if ( empty( $current_version ) ) { |
| 204 | $current_version = EEB_VERSION; |
| 205 | update_option( $this->get_version_key(), $current_version ); |
| 206 | |
| 207 | add_action( 'init', array( $this, 'first_version_init' ), $this->get_hook_priorities( 'first_version_init' ) ); |
| 208 | } |
| 209 | else { |
| 210 | if ( $current_version !== EEB_VERSION ) { |
| 211 | $this->previous_version = $current_version; |
| 212 | $current_version = EEB_VERSION; |
| 213 | update_option( $this->get_version_key(), $current_version ); |
| 214 | |
| 215 | add_action( 'init', array( $this, 'version_update' ), $this->get_hook_priorities( 'version_update' ) ); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | $this->version = $current_version; |
| 220 | return $current_version; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | public function load_email_image_secret(): void { |
| 225 | |
| 226 | if ( ! (bool) $this->get_setting( 'convert_plain_to_image', true, 'filter_body' ) ) { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | $image_descret = get_option( $this->get_image_secret_key() ); |
| 231 | |
| 232 | if ( ! empty( $image_descret ) ) { |
| 233 | $this->email_image_secret = $image_descret; |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | $key = ''; |
| 238 | |
| 239 | for ( $i = 0; $i < 265; $i++ ) { |
| 240 | $key .= chr( wp_rand( 33, 126 ) ); |
| 241 | } |
| 242 | |
| 243 | update_option( $this->get_image_secret_key(), $key ); |
| 244 | |
| 245 | $this->email_image_secret = $key; |
| 246 | // return $key; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Fires an action after our settings key was initially set |
| 251 | * the very first time. |
| 252 | * |
| 253 | * @return void |
| 254 | */ |
| 255 | public function first_version_init(): void { |
| 256 | do_action( 'eeb/settings/first_version_init', EEB_VERSION ); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Fires after the version of the plugin is initially updated |
| 261 | * |
| 262 | * @return void |
| 263 | */ |
| 264 | public function version_update(): void { |
| 265 | do_action( 'eeb/settings/version_update', EEB_VERSION, $this->previous_version ); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * ###################### |
| 270 | * ### |
| 271 | * #### CALLABLE FUNCTIONS |
| 272 | * ### |
| 273 | * ###################### |
| 274 | */ |
| 275 | |
| 276 | /** |
| 277 | * Our admin cap handler function |
| 278 | * |
| 279 | * This function handles the admin capability throughout |
| 280 | * the whole plugin. |
| 281 | * |
| 282 | * $target - With the target function you can make a more precised filtering |
| 283 | * by changing it for specific actions. |
| 284 | * |
| 285 | * @param string $target - A identifier where the call comes from |
| 286 | * @return mixed |
| 287 | */ |
| 288 | public function get_admin_cap( $target = 'main' ) { |
| 289 | /** |
| 290 | * Customize the globally used capability for this plugin |
| 291 | * |
| 292 | * This filter is called every time the capability is needed. |
| 293 | */ |
| 294 | return apply_filters( 'eeb/settings/capability', $this->admin_cap, $target ); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Return the page name for our admin page |
| 299 | * |
| 300 | * @return string - the page name |
| 301 | */ |
| 302 | public function get_page_name() { |
| 303 | return apply_filters( 'eeb/settings/page_name', $this->page_name ); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Return the page title for our admin page |
| 308 | * |
| 309 | * @return string - the page title |
| 310 | */ |
| 311 | public function get_page_title() { |
| 312 | return apply_filters( 'eeb/settings/page_title', $this->page_title ); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Return the settings_key |
| 317 | * |
| 318 | * @return string - the settings key |
| 319 | */ |
| 320 | public function get_settings_key() { |
| 321 | return $this->settings_key; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Return the version_key |
| 326 | * |
| 327 | * @return string - the version_key |
| 328 | */ |
| 329 | public function get_version_key() { |
| 330 | return $this->version_key; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Return the image_secret_key |
| 335 | * |
| 336 | * @return string - the image_secret_key |
| 337 | */ |
| 338 | public function get_image_secret_key() { |
| 339 | return $this->image_secret_key; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Return the email_image_secret |
| 344 | * |
| 345 | * @return string - the email_image_secret |
| 346 | */ |
| 347 | public function get_email_image_secret() { |
| 348 | return $this->email_image_secret; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Return the version |
| 353 | * |
| 354 | * @return string - the version |
| 355 | */ |
| 356 | public function get_version() { |
| 357 | return apply_filters( 'eeb/settings/get_version', $this->version ); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Return the default template tags |
| 362 | * |
| 363 | * @return array< string, string > - the template tags |
| 364 | */ |
| 365 | public function get_template_tags() { |
| 366 | return apply_filters( 'eeb/settings/get_template_tags', $this->template_tags ); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Return the widget callback hook name |
| 371 | * |
| 372 | * @return string - the final widget callback hook name |
| 373 | */ |
| 374 | public function get_widget_callback_hook() { |
| 375 | return apply_filters( 'eeb/settings/widget_callback_hook', $this->widget_callback_hook ); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Return the final output buffer hook name |
| 380 | * |
| 381 | * @return string - the final output buffer hook name |
| 382 | */ |
| 383 | public function get_final_output_buffer_hook() { |
| 384 | return apply_filters( 'eeb/settings/final_output_buffer_hook', $this->final_output_buffer_hook ); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Return the @ symbol identifier |
| 389 | * |
| 390 | * @return string - the @ symbol identifier |
| 391 | */ |
| 392 | public function get_at_identifier() { |
| 393 | return apply_filters( 'eeb/settings/at_identifier', $this->at_identifier ); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * @link http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/ |
| 398 | * @param boolean $include |
| 399 | * @return string |
| 400 | */ |
| 401 | public function get_email_regex( $include = false ) { |
| 402 | |
| 403 | if ( $include === true ) { |
| 404 | $return = $this->email_regex; |
| 405 | } else { |
| 406 | $return = '/' . $this->email_regex . '/i'; |
| 407 | } |
| 408 | |
| 409 | return apply_filters( 'eeb/settings/get_email_regex', $return, $include ); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Get Woocommerce variation attribute regex |
| 414 | * |
| 415 | * @param string $single |
| 416 | * @return string |
| 417 | */ |
| 418 | public function get_soft_attribute_regex( $single = null ) { |
| 419 | |
| 420 | $return = $this->soft_attribute_regex; |
| 421 | |
| 422 | if ( $single !== null ) { |
| 423 | if ( isset( $this->soft_attribute_regex[ $single ] ) ) { |
| 424 | $return = $this->soft_attribute_regex[ $single ]; |
| 425 | } else { |
| 426 | $return = false; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return apply_filters( 'eeb/settings/get_soft_attribute_regex', $return, $single ); |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Get hook priorities |
| 435 | * |
| 436 | * @param string $single - wether you want to return only a single hook priority or not |
| 437 | * @return mixed - An array or string of hook priority(-ies) |
| 438 | */ |
| 439 | public function get_hook_priorities( ?string $single = null ) { |
| 440 | |
| 441 | $is_single = $single && isset( $this->hook_priorities[ $single ] ); |
| 442 | |
| 443 | $return = $is_single |
| 444 | ? $this->hook_priorities[ $single ] |
| 445 | : ( $single ? 10 : $this->hook_priorities ) |
| 446 | ; |
| 447 | $default = $is_single ? true : false; |
| 448 | |
| 449 | // if ( $single ) { |
| 450 | // if ( isset( $this->hook_priorities[ $single ] ) ) { |
| 451 | // $return = $this->hook_priorities[ $single ]; |
| 452 | // $default = false; |
| 453 | // } else { |
| 454 | // $return = 10; |
| 455 | // $default = true; |
| 456 | // } |
| 457 | // } |
| 458 | |
| 459 | return apply_filters( 'eeb/settings/get_hook_priorities', $return, $default, $single ); |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Get a collection of safe HTML attributes |
| 464 | * |
| 465 | * @return array< string, array< string, mixed > > |
| 466 | */ |
| 467 | public function get_safe_html_attr() { |
| 468 | return apply_filters( 'eeb/settings/get_safe_html_attr', $this->safe_attr_html ); |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * ###################### |
| 473 | * ### |
| 474 | * #### Settings helper |
| 475 | * ### |
| 476 | * ###################### |
| 477 | */ |
| 478 | |
| 479 | /** |
| 480 | * Get the admin page url |
| 481 | * |
| 482 | * @return string - The admin page url |
| 483 | */ |
| 484 | public function get_admin_page_url() { |
| 485 | |
| 486 | $url = admin_url( "options-general.php?page=" . $this->get_page_name() ); |
| 487 | |
| 488 | return apply_filters( 'eeb/settings/get_admin_page_url', $url ); |
| 489 | } |
| 490 | |
| 491 | /** |
| 492 | * Helper function to reload the settings |
| 493 | * |
| 494 | * @return void |
| 495 | */ |
| 496 | public function reload_settings() { |
| 497 | $this->load_values(); |
| 498 | $this->load_settings(); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Return the default strings that are available for this plugin. |
| 503 | * |
| 504 | * @param string $slug - the identifier for your specified setting |
| 505 | * @param bool $single - wether you only want to return the value or the whole settings element |
| 506 | * @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 |
| 507 | * @return mixed - the default string |
| 508 | */ |
| 509 | public function get_setting( $slug = '', $single = false, $group = '' ) { |
| 510 | |
| 511 | // When only the value is needed and full field definitions haven't loaded yet, |
| 512 | // return directly from the lightweight values array to avoid triggering |
| 513 | // translation loading (SettingsConfig.php) too early. |
| 514 | if ( $this->settings === [] && $single && $slug !== '' ) { |
| 515 | if ( $this->values === [] ) { |
| 516 | $this->load_values(); |
| 517 | } |
| 518 | |
| 519 | return $this->values[ $slug ] ?? false; |
| 520 | } |
| 521 | |
| 522 | // Full field structure requested — ensure fields are loaded |
| 523 | if ( $this->settings === [] ) { |
| 524 | $this->load_settings(); |
| 525 | } |
| 526 | |
| 527 | $return = $this->settings; |
| 528 | |
| 529 | if ( empty( $slug ) ) { |
| 530 | return $return; |
| 531 | } |
| 532 | |
| 533 | if ( isset( $this->settings[ $slug ] ) || ( ! empty( $group ) && isset( $this->settings[ $group ] ) ) ) { |
| 534 | if ( $single ) { |
| 535 | $return = false; // Default false |
| 536 | |
| 537 | //Set default to the main valie if available given with radio buttons) |
| 538 | if ( isset( $this->settings[ $slug ]['value'] ) ) { |
| 539 | $return = $this->settings[ $slug ]['value']; |
| 540 | } |
| 541 | |
| 542 | if ( |
| 543 | ! empty( $group ) |
| 544 | && isset( $this->settings[ $group ]['type'] ) |
| 545 | && $this->settings[ $group ]['type'] === 'multi-input' |
| 546 | ) |
| 547 | { |
| 548 | if ( isset( $this->settings[ $group ]['inputs'][ $slug ] ) && isset( $this->settings[ $group ]['inputs'][ $slug ]['value'] ) ) { |
| 549 | $return = $this->settings[ $group ]['inputs'][ $slug ]['value']; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | } else { |
| 554 | |
| 555 | if ( ! empty( $group ) && isset( $this->settings[ $group ] ) ) { |
| 556 | $return = $this->settings[ $group ]; |
| 557 | } else { |
| 558 | $return = $this->settings[ $slug ]; |
| 559 | } |
| 560 | |
| 561 | } |
| 562 | |
| 563 | } |
| 564 | |
| 565 | return $return; |
| 566 | } |
| 567 | |
| 568 | } |