Created
November 5, 2019 11:18
-
-
Save damiencarbery/99aa341b9af94b0b5b5368c6aaad5aa7 to your computer and use it in GitHub Desktop.
Retrieve Ninja Forms Submissions for date range - Use a direct query to retrieve Ninja Forms submissions for a specified date range. https://www.damiencarbery.com/2019/11/retrieve-ninja-forms-submissions-for-date-range/
This file contains hidden or 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 | |
// Restrict who can access this script. | |
/*$permitted_ips = array( '12.34.56.78' ); | |
if ( false == in_array($_SERVER['REMOTE_ADDR'], $permitted_ips) ) { | |
header( 'HTTP/1.0 403 Forbidden' ); | |
die(); | |
}*/ | |
header('HTTP/1.1 200 OK'); | |
define('WP_USE_THEMES', false); | |
/** Loads the WordPress Environment */ | |
require( dirname( __FILE__ ) . '/wp-blog-header.php' ); | |
?> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<title>Ninja Forms Submissions</title> | |
<style>table, th, td { border: 1px solid #ccc; border-collapse: collapse; } | |
/* Based on blueprintcss.org */ | |
table { margin-bottom: 1.4em; width: 100%; } | |
th { font-weight: bold; } | |
thead th { background: #c3d9ff; } th, td, caption { padding: 0.5em; } | |
tbody tr:nth-child(even) td, tbody tr.even td { background-color: #f1f1f1; } | |
tfoot { font-style: italic; } | |
</style> | |
</head> | |
<body> | |
<?php | |
// Retrieve the submissions for the specified number of days. | |
$end_date = '2019-11-04'; | |
$num_days = 7; | |
$start_date_time = strtotime( $end_date ) - ( $num_days * DAY_IN_SECONDS ); | |
$start_date = date( 'Y-m-d', $start_date_time ); | |
$forms = Ninja_Forms()->form()->get_forms(); | |
foreach ( $forms as $form ) { | |
$form_id = $form->get_id(); | |
echo '<h2>', $form->get_setting( 'title' ), '</h2>'; | |
// Get the form's fields and their internal name (key) and external name (label). | |
$form_fields = Ninja_Forms()->form( $form_id )->get_fields(); | |
$key_to_fieldname = array(); | |
foreach ( $form_fields as $form_field ) { | |
$key_to_fieldname[ $form_field->get_setting( 'key' ) ] = $form_field->get_setting( 'label' ); | |
} | |
// Remove unneeded fields as there won't be any data in them (or they aren't needed in this script). | |
$unneeded_fields = array( 'submit', 'hr', ); | |
foreach ( $unneeded_fields as $unneeded_field ) { | |
unset( $key_to_fieldname[ $unneeded_field ] ); | |
} | |
// Prepare the query - all submissions for the specified form, oldest first. | |
$args = array( | |
'posts_per_page' => -1, | |
'post_type' => 'nf_sub', | |
'order' => 'ASC', | |
'meta_query' => array( | |
array( | |
'key' => '_form_id', | |
'value' => $form_id, | |
), | |
), | |
'date_query' => array( | |
'after' => $start_date, | |
'before' => $end_date, | |
'inclusive' => true, | |
), | |
'fields' => 'ids', // Just return IDs as get_sub() will get more info. | |
); | |
$submissions = array(); | |
$submissions = get_posts( $args ); | |
if ( $submissions ) { | |
echo '<table><thead><tr><th>Sequence</th><th>Submission Date</th><th>', implode( '</th><th>', array_values( $key_to_fieldname ) ), '</th></tr></thead><tbody>'; | |
foreach ( $submissions as $submission_id ) { | |
$submission = Ninja_Forms()->form()->get_sub( $submission_id ); | |
echo '<tr><td>', $submission_id, '</td><td>', $submission->get_sub_date( 'd/m/Y H:i' ), '</td>'; | |
foreach ( $form_fields as $field ) { | |
// TODO: Consider creating an array of the field key, id and type outside | |
// this loop to reduce the number of calls to the Ninja Forms API. | |
$field_key = $field->get_setting( 'key' ); | |
// Could exclude fields based on their type. | |
//$field_type = $field->get_setting( 'type' ); | |
if ( !array_key_exists( $field_key, $key_to_fieldname ) ) { | |
continue; | |
} | |
$field_class = Ninja_Forms()->fields[ $field->get_setting( 'type' ) ]; | |
echo '<td>', $submission->get_field_value( $field->get_id() ), '</td>'; | |
} | |
echo '</tr>'; | |
} | |
echo '</tbody></table>'; | |
} | |
else { | |
echo '<p>No submissions in the specified date range.</p>'; | |
} | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment