Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for plugin review. [TMZ-199] [TMZ-202] #101

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public static function get_client_ip() {
];

foreach ( $server_ip_keys as $key ) {
$value = ElementorUtils::get_super_global_value( $_SERVER, $key );
if ( $value && filter_var( $value, FILTER_VALIDATE_IP ) ) {
$value = filter_input( INPUT_SERVER, $key, FILTER_VALIDATE_IP );

if ( $value ) {
return $value;
}
}
Expand Down
2 changes: 2 additions & 0 deletions modules/forms/assets/js/frontend/handlers/form-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default elementorModules.frontend.handlers.Base.extend( {
},
action: 'hello_plus_forms_lite_send_form',
ajaxUrl: elementorFrontendConfig.urls.ajaxurl,
nonce: ehpForms.nonce,
};
},

Expand Down Expand Up @@ -59,6 +60,7 @@ export default elementorModules.frontend.handlers.Base.extend( {
getFormData() {
const formData = new FormData( this.elements.$form[ 0 ] );
formData.append( 'action', this.getSettings( 'action' ) );
formData.append( 'nonce', this.getSettings( 'nonce' ) );
formData.append( 'referrer', location.toString() );

return formData;
Expand Down
30 changes: 18 additions & 12 deletions modules/forms/classes/ajax-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use HelloPlus\Includes\Utils;
use HelloPlus\Modules\Forms\Module;
use Elementor\Utils as ElementorUtils;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
Expand All @@ -28,10 +27,13 @@ class Ajax_Handler {
const INVALID_FORM = 'invalid_form';
const SERVER_ERROR = 'server_error';
const SUBSCRIBER_ALREADY_EXISTS = 'subscriber_already_exists';
const NONCE_ACTION = 'ehp-form-submission';

public static function is_form_submitted() {
// PHPCS - No nonce is required, all visitors may send the form.
return wp_doing_ajax() && isset( $_POST['action'] ) && 'hello_plus_forms_lite_send_form' === $_POST['action']; // phpcs:ignore WordPress.Security.NonceVerification.Missing
return wp_doing_ajax() &&
check_ajax_referer( self::NONCE_ACTION, 'nonce' ) &&
isset( $_POST['action'] ) &&
'hello_plus_forms_lite_send_form' === $_POST['action'];
}

public static function get_default_messages() {
Expand Down Expand Up @@ -59,29 +61,29 @@ public static function get_default_message( $id, $settings ) {
}

public function ajax_send_form() {
$post_data = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing
check_ajax_referer( self::NONCE_ACTION, 'nonce' );

// $post_id that holds the form settings.
$post_id = $post_data['post_id'];
$post_id = filter_input( INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT );
$queried_id = filter_input( INPUT_POST, 'queried_id', FILTER_SANITIZE_NUMBER_INT );

// $queried_id the post for dynamic values data.
if ( isset( $post_data['queried_id'] ) ) {
$queried_id = $post_data['queried_id'];
} else {
if ( ! $queried_id ) {
$queried_id = $post_id;
}

// Make the post as global post for dynamic values.
Utils::elementor()->db->switch_to_post( $queried_id );

$form_id = $post_data['form_id'];
$form_id = filter_input( INPUT_POST, 'form_id', FILTER_SANITIZE_STRING );

$elementor = Utils::elementor();
$document = $elementor->documents->get( $post_id );
$form = null;
$template_id = null;

if ( $document ) {
$form = Module::find_element_recursive( $document->get_elements_data(), $form_id );
$form = Module::find_element_recursive( $document->get_elements_data(), (string) $form_id );
}

if ( ! empty( $form['templateID'] ) ) {
Expand Down Expand Up @@ -118,7 +120,10 @@ public function ajax_send_form() {
->send();
}

$record = new Form_Record( $post_data['form_fields'], $form );
// the fields are not fixed so they will be validated afterwards
$form_fields = filter_input( INPUT_POST, 'form_fields', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY );

$record = new Form_Record( $form_fields, $form );

if ( ! $record->validate( $this ) ) {
$this
Expand Down Expand Up @@ -220,9 +225,10 @@ public function send() {
$this->add_error_message( $this->get_default_message( self::INVALID_FORM, $this->current_form['settings'] ) );
}

$post_id = ElementorUtils::get_super_global_value( $_POST, 'post_id' ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
$post_id = filter_input( INPUT_POST, 'post_id', FILTER_SANITIZE_NUMBER_INT );

$error_msg = implode( '<br>', $this->messages['error'] );

if ( current_user_can( 'edit_post', $post_id ) && ! empty( $this->messages['admin_error'] ) ) {
$this->add_admin_error_message( esc_html__( 'This message is not visible to site visitors.', 'hello-plus' ) );
$error_msg .= '<div class="elementor-forms-admin-errors">' . implode( '<br>', $this->messages['admin_error'] ) . '</div>';
Expand Down
10 changes: 7 additions & 3 deletions modules/forms/classes/form-record.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public function update_field( $field_id, $property, $value ) {
}

public function get_form_meta( $meta_keys = [] ) {

$result = [];

foreach ( $meta_keys as $metadata_type ) {
Expand All @@ -174,23 +175,26 @@ public function get_form_meta( $meta_keys = [] ) {
break;

case 'page_url':
$referrer = filter_input( INPUT_POST, 'referrer', FILTER_SANITIZE_URL );
$result['page_url'] = [
'title' => esc_html__( 'Page URL', 'hello-plus' ),
'value' => isset( $_POST['referrer'] ) ? esc_url_raw( wp_unslash( $_POST['referrer'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
'value' => $referrer ? esc_url_raw( wp_unslash( $referrer ) ) : '',
];
break;

case 'page_title':
$referrer_title = filter_input( INPUT_POST, 'referer_title', FILTER_SANITIZE_STRING );
$result['page_title'] = [
'title' => esc_html__( 'Page Title', 'hello-plus' ),
'value' => isset( $_POST['referer_title'] ) ? sanitize_text_field( wp_unslash( $_POST['referer_title'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification.Missing
'value' => $referrer_title ? sanitize_text_field( wp_unslash( $referrer_title ) ) : '',
];
break;

case 'user_agent':
$user_agent = filter_input( INPUT_SERVER, 'HTTP_USER_AGENT', FILTER_SANITIZE_STRING );
$result['user_agent'] = [
'title' => esc_html__( 'User Agent', 'hello-plus' ),
'value' => isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_textarea_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '',
'value' => $user_agent ? sanitize_textarea_field( wp_unslash( $user_agent ) ) : '',
];
break;

Expand Down
44 changes: 27 additions & 17 deletions modules/forms/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use HelloPlus\Includes\Module_Base;

use HelloPlus\Modules\Forms\Classes\Ajax_Handler;
use HelloPlus\Modules\Forms\Controls\Fields_Map;
use HelloPlus\Modules\Forms\Registrars\Form_Actions_Registrar;
use HelloPlus\Modules\Forms\Registrars\Form_Fields_Registrar;
Expand Down Expand Up @@ -106,6 +107,14 @@ public function register_scripts() {
HELLO_PLUS_VERSION,
true
);

wp_localize_script(
'hello-plus-forms-editor-fe',
'ehpForms',
[
'nonce' => wp_create_nonce( Ajax_Handler::NONCE_ACTION ),
]
);
}

protected function get_component_ids(): array {
Expand All @@ -131,23 +140,24 @@ public function __construct() {
$this->actions_registrar = new Form_Actions_Registrar();
$this->fields_registrar = new Form_Fields_Registrar();

// TODO: refactor & move to components:
// Ajax Handler
if ( Classes\Ajax_Handler::is_form_submitted() ) {
$this->add_component( 'ajax_handler', new Classes\Ajax_Handler() );

/**
* Hello+ form submitted.
*
* Fires when the form is submitted. This hook allows developers
* to add functionality after form submission.
*
* @param Module $this An instance of the form module.
*
* @since 1.0.0
*
*/
do_action( 'hello_plus/forms/form_submitted', $this );
}
add_action( 'init', function () {
if ( Classes\Ajax_Handler::is_form_submitted() ) {
$this->add_component( 'ajax_handler', new Classes\Ajax_Handler() );

/**
* Hello+ form submitted.
*
* Fires when the form is submitted. This hook allows developers
* to add functionality after form submission.
*
* @param Module $this An instance of the form module.
*
* @since 1.0.0
*
*/
do_action( 'hello_plus/forms/form_submitted', $this );
}
} );
}
}
Loading