|
| 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