Skip to content

Commit

Permalink
PLANET-7598: Form Builder - Add a petition counter id field (#2380)
Browse files Browse the repository at this point in the history
* PLANET-7598: Add a petition counter ID field

- The new field was added to Planet 4 Options of the Form Settings

* PLANET-7598: Send get request

- The get request is made after form submission

* Update GravityFormsExtensions.php

- The get request is made after form submission
  • Loading branch information
mardelnet authored Oct 2, 2024
1 parent 1a74b7c commit 1326dec
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/GravityFormsExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,47 @@ private function hooks(): void
add_filter('gform_pre_render', [ $this, 'p4_client_side_gravityforms_prefill' ], 10, 1);
add_filter('gform_form_post_get_meta', [$this, 'p4_gf_enable_default_meta_settings'], 10, 1);
add_filter('gform_hubspot_form_object_pre_save_feed', [$this, 'p4_gf_hb_form_object_pre_save_feed'], 10, 1);
add_action('gform_after_submission', [$this, 'p4_send_gp_pixel_counter'], 10, 2);

add_action('gform_stripe_fulfillment', [ $this, 'record_fulfillment_entry' ], 10, 2);
add_action('gform_post_payment_action', [ $this, 'check_stripe_payment_status' ], 10, 2);
}

/**
* Increase in one unit the total number of submissions for a counter by
* making an API call to the Greenpeace Pixel Counter, or
* adding an iframe.
*
* @link https://counter.greenpeace.org/documentation
* @param array $form The form setting.
* @param array $entry A form entry.
*
*/
public function p4_send_gp_pixel_counter(array $entry, array $form): void
{
if (!$form['p4_gf_counter']) {
return;
}

$endpoint_url = 'https://counter.greenpeace.org/count';
$endpoint_url .= '?id=' . $form['p4_gf_counter'];
$endpoint_url .= '&email_hash=' . $this->p4_gf_get_email_hash($form, $entry);

// Pass the referer explicitily.
$wp_remote_get_args = array(
'headers' => array(
'Referer' => home_url(),
),
);

$response = wp_remote_get($endpoint_url, $wp_remote_get_args);

GFCommon::log_debug('Pixel Counter Response Message: ' . $response['body']);
GFCommon::log_debug('Pixel Counter Response Status Code: ' . $response['response']['code']);
GFCommon::log_debug('Pixel Counter Response Status Message: ' . $response['response']['message']);
return;
}

/**
* Save resulting entry after Stripe successful submission.
*
Expand Down Expand Up @@ -176,6 +212,17 @@ public function p4_gf_form_settings(array $fields): array
'choices' => self::P4_GF_TYPES,
];

$fields['p4_options']['fields'][] = [
'type' => 'text',
'name' => 'p4_gf_counter',
'label' => __('Global Counter ID', 'planet4-master-theme-backend'),
'tooltip' => __(
'Add the Counter Name from counter.greenpeace.org',
'planet4-master-theme-backend'
),
'required' => false,
];

return $fields;
}

Expand Down

0 comments on commit 1326dec

Please sign in to comment.