File manager - Edit - /home/verseaumee/blueversion/wp-content/plugins/hcaptcha-for-forms-and-more/src/php/Spectra/Form.php
Back
<?php /** * Form class file. * * @package hcaptcha-wp */ namespace HCaptcha\Spectra; use HCaptcha\Helpers\HCaptcha; use HCaptcha\Helpers\Request; use WP_Block; /** * Class Form. */ class Form { /** * Nonce action. */ const ACTION = 'hcaptcha_spectra_form'; /** * Nonce name. */ const NONCE = 'hcaptcha_spectra_form_nonce'; /** * Script handle. */ const HANDLE = 'hcaptcha-spectra'; /** * Whether form has reCaptcha field. * * @var bool */ private $has_recaptcha_field; /** * Form constructor. */ public function __construct() { $this->init_hooks(); } /** * Add hooks. * * @return void */ public function init_hooks() { add_action( 'wp_ajax_uagb_process_forms', [ $this, 'process_ajax' ], 9 ); add_action( 'wp_ajax_nopriv_uagb_process_forms', [ $this, 'process_ajax' ], 9 ); if ( ! Request::is_frontend() ) { return; } add_filter( 'render_block', [ $this, 'render_block' ], 10, 3 ); add_action( 'wp_head', [ $this, 'print_inline_styles' ], 20 ); add_action( 'hcap_print_hcaptcha_scripts', [ $this, 'print_hcaptcha_scripts' ] ); add_action( 'wp_print_footer_scripts', [ $this, 'enqueue_scripts' ], 9 ); } /** * Render block filter. * * @param string|mixed $block_content Block content. * @param array $block Block. * @param WP_Block $instance Instance. * * @return string|mixed * @noinspection PhpUnusedParameterInspection */ public function render_block( $block_content, array $block, WP_Block $instance ) { if ( 'uagb/forms' !== $block['blockName'] ) { return $block_content; } $args = [ 'action' => self::ACTION, 'name' => self::NONCE, 'id' => [ 'source' => HCaptcha::get_class_source( __CLASS__ ), 'form_id' => isset( $block['attrs']['block_id'] ) ? (int) $block['attrs']['block_id'] : 0, ], ]; $block_content = (string) $block_content; $this->has_recaptcha_field = false; if ( false !== strpos( $block_content, 'uagb-forms-recaptcha' ) ) { $this->has_recaptcha_field = true; // Do not replace reCaptcha. return $block_content; } $search = '<div class="uagb-forms-main-submit-button-wrap'; return (string) str_replace( $search, HCaptcha::form( $args ) . $search, $block_content ); } /** * Process ajax. * * @return void */ public function process_ajax() { if ( $this->has_recaptcha() ) { return; } // phpcs:disable WordPress.Security.NonceVerification.Missing $form_data = isset( $_POST['form_data'] ) ? json_decode( sanitize_text_field( wp_unslash( $_POST['form_data'] ) ), true ) : []; // phpcs:enable WordPress.Security.NonceVerification.Missing $_POST['h-captcha-response'] = $form_data['h-captcha-response'] ?? ''; $_POST[ self::NONCE ] = $form_data[ self::NONCE ] ?? ''; $error_message = hcaptcha_verify_post( self::NONCE, self::ACTION ); unset( $_POST['h-captcha-response'], $_POST[ self::NONCE ] ); if ( null === $error_message ) { return; } // Spectra cannot process error messages from the backend. wp_send_json_error( 400 ); } /** * Print inline styles. * * @return void * @noinspection CssUnusedSymbol */ public function print_inline_styles() { static $style_shown; if ( $style_shown ) { return; } $style_shown = true; $css = <<<CSS .uagb-forms-main-form .h-captcha { margin-bottom: 20px; } CSS; HCaptcha::css_display( $css ); } /** * Filter print hCaptcha scripts status and return true if no reCaptcha is in the form. * * @param bool|mixed $status Print scripts status. * * @return bool * @noinspection PhpUnusedParameterInspection */ public function print_hcaptcha_scripts( $status ): bool { return ! $this->has_recaptcha_field; } /** * Enqueue scripts. * * @return void */ public function enqueue_scripts() { $min = hcap_min_suffix(); wp_enqueue_script( self::HANDLE, HCAPTCHA_URL . "/assets/js/hcaptcha-spectra$min.js", [], HCAPTCHA_VERSION, true ); } /** * Whether form has recaptcha. * * @return bool */ private function has_recaptcha(): bool { // Spectra check nonce. // phpcs:disable WordPress.Security.NonceVerification.Missing $post_id = isset( $_POST['post_id'] ) ? sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) : ''; $block_id = isset( $_POST['block_id'] ) ? sanitize_text_field( wp_unslash( $_POST['block_id'] ) ) : ''; // phpcs:enable WordPress.Security.NonceVerification.Missing $post_content = get_post_field( 'post_content', sanitize_text_field( $post_id ) ); foreach ( parse_blocks( $post_content ) as $block ) { if ( isset( $block['blockName'], $block['attrs']['block_id'] ) && 'uagb/forms' === $block['blockName'] && $block_id === $block['attrs']['block_id'] && ! empty( $block['attrs']['reCaptchaEnable'] ) ) { return true; } } return false; } }
| ver. 1.4 |
Github
|
.
| PHP 8.5.7 | Generation time: 0 |
proxy
|
phpinfo
|
Settings