Skip to content

Instantly share code, notes, and snippets.

@uamv
Created May 5, 2020 12:19
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save uamv/e597d1c6fbf5dda77ddf5f424f0ae5a2 to your computer and use it in GitHub Desktop.
Save uamv/e597d1c6fbf5dda77ddf5f424f0ae5a2 to your computer and use it in GitHub Desktop.
List recent entries from Gravity Forms via shortcode with basic parameters
<?php
/*
* Adds a [gravityget] shortcode with parameters for listing specific GF entries
* form="2,4,11" (comma-separated list of forms from which to pull entries)
* user="current|user_id|all" (identify the user for which entries should be pulled)
* type="save|submitted" (show either saved or submitted entries)
* since="" (not sure this works, yet)
* count="" (not yet implemented)
*/
add_shortcode( 'gravityget', function ( $atts, $content ) {
extract( shortcode_atts( array(
'form' => array(),
'user' => 'current',
'type' => 'saved',
'since' => ''
), $atts ) );
$return_submission_forms = array();
$gforms = GFAPI::get_forms();
if ( ! empty( $form ) ) {
$form = explode( ',', $form );
foreach ( $gforms as $key => $gform ) {
if ( ! in_array( (string) $gform['id'], $form, true ) ) {
$return_submission_forms[ $gform['id'] ] = $gform;
}
}
} else {
foreach ( $gforms as $key => $gform ) {
$return_submission_forms[ $gform['id'] ] = $gform;
}
}
if ( $type == 'saved' ) {
// get all draft submissions
$draft_submissions = GFFormsModel::get_draft_submissions();
if ( empty( $draft_submissions ) ) {
return array();
}
foreach ( $return_submission_forms as $key => &$return_submission_form ) {
$return_submission_form['expiration_days'] = apply_filters( 'gform_temp_file_expiration_days', 30, $return_submission_form );
$return_submission_form['expiration_days'] = apply_filters( 'gform_incomplete_submissions_expiration_days', $return_submission_form['expiration_days'] );
}
$return_submissions = array();
// loop through draft submissions
foreach ( $draft_submissions as $i => $draft_submission ) {
// loop through forms we want to include
foreach ( $return_submission_forms as $form_id => &$return_submission_form ) {
// if draft submission belongs to form, add it to the return array
if ( $form_id == $draft_submission['form_id'] ) {
$return_submissions[] = $draft_submission;
}
}
}
// loop through return submission array and check that user matches our request
foreach ( $return_submissions as $i => &$return_submission ) {
$submission = json_decode( $return_submission['submission'], true );
$return_submission['submission'] = $submission;
if ( $user == 'current' ) {
if ( get_current_user_id() != $submission['partial_entry']['created_by'] ) {
unset( $return_submissions[ $i ] );
}
} elseif ( $user == 'public' ) {
if ( 'NULL' != $submission['partial_entry']['created_by'] ) {
unset( $return_submissions[ $i ] );
}
} elseif ( $user != 'all' ) {
if ( (int) $user != $submission['partial_entry']['created_by'] ) {
unset( $return_submissions[ $i ] );
}
}
}
usort( $return_submissions, function( $a, $b ) {
return $a['form_id'] <=> $b['form_id'];
});
// render the html
$html = '<table><thead><tr><th>Form</th><th>User</th><th>Started</th><th>Saved</th><th>Expires</th><th>Link</th><th>Send</th></thead>';
$html .= '<tbody>';
foreach ( $return_submissions as $return_submission ) {
$html .= '<tr>';
$html .= '<td>' . $return_submission_forms[ (string) $return_submission['form_id'] ]['title'] . '</td>';
if ( 'NULL' != $return_submission['submission']['partial_entry']['created_by'] ) {
$submission_user = get_user_by( 'id', $return_submission['submission']['partial_entry']['created_by'] );
$submission_user = $submission_user->user_login;
} else {
$submission_user = 'Logged Out User';
}
$html .= '<td>' . $submission_user . '</td>';
$html .= '<td>' . date( "d M Y", strtotime( $return_submission['date_created'] ) ) . '</td>';
$html .= '<td>';
if ( isset( $return_submission['submission']['last_saved'] ) ) {
$html .= date( "d M Y", $return_submission['submission']['last_saved'] );
}
$html .= '</td>';
$html .= '<td>' . date( "d M Y", strtotime( $return_submission['date_created'] . ' + ' . $return_submission_forms[ (string) $return_submission['form_id'] ]['expiration_days'] . ' days' ) ) . '</td>';
$html .= '<td><a href="' . $return_submission['source_url'] . '?gf_token=' . $return_submission['uuid'] . '" class="mdi mdi-clipboard-plus">Continue</a></td>';
$subject = urlencode( $return_submission_forms[ (string) $return_submission['form_id'] ]['title'] );
$body = urlencode( $return_submission['source_url'] . '?gf_token=' . $return_submission['uuid'] );
if ( 'NULL' != $return_submission['submission']['partial_entry']['created_by'] ) {
$submission_user = get_user_by( 'id', $return_submission['submission']['partial_entry']['created_by'] );
$html .= '<td><a href="mailto:' . $submission_user->user_email . '?subject=' . $subject . '&body=' . $body . '" class="mdi mdi-email-edit">Send</a></td>';
} elseif ( $return_submission['email'] ) {
$html .= '<td><a href="mailto:' . $return_submission['email'] . '?subject=' . $subject . '&body=' . $body . '" class="mdi mdi-email-edit">Send</a></td>';
} else {
$html .= '<td></td>';
}
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
} elseif ( $type == 'submitted' ) {
foreach ( $return_submission_forms as $key => &$gform ) {
if ( class_exists( 'GPDFAPI' ) ) {
/* Get a list of PDFs assigned to form #2 */
$pdfs = GPDFAPI::get_form_pdfs( $key );
if ( ! is_wp_error( $pdfs ) && sizeof( $pdfs ) > 0 ) {
foreach ( $pdfs as $key => $pdf ) {
$gform['pdf_key'] = $key;
break;
}
}
}
}
$search_criteria = array();
$search_criteria['status'] = 'active';
if ( $since != '' ) {
$search_criteria['start_date'] = date( 'Y-m-d', $since );
$search_criteria['end_date'] = date( 'Y-m-d', time() );
}
$paging = array( 'offset' => 0, 'page_size' => 200 );
$submissions = GFAPI::get_entries( 0, $search_criteria, array(), $paging );
// loop through draft submissions
foreach ( $submissions as $i => $submission ) {
// loop through forms we want to include
foreach ( $return_submission_forms as $form_id => &$return_submission_form ) {
// if draft submission belongs to form, add it to the return array
if ( $form_id == $submission['form_id'] ) {
$return_submissions[] = $submission;
}
}
}
// loop through return submission array and check that user matches our request
foreach ( $return_submissions as $i => &$return_submission ) {
if ( $user == 'current' ) {
if ( get_current_user_id() != $return_submission['created_by'] ) {
unset( $return_submissions[ $i ] );
}
} elseif ( $user == 'public' ) {
if ( 'NULL' != $return_submission['created_by'] ) {
unset( $return_submissions[ $i ] );
}
} elseif ( $user != 'all' ) {
if ( (int) $user != $return_submission['created_by'] ) {
unset( $return_submissions[ $i ] );
}
}
}
// render the html
$html = '<table><thead><tr><th>Form</th><th>User</th><th>Submitted</th>';
$html .= ( current_user_can( 'gravityforms_view_entries' ) || current_user_can( 'manage_options' ) ) ? '<th>Entry</th>' : '';
$html .= '<th>PDF</th></thead>';
$html .= '<tbody>';
foreach ( $return_submissions as $return_submission ) {
$html .= '<tr>';
$html .= '<td>' . $return_submission_forms[ (string) $return_submission['form_id'] ]['title'] . '</td>';
if ( 'NULL' != $return_submission['created_by'] ) {
$submission_user = get_user_by( 'id', $return_submission['created_by'] );
$submission_user = $submission_user->user_login;
} else {
$submission_user = 'Logged Out User';
}
$html .= '<td>' . $submission_user . '</td>';
$html .= '<td>' . date( "d M Y", strtotime( $return_submission['date_created'] ) ) . '</td>';
if ( current_user_can( 'gravityforms_view_entries' ) || current_user_can( 'manage_options' ) ) {
$entry_url = get_bloginfo( 'wpurl' ) . '/wp-admin/admin.php?page=gf_entries&view=entry&id=' . $return_submission['form_id'] . '&lid=' . $return_submission['id'];
$entry_url = esc_url( apply_filters( 'gform_entry_detail_url', $entry_url, $return_submission_forms[ (string) $return_submission['form_id'] ], $return_submission ) );
$html .= '<td><a href="' . $entry_url . '" class="mdi mdi-eye">View</a></td>';
}
if ( isset( $return_submission_forms[ (string) $return_submission['form_id'] ]['pdf_key'] ) ) {
$pdf = GPDFAPI::get_pdf( $return_submission['form_id'], $return_submission_forms[ (string) $return_submission['form_id'] ]['pdf_key'] );
if ( $pdf['restrict_owner'] == 'No' && ! current_user_can( 'gravityforms_view_entries' ) || current_user_can( 'manage_options' ) ) {
$gravitycode = '[gravitypdf id="' . $return_submission_forms[ (string) $return_submission['form_id'] ]['pdf_key'] . '" raw="1" type="view" entry="' . $return_submission['id'] . '"]';
$html .= '<td><a href="' . do_shortcode( $gravitycode ) . '" class="mdi mdi-file-pdf-box" target="_blank">View</a></td>';
} else {
$html .= '<td><span class="mdi mdi-close-octagon"></span>';
}
} else {
$html .= '<td></td>';
}
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
}
return $html;
}, 10, 2 );
@tfeige91
Copy link

Hi Joshua,
this one is just awesome!
I only got one issue: If i specify the form-ID the shortcode shows me any form data but the specified one.
I guess I found the issue (at least it is working for me):

foreach ( $gforms as $key => $gform ) {

		if ( **!** in_array( (string) $gform['id'], $form, true ) ) {

			$return_submission_forms[ $gform['id'] ] = $gform;

		}

	}

In the foreach -> if i left out the ! -> now it is showing me exactly the forms that I specified.

Kind regards,
Tim

@pdahl95
Copy link

pdahl95 commented Mar 24, 2021

Thanks, this code works perfectly! One question, how can you show the latest entry? Sorting it by newest date, saved or edited?

@bharrison89
Copy link

Cool code. Works great for me. Would it be possible to include the value of a field that has been completed. For instance field ID 1 telephone.

Cheers

@warengonzaga
Copy link

the last saved is not working. I don't know why can someone fix it?

@raphtech80
Copy link

Hello,

Is it possible to extract the link to resume the form to display it in gravity view?
Thanks

@warengonzaga
Copy link

Hello,

Is it possible to extract the link to resume the form to display it in gravity view? Thanks

Yes

@raphtech80
Copy link

Thanks how i can do it please?

@raphtech80
Copy link

I

someone help me?

@warengonzaga
Copy link

warengonzaga commented Oct 25, 2021

I apply the changes made by @tfeige91 and also fixes the bug in repeating items in the "saved" function.
Here's my version: https://gist.github.com/WarenGonzaga/0d93e2b1b706e0b47096246aee4df04e

Changes: (line 81 to 82)

<<< Before
// loop through return submission array and check that user matches our request
foreach ( $return_submissions as $i => &$return_submission ) {
>>> After
foreach ( $return_submissions as $i => $return_submission ) {

Buy Me a Coffee later.

@sim2
Copy link

sim2 commented Nov 11, 2021

@raphtech80

Simply write in the 'form' => array(), on line 14 your form ID's you would like to save the recent entries of. (i.e. 'form' => array(7,3,11),)
Then insert the [gravityget] shortcode wherever you like.

Thanks @uamv , works like a charm! 👍

@Web-Assembler
Copy link

In the foreach -> if i left out the ! -> now it is showing me exactly the forms that I specified.

I also found this rogue false condition mentioned by @tfeige91 and removed it to fix the problem

@Chronoweb27
Copy link

Chronoweb27 commented May 16, 2022

Hi,
can you tell me where to put the php file in the wordpress please? @tfeige91 @raphtech80 @warengonzaga @Web-Assembler @sim2

@sim2
Copy link

sim2 commented May 17, 2022

Hi, can you tell me where to put the php file in the wordpress please? @tfeige91 @raphtech80 @warengonzaga @Web-Assembler @sim2

Appearance -> Theme File Editor. Place it in the Theme Functions file. (functions.php)

@Chronoweb27
Copy link

Hi, can you tell me where to put the php file in the wordpress please? @tfeige91 @raphtech80 @warengonzaga @Web-Assembler @sim2

Appearance -> Theme File Editor. Place it in the Theme Functions file. (functions.php)

great thanks ^^

@Chronoweb27
Copy link

Chronoweb27 commented May 17, 2022

Hi, can you tell me where to put the php file in the wordpress please? @tfeige91 @raphtech80 @warengonzaga @Web-Assembler @sim2

Appearance -> Theme File Editor. Place it in the Theme Functions file. (functions.php)

I have this message when i put the shortcode in a page. Have you got an idea ?

Form User Started Saved Expires Link Send

@ajoe101
Copy link

ajoe101 commented Jun 13, 2022

Hi, How do you number your saved forms?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment