prefix.'uacf7_form';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id bigint(20) NOT NULL AUTO_INCREMENT,
form_id bigint(20) NOT NULL,
form_value longtext NOT NULL,
form_date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
/*
* Enqueue script Backend
*/
public function wp_enqueue_admin_script() {
wp_enqueue_style( 'database-admin-style', UACF7_ADDONS . '/database/assets/css/database-admin.css' );
wp_enqueue_script( 'database-admin', UACF7_ADDONS . '/database/assets/js/database-admin.js', array('jquery'), null, true );
wp_localize_script( 'database-admin', 'database_admin_url',
array(
'admin_url' => get_admin_url().'/admin.php',
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'plugin_dir_url' => plugin_dir_url( __FILE__ ),
)
);
}
/*
* Database menu
*/
public function uacf7_add_db_menu(){
add_submenu_page(
'wpcf7', //parent slug
__('Ultimate DB','ultimate-addons-cf7'), // page_title
__('Ultimate DB','ultimate-addons-cf7'), // menu_title
'manage_options', // capability
'ultimate-addons-db', // menu_slug
array( $this, 'uacf7_create_database_page' ) // function
);
}
/*
* Database Admin Page
*/
public function uacf7_create_database_page(){
$form_id = empty($_GET['form_id']) ? 0 : (int) $_GET['form_id'];
$csv = empty($_GET['csv']) ? 0 : $_GET['csv'];
$pdf = empty($_GET['pdf']) ? 0 : $_GET['pdf'];
$data_id = empty($_GET['data_id']) ? 0 : $_GET['data_id'];
if(!empty($form_id) && $pdf == true && !empty($data_id)){
return apply_filters( 'uacf7_get_generated_pdf', $form_id, $data_id); // export as pdf
}
if(!empty($form_id) && $csv == true ){
$this->uacf7_database_export_csv($form_id, $csv); // export as CSV
}
if( !empty($form_id)){
$uacf7_ListTable = new uacf7_form_List_Table();
$uacf7_ListTable->prepare_items();
?>
'wpcf7_contact_form',
'posts_per_page' => -1
));
?>
|
|
|
prefix.'uacf7_form';
$submission = WPCF7_Submission::get_instance();
$ContactForm = WPCF7_ContactForm::get_instance($form->id());
$tags = $ContactForm->scan_form_tags();
$skip_tag_insert = [];
foreach ($tags as $tag){
if( $tag->type == 'uacf7_step_start' || $tag->type == 'uacf7_step_end' || $tag->type == 'uarepeater' || $tag->type == 'conditional' ){
if($tag->name != ''){
$skip_tag_insert[] = $tag->name;
}
}
}
$contact_form_data = $submission->get_posted_data();
$files = $submission->uploaded_files();
$upload_dir = wp_upload_dir();
$dir = $upload_dir['basedir'];
$uploaded_files = [];
$time_now = time();
$data_file = [];
$uacf7_dirname = $upload_dir['basedir'].'/uacf7-uploads';
if ( ! file_exists( $uacf7_dirname ) ) {
wp_mkdir_p( $uacf7_dirname );
}
foreach ($_FILES as $file_key => $file) {
array_push($uploaded_files, $file_key);
}
foreach ($files as $file_key => $file) {
if(!empty($file)){
if(in_array($file_key, $uploaded_files)){
$file = is_array( $file ) ? reset( $file ) : $file;
$dir_link = '/uacf7-uploads/'.$time_now.'-'.$file_key.'-'.basename($file);
copy($file, $dir.$dir_link);
array_push($data_file, [$file_key=> $dir_link]);
}
}
}
foreach($contact_form_data as $key => $value){
if(in_array($key, $uploaded_files)){
if(empty($data_file)){$data_file = ''; }else{ $data_file = $data_file[0][$file_key] ; };
$contact_form_data[$key] = $data_file;
}
}
$data = [
'status' => 'unread',
];
$data = array_merge($data, $contact_form_data);
$insert_data = [];
foreach ($data as $key => $value){
if(!in_array($key, $skip_tag_insert)){
$insert_data[$key] = $value;
}
}
$insert_data = json_encode($insert_data) ;
$wpdb->insert($table_name, array(
'form_id' => $form->id(),
'form_value' => $insert_data,
'form_date' => current_time('Y-m-d H:i:s'),
));
$uacf7_db_insert_id = $wpdb->insert_id;
// print_r($uacf7_enable_track_order);
do_action( 'uacf7_checkout_order_traking', $uacf7_db_insert_id, $form->id());
}
/*
* Ultimate form save into the database
*/
public function uacf7_ajax_database_popup(){
global $wpdb;
$id = intval($_POST['id']); // data id
$upload_dir = wp_upload_dir();
$dir = $upload_dir['baseurl'];
$replace_dir = '/uacf7-uploads/';
$form_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM ".$wpdb->prefix."uacf7_form WHERE id = %d", $id));
$ContactForm = WPCF7_ContactForm::get_instance( $form_data->form_id );
$form_fields = $ContactForm->scan_form_tags();
$data = json_decode($form_data->form_value);
$fields = [];
foreach($form_fields as $field){
if($field['type'] != 'submit' && $field['type'] !='uacf7_step_start' && $field['type'] !='uacf7_step_end' && $field['type'] !='uarepeater' ){
$fields[$field['name']] = '';
}
}
foreach($data as $key => $value){
$fields[$key] = $value;
}
$html = '
'.get_the_title( $form_data->form_id ).'
'.$form_data->form_date.'
';
$html .= ' | Fields | Values |
';
foreach($fields as $key => $value){
if($key !='status'){
if(is_array($value)){
$value = implode(", ",$value);
}
if (strstr($value, $replace_dir)) {
$value = str_replace($replace_dir,"",$value);
$html .= ' | '.$key.' | '.$value.' |
';
}else{
$html .= ' | '.$key.' | '.$value.' |
';
}
}
}
$html .= '
';
// Update data as read
if($data->status == 'unread'){
$data->status = 'read';
$data = json_encode($data);
$table_name = $wpdb->prefix.'uacf7_form';
$data = array(
'form_value' => $data,
);
$where = array(
'id' => $id
);
$wpdb->update( $table_name, $data, $where );
}
echo $html; // return all data
wp_die();
}
/*
* Export CSV
*/
public function uacf7_database_export_csv(){
if( isset($_REQUEST['csv']) && ( $_REQUEST['csv'] == true && $_REQUEST['form_id'] !='' ) ) {
$today = date("Y-m-d");
global $wpdb;
$upload_dir = wp_upload_dir();
$dir = $upload_dir['baseurl'];
$replace_dir = '/uacf7-uploads/';
$form_id = intval($_REQUEST['form_id']);
$file_name = get_the_title( $_REQUEST['form_id']);
$file_name = str_replace(" ","-", $file_name);
$form_data = $wpdb->get_results($wpdb->prepare("SELECT * FROM ".$wpdb->prefix."uacf7_form WHERE form_id = %d ", $form_id ));
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Description: File Transfer");
header("Content-Encoding: UTF-8");
header("Content-Type: text/csv; charset=UTF-8");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename=".$file_name."-".$today.".csv");
header("Content-Transfer-Encoding: binary");
ob_start();
$list = [];
$count = 0;
foreach($form_data as $fkey => $fdata){
$ContactForm = WPCF7_ContactForm::get_instance( $fdata->form_id );
$form_fields = $ContactForm->scan_form_tags();
$fields = [];
$data = json_decode($fdata->form_value);
foreach($form_fields as $field){
if($field['type'] != 'submit' && $field['type'] !='uacf7_step_start' && $field['type'] !='uacf7_step_end' && $field['type'] !='uarepeater' ){
$fields[$field['name']] = '';
}
}
foreach($data as $key => $value){
$fields[$key] = $value;
}
$list_head = [];
$list_data = [];
foreach($fields as $key => $value){
if($fkey == 0){
$list_head[] = $key;
}
if(is_array($value)){
$value = implode(", ",$value);
}
if (strstr($value, $replace_dir)) {
$value = str_replace($replace_dir,"",$value);
$value = $dir.$replace_dir.$value;
}
$list_data[] = $value;
}
if($fkey == 0){
$list_head[] = 'Date';
$list[] = $list_head;
}
$list_data[] = $fdata->form_date;
$list[] = $list_data;
}
$fp = fopen('php://output', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
echo ob_get_clean();
fclose($fp);
die();
}
}
}
/*
* WP_List_Table Class Call
*/
if( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
/*
* extends uacf7_form_List_Table class will create the page to load the table
*/
class uacf7_form_List_Table extends WP_List_Table{
/**
* Prepare the items for the table to process
*
* @return $columns
*/
public function prepare_items()
{
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$data = $this->table_data();
$this->process_bulk_action();
$perPage = 10;
$currentPage = $this->get_pagenum();
$totalItems = count($data);
$this->set_pagination_args( array(
'total_items' => $totalItems,
'per_page' => $perPage
) );
$data = array_slice($data,(($currentPage-1)*$perPage),$perPage);
$this->_column_headers = array($columns, $hidden);
$this->items = $data;
}
/**
* Override the parent columns method. Defines the columns to use in your listing table
*
* @return Array
*/
public function get_columns()
{
$form_id = empty($_GET['form_id']) ? 0 : (int) $_GET['form_id'];
$ContactForm = WPCF7_ContactForm::get_instance( $form_id );
$form_fields = $ContactForm->scan_form_tags();
$columns = [];
$columns['cb'] = '';
if(count($form_fields) > 4){
$count = 4;
}else{
$count = count($form_fields);
}
for ($x = 0; $x < $count; $x++) {
if($form_fields[$x]['type'] != 'submit' && $form_fields[$x]['type'] !='uacf7_step_start' && $form_fields[$x]['type'] !='uacf7_step_end' && $form_fields[$x]['type'] !='uarepeater' && $form_fields[$x]['type'] !='conditional' ){
$columns[$form_fields[$x]['name']] = $form_fields[$x]['name'];
}
}
// Checked Star Review Status
if($this->uacf7_star_review_status($form_id) == true){ $columns['review_publish'] = 'Review Publish'; }
$columns['action'] = 'Action';
return $columns;
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns()
{
return array();
}
/**
* Get the table data
*
* @return Array
*/
private function table_data()
{
global $wpdb;
$form_id = empty($_GET['form_id']) ? 0 : (int) $_GET['form_id'];
$search = empty( $_REQUEST['s'] ) ? false : esc_sql( $_REQUEST['s'] );
$upload_dir = wp_upload_dir();
$dir = $upload_dir['baseurl'];
$replace_dir = '/uacf7-uploads/';
$data = [];
if(isset($search) && !empty($search)){
$form_data = $wpdb->get_results(
$wpdb->prepare("SELECT * FROM ".$wpdb->prefix."uacf7_form WHERE form_id = %d AND form_value LIKE '%$search%' ORDER BY id DESC", $form_id)
);
}else{
$form_data = $wpdb->get_results(
$wpdb->prepare("SELECT * FROM ".$wpdb->prefix."uacf7_form WHERE form_id = %d ORDER BY id DESC", $form_id)
);
}
foreach($form_data as $fdata){
$field_data = json_decode($fdata->form_value);
$repetar_value = '';
$repetar_key = '';
$enable_pdf = !empty(get_post_meta( $fdata->form_id, 'uacf7_enable_pdf_generator', true )) ? get_post_meta( $fdata->form_id, 'uacf7_enable_pdf_generator', true ) : '';
if($enable_pdf == 'on' && uacf7_checked( 'uacf7_enable_pdf_generator_field') != ''){ $pdf_btn = "id."' data-id='".$fdata->id."' data-value='".$fdata->form_value."' class='button-primary uacf7-db-pdf'> Export as PDF";}else{ $pdf_btn = '';}
$order_btn = isset($field_data->order_id) && $field_data->order_id != 0 ? "order_id . '&action=edit')."' class='button-primary uacf7-db-pdf'> View Order" : '';
foreach($field_data as $key => $value){
if(is_array($value)){
$value = implode(", ",$value);
}
if (strstr($value, $replace_dir)) {
$value = str_replace($replace_dir,"",$value);
$f_data[$key] = ''.$value.'';
}else{
$f_data[$key] =$value;
}
if (strpos($key, '__') !== false) {
$repetar_key = explode('__', $key);
$repetar_key = $repetar_key[0];
$f_data[$repetar_key] =$value;
}
}
$f_data['id'] = $fdata->id;
// Checked Star Review Status
if($this->uacf7_star_review_status($form_id) == true){
$checked = $fdata->is_review == 1 ? 'checked' : '';
$f_data['review_publish'] = '';
}
$f_data['action'] = "". $pdf_btn . $order_btn;
$data[] = $f_data;
}
return $data;
}
/**
* Define what data to show on each column of the table
*/
public function column_default( $item, $column_name ){
// echo "";
// print_r($item);
return $item[ $column_name ];
}
/**
* Single row add css class for unread data
*
*/
public function single_row( $item ) {
$cssClass = ($item['status'] == 'unread') ? 'unread' : 'read';
echo '';
$this->single_row_columns( $item );
echo '
';
}
/**
* Culumn checkbox for data filter
*
*/
public function column_cb($item){
return sprintf(
'',
$item['id']
);
}
/**
* Bulk action
*
*/
function get_bulk_actions() {
$actions = array(
'delete' => __( 'Delete' , 'visual-form-builder'),
);
return $actions;
}
protected function bulk_actions( $which = '' ) {
if ( is_null( $this->_actions ) ) {
$this->_actions = $this->get_bulk_actions();
/**
* Filters the items in the bulk actions menu of the list table.
*
* The dynamic portion of the hook name, `$this->screen->id`, refers
* to the ID of the current screen.
*
* @since 3.1.0
* @since 5.6.0 A bulk action can now contain an array of options in order to create an optgroup.
*
* @param array $actions An array of the available bulk actions.
*/
$this->_actions = apply_filters( "bulk_actions-{$this->screen->id}", $this->_actions ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
$two = '';
} else {
$two = '2';
}
if ( empty( $this->_actions ) ) {
return;
}
echo '';
echo '\n";
submit_button( __( 'Apply' ), 'action', '', false, array( 'id' => "doaction $two" ) );
echo " Export CSV";
echo "\n";
}
/**
* Bulk action Filter
*
*/
function process_bulk_action() {
global $wpdb;
if ( 'delete' === $this->current_action() ) {
$ids = isset( $_POST['uacf7_db_id'] ) ? $_POST['uacf7_db_id'] : array();
foreach ( $ids as $id ) {
$id = absint( $id );
$wpdb->query( $wpdb->prepare("DELETE FROM ".$wpdb->prefix."uacf7_form WHERE id = %d", $id) );
}
}
}
// Checked Star Review Status Function
public function uacf7_star_review_status($id){
if(class_exists('UACF7_STAR_RATING_PRO') && class_exists('UACF7_STAR_RATING')){
return apply_filters( 'uacf7_star_review_status', false, $id); // checked star review status
}else{
return false;
}
}
}
new UACF7_DATABASE();