Skip to content

Instantly share code, notes, and snippets.

@shadyvb
Created December 19, 2017 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shadyvb/4db31abaab0de34e6795498efd7ffb90 to your computer and use it in GitHub Desktop.
Save shadyvb/4db31abaab0de34e6795498efd7ffb90 to your computer and use it in GitHub Desktop.
Override Ninja Forms Submission download link to download all the things without writing to filesystem
<?php
namespace MY_R_KEY_DOESNT_WORRRRK_PROPERLY;
/**
* Download all submissions of a form, between a specific range if provided.
*
* @action wp_ajax_ninja_forms_export_all_subs
*/
function download_all_subs() {
$form_id = filter_input( INPUT_GET, 'form_id', FILTER_SANITIZE_NUMBER_INT );
$from = filter_input( INPUT_GET, 'from' );
$to = filter_input( INPUT_GET, 'to' );
if ( empty( $form_id ) ) {
wp_die( esc_html__( 'Invalid form ID passed', 'hello-dolly' ) );
}
$args = [
'posts_per_page' => -1,
'post_type' => 'nf_sub',
'fields' => 'ids',
'meta_query' => [
[
'key' => '_form_id',
'value' => $form_id,
],
],
'no_found_posts' => true,
];
if ( $from && $to ) {
$args['date_query'] = [
'after' => date( 'r', $from ),
'before' => date( 'r', $to ),
'inclusive' => true,
];
}
$post_ids = get_posts( $args );
if ( empty( $post_ids ) ) {
wp_die( esc_html__( 'No submissions found in the chosen date range.', 'byebye-dolly' ) );
}
\NF_Database_Models_Submission::export( $form_id, $post_ids );
}
add_action( 'wp_ajax_ninja_forms_export_all_subs', __NAMESPACE__ . '\\download_all_subs' );
/**
* Alter the URL of the submissions download link to point to our new endpoint
*
* @filter ninja_forms_download_submissions_url
*
* @return string
*/
function download_all_subs_url_change( $url, $form_id ) {
$args = [
'form_id' => absint( $form_id ),
'from' => get_date_from_gmt( date( 'Y-m-d H:i:s', strtotime( filter_input( INPUT_GET, 'begin_date' ) ) ), 'U' ),
'to' => get_date_from_gmt( date( 'Y-m-d H:i:s', strtotime( filter_input( INPUT_GET, 'end_date' ) ) ), 'U' ),
];
$url = add_query_arg( array_filter( $args ), admin_url( 'admin-ajax.php?action=ninja_forms_export_all_subs' ) );
return $url;
}
// See https://github.com/wpninjas/ninja-forms/pull/3259
add_filter( 'ninja_forms_download_all_submissions_url', __NAMESPACE__ . '\\download_all_subs_url_change', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment