class-email-encoder-bundle-ajax.php
3 months ago
class-email-encoder-bundle-helpers.php
3 months ago
class-email-encoder-bundle-settings.php
3 months ago
index.php
6 years ago
class-email-encoder-bundle-ajax.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Legacy\EmailEncoderBundle; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | use OnlineOptimisation\EmailEncoderBundle\Traits\PluginHelper; |
| 8 | |
| 9 | class Email_Encoder_Ajax{ |
| 10 | |
| 11 | use PluginHelper; |
| 12 | |
| 13 | public function boot(): void { |
| 14 | add_action( 'init', [ $this, 'register_hooks' ] ); |
| 15 | } |
| 16 | |
| 17 | public function register_hooks(): void |
| 18 | { |
| 19 | $EEB = Email_Encoder::instance(); |
| 20 | $page = $EEB->settings->get_page_name(); |
| 21 | |
| 22 | $is_target_admin_page = $EEB->helpers->is_page( $page ) |
| 23 | || ( wp_doing_ajax() && ( $_POST['action'] ?? '' ) === 'eeb_get_email_form_output') |
| 24 | ; |
| 25 | |
| 26 | if ( $is_target_admin_page ) { |
| 27 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 28 | add_action( 'wp_ajax_eeb_get_email_form_output', [ $this, 'handle' ] ); |
| 29 | } |
| 30 | |
| 31 | if ( (bool) $EEB->settings->get_setting( 'encoder_form_frontend', true, 'encoder_form' ) ) { |
| 32 | add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 33 | add_action( 'wp_ajax_nopriv_eeb_get_email_form_output', [ $this, 'handle' ] ); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | |
| 38 | public function enqueue_scripts(): void |
| 39 | { |
| 40 | $file = EEB_PLUGIN_DIR . 'core/includes/assets/js/encoder-form.js'; |
| 41 | $ver = file_exists( $file ) ? filemtime( $file ) : false; |
| 42 | |
| 43 | wp_enqueue_script( |
| 44 | 'eeb-js-ajax-ef', |
| 45 | EEB_PLUGIN_URL . 'core/includes/assets/js/encoder-form.js', |
| 46 | [ 'jquery' ], |
| 47 | (string) $ver, |
| 48 | true |
| 49 | ); |
| 50 | |
| 51 | wp_localize_script( 'eeb-js-ajax-ef', 'eeb_ef', [ |
| 52 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 53 | 'security' => wp_create_nonce( 'eeb_form' ) |
| 54 | ] ); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | public function handle(): void |
| 59 | { |
| 60 | check_ajax_referer( 'eeb_form', 'eebsec' ); |
| 61 | |
| 62 | $email = sanitize_email( $_POST['eebEmail'] ?? '' ); |
| 63 | $method = sanitize_text_field( $_POST['eebMethod'] ?? '' ); |
| 64 | $display = wp_kses_post( $_POST['eebDisplay'] ?? '' ); |
| 65 | $display = $display ?: $email; |
| 66 | |
| 67 | $EEB = Email_Encoder::instance(); |
| 68 | |
| 69 | $class = esc_attr( $this->getSetting( 'class_name', true ) ); |
| 70 | $protect = (string) $this->getSetting( 'protection_text', true ); |
| 71 | $link = '<a href="mailto:' . $email . '" class="' . $class . '">' . $display . '</a>'; |
| 72 | |
| 73 | switch ( $method ) { |
| 74 | case 'rot13': |
| 75 | $link = $this->encodeAscii($link, $protect); |
| 76 | break; |
| 77 | |
| 78 | case 'escape': |
| 79 | $link = $this->encodeEscape($link, $protect); |
| 80 | break; |
| 81 | |
| 82 | default: |
| 83 | $link = '<a href="mailto:' . antispambot($email) . '" class="' . $class . '">' . antispambot($display) . '</a>'; |
| 84 | } |
| 85 | |
| 86 | # @TODO: Proper way to do this |
| 87 | // wp_send_json_success( apply_filters('eeb/ajax/encoder_form_response', $link) ); |
| 88 | |
| 89 | # @TODO: Old way |
| 90 | // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is encoded email HTML built with antispambot/rot13/escape encoding; escaping would break the protection. |
| 91 | echo apply_filters('eeb/ajax/encoder_form_response', $link); |
| 92 | exit; |
| 93 | } |
| 94 | |
| 95 | } |
| 96 |