From f010844dd1aafce2932215d84e8a6e63bd9f7512 Mon Sep 17 00:00:00 2001 From: Malay Ladu Date: Fri, 10 Nov 2023 01:22:17 +0530 Subject: [PATCH 1/4] Fix conflict with stripe add-on [ci skip] --- gravityforms-zero-spam.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/gravityforms-zero-spam.php b/gravityforms-zero-spam.php index a55fca2..4fa3b3d 100644 --- a/gravityforms-zero-spam.php +++ b/gravityforms-zero-spam.php @@ -51,6 +51,28 @@ public static function deactivate() { public function __construct() { add_action( 'gform_register_init_scripts', array( $this, 'add_key_field' ), 9999 ); add_filter( 'gform_entry_is_spam', array( $this, 'check_key_field' ), 10, 3 ); + add_filter( 'gform_incomplete_submission_pre_save', array( $this, 'add_zero_spam_key_to_entry' ), 10, 3 ); + } + + /** + * Adds the zero spam key to the Gravity Forms entry during form submission. + * + * @param string $submission_json The JSON representation of the form submission. + * @param string $resume_token The resume token for the submission. + * @param array $form The Gravity Forms form object. + * + * @return string The modified JSON representation of the form submission. + */ + public function add_zero_spam_key_to_entry( $submission_json, $resume_token, $form ) { + $submission = json_decode( $submission_json, true ); + + if ( ! isset( $submission['partial_entry']['gf_zero_spam_key'] ) ) { + $spam_key = rgpost( 'gf_zero_spam_key' ); + + $submission['partial_entry']['gf_zero_spam_key'] = $spam_key; + } + + return json_encode( $submission ); } /** @@ -97,7 +119,7 @@ public function add_key_field( $form ) { $autocomplete = RGFormsModel::is_html5_enabled() ? ".attr( 'autocomplete', 'new-password' )\n\t\t" : ''; $script = <<' ) .attr( 'type', 'hidden' ) .attr( 'name', 'gf_zero_spam_key' ) From fb1b81979f1f748a2ffa51e018e3cde24ac8cf72 Mon Sep 17 00:00:00 2001 From: Malay Ladu Date: Fri, 10 Nov 2023 15:51:00 +0530 Subject: [PATCH 2/4] Set priority of `gform_register_init_scripts` action to `1` It makes sure `gf_zero_spam_key` hidden field is properly set while submitting the form. Fixes https://github.com/GravityKit/gravity-forms-zero-spam/issues/22 --- gravityforms-zero-spam.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/gravityforms-zero-spam.php b/gravityforms-zero-spam.php index 4fa3b3d..5fc41a4 100644 --- a/gravityforms-zero-spam.php +++ b/gravityforms-zero-spam.php @@ -49,7 +49,7 @@ public static function deactivate() { } public function __construct() { - add_action( 'gform_register_init_scripts', array( $this, 'add_key_field' ), 9999 ); + add_action( 'gform_register_init_scripts', array( $this, 'add_key_field' ), 1 ); add_filter( 'gform_entry_is_spam', array( $this, 'check_key_field' ), 10, 3 ); add_filter( 'gform_incomplete_submission_pre_save', array( $this, 'add_zero_spam_key_to_entry' ), 10, 3 ); } @@ -66,10 +66,14 @@ public function __construct() { public function add_zero_spam_key_to_entry( $submission_json, $resume_token, $form ) { $submission = json_decode( $submission_json, true ); - if ( ! isset( $submission['partial_entry']['gf_zero_spam_key'] ) ) { - $spam_key = rgpost( 'gf_zero_spam_key' ); + // If it's not a valid JSON, just return the original submission. + if ( ! is_array( $submission ) ) { + return $submission_json; + } - $submission['partial_entry']['gf_zero_spam_key'] = $spam_key; + // If the zero spam key is already set, just return the original submission. + if ( isset( $submission['partial_entry'] ) && ! isset( $submission['partial_entry']['gf_zero_spam_key'] ) ) { + $submission['partial_entry']['gf_zero_spam_key'] = rgpost( 'gf_zero_spam_key' );; } return json_encode( $submission ); From 43dd6f1306d18b50aaf735c0da043dabe5f00a7a Mon Sep 17 00:00:00 2001 From: Malay Ladu Date: Fri, 10 Nov 2023 15:59:56 +0530 Subject: [PATCH 3/4] Update docblock --- gravityforms-zero-spam-form-settings.php | 18 +++++------ gravityforms-zero-spam.php | 39 +++++++++++------------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/gravityforms-zero-spam-form-settings.php b/gravityforms-zero-spam-form-settings.php index 0639781..b0dcfb5 100644 --- a/gravityforms-zero-spam-form-settings.php +++ b/gravityforms-zero-spam-form-settings.php @@ -91,9 +91,9 @@ public function filter_gf_zero_spam_add_key_field( $add_key_field = true ) { } /** - * Include custom tooltip text for the Zero Spam setting in the Form Settings page + * Include custom tooltip text for the Zero Spam setting in the Form Settings page. * - * @param array $tooltips Key/Value pair of tooltip/tooltip text + * @param array $tooltips Key/Value pair of tooltip/tooltip text. * * @return array */ @@ -105,12 +105,12 @@ public function add_tooltip( $tooltips ) { } /** - * Adds the Zero Spam field to the "Form Options" settings group in GF 2.5+ + * Adds the Zero Spam field to the "Form Options" settings group in GF 2.5+. * * @see https://docs.gravityforms.com/gform_form_settings_fields/ * * @param array $fields Form Settings fields. - * @param array $form The current form + * @param array $form The current form. * * @return array */ @@ -128,7 +128,7 @@ function add_settings_field( $fields, $form = array() ) { } /** - * Register addon global settings + * Register addon global settings. * * @return array */ @@ -341,7 +341,7 @@ public function plugin_settings_fields() { * * @since 1.4 * - * @param int $entry_id The entry ID. + * @param int $entry_id The entry ID. * @param string $property_value The new status. * * @return void @@ -360,8 +360,8 @@ public function update_status( $entry_id, $property_value ) { * * @since 1.4 * - * @param array $entry - * @param array $form + * @param array $entry The entry object. + * @param array $form The form object. * * @return void */ @@ -612,7 +612,7 @@ private function get_spam_count() { /** * Add cron job for spam reporting. * - * @param string $frequency + * @param string $frequency The frequency of the cron job. * * @return string */ diff --git a/gravityforms-zero-spam.php b/gravityforms-zero-spam.php index 5fc41a4..d75d2e7 100644 --- a/gravityforms-zero-spam.php +++ b/gravityforms-zero-spam.php @@ -10,26 +10,25 @@ * License URI: http://www.gnu.org/licenses/gpl-2.0.txt */ -// my mother always said to use things as they're intended or not at all +// my mother always said to use things as they're intended or not at all. if ( ! defined( 'WPINC' ) ) { die; } define( 'GF_ZERO_SPAM_BASENAME', plugin_basename( __FILE__ ) ); -// clean up after ourselves +// clean up after ourselves. register_deactivation_hook( __FILE__, array( 'GF_Zero_Spam', 'deactivate' ) ); -// Fire it up +// Fire it up. add_action( 'gform_loaded', array( 'GF_Zero_Spam', 'gform_loaded' ) ); class GF_Zero_Spam { /** - * Instantiate the plugin on Gravity Forms loading + * Instantiate the plugin on Gravity Forms loading. */ public static function gform_loaded() { - include_once plugin_dir_path( __FILE__ ) . 'gravityforms-zero-spam-form-settings.php'; new self; @@ -80,12 +79,11 @@ public function add_zero_spam_key_to_entry( $submission_json, $resume_token, $fo } /** - * Retrieves the zero spam key (generating if needed) + * Retrieves the zero spam key (generating if needed). * * @return false|mixed|void */ public function get_key() { - $key = get_option( 'gf_zero_spam_key' ); if ( ! $key ) { @@ -97,19 +95,20 @@ public function get_key() { } /** - * Injects the hidden field and key into the form at submission + * Injects the hidden field and key into the form at submission. * * @uses GFFormDisplay::add_init_script() to inject the code into the `gform_post_render` jQuery hook. * - * @param array $form The Form Object + * @param array $form The Form Object. * * @return void */ public function add_key_field( $form ) { - /** * Allows the zero spam key field to be disabled by returning false. + * * @since 1.4 + * * @param bool $add_key_field Whether to add the key field to the form. Default true. */ $add_key_field = apply_filters( 'gf_zero_spam_add_key_field', true ); @@ -136,11 +135,11 @@ public function add_key_field( $form ) { } /** - * Checks for our zero spam key during validation + * Checks for our zero spam key during validation. * - * @param bool $is_spam Indicates if the submission has been flagged as spam. - * @param array $form The form currently being processed. - * @param array $entry The entry currently being processed. + * @param bool $is_spam Indicates if the submission has been flagged as spam. + * @param array $form The form currently being processed. + * @param array $entry The entry currently being processed. * * @return bool True: it's spam; False: it's not spam! */ @@ -153,9 +152,9 @@ public function check_key_field( $is_spam = false, $form = array(), $entry = arr * * @since 1.2 * - * @param bool $should_check_key_field Whether the Zero Spam plugin should check for the existence and validity of the key field. Default: true. - * @param array $form The form currently being processed. - * @param array $entry The entry currently being processed. + * @param bool $should_check_key_field Whether the Zero Spam plugin should check for the existence and validity of the key field. Default: true. + * @param array $form The form currently being processed. + * @param array $entry The entry currently being processed. */ $should_check_key_field = gf_apply_filters( 'gf_zero_spam_check_key_field', rgar( $form, 'id' ), $should_check_key_field, $form, $entry ); @@ -168,12 +167,12 @@ public function check_key_field( $is_spam = false, $form = array(), $entry = arr return $is_spam; } - // This was not submitted using a web form; created using API + // This was not submitted using a web form; created using API. if ( ! $supports_context && ! did_action( 'gform_pre_submission' ) ) { return $is_spam; } - // Created using REST API or GFAPI + // Created using REST API or GFAPI. if ( isset( $entry['user_agent'] ) && 'API' === $entry['user_agent'] ) { return $is_spam; } @@ -195,7 +194,6 @@ public function check_key_field( $is_spam = false, $form = array(), $entry = arr * @param array $entry The entry data. */ public function add_entry_note( $entry ) { - if ( 'spam' !== rgar( $entry, 'status' ) ) { return; } @@ -206,5 +204,4 @@ public function add_entry_note( $entry ) { GFAPI::add_note( $entry['id'], 0, 'Zero Spam', __( 'This entry has been marked as spam.', 'gf-zero-spam' ), 'gf-zero-spam', 'success' ); } - } From 0514174bb92bce6b38ebb9cf25ef061f1e373ec9 Mon Sep 17 00:00:00 2001 From: Malay Ladu Date: Fri, 10 Nov 2023 20:54:12 +0530 Subject: [PATCH 4/4] Refactor code to return early --- gravityforms-zero-spam.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/gravityforms-zero-spam.php b/gravityforms-zero-spam.php index d75d2e7..7e25b12 100644 --- a/gravityforms-zero-spam.php +++ b/gravityforms-zero-spam.php @@ -65,15 +65,13 @@ public function __construct() { public function add_zero_spam_key_to_entry( $submission_json, $resume_token, $form ) { $submission = json_decode( $submission_json, true ); - // If it's not a valid JSON, just return the original submission. - if ( ! is_array( $submission ) ) { + // If it's not a valid JSON, partial entry is not set or the zero spam key is already set, return the original submission. + if ( ! is_array( $submission ) || ! isset( $submission['partial_entry'] ) || isset( $submission['partial_entry']['gf_zero_spam_key'] )) { return $submission_json; } - // If the zero spam key is already set, just return the original submission. - if ( isset( $submission['partial_entry'] ) && ! isset( $submission['partial_entry']['gf_zero_spam_key'] ) ) { - $submission['partial_entry']['gf_zero_spam_key'] = rgpost( 'gf_zero_spam_key' );; - } + // Add the zero spam key to the partial entry if it's available in the POST data. + $submission['partial_entry']['gf_zero_spam_key'] = rgpost( 'gf_zero_spam_key' );; return json_encode( $submission ); }