post_content, 'aiprofile' ) ) { aiprofile_inside_enqueue_form_style(); } } add_action( 'wp_enqueue_scripts', 'aiprofile_inside_maybe_enqueue_form_style' ); /** Is the form on? */ function aiprofile_inside_form_enabled() { return (bool) aiprofile_inside_option( 'questions' ) && aiprofile_inside_link_verified(); } /** * The form, under the answers on the connected profile's page. * * @param string $name The business name. * @return string HTML. */ function aiprofile_inside_question_form( $name ) { if ( ! aiprofile_inside_form_enabled() ) { return ''; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only picks which message to show after the redirect. $state = isset( $_GET['aiprofile_vraag'] ) ? sanitize_key( wp_unslash( $_GET['aiprofile_vraag'] ) ) : ''; $messages = array( 'ok' => __( 'Thank you. Your question has been sent.', 'aiprofile-inside' ), 'invalid' => __( 'Please write your question in 8 to 300 characters, without web addresses.', 'aiprofile-inside' ), 'consent' => __( 'Tick the box if you add your email address, or leave the address empty.', 'aiprofile-inside' ), 'limit' => __( 'You just sent a question. Please try again later.', 'aiprofile-inside' ), 'error' => __( 'That did not work. Please try again later.', 'aiprofile-inside' ), ); $page_id = (int) get_queried_object_id(); $name = '' !== $name ? $name : get_bloginfo( 'name' ); aiprofile_inside_enqueue_form_style(); // A block theme styles .wp-element-button from theme.json. A classic // theme gets our fallback, which yields to any button style it has. $button = function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ? 'wp-element-button' : 'aiprofile-ask-button--classic'; $tone = 'ok' === $state ? 'ok' : 'error'; ob_start(); ?>

= $limit ) { return false; } set_transient( $name, $count + 1, $window ); return true; } /** The form's handler, for visitors and logged-in users alike. */ function aiprofile_inside_handle_question() { $page_id = isset( $_POST['aiprofile_page'] ) ? absint( wp_unslash( $_POST['aiprofile_page'] ) ) : 0; $nonce = isset( $_POST['aiprofile_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['aiprofile_nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'aiprofile_inside_question' ) || ! aiprofile_inside_form_enabled() ) { aiprofile_inside_question_redirect( $page_id, 'error' ); } // The honeypot: a person never sees this field, so a filled one is a bot. // It gets the same "thank you" so it learns nothing. if ( ! empty( $_POST['aiprofile_website'] ) ) { aiprofile_inside_question_redirect( $page_id, 'ok' ); } $question = isset( $_POST['aiprofile_question'] ) ? sanitize_textarea_field( wp_unslash( $_POST['aiprofile_question'] ) ) : ''; $question = trim( (string) preg_replace( '/\s+/', ' ', $question ) ); $length = function_exists( 'mb_strlen' ) ? mb_strlen( $question ) : strlen( $question ); if ( $length < 8 || $length > 300 || preg_match( '#https?://|\bwww\.#i', $question ) ) { aiprofile_inside_question_redirect( $page_id, 'invalid' ); } $email = isset( $_POST['aiprofile_email'] ) ? sanitize_email( wp_unslash( $_POST['aiprofile_email'] ) ) : ''; $consent = ! empty( $_POST['aiprofile_consent'] ); if ( '' !== $email && ( ! is_email( $email ) || ! $consent ) ) { aiprofile_inside_question_redirect( $page_id, 'consent' ); } if ( ! $consent ) { $email = ''; } $ip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; $visitor = 'aiprofile_inside_q_' . substr( hash_hmac( 'sha256', $ip, wp_salt( 'nonce' ) ), 0, 20 ); if ( ! aiprofile_inside_question_allowed( $visitor, 3, 10 * MINUTE_IN_SECONDS ) || ! aiprofile_inside_question_allowed( 'aiprofile_inside_q_site_' . gmdate( 'Ymd' ), 30, DAY_IN_SECONDS ) ) { aiprofile_inside_question_redirect( $page_id, 'limit' ); } $page_url = $page_id ? (string) get_permalink( $page_id ) : home_url( '/' ); aiprofile_inside_question_redirect( $page_id, aiprofile_inside_send_question( $question, $email, $page_url ) ); } add_action( 'admin_post_nopriv_aiprofile_inside_question', 'aiprofile_inside_handle_question' ); add_action( 'admin_post_aiprofile_inside_question', 'aiprofile_inside_handle_question' );