Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Export CSV FIle Using Ajax
- */
- if( ! defined( 'ABSPATH' ) ) exit;
- if( ! class_exists('esb_cf7_custom_ajax') ) {
- class esb_cf7_custom_ajax {
- function esb_cf7_export_csv_form(){
- //For Export CSV File
- add_action( 'wp_ajax_esb_cf7_csv' , array( $this , 'fun_esb_cf7_csv' ) );
- add_action( 'wp_ajax_nopriv_esb_cf7_csv' , array( $this , 'fun_esb_cf7_csv' ) );
- //For Abandoned Entry
- add_action( 'wp_ajax_esb_cf7_abandoned_entry' , array( $this , 'fun_esb_cf7_abandoned_entry' ) );
- add_action( 'wp_ajax_nopriv_esb_cf7_abandoned_entry' , array( $this , 'fun_esb_cf7_abandoned_entry' ) );
- //For Add Note & delete Note
- add_action( 'wp_ajax_esb_cf7_add_delete_note' , array( $this , 'fun_esb_cf7_add_delete_note' ) );
- add_action( 'wp_ajax_nopriv_esb_cf7_add_delete_note' , array( $this , 'fun_esb_cf7_add_delete_note' ) );
- }
- /*
- * Ajax For Add Note $ delete Note
- */
- function fun_esb_cf7_add_delete_note(){
- if( !empty( $_POST ) ){
- $post_id = $_POST['post_id'];
- $post_note = $_POST['post_note'];
- $action = $_POST['do_action'];
- $key_id = $_POST['key_id'];
- }
- if( $action == "add-note" ){
- $current_time = current_time( 'mysql' );
- $current_user_id = get_current_user_id();
- $old_count = get_post_meta( $post_id , 'esb_cf7_note_count', true);
- $count = $old_count + 1;
- update_post_meta( $post_id , 'esb_cf7_note_count', $count );
- add_post_meta( $post_id , 'esb_cf7_note_'.$count , $post_note );
- add_post_meta( $post_id , 'esb_cf7_note_time_'.$count , $current_time );
- add_post_meta( $post_id , 'esb_cf7_note_user_'.$count , $current_user_id );
- }
- if( $action == "remove-note" ){
- delete_post_meta( $post_id, 'esb_cf7_note_'.$key_id );
- delete_post_meta( $post_id, 'esb_cf7_note_time_'.$key_id );
- delete_post_meta( $post_id, 'esb_cf7_note_user_'.$key_id );
- }
- ?>
- <div class="esb-note-display">
- <?php
- $count = get_post_meta($post_id,'esb_cf7_note_count',true);
- for($i = $count ; $i >= 1 ; $i-- ){
- $data_note = get_post_meta( $post_id , 'esb_cf7_note_'.$i , true );
- $data_time = get_post_meta( $post_id , 'esb_cf7_note_time_'.$i , true );
- $data_user = get_post_meta( $post_id , 'esb_cf7_note_user_'.$i , true );
- $user_name = get_userdata($data_user);
- $user_name = !empty( $user_name ) ? $user_name->data->display_name : $data_user;
- if( !empty( $data_note ) && !empty( $data_time ) && !empty( $data_user ) ){
- ?>
- <div class="esb-cf7-single-note">
- <p> added by
- <a href="<?php echo admin_url().'user-edit.php?user_id='.$data_user; ?>" target="_blank">
- <?php echo $user_name; ?>
- </a> on <?php echo $data_time; ?>
- |
- <a class="esb-cf7-remove-meta" data-id="<?php echo $i;?>">Delete</a>
- </p>
- <p> <?php echo $data_note; ?> </p>
- </div>
- <?php } } ?>
- </div>
- <?php
- exit();
- }
- /*
- * Ajax For Abandoned Entry
- */
- function fun_esb_cf7_abandoned_entry(){
- if( !empty( $_POST ) ){
- $title_field_name = $_POST['title_field_name'];
- $form_id = $_POST['form_id'];
- $form_data = json_decode( stripslashes( $_POST['form_value'] ) );
- }
- if( isset( $form_id ) && !empty( $form_id )){
- $prevent_duplicate_val = get_post_meta( $form_id , 'esb_form_prevente_duplicate' , true );
- }
- if( !empty( $prevent_duplicate_val ) ){
- if( $prevent_duplicate_val == 'yes' ){
- if( isset( $_SESSION['uniq_id'] ) ){
- $uniq_id = $_SESSION['uniq_id'];
- $query = new WP_Query(array(
- 'post_type' =>'esb_cf7_entries',
- 'posts_per_page'=> -1,
- 'meta_query' => array ( array (
- 'key' => 'entry_uniq_key',
- 'value' => $uniq_id,
- 'compare' => 'IN'
- ) )
- ));
- while($query->have_posts()){
- $query->the_post();
- $post_id = get_the_ID();
- }
- if( !empty( $post_id ) ){
- $entry_id = $post_id;
- }
- }
- else{
- $uniq_id = uniqid();
- $_SESSION['uniq_id'] = $uniq_id;
- $entry_id = wp_insert_post( array(
- 'post_type' => ESB_CF7_ENTRIES_POST_TYPE,
- 'post_status' => 'publish',
- ));
- update_post_meta( $entry_id , 'esb_cf7_note_count', 0);
- }
- }
- if( $prevent_duplicate_val == 'no' ){
- $entry_id = wp_insert_post( array(
- 'post_type' => ESB_CF7_ENTRIES_POST_TYPE,
- 'post_status' => 'publish',
- ));
- update_post_meta( $entry_id , 'esb_cf7_note_count', 0);
- }
- }
- $form_fix_key_tmp = array( '_wpcf7' , '_wpcf7_version' , '_wpcf7_locale' , '_wpcf7_unit_tag' , '_wpcf7_container_post' );
- $form_fix_key = apply_filters('esb_cf7_add_form_fix_key', $form_fix_key_tmp , $current_form_id );
- if( !empty( $form_data ) && !empty( $entry_id )){
- update_post_meta( $entry_id, 'entry_uniq_key' , $uniq_id );
- $all_key = array();
- foreach ( $form_data as $form_data_key => $form_data_value ) {
- if( $form_data_value->name == $title_field_name ){
- update_post_meta( $entry_id, 'esb_post_title', $form_data_value->value );
- wp_update_post( array(
- 'ID' => $entry_id,
- 'post_title' => $form_data_value->value
- ));
- }
- /*
- * check First 5 field of cf7 fix key
- */
- if( in_array( $form_data_value->name, $form_fix_key ) ){
- update_post_meta( $entry_id, 'esb_' . $form_data_value->name, $form_data_value->value );
- }else{
- if(in_array($form_data_value->name, $all_key) ){
- $old_data = get_post_meta( $entry_id , 'esb_form_field_' . $form_data_value->name , true);
- $new_data = $old_data .' , '.$form_data_value->value;
- update_post_meta( $entry_id, 'esb_form_field_' . $form_data_value->name, $new_data );
- }
- else{
- update_post_meta( $entry_id, 'esb_form_field_' . $form_data_value->name, $form_data_value->value );
- array_push($all_key, $form_data_value->name);
- }
- }
- }
- }
- $extra_meta = array(
- 'date_time' => current_time( 'd/m/Y h:i A' ),
- 'http_user_agent' => sanitize_text_field( $_SERVER['HTTP_USER_AGENT'] ),
- 'remote_addr' => sanitize_text_field( $_SERVER['REMOTE_ADDR'] ),
- 'http_referer' => sanitize_text_field( $_SERVER['HTTP_REFERER'] ),
- );
- if( !empty( $extra_meta ) && !empty( $entry_id )){
- foreach ( $extra_meta as $key => $value ){
- update_post_meta( $entry_id, 'esb_extra_' . $key, $value );
- }
- }
- update_post_meta( $entry_id, 'esb_form_status' , 'Abandoned' );
- update_post_meta( $entry_id, 'esb_abandoned_flag' , 'false' );
- exit();
- }
- /*
- * Ajax For Export CSV file
- */
- function fun_esb_cf7_csv(){
- $selected_form = $_POST['selected_val'];
- $esb_entries_arg = array(
- 'post_type' => ESB_CF7_ENTRIES_POST_TYPE,
- 'post_status' => 'publish',
- 'posts_per_page' => -1,
- );
- $esb_cf7_all_entries = get_posts( $esb_entries_arg );
- if ( $esb_cf7_all_entries ) {
- $file = fopen('php://output', 'w');
- foreach ($esb_cf7_all_entries as $form_key => $form_value) {
- $form_id = get_post_meta( $form_value->ID , 'esb__wpcf7' , true );
- if( $selected_form == $form_id ) {
- $all_key = array();
- $esb_form = esb_get_all_entries ( $form_value->ID );
- foreach ($esb_form as $esb_key => $esb_value) {
- array_push($all_key, $esb_key);
- }
- break;
- }
- }
- fputcsv($file, $all_key);
- foreach ($esb_cf7_all_entries as $form_key => $form_value) {
- $all_value = array();
- $form_id = get_post_meta( $form_value->ID , 'esb__wpcf7' , true );
- if( $selected_form == $form_id ) {
- $esb_form = esb_get_all_entries ( $form_value->ID );
- foreach ($esb_form as $esb_key => $esb_value) {
- array_push($all_value, $esb_value);
- }
- fputcsv($file, $all_value);
- }
- }
- }
- exit();
- }
- }//End Of Class
- }// class_exists check
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement