EncoderForm.php
1 month ago
Encoding.php
2 months ago
Filters.php
2 months ago
Validate.php
5 months ago
EncoderForm.php
215 lines
| 1 | <?php |
| 2 | |
| 3 | namespace OnlineOptimisation\EmailEncoderBundle\Validate; |
| 4 | |
| 5 | use OnlineOptimisation\EmailEncoderBundle\Traits\PluginHelper; |
| 6 | |
| 7 | class EncoderForm |
| 8 | { |
| 9 | use PluginHelper; |
| 10 | |
| 11 | public function boot(): void |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * ###################### |
| 18 | * ### |
| 19 | * #### ENCODER FORM |
| 20 | * ### |
| 21 | * ###################### |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * Get the encoder form (to use as a demo, like on the options page) |
| 26 | * @return string |
| 27 | */ |
| 28 | public function get_encoder_form() |
| 29 | { |
| 30 | $powered_by = '<p class="powered-by">' . __('Powered by free', 'email-encoder-bundle') . ' <a rel="external" href="https://wordpress.org/plugins/email-encoder-bundle/">Email Encoder</a></p>'; |
| 31 | |
| 32 | $smethods = array( |
| 33 | 'rot13' => __( 'Rot13 (Javascript)', 'email-encoder-bundle' ), |
| 34 | 'escape' => __( 'Escape (Javascript)', 'email-encoder-bundle' ), |
| 35 | 'encode' => __( 'Encode (HTML)', 'email-encoder-bundle' ), |
| 36 | ); |
| 37 | $method_options = ''; |
| 38 | $selected = false; |
| 39 | foreach ( $smethods as $method_name => $name ) { |
| 40 | $method_options .= '<option value="' . $method_name . '"' . ( ($selected === false ) ? ' selected="selected"' : '') . '>' . $name . '</option>'; |
| 41 | $selected = true; |
| 42 | } |
| 43 | |
| 44 | $labels = array( |
| 45 | 'email' => __( 'Email Address:', 'email-encoder-bundle' ), |
| 46 | 'display' => __( 'Display Text:', 'email-encoder-bundle' ), |
| 47 | 'mailto' => __( 'Mailto Link:', 'email-encoder-bundle' ), |
| 48 | 'method' => __( 'Encoding Method:', 'email-encoder-bundle' ), |
| 49 | 'create_link' => __( 'Create Protected Mail Link >>', 'email-encoder-bundle' ), |
| 50 | 'output' => __( 'Protected Mail Link (code):', 'email-encoder-bundle' ), |
| 51 | 'powered_by' => $powered_by, |
| 52 | ); |
| 53 | |
| 54 | extract( $labels ); |
| 55 | |
| 56 | $form = '<div class="eeb-form">' |
| 57 | . '<form>' |
| 58 | . '<fieldset>' |
| 59 | . '<div class="input">' |
| 60 | . '<table>' |
| 61 | . '<tbody>' |
| 62 | . '<tr>' |
| 63 | . '<th><label for="eeb-email">' . $email . '</label></th>' |
| 64 | . '<td><input type="text" class="regular-text" id="eeb-email" name="eeb-email" /></td>' |
| 65 | . '</tr>' |
| 66 | . '<tr>' |
| 67 | . '<th><label for="eeb-display">' . $display . '</label></th>' |
| 68 | . '<td><input type="text" class="regular-text" id="eeb-display" name="eeb-display" /></td>' |
| 69 | . '</tr>' |
| 70 | . '<tr>' |
| 71 | . '<th>' . $mailto . '</th>' |
| 72 | . '<td><span class="eeb-example"></span></td>' |
| 73 | . '</tr>' |
| 74 | . '<tr>' |
| 75 | . '<th><label for="eeb-encode-method">' . $method . '</label></th>' |
| 76 | . '<td><select id="eeb-encode-method" name="eeb-encode-method" class="postform">' |
| 77 | . $method_options |
| 78 | . '</select>' |
| 79 | . '<input type="button" id="eeb-ajax-encode" name="eeb-ajax-encode" value="' . $create_link . '" />' |
| 80 | . '</td>' |
| 81 | . '</tr>' |
| 82 | . '</tbody>' |
| 83 | . '</table>' |
| 84 | . '</div>' |
| 85 | . '<div class="eeb-output">' |
| 86 | . '<table>' |
| 87 | . '<tbody>' |
| 88 | . '<tr>' |
| 89 | . '<th><label for="eeb-encoded-output">' . $output . '</label></th>' |
| 90 | . '<td><textarea class="large-text node" id="eeb-encoded-output" name="eeb-encoded-output" cols="50" rows="4"></textarea></td>' |
| 91 | . '</tr>' |
| 92 | . '</tbody>' |
| 93 | . '</table>' |
| 94 | . '</div>' |
| 95 | . $powered_by |
| 96 | . '</fieldset>' |
| 97 | . '</form>' |
| 98 | . '</div>'; |
| 99 | |
| 100 | // apply filters — third arg kept as `true` for back-compat with existing |
| 101 | // `eeb_form_content` filter callbacks that read it as the powered_by toggle. |
| 102 | $form = apply_filters('eeb_form_content', $form, $labels, true ); |
| 103 | |
| 104 | $this->enqueue_form_script(); |
| 105 | |
| 106 | return $form; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | /** |
| 111 | * Lazy-enqueue the encoder form's submit-handler script. Called from |
| 112 | * get_encoder_form() so the script loads exactly when the form |
| 113 | * renders — no matter the surface (shortcode in a widget, FSE block, |
| 114 | * eeb_form() in a theme file, etc.). wp_enqueue_script is idempotent, |
| 115 | * so the admin-side admin_enqueue_scripts path still works fine. |
| 116 | */ |
| 117 | private function enqueue_form_script(): void |
| 118 | { |
| 119 | $file = EEB_PLUGIN_DIR . 'core/includes/assets/js/encoder-form.js'; |
| 120 | $ver = file_exists( $file ) ? filemtime( $file ) : false; |
| 121 | |
| 122 | wp_enqueue_script( |
| 123 | 'eeb-js-ajax-ef', |
| 124 | EEB_PLUGIN_URL . 'core/includes/assets/js/encoder-form.js', |
| 125 | [ 'jquery' ], |
| 126 | (string) $ver, |
| 127 | true |
| 128 | ); |
| 129 | |
| 130 | wp_localize_script( 'eeb-js-ajax-ef', 'eeb_ef', [ |
| 131 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 132 | 'security' => wp_create_nonce( 'eeb_form' ), |
| 133 | ] ); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * @param int $post_id |
| 139 | * @return bool |
| 140 | */ |
| 141 | public function is_post_excluded( ?int $post_id = null ) |
| 142 | { |
| 143 | |
| 144 | $return = false; |
| 145 | $skip_posts = (string) $this->getSetting( 'skip_posts', true ); |
| 146 | if ( ! empty( $skip_posts ) ) { |
| 147 | |
| 148 | if ( empty( $post_id ) ) { |
| 149 | global $post; |
| 150 | if ( ! empty( $post ) ) { |
| 151 | $post_id = $post->ID; |
| 152 | } |
| 153 | } else { |
| 154 | $post_id = intval( $post_id ); |
| 155 | } |
| 156 | |
| 157 | $exclude_pages = explode( ',', $skip_posts ); |
| 158 | |
| 159 | $exclude_pages_validated = []; |
| 160 | |
| 161 | foreach ( $exclude_pages as $spost_id ) { |
| 162 | $spost_id = trim( $spost_id ); |
| 163 | if ( is_numeric( $spost_id ) ) { |
| 164 | $exclude_pages_validated[] = intval( $spost_id ); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if ( in_array( $post_id, $exclude_pages_validated ) ) { |
| 169 | $return = true; |
| 170 | } |
| 171 | |
| 172 | } |
| 173 | |
| 174 | return (bool) apply_filters( 'eeb/validate/is_post_excluded', $return, $post_id, $skip_posts ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Filter if to exclude specific URL parameters from filtering |
| 179 | * |
| 180 | * @since 2.2.0 |
| 181 | * @param array< string > $parameters |
| 182 | * @return boolean |
| 183 | */ |
| 184 | public function is_query_parameter_excluded( $parameters = null ) |
| 185 | { |
| 186 | |
| 187 | if ( $parameters === null ) { |
| 188 | $parameters = $_GET; |
| 189 | } |
| 190 | |
| 191 | $return = false; |
| 192 | $skip_query_parameters = (string) $this->getSetting( 'skip_query_parameters', true ); |
| 193 | if ( ! empty( $skip_query_parameters ) && ! empty( $parameters ) ) { |
| 194 | |
| 195 | $excluded_parameters = explode( ',', $skip_query_parameters ); |
| 196 | |
| 197 | // if ( is_array( $excluded_parameters ) ) { |
| 198 | |
| 199 | foreach ( $excluded_parameters as $param ) { |
| 200 | $param = trim($param); |
| 201 | |
| 202 | if ( isset( $parameters[ $param ] ) ) { |
| 203 | $return = true; |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | // } |
| 209 | |
| 210 | } |
| 211 | |
| 212 | return (bool) apply_filters( 'eeb/validate/is_query_parameter_excluded', $return, $parameters ); |
| 213 | } |
| 214 | } |
| 215 |