class-email-encoder-bundle-ajax.php
2 years ago
class-email-encoder-bundle-helpers.php
2 years ago
class-email-encoder-bundle-run-admin.php
2 years ago
class-email-encoder-bundle-run.php
2 years ago
class-email-encoder-bundle-settings.php
2 years ago
class-email-encoder-bundle-validate.php
2 years ago
index.php
2 years ago
class-email-encoder-bundle-ajax.php
132 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Email_Encoder_Ajax |
| 5 | * |
| 6 | * Thats where we bring the plugin to life |
| 7 | * |
| 8 | * @since 2.0.0 |
| 9 | * @package EEB |
| 10 | * @author Ironikus <info@ironikus.com> |
| 11 | */ |
| 12 | |
| 13 | class Email_Encoder_Ajax{ |
| 14 | |
| 15 | /** |
| 16 | * The main page name for our admin page |
| 17 | * |
| 18 | * @var string |
| 19 | * @since 2.0.0 |
| 20 | */ |
| 21 | private $page_name; |
| 22 | |
| 23 | /** |
| 24 | * The main page title for our admin page |
| 25 | * |
| 26 | * @var string |
| 27 | * @since 2.0.0 |
| 28 | */ |
| 29 | private $page_title; |
| 30 | |
| 31 | /** |
| 32 | * Our Email_Encoder_Run constructor. |
| 33 | */ |
| 34 | function __construct(){ |
| 35 | $this->page_name = EEB()->settings->get_page_name(); |
| 36 | $this->page_title = EEB()->settings->get_page_title(); |
| 37 | $this->add_hooks(); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Define all of our necessary hooks |
| 42 | */ |
| 43 | private function add_hooks(){ |
| 44 | |
| 45 | if( |
| 46 | EEB()->helpers->is_page( $this->page_name ) |
| 47 | || ( wp_doing_ajax() && isset( $_POST['action'] ) && $_POST['action'] === 'eeb_get_email_form_output' ) |
| 48 | ){ |
| 49 | add_action( 'admin_enqueue_scripts', array( $this, 'load_ajax_scripts_styles' ), EEB()->settings->get_hook_priorities( 'load_ajax_scripts_styles_admin' ) ); |
| 50 | add_action( 'wp_ajax_eeb_get_email_form_output', array( $this, 'eeb_ajax_email_encoder_response' ) ); |
| 51 | } |
| 52 | |
| 53 | $form_frontend = (bool) EEB()->settings->get_setting( 'encoder_form_frontend', true, 'encoder_form' ); |
| 54 | |
| 55 | if( $form_frontend ){ |
| 56 | add_action( 'wp_enqueue_scripts', array( $this, 'load_ajax_scripts_styles' ), EEB()->settings->get_hook_priorities( 'load_ajax_scripts_styles' ) ); |
| 57 | add_action( 'wp_ajax_nopriv_eeb_get_email_form_output', array( $this, 'eeb_ajax_email_encoder_response' ) ); |
| 58 | } |
| 59 | |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * ###################### |
| 64 | * ### |
| 65 | * #### SCRIPTS & STYLES |
| 66 | * ### |
| 67 | * ###################### |
| 68 | */ |
| 69 | |
| 70 | /** |
| 71 | * Register all necessary scripts and styles |
| 72 | * |
| 73 | * @since 2.0.0 |
| 74 | */ |
| 75 | public function load_ajax_scripts_styles() { |
| 76 | |
| 77 | $js_version_form = date( "ymd-Gis", filemtime( EEB_PLUGIN_DIR . 'core/includes/assets/js/encoder-form.js' )); |
| 78 | wp_enqueue_script( 'eeb-js-ajax-ef', EEB_PLUGIN_URL . 'core/includes/assets/js/encoder-form.js', array('jquery'), $js_version_form, true ); |
| 79 | wp_localize_script( 'eeb-js-ajax-ef', 'eeb_ef', array( |
| 80 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 81 | 'security' => wp_create_nonce( $this->page_name ) |
| 82 | )); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * ###################### |
| 88 | * ### |
| 89 | * #### CORE LOGIC |
| 90 | * ### |
| 91 | * ###################### |
| 92 | */ |
| 93 | |
| 94 | public function eeb_ajax_email_encoder_response(){ |
| 95 | check_ajax_referer( $this->page_name, 'eebsec' ); |
| 96 | |
| 97 | $email = html_entity_decode( sanitize_email( $_POST['eebEmail'] ) ); |
| 98 | $method = sanitize_text_field( $_POST['eebMethod'] ); |
| 99 | $display = html_entity_decode( $_POST['eebDisplay'] ); |
| 100 | $custom_class = (string) EEB()->settings->get_setting( 'class_name', true ); |
| 101 | $protection_text = __( EEB()->settings->get_setting( 'protection_text', true ), 'email-encoder-bundle' ); |
| 102 | |
| 103 | if( empty( $display ) ) { |
| 104 | $display = $email; |
| 105 | } else { |
| 106 | $display = wp_kses_post( $display ); |
| 107 | } |
| 108 | |
| 109 | $display = sanitize_text_field( $display ); |
| 110 | |
| 111 | $class_name = ' class="' . esc_attr( $custom_class ) . '"'; |
| 112 | $mailto = '<a href="mailto:' . $email . '"'. $class_name . '>' . $display . '</a>'; |
| 113 | |
| 114 | switch( $method ){ |
| 115 | case 'rot13': |
| 116 | $mailto = EEB()->validate->encode_ascii( $mailto, $protection_text ); |
| 117 | break; |
| 118 | case 'escape': |
| 119 | $mailto = EEB()->validate->encode_escape( $mailto, $protection_text ); |
| 120 | break; |
| 121 | case 'encode': |
| 122 | default: |
| 123 | $mailto = '<a href="mailto:' . antispambot( $email ) . '"'. $class_name . '>' . antispambot( $display ) . '</a>'; |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | echo apply_filters( 'eeb/ajax/encoder_form_response', $mailto ); |
| 128 | exit; |
| 129 | } |
| 130 | |
| 131 | } |
| 132 |