Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/ninja-fo.../deprecat.../classes
File: subs.php
<?php if ( ! defined( 'ABSPATH' ) ) exit;
[0] Fix | Delete
/**
[1] Fix | Delete
* Submissions.
[2] Fix | Delete
* This class handles creating and exporting submissions.
[3] Fix | Delete
*
[4] Fix | Delete
* @package Ninja Forms
[5] Fix | Delete
* @subpackage Classes/Submissions
[6] Fix | Delete
* @copyright Copyright (c) 2014, WPNINJAS
[7] Fix | Delete
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
[8] Fix | Delete
* @since 2.7
[9] Fix | Delete
*/
[10] Fix | Delete
[11] Fix | Delete
class NF_Subs {
[12] Fix | Delete
[13] Fix | Delete
/**
[14] Fix | Delete
* Get things started
[15] Fix | Delete
*
[16] Fix | Delete
* @access public
[17] Fix | Delete
* @since 2.7
[18] Fix | Delete
* @return void/
[19] Fix | Delete
*/
[20] Fix | Delete
public function __construct() {
[21] Fix | Delete
[22] Fix | Delete
}
[23] Fix | Delete
[24] Fix | Delete
/**
[25] Fix | Delete
* Create a submission.
[26] Fix | Delete
*
[27] Fix | Delete
* @access public
[28] Fix | Delete
* @since 2.7
[29] Fix | Delete
* @return int $sub_id
[30] Fix | Delete
*/
[31] Fix | Delete
public function create( $form_id = '' ) {
[32] Fix | Delete
// Create Submission
[33] Fix | Delete
$post = array(
[34] Fix | Delete
'post_status' => 'publish',
[35] Fix | Delete
'post_type' => 'nf_sub'
[36] Fix | Delete
);
[37] Fix | Delete
$sub_id = wp_insert_post( $post );
[38] Fix | Delete
[39] Fix | Delete
// Add our form ID to the submission
[40] Fix | Delete
Ninja_Forms()->sub( $sub_id )->update_form_id( $form_id );
[41] Fix | Delete
[42] Fix | Delete
// Get the current sequential ID
[43] Fix | Delete
$last_sub = Ninja_Forms()->form( $form_id )->get_setting( 'last_sub', true );
[44] Fix | Delete
$seq_num = ! empty ( $last_sub ) ? $last_sub + 1 : 1;
[45] Fix | Delete
[46] Fix | Delete
$seq_num = apply_filters( 'nf_sub_seq_num', $seq_num, $form_id );
[47] Fix | Delete
[48] Fix | Delete
// Add the sequential ID to the post meta
[49] Fix | Delete
Ninja_Forms()->sub( $sub_id )->update_seq_num( $seq_num );
[50] Fix | Delete
[51] Fix | Delete
// Update our form data with the new "last seq id."
[52] Fix | Delete
Ninja_Forms()->form( $form_id )->update_setting( 'last_sub', $seq_num );
[53] Fix | Delete
[54] Fix | Delete
// Update our sub count
[55] Fix | Delete
Ninja_Forms()->form( $form_id )->sub_count = $seq_num - 1;
[56] Fix | Delete
[57] Fix | Delete
return $sub_id;
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Get submissions based on specific critera.
[62] Fix | Delete
*
[63] Fix | Delete
* @since 2.7
[64] Fix | Delete
* @param array $args
[65] Fix | Delete
* @return array $sub_ids
[66] Fix | Delete
*/
[67] Fix | Delete
public function get( $args = array() ) {
[68] Fix | Delete
[69] Fix | Delete
$query_args = array(
[70] Fix | Delete
'post_type' => 'nf_sub',
[71] Fix | Delete
'posts_per_page' => -1,
[72] Fix | Delete
'date_query' => array(
[73] Fix | Delete
'inclusive' => true,
[74] Fix | Delete
),
[75] Fix | Delete
);
[76] Fix | Delete
[77] Fix | Delete
if( isset( $args['form_id'] ) ) {
[78] Fix | Delete
$query_args['meta_query'][] = array(
[79] Fix | Delete
'key' => '_form_id',
[80] Fix | Delete
'value' => $args['form_id'],
[81] Fix | Delete
);
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
if( isset( $args['seq_num'] ) ) {
[85] Fix | Delete
$query_args['meta_query'][] = array(
[86] Fix | Delete
'key' => '_seq_num',
[87] Fix | Delete
'value' => $args['seq_num'],
[88] Fix | Delete
);
[89] Fix | Delete
}
[90] Fix | Delete
[91] Fix | Delete
if( isset( $args['user_id'] ) ) {
[92] Fix | Delete
$query_args['author'] = $args['user_id'];
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
if( isset( $args['action'])){
[96] Fix | Delete
$query_args['meta_query'][] = array(
[97] Fix | Delete
'key' => '_action',
[98] Fix | Delete
'value' => $args['action'],
[99] Fix | Delete
);
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
if ( isset ( $args['meta'] ) ) {
[103] Fix | Delete
foreach ( $args['meta'] as $key => $value ) {
[104] Fix | Delete
$query_args['meta_query'][] = array(
[105] Fix | Delete
'key' => $key,
[106] Fix | Delete
'value' => $value,
[107] Fix | Delete
);
[108] Fix | Delete
}
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
if ( isset ( $args['fields'] ) ) {
[112] Fix | Delete
foreach ( $args['fields'] as $field_id => $value ) {
[113] Fix | Delete
$query_args['meta_query'][] = array(
[114] Fix | Delete
'key' => '_field_' . $field_id,
[115] Fix | Delete
'value' => $value,
[116] Fix | Delete
);
[117] Fix | Delete
}
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
if( isset( $args['begin_date'] ) AND $args['begin_date'] != '') {
[121] Fix | Delete
$query_args['date_query']['after'] = nf_get_begin_date( $args['begin_date'] )->format("Y-m-d G:i:s");
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
if( isset( $args['end_date'] ) AND $args['end_date'] != '' ) {
[125] Fix | Delete
$query_args['date_query']['before'] = nf_get_end_date( $args['end_date'] )->format("Y-m-d G:i:s");
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
$subs = new WP_Query( $query_args );;
[129] Fix | Delete
[130] Fix | Delete
$sub_objects = array();
[131] Fix | Delete
[132] Fix | Delete
if ( is_array( $subs->posts ) && ! empty( $subs->posts ) ) {
[133] Fix | Delete
foreach ( $subs->posts as $sub ) {
[134] Fix | Delete
$sub_objects[] = Ninja_Forms()->sub( $sub->ID );
[135] Fix | Delete
}
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
wp_reset_postdata();
[139] Fix | Delete
return $sub_objects;
[140] Fix | Delete
}
[141] Fix | Delete
[142] Fix | Delete
/**
[143] Fix | Delete
* Export submissions.
[144] Fix | Delete
*
[145] Fix | Delete
* @access public
[146] Fix | Delete
* @param array $sub_ids
[147] Fix | Delete
* @param bool @return
[148] Fix | Delete
* @since 2.7
[149] Fix | Delete
* @return void
[150] Fix | Delete
*/
[151] Fix | Delete
public function export( $sub_ids = '', $return = false ){
[152] Fix | Delete
global $ninja_forms_fields;
[153] Fix | Delete
[154] Fix | Delete
// Bail if we haven't been sent any IDs.
[155] Fix | Delete
if ( empty( $sub_ids ) )
[156] Fix | Delete
return false;
[157] Fix | Delete
[158] Fix | Delete
if ( ! is_array( $sub_ids ) )
[159] Fix | Delete
$sub_ids = array( $sub_ids );
[160] Fix | Delete
[161] Fix | Delete
$plugin_settings = nf_get_settings();
[162] Fix | Delete
$date_format = $plugin_settings['date_format'];
[163] Fix | Delete
[164] Fix | Delete
$label_array = array();
[165] Fix | Delete
// Get our Form ID.
[166] Fix | Delete
$form_id = Ninja_Forms()->sub( $sub_ids[0] )->form_id;
[167] Fix | Delete
[168] Fix | Delete
// Get our list of fields.
[169] Fix | Delete
$fields = Ninja_Forms()->form( $form_id )->fields;
[170] Fix | Delete
[171] Fix | Delete
// Add our sequential number.
[172] Fix | Delete
$label_array[0]['_seq_num'] = __( '#', 'ninja-forms' );
[173] Fix | Delete
[174] Fix | Delete
// Add our "Date" label.
[175] Fix | Delete
$label_array[0]['_date_submitted'] = __( 'Date Submitted', 'ninja-forms' );
[176] Fix | Delete
[177] Fix | Delete
$label_array = apply_filters( 'nf_subs_csv_label_array_before_fields', $label_array, $sub_ids );
[178] Fix | Delete
[179] Fix | Delete
foreach ( $fields as $field_id => $field ) {
[180] Fix | Delete
// Get our field type
[181] Fix | Delete
$field_type = $field['type'];
[182] Fix | Delete
// Check to see if our field type has been set as a "process_field".
[183] Fix | Delete
if ( isset ( $ninja_forms_fields[ $field_type ] ) ) {
[184] Fix | Delete
$reg_field = $ninja_forms_fields[ $field_type ];
[185] Fix | Delete
$process_field = $reg_field['process_field'];
[186] Fix | Delete
} else {
[187] Fix | Delete
$process_field = false;
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
// If this field's "process_field" is set to true, then add its label to the array.
[191] Fix | Delete
if ( $process_field ) {
[192] Fix | Delete
if ( isset ( $field['data']['admin_label'] ) && $field['data']['admin_label'] != '' ) {
[193] Fix | Delete
$label = $field['data']['admin_label'];
[194] Fix | Delete
} else if( isset ( $field['data']['label'] ) ) {
[195] Fix | Delete
$label = $field['data']['label'];
[196] Fix | Delete
}else{
[197] Fix | Delete
$label = '';
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
$label_array[0][ $field_id ] = apply_filters( 'nf_subs_csv_field_label', $label, $field_id );
[201] Fix | Delete
}
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
$label_array = ninja_forms_stripslashes_deep( $label_array );
[205] Fix | Delete
$label_array = apply_filters( 'nf_subs_csv_label_array', $label_array, $sub_ids );
[206] Fix | Delete
[207] Fix | Delete
$value_array = array();
[208] Fix | Delete
$x = 0;
[209] Fix | Delete
// Loop through our submissions and create a new row for each one.
[210] Fix | Delete
foreach ( $sub_ids as $sub_id ) {
[211] Fix | Delete
foreach ( $label_array[0] as $field_id => $label ) {
[212] Fix | Delete
// Make sure we aren't working with our date field, which will always have a field id of 0.
[213] Fix | Delete
if ( $field_id !== 0 ) {
[214] Fix | Delete
// Check to see if our field_id is numeric. If it isn't, then we're working with meta, not a field.
[215] Fix | Delete
if ( is_numeric( $field_id ) ) {
[216] Fix | Delete
// We're working with a field, grab the value.
[217] Fix | Delete
$user_value = Ninja_Forms()->sub( $sub_id )->get_field( $field_id );
[218] Fix | Delete
} else if ( '_date_submitted' == $field_id ) {
[219] Fix | Delete
// Get the date of our submission.
[220] Fix | Delete
$date = strtotime( Ninja_Forms()->sub( $sub_id )->date_submitted );
[221] Fix | Delete
// The first item is our date field.
[222] Fix | Delete
$user_value = date( $date_format, $date );
[223] Fix | Delete
} else if ( '_seq_num' == $field_id ) {
[224] Fix | Delete
$user_value = Ninja_Forms()->sub( $sub_id )->get_seq_num();
[225] Fix | Delete
} else {
[226] Fix | Delete
// We're working with a piece of meta, grabe the value.
[227] Fix | Delete
$user_value = Ninja_Forms()->sub( $sub_id )->get_meta( $field_id );
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
// Run our value through the appropriate filters before we flatten any arrays.
[231] Fix | Delete
$user_value = apply_filters( 'nf_subs_export_pre_value', $user_value, $field_id );
[232] Fix | Delete
[233] Fix | Delete
// Implode any arrays we might have.
[234] Fix | Delete
if ( is_array( $user_value ) ) {
[235] Fix | Delete
$user_value = implode( ',', $user_value );
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
// Add an ' to the beginning = sign to prevent any CSV/Excel security issues.
[239] Fix | Delete
if ( strpos( $user_value, '=' ) === 0 ) {
[240] Fix | Delete
$user_value = "'" . $user_value;
[241] Fix | Delete
}
[242] Fix | Delete
[243] Fix | Delete
// Run our final value through the appropriate filters and assign it to the array.
[244] Fix | Delete
$value_array[ $x ][ $field_id ] = htmlspecialchars_decode( apply_filters( 'nf_subs_csv_field_value', $user_value, $field_id ), ENT_QUOTES );
[245] Fix | Delete
}
[246] Fix | Delete
}
[247] Fix | Delete
$x++;
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
$value_array = ninja_forms_stripslashes_deep( $value_array );
[251] Fix | Delete
$value_array = apply_filters( 'nf_subs_csv_value_array', $value_array, $sub_ids );
[252] Fix | Delete
[253] Fix | Delete
$array = array( $label_array, $value_array );
[254] Fix | Delete
$today = date( $date_format, current_time( 'timestamp' ) );
[255] Fix | Delete
$filename = apply_filters( 'nf_subs_csv_filename', 'nf_subs_' . $today );
[256] Fix | Delete
$filename = $filename . ".csv";
[257] Fix | Delete
[258] Fix | Delete
if( $return ){
[259] Fix | Delete
return str_putcsv( $array,
[260] Fix | Delete
apply_filters( 'nf_sub_csv_delimiter', ',' ),
[261] Fix | Delete
apply_filters( 'nf_sub_csv_enclosure', '"' ),
[262] Fix | Delete
apply_filters( 'nf_sub_csv_terminator', "\n" )
[263] Fix | Delete
);
[264] Fix | Delete
}else{
[265] Fix | Delete
header( 'Content-type: application/csv');
[266] Fix | Delete
header( 'Content-Disposition: attachment; filename="'.$filename .'"' );
[267] Fix | Delete
header( 'Pragma: no-cache');
[268] Fix | Delete
header( 'Expires: 0' );
[269] Fix | Delete
echo apply_filters( 'nf_sub_csv_bom',"\xEF\xBB\xBF" ) ; // Byte Order Mark
[270] Fix | Delete
echo str_putcsv( $array,
[271] Fix | Delete
apply_filters( 'nf_sub_csv_delimiter', ',' ),
[272] Fix | Delete
apply_filters( 'nf_sub_csv_enclosure', '"' ),
[273] Fix | Delete
apply_filters( 'nf_sub_csv_terminator', "\n" )
[274] Fix | Delete
);
[275] Fix | Delete
[276] Fix | Delete
die();
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
}
[280] Fix | Delete
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function