/shortcode-gravityget.php Secret
Last active
March 28, 2022 20:34
-
-
Save warengonzaga/0d93e2b1b706e0b47096246aee4df04e to your computer and use it in GitHub Desktop.
List recent entries from Gravity Forms via shortcode with basic parameters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello, I'm using your code, and it's helping me a lot, thanks for it, but in the table with the records, I want to show other form fields, how do I get them? and your code is not catching the user who submitted the record.