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

Fix conflict with Gravity Forms Stripe Add-on #23

Merged
merged 4 commits into from
Nov 10, 2023
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
18 changes: 9 additions & 9 deletions gravityforms-zero-spam-form-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -128,7 +128,7 @@ function add_settings_field( $fields, $form = array() ) {
}

/**
* Register addon global settings
* Register addon global settings.
*
* @return array
*/
Expand Down Expand Up @@ -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
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
67 changes: 44 additions & 23 deletions gravityforms-zero-spam.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,17 +48,40 @@ 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 );
}

/**
* Retrieves the zero spam key (generating if needed)
* 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 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;
}

// 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 );
}

/**
* 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 ) {
Expand All @@ -71,19 +93,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 );
Expand All @@ -97,7 +120,7 @@ public function add_key_field( $form ) {
$autocomplete = RGFormsModel::is_html5_enabled() ? ".attr( 'autocomplete', 'new-password' )\n\t\t" : '';

$script = <<<EOD
jQuery( document ).on( 'submit.gravityforms', '.gform_wrapper form', function( event ) {
jQuery( "#gform_{$form['id']}" ).on( 'submit', function( event ) {
malayladu marked this conversation as resolved.
Show resolved Hide resolved
jQuery( '<input>' )
.attr( 'type', 'hidden' )
.attr( 'name', 'gf_zero_spam_key' )
Expand All @@ -110,11 +133,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!
*/
Expand All @@ -127,9 +150,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 );

Expand All @@ -142,12 +165,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;
}
Expand All @@ -169,7 +192,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;
}
Expand All @@ -180,5 +202,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' );
}

}