| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly // FixIn: 9.8.0.4. |
| 4 | 4 | |
| @@ -16,32 +16,30 @@ | ||
| 16 | 16 | * @return void wp_send_json() TO front-end or skip and continue |
| 17 | 17 | */ |
| 18 | 18 | function wpbc_captcha__in_ajx__check( $request_params, $is_from_admin_panel , $original_ajx_search_params ) { |
| 19 | 19 | |
| 20 | - if ( | |
| 21 | - ( 'On' === get_bk_option( 'booking_is_use_captcha' ) ) | |
| 22 | - && ( ! $is_from_admin_panel ) | |
| 23 | - && ( ( isset( $original_ajx_search_params['captcha_user_input'] ) ) && ( isset( $original_ajx_search_params['captcha_chalange'] ) ) ) | |
| 24 | - ) { | |
| 20 | + if ( ( ! $is_from_admin_panel ) && ( ( isset( $original_ajx_search_params['captcha_user_input'] ) ) && ( isset( $original_ajx_search_params['captcha_chalange'] ) ) ) ) { | |
| 25 | 21 | |
| 26 | 22 | if ( ! wpbc_captcha__simple__is_ansfer_correct( $request_params['captcha_user_input'], $request_params['captcha_chalange'] ) ) { |
| 27 | 23 | |
| 28 | 24 | $captcha_arr = wpbc_captcha__simple__generate_new(); |
| 29 | 25 | |
| 30 | - $ajx_data_arr = array(); | |
| 31 | - $ajx_data_arr['status'] = 'error'; | |
| 32 | - $ajx_data_arr['status_error'] = 'captcha_simple_wrong'; | |
| 33 | - $ajx_data_arr['captcha__simple'] = $captcha_arr; | |
| 34 | - $ajx_data_arr['ajx_after_action_message'] = __( 'The code you entered is incorrect', 'booking' ); | |
| 26 | + $ajx_data_arr = array(); | |
| 27 | + $ajx_data_arr['status'] = 'error'; | |
| 28 | + $ajx_data_arr['status_error'] = 'captcha_simple_wrong'; | |
| 29 | + $ajx_data_arr['captcha__simple'] = $captcha_arr; | |
| 30 | + $ajx_data_arr['ajx_after_action_message'] = wpbc_frontend_messages__get( 'message_captcha_incorrect', array(), $request_params['resource_id'] ); | |
| 35 | 31 | $ajx_data_arr['ajx_after_action_message_status'] = 'warning'; |
| 36 | 32 | |
| 37 | - wp_send_json( array( | |
| 38 | - 'ajx_data' => $ajx_data_arr, | |
| 39 | - 'ajx_search_params' => $original_ajx_search_params, | |
| 40 | - 'ajx_cleaned_params' => $request_params, | |
| 41 | - 'resource_id' => $request_params['resource_id'] | |
| 42 | - ) ); | |
| 43 | - // After this page will die; | |
| 33 | + wp_send_json( | |
| 34 | + array( | |
| 35 | + 'ajx_data' => $ajx_data_arr, | |
| 36 | + 'ajx_search_params' => $original_ajx_search_params, | |
| 37 | + 'ajx_cleaned_params' => $request_params, | |
| 38 | + 'resource_id' => $request_params['resource_id'], | |
| 39 | + ) | |
| 40 | + ); | |
| 41 | + // After this page will die;. | |
| 44 | 42 | } |
| 45 | 43 | } |
| 46 | 44 | } |
| 47 | 45 | |
| @@ -171,5 +169,120 @@ | ||
| 171 | 169 | .'Please check more <a href="https://wpbookingcalendar.com/faq/captcha-showing-problems/">here</a>.'; |
| 172 | 170 | } |
| 173 | 171 | |
| 174 | 172 | |
| 175 | -} | |
| 173 | +} | |
| 174 | + | |
| 175 | + | |
| 176 | +/** | |
| 177 | + * Decide if CAPTCHA can be shown for current request/context. | |
| 178 | + * | |
| 179 | + * Rules: | |
| 180 | + * - Must be enabled in settings (booking_is_use_captcha = On) | |
| 181 | + * - Must not be a WP Admin page, except plugin settings form page | |
| 182 | + * | |
| 183 | + * @return bool | |
| 184 | + */ | |
| 185 | +function wpbc_booking_form_is_captcha_allowed_here() { | |
| 186 | + | |
| 187 | + // In admin area we usually do not show CAPTCHA, except booking form builder preview pages. | |
| 188 | + // if ( is_admin() && ( ! wpbc_is_settings_form_page() ) ) { return false; } //. | |
| 189 | + | |
| 190 | + // old URI-based logic for edge cases, keep it as extra guard:. | |
| 191 | + $server_request_uri = ( isset( $_SERVER['REQUEST_URI'] ) ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 192 | + | |
| 193 | + // Optional legacy-style admin URI check. | |
| 194 | + $admin_uri = ltrim( str_replace( get_site_url( null, '', 'admin' ), '', admin_url( 'admin.php?' ) ), '/' ); | |
| 195 | + | |
| 196 | + if ( | |
| 197 | + ( '' !== $server_request_uri ) | |
| 198 | + && ( false !== strpos( $server_request_uri, $admin_uri ) ) | |
| 199 | + && ( ! wpbc_is_settings_form_page() ) | |
| 200 | + && ( ! wpbc_is_builder_booking_form_page() ) | |
| 201 | + ) { | |
| 202 | + return false; | |
| 203 | + } | |
| 204 | + | |
| 205 | + return true; | |
| 206 | +} | |
| 207 | + | |
| 208 | + | |
| 209 | +/** | |
| 210 | + * Default CAPTCHA placeholder replacement for booking form HTML. | |
| 211 | + * | |
| 212 | + * Replaces [captcha] token with generated CAPTCHA markup (if enabled), | |
| 213 | + * or removes the token when CAPTCHA is disabled/not allowed in current context. | |
| 214 | + * | |
| 215 | + * Add-ons can: | |
| 216 | + * - Replace HTML entirely by filtering 'wpbc_booking_form_captcha_html' | |
| 217 | + * - Or override this whole replacement by using a lower priority on | |
| 218 | + * 'wpbc_booking_form_html__create_captha' / '__create_captcha' | |
| 219 | + * | |
| 220 | + * @param string $form_html Booking form HTML. | |
| 221 | + * @param int $resource_id Booking resource ID. | |
| 222 | + * | |
| 223 | + * @return string | |
| 224 | + */ | |
| 225 | +function wpbc_booking_form_html__create_captcha_default( $form_html, $resource_id ) { | |
| 226 | + | |
| 227 | + if ( false === strpos( $form_html, '[captcha]' ) ) { | |
| 228 | + return $form_html; | |
| 229 | + } | |
| 230 | + | |
| 231 | + $resource_id = (int) $resource_id; | |
| 232 | + | |
| 233 | + // If CAPTCHA is not allowed here, remove placeholder. | |
| 234 | + if ( ! wpbc_booking_form_is_captcha_allowed_here() ) { | |
| 235 | + return str_replace( '[captcha]', '', $form_html ); | |
| 236 | + } | |
| 237 | + | |
| 238 | + /** | |
| 239 | + * Filter: Provide CAPTCHA HTML for booking form. | |
| 240 | + * | |
| 241 | + * Return non-empty string to supply your own CAPTCHA markup (addons). | |
| 242 | + * Return empty string to let WPBC fallback to the built-in simple CAPTCHA. | |
| 243 | + * | |
| 244 | + * @param string $captcha_html CAPTCHA HTML (empty by default). | |
| 245 | + * @param int $resource_id Booking resource ID. | |
| 246 | + * @param array $args Context args. | |
| 247 | + */ | |
| 248 | + $captcha_html = apply_filters( 'wpbc_booking_form_captcha_html', '', $resource_id, | |
| 249 | + array( | |
| 250 | + 'context' => 'booking_form', | |
| 251 | + 'placeholder' => '[captcha]', | |
| 252 | + ) | |
| 253 | + ); | |
| 254 | + | |
| 255 | + if ( '' === $captcha_html ) { | |
| 256 | + // Core fallback (your existing generator). | |
| 257 | + $captcha_html = wpbc_captcha__simple__generate_html_content( $resource_id ); | |
| 258 | + } | |
| 259 | + | |
| 260 | + return str_replace( '[captcha]', $captcha_html, $form_html ); | |
| 261 | +} | |
| 262 | +add_filter( 'wpbc_booking_form_html__create_captcha', 'wpbc_booking_form_html__create_captcha_default', 10, 2 ); | |
| 263 | + | |
| 264 | + | |
| 265 | +/** | |
| 266 | + * How an add-on supplies a different CAPTCHA (example). | |
| 267 | + * add_filter( 'wpbc_booking_form_captcha_html', 'my_addon_wpbc_captcha_html', 20, 3 ); | |
| 268 | + * | |
| 269 | + * | |
| 270 | + * Provide custom CAPTCHA HTML (e.g., reCAPTCHA/hCaptcha). | |
| 271 | + * | |
| 272 | + * @param string $captcha_html Current HTML (empty by default). | |
| 273 | + * @param int $resource_id Booking resource ID. | |
| 274 | + * @param array $args Context args. | |
| 275 | + * | |
| 276 | + * @return string | |
| 277 | + * | |
| 278 | + * function my_addon_wpbc_captcha_html( $captcha_html, $resource_id, $args ) { | |
| 279 | + * | |
| 280 | + * // Only replace on booking form rendering context. | |
| 281 | + * if ( empty( $args['context'] ) || ( 'booking_form' !== $args['context'] ) ) { | |
| 282 | + * return $captcha_html; | |
| 283 | + * } | |
| 284 | + * | |
| 285 | + * // Return your custom markup here. | |
| 286 | + * return '<div class="my-addon-captcha">...</div>'; | |
| 287 | + * } | |
| 288 | + */ | |