Skip to content

Commit babbb20

Browse files
authored
Feat/jamstack deployments (#224)
* dev: change gatsby monitor hook to `gform_post_form_duplicated` * feat: add support for WP Jamstack Deployments * tests: use databaseId for FormQueriesTest checks * chore: add WPGatsby and Jamstack Deployments to supported features.
1 parent a9cb036 commit babbb20

File tree

12 files changed

+221
-33
lines changed

12 files changed

+221
-33
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Our hope for this open source project is that it will enable more teams to lever
2222

2323
## System Requirements
2424

25-
* PHP 7.4+ | 8.0
25+
* PHP 7.4+ || 8.0
2626
* WordPress 5.4.1+
2727
* WPGraphQL 1.6.4+
2828
* Gravity Forms 2.5+
@@ -40,6 +40,7 @@ Our hope for this open source project is that it will enable more teams to lever
4040
* Submitting forms.
4141
* Updating entries and draft entries.
4242
* Deleting entries and draft entries.
43+
* Triggering builds with [WPGatsby](https://wordpress.org/plugins/wp-gatsby/) and [Jamstack Deployments](https://wordpress.org/plugins/wp-jamstack-deployments/)
4344

4445
## Future Feature Enhancements
4546

bin/install-test-env.sh

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ install_gravityforms_signature() {
180180

181181
install_gravityforms_chainedselects() {
182182
if [ ! -d $WP_CORE_DIR/wp-content/plugins/gravityformschainedselects ]; then
183-
echo "Cloning Gravity Forms Signature"
183+
echo "Cloning Gravity Forms Chained Selects"
184184
if [ -n "$GIT_USER" ] && [ -n "$GIT_TOKEN" ] && [ -n "$GF_CHAINEDSELECTS_REPO" ]; then
185185
git clone https://$GIT_USER:$GIT_TOKEN@$GF_CHAINEDSELECTS_REPO $WP_CORE_DIR/wp-content/plugins/gravityformschainedselects
186186
else
@@ -192,7 +192,7 @@ install_gravityforms_chainedselects() {
192192

193193
install_gravityforms_quiz() {
194194
if [ ! -d $WP_CORE_DIR/wp-content/plugins/gravityformsquiz ]; then
195-
echo "Cloning Gravity Forms Signature"
195+
echo "Cloning Gravity Forms Quiz"
196196
if [ -n "$GIT_USER" ] && [ -n "$GIT_TOKEN" ] && [ -n "$GF_CHAINEDSELECTS_REPO" ]; then
197197
git clone https://$GIT_USER:$GIT_TOKEN@$GF_QUIZ_REPO $WP_CORE_DIR/wp-content/plugins/gravityformsquiz
198198
else
@@ -237,6 +237,10 @@ setup_plugin() {
237237
wp plugin install wp-gatsby
238238
wp plugin activate wp-gatsby
239239

240+
# Install WPJamstack Deployments and Activate
241+
wp plugin install wp-jamstack-deployments
242+
wp plugin activate wp-jamstack-deployments
243+
240244
# activate the plugin
241245
wp plugin activate wp-graphql-gravity-forms
242246

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ parameters:
2121
- ../gravityformschainedselects/
2222
- ../gravityformsquiz/
2323
- ../wp-gatsby/
24+
- ../wp-jamstack-deployments/
2425
ignoreErrors:
2526
- '#^Function apply_filters(_ref_array)? invoked with ([1-9]|1[0-2]) parameters, 2 required\.$#'
2627
- '#^Function gf_apply_filters(_ref_array)? invoked with ([1-9]|1[0-2]) parameters, 2 required\.$#'

phpstan/constants.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
*/
55

66
define( 'WPGRAPHQL_GF_AUTOLOAD', true );
7+
define( 'CRGEARY_JAMSTACK_DEPLOYMENTS_OPTIONS_KEY', 'wp-jamstack-deployments' );

src/Extensions/Extensions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
use WPGraphQL\GF\Extensions\GFQuiz\GFQuiz;
1313
use WPGraphQL\GF\Extensions\GFSignature\GFSignature;
1414
use WPGraphQL\GF\Extensions\WPGatsby\WPGatsby;
15+
use WPGraphQL\GF\Extensions\WPJamstackDeployments\WPJamstackDeployments;
1516

1617
/**
1718
* Class - GFSignature
1819
*/
1920
class Extensions {
20-
2121
/**
2222
* Register Gravity Forms Extensions.
2323
*/
@@ -26,5 +26,6 @@ public static function register() : void {
2626
GFQuiz::register_hooks();
2727
GFSignature::register_hooks();
2828
WPGatsby::register_hooks();
29+
WPJamstackDeployments::register_hooks();
2930
}
3031
}

src/Extensions/WPGatsby/GravityFormsMonitor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function __construct( ActionMonitor $action_monitor ) {
4646
*/
4747
public function init() : void {
4848
// Create form.
49-
add_action( 'gform_after_duplicate_form', [ $this, 'after_save_form' ], 10, 2 );
49+
add_action( 'gform_post_form_duplicated', [ $this, 'after_duplicate_form' ], 10, 2 );
5050
// Create or update form.
5151
add_action( 'gform_after_save_form', [ $this, 'after_save_form' ], 10, 2 );
5252
// Update form.

src/Extensions/WPGatsby/Settings.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public static function register_settings() : void {
5959
'label' => __( 'Monitor Gravity Forms Actions', 'wp-graphql-gravity-forms' ),
6060
'type' => 'multicheck',
6161
'options' => [
62-
'create_form' => 'Form Creation',
63-
'update_form' => 'Form Updates',
64-
'delete_form' => 'Form Deletions',
65-
'create_entry' => 'Entry Submission',
66-
'update_entry' => 'Entry Updates',
67-
'create_draft_entry' => 'Draft Entry Creation',
62+
'create_form' => __( 'Form Creation', 'wp-graphql-gravity-forms' ),
63+
'update_form' => __( 'Form Updates', 'wp-graphql-gravity-forms' ),
64+
'delete_form' => __( 'Form Deletions', 'wp-graphql-gravity-forms' ),
65+
'create_entry' => __( 'Entry Submission', 'wp-graphql-gravity-forms' ),
66+
'update_entry' => __( 'Entry Updates', 'wp-graphql-gravity-forms' ),
67+
'create_draft_entry' => __( 'Draft Entry Creation', 'wp-graphql-gravity-forms' ),
6868
],
6969
'default' => [
7070
'create_form' => 'create_form',
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
/**
3+
* Enables and initializes the Gravity Forms Action Monitor
4+
*
5+
* @package WPGraphQL\GF\Extensions\WPJamstackDeployments
6+
* @since @todo
7+
*/
8+
9+
namespace WPGraphQL\GF\Extensions\WPJamstackDeployments;
10+
11+
/**
12+
* Class - WPJamstackDeployments
13+
*/
14+
class WPJamstackDeployments {
15+
/**
16+
* The option named used in the settings API.
17+
*
18+
* @var string
19+
*/
20+
public static string $option_name = 'webhook_gf';
21+
22+
/**
23+
* The options array.
24+
*
25+
* @var array
26+
*/
27+
public static array $options;
28+
29+
30+
/**
31+
* {@inheritDoc}
32+
*/
33+
public static function register_hooks(): void {
34+
if ( ! self::is_wp_jamstack_deployments_enabled() ) {
35+
return;
36+
}
37+
38+
// Register settings.
39+
add_action( 'admin_init', [ __CLASS__, 'register_settings' ], 11 );
40+
// Filters sanitization callback.
41+
add_filter( 'sanitize_option_' . self::get_options_key(), [ __CLASS__, 'sanitize' ], 10 );
42+
43+
// Trigger deployments.
44+
self::trigger_deployments();
45+
}
46+
47+
/**
48+
* Returns whether WPJamstackDeployments is enabled.
49+
*
50+
* @return boolean
51+
*/
52+
public static function is_wp_jamstack_deployments_enabled() : bool {
53+
return class_exists( 'Crgeary\JAMstackDeployments\App' );
54+
}
55+
56+
/**
57+
* Returns the Options Key used by WPJamstackDeployments.
58+
*/
59+
public static function get_options_key() : string {
60+
return defined( 'CRGEARY_JAMSTACK_DEPLOYMENTS_OPTIONS_KEY' ) ? CRGEARY_JAMSTACK_DEPLOYMENTS_OPTIONS_KEY : 'wp_jamstack_deployments';
61+
}
62+
63+
/**
64+
* Returns the array of options.
65+
*/
66+
public static function get_options() : array {
67+
if ( empty( self::$options ) ) {
68+
self::$options = \jamstack_deployments_get_options();
69+
}
70+
71+
return self::$options;
72+
}
73+
74+
/**
75+
* Registers settings to enable/disable deployments.
76+
*/
77+
public static function register_settings() : void {
78+
$key = self::get_options_key();
79+
80+
$option = self::get_options();
81+
82+
$option_name = self::$option_name;
83+
84+
add_settings_field(
85+
$option_name,
86+
__( 'Gravity Forms', 'wp-graphql-gravity-forms' ),
87+
[ 'Crgeary\JAMstackDeployments\Field', 'checkboxes' ],
88+
$key,
89+
'general',
90+
[
91+
'name' => "{$key}[{$option_name}]",
92+
'value' => isset( $option[ $option_name ] ) ? $option[ $option_name ] : [],
93+
'choices' => [
94+
'create_form' => __( 'Form Creation', 'wp-graphql-gravity-forms' ),
95+
'update_form' => __( 'Form Updates', 'wp-graphql-gravity-forms' ),
96+
'delete_form' => __( 'Form Deletions', 'wp-graphql-gravity-forms' ),
97+
'create_entry' => __( 'Entry Submission', 'wp-graphql-gravity-forms' ),
98+
'update_entry' => __( 'Entry Updates', 'wp-graphql-gravity-forms' ),
99+
'create_draft_entry' => __( 'Draft Entry Creation', 'wp-graphql-gravity-forms' ),
100+
],
101+
'description' => __( 'Only selected Gravity Forms actions will trigger a deployment.', 'wp-graphql-gravity-forms' ),
102+
'legend' => __( 'Gravity Forms', 'wp-graphql-gravity-forms' ),
103+
]
104+
);
105+
}
106+
107+
/**
108+
* Sanitize user input.
109+
*
110+
* @param array $input .
111+
*/
112+
public static function sanitize( array $input ) : array {
113+
if ( ! isset( $input[ self::$option_name ] ) || ! is_array( $input[ self::$option_name ] ) ) {
114+
$input[ self::$option_name ] = [];
115+
}
116+
117+
return $input;
118+
}
119+
120+
/**
121+
* Adds actions to trigger deployments based on the settings.
122+
*/
123+
public static function trigger_deployments() : void {
124+
$options = self::get_options();
125+
if ( empty( $options[ self::$option_name ] ) ) {
126+
return;
127+
}
128+
129+
foreach ( $options[ self::$option_name ] as $gf_hook ) {
130+
switch ( $gf_hook ) {
131+
case 'create_form':
132+
add_action( 'gform_post_form_duplicated', 'jamstack_deployments_fire_webhook' );
133+
add_action( 'gform_after_save_form', [ __CLASS__, 'after_save_form' ], 10, 2 );
134+
break;
135+
case 'update_form':
136+
// Only add action if it doenst already exist.
137+
if ( ! has_action( 'gform_after_save_form', [ __CLASS__, 'after_save_form' ] ) ) {
138+
add_action( 'gform_after_save_form', [ __CLASS__, 'after_save_form' ], 10, 2 );
139+
}
140+
add_action( 'gform_post_update_form_meta', 'jamstack_deployments_fire_webhook' );
141+
add_action( 'gform_post_form_activated', 'jamstack_deployments_fire_webhook' );
142+
add_action( 'gform_post_form_deactivated', 'jamstack_deployments_fire_webhook' );
143+
add_action( 'gform_post_form_restored', 'jamstack_deployments_fire_webhook' );
144+
add_action( 'gform_post_form_trashed', 'jamstack_deployments_fire_webhook' );
145+
break;
146+
case 'delete_form':
147+
add_action( 'gform_after_delete_form', 'jamstack_deployments_fire_webhook' );
148+
break;
149+
case 'create_entry':
150+
add_action( 'gform_after_submission', 'jamstack_deployments_fire_webhook' );
151+
break;
152+
case 'update_entry':
153+
add_action( 'gform_after_update_entry', 'jamstack_deployments_fire_webhook' );
154+
add_action( 'gform_post_update_entry', 'jamstack_deployments_fire_webhook' );
155+
break;
156+
case 'create_draft_entry':
157+
add_action( 'gform_incomplete_submission_post_save', 'jamstack_deployments_fire_webhook' );
158+
break;
159+
}
160+
}
161+
}
162+
163+
/**
164+
* Triggers the correct deployment when a form is saved.
165+
*
166+
* @param array $form .
167+
* @param boolean $is_new .
168+
*/
169+
public static function after_save_form( array $form, bool $is_new ) : void {
170+
$options = self::get_options();
171+
172+
if ( in_array( 'create_form', $options[ self::$option_name ], true ) && $is_new ) {
173+
\jamstack_deployments_fire_webhook();
174+
} elseif ( in_array( 'update_form', $options[ self::$option_name ], true ) && ! $is_new ) {
175+
\jamstack_deployments_fire_webhook();
176+
}
177+
}
178+
}

0 commit comments

Comments
 (0)