Skip to content

Instantly share code, notes, and snippets.

@codearachnid
Created February 14, 2024 21:56
Show Gist options
  • Save codearachnid/13a859d369befbebfc00a49f6fed7124 to your computer and use it in GitHub Desktop.
Save codearachnid/13a859d369befbebfc00a49f6fed7124 to your computer and use it in GitHub Desktop.
Gravity Forms: Webhooks Entry Caller
<?php
// add meta boxes on entry detail
add_filter('gform_entry_detail_meta_boxes', function( $meta_boxes ){
// check that Webhooks class is available
if( class_exists('GF_Webhooks') ){
$meta_boxes['webhooks'] = [
'title' => 'Webhooks',
'callback' => 'gf_webhooks_replay_meta_box',
'context' => 'side',
];
}
return $meta_boxes;
});
// entry detail hook ajax replay webhooks
add_action( 'wp_ajax_gf_replay_webhooks', function(){
check_admin_referer( 'gf_replay_webhooks', 'gf_replay_webhooks' );
$form_id = absint( rgpost( 'formId' ) );
$entry_id = absint( rgpost( 'entryId' ) );
$return = [
'status' => '',
];
$status = 0;
if ( function_exists( 'gf_webhooks' ) ) {
$entry = GFAPI::get_entry( $entry_id );
$form = GFAPI::get_form( $form_id );
gf_webhooks()->maybe_process_feed( $entry, $form );
gf_feed_processor()->save()->dispatch();
$status = 200;
$return = [
'status' => 'success',
];
}
wp_send_json($return, $status);
});
function gf_webhooks_replay_meta_box( $args, $metabox ){
$entry_id = $args['entry']['id'];
$form_id = $args['form']['id'];
$feeds = [];
if ( function_exists( 'gf_webhooks' ) ) {
$feeds = gf_webhooks()->get_feeds( $form_id );
}
// if user can't edit notes + prevent replay webhooks
if ( ! GFCommon::current_user_can_any( 'gravityforms_edit_entry_notes' ) ) {
return;
}
?>
<div class="message" style="display:none;"></div>
<div>
<?php
if ( ! is_array( $feeds ) || empty( $feeds ) ) {
// notify when no feeds are available
printf('<p class="description">%s</p><a href="%s" class="button">%s</a>',
esc_html( 'You cannot replay webhooks for this entry because this form does not currently have any webhooks configured.', 'gravityforms' ),
admin_url( "admin.php?subview=gravityformswebhooks&page=gf_edit_forms&view=settings&id={$form_id}" ),
esc_html( 'Configure Webhooks', 'gravityforms' )
);
} else {
foreach ( $feeds as $feed ) {
printf( '<input type="checkbox" class="gform_webhooks" value="%1$s" id="webhook_%1$s" onclick="clearWebhooksMessage();"/><label for="webhook_%1$s">%2$s</label><br /><br />',
esc_attr( $feed['id'] ),
esc_html( $feed['meta']['feedName'] )
);
}
?>
<input type="button" name="webhook_replay" value="<?php esc_attr_e( 'Replay', 'gravityforms' ) ?>" class="button" onclick="ReplayWebhooks();" />
<span id="webhook_wait_container" style="display:none; margin-left: 5px;">
<i class='gficon-gravityforms-spinner-icon gficon-spin'></i> <?php esc_html_e( 'Replaying...', 'gravityforms' ); ?>
</span>
<script type="text/javascript">
function ReplayWebhooks() {
var selectedWebhooks = new Array();
jQuery(".gform_webhooks:checked").each(function () {
selectedWebhooks.push(jQuery(this).val());
});
if (selectedWebhooks.length <= 0) {
displayMessage(<?php echo json_encode( __( 'You must select at least one webhook to replay.', 'gravityforms' ) ); ?>, 'error', '#webhooks');
return;
}
jQuery('#webhook_wait_container').fadeIn();
jQuery.post(ajaxurl, {
action : "gf_replay_webhooks",
gf_replay_webhooks : '<?php echo wp_create_nonce( 'gf_replay_webhooks' ); ?>',
webhooks : jQuery.toJSON(selectedWebhooks),
entryId : '<?php echo absint( $entry_id ); ?>',
formId : '<?php echo absint( $form_id ); ?>'
},
function (response) {
if (response.status == 'success') {
displayMessage(<?php echo json_encode( esc_html__( 'Selected webhooks replayed successfully.', 'gravityforms' ) ); ?>, "success", "#webhooks" );
// reset UI
jQuery(".gform_webhooks:checked").prop( 'checked', false );
setTimeout(function () {
clearWebhooksMessage();
}, 5000);
} else {
displayMessage(response, "error", "#webhooks");
}
jQuery('#webhook_wait_container').hide();
}
);
}
function clearWebhooksMessage(){
jQuery( '#webhooks .message' ).slideUp();
}
</script>
<?php
}
?>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment