This commit is contained in:
2026-10-06 18:51:23 -06:00
parent 8424dacd03
commit e2c26b7f21
4223 changed files with 737986 additions and 53 deletions
@@ -0,0 +1,64 @@
<?php
if (!defined( 'ABSPATH')) exit;
add_action('admin_menu', 'register_addon_submenu', 99);
function register_addon_submenu(){
add_submenu_page(
'cfdb7-list.php',
__( 'Extensions', 'contact-form-cfdb7' ),
'<span style="color:#f18500">' . esc_html__( 'Addons', 'contact-form-cfdb7' ) . '</span>',
'manage_options', 'cfdb7-extensions',
'cfdb7_extensions',
);
}
/**
* Extensions page
*/
function cfdb7_extensions(){
?>
<div class="wrap">
<h2><?php _e( 'Extensions for CFDB7', 'contact-form-cfdb7' ); ?>
<span>
<a class="button-primary" href="https://ciphercoin.com/contact-form-7-database-cfdb7-add-ons/"><?php _e( 'Browse All Extensions', 'contact-form-cfdb7' ); ?></a>
</span>
</h2>
<p><?php _e( 'Add extra features to CFDB7 with these extensions.', 'contact-form-cfdb7' ); ?></p>
<?php echo cfdb7_add_ons_get_feed(); ?>
</div>
<?php
}
/**
* Add-ons Get Feed
*
* Gets the add-ons page feed.
*
* @since 1.0
* @return void
*/
function cfdb7_add_ons_get_feed(){
$cache = get_transient( 'cfdb7_add_ons_feed' );
if ( false === $cache ) {
$url = 'https://ciphercoin.com/cfdb7/?feed=true';
$feed = wp_remote_get( esc_url_raw( $url ), array(
'sslverify' => false,
'timeout' => 30,
) );
if ( ! is_wp_error( $feed ) ) {
if ( isset( $feed['body'] ) && strlen( $feed['body'] ) > 0 ) {
$cache = wp_remote_retrieve_body( $feed );
set_transient( 'cfdb7_add_ons_feed', $cache, 3600 );
}
} else {
$cache = '<div class="error"><p>' . __( 'There was an error retrieving the extensions list from the server. Please try again later.', 'contact-form-cfdb7' ) . '</div>';
}
}
return $cache;
}
// delete_transient('cfdb7_add_ons_feed');
@@ -0,0 +1,113 @@
<?php
if (!defined( 'ABSPATH')) exit;
/**
* Details Page
*/
class CFDB7_Form_Details
{
private $form_id;
private $form_post_id;
public function __construct()
{
$this->form_post_id = isset( $_GET['fid'] ) ? (int) $_GET['fid'] : 0;
$this->form_id = isset( $_GET['ufid'] ) ? (int) $_GET['ufid'] : 0;
$this->form_details_page();
}
public function form_details_page(){
global $wpdb;
$cfdb = apply_filters( 'cfdb7_database', $wpdb );
$table_name = $cfdb->prefix.'db7_forms';
$upload_dir = wp_upload_dir();
$cfdb7_dir_url = $upload_dir['baseurl'].'/cfdb7_uploads';
$rm_underscore = apply_filters('cfdb7_remove_underscore_data', true);
$result = $cfdb->get_row( "SELECT * FROM $table_name WHERE form_post_id = $this->form_post_id AND form_id = $this->form_id LIMIT 1", OBJECT );
if ( empty($result) ) {
wp_die( 'Not valid contact form' );
}
?>
<div class="wrap">
<div id="welcome-panel" class="cfdb7-panel">
<div class="cfdb7-panel-content">
<div class="welcome-panel-column-container">
<?php do_action('cfdb7_before_formdetails_title',$this->form_post_id ); ?>
<h3><?php esc_html_e( get_the_title( $this->form_post_id ), 'contact-form-cfdb7' )?></h3>
<?php do_action('cfdb7_after_formdetails_title', $this->form_post_id ); ?>
<p></span><?php esc_html_e( $result->form_date, 'contact-form-cfdb7' ) ?></p>
<?php $form_data = unserialize( $result->form_value, ['allowed_classes' => false] );
foreach ($form_data as $key => $data):
$matches = array();
$key = esc_html( $key );
if ( $key == 'cfdb7_status' ) continue;
if( $rm_underscore ) preg_match('/^_.*$/m', $key, $matches);
if( ! empty($matches[0]) ) continue;
if ( strpos($key, 'cfdb7_file') !== false ){
$key_val = str_replace('cfdb7_file', '', $key);
$key_val = str_replace('your-', '', $key_val);
$key_val = str_replace( array('-','_'), ' ', $key_val);
$key_val = ucwords( $key_val );
echo '<p><b>'.esc_html($key_val).'</b>: <a href="'.esc_url($cfdb7_dir_url.'/'.$data).'">'
.esc_html($data).'</a></p>';
}else{
if ( is_array($data) ) {
$key_val = str_replace('your-', '', $key);
$key_val = str_replace( array('-','_'), ' ', $key_val);
$key_val = ucwords( $key_val );
$arr_str_data = implode(', ',$data);
$arr_str_data = esc_html( $arr_str_data );
echo '<p><b>'.esc_html($key_val).'</b>: '. nl2br($arr_str_data) .'</p>';
}else{
$key_val = str_replace('your-', '', $key);
$key_val = str_replace( array('-','_'), ' ', $key_val);
$key_val = ucwords( $key_val );
$data = esc_html( $data );
echo '<p><b>'.esc_html($key_val).'</b>: '.nl2br($data).'</p>';
}
}
endforeach;
$form_data['cfdb7_status'] = 'read';
$form_data = serialize( $form_data );
$form_id = $result->form_id;
$sql = $cfdb->prepare(
"UPDATE {$table_name} SET form_value = %s WHERE form_id = %d",
$form_data,
$form_id
);
$cfdb->query( $sql );
?>
</div>
</div>
</div>
</div>
<?php
do_action('cfdb7_after_formdetails', $this->form_post_id );
}
}
@@ -0,0 +1,202 @@
<?php
/**
* CFDB7 Admin section
*/
if (!defined( 'ABSPATH')) exit;
/**
* Cfdb7_Wp_List_Table class will create the page to load the table
*/
class CFDB7_Wp_Main_Page
{
private static $instance = null;
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor will create the menu item
*/
private function __construct()
{
add_action( 'admin_menu', array($this, 'admin_list_table_page' ) );
}
/**
* Menu item will allow us to load the page to display the table
*/
public function admin_list_table_page()
{
wp_enqueue_style( 'cfdb7-admin-style', plugin_dir_url(dirname(__FILE__)).'css/admin-style.css' );
// Fallback: Make sure admin always has access
$cfdb7_cap = ( current_user_can( 'cfdb7_access') ) ? 'cfdb7_access' : 'manage_options';
add_menu_page( __( 'Contact Forms', 'contact-form-cfdb7' ), __( 'Contact Forms', 'contact-form-cfdb7' ), $cfdb7_cap, 'cfdb7-list.php', array($this, 'list_table_page'), 'dashicons-list-view' );
require_once 'add-ons.php';
}
/**
* Display the list table page
*
* @return Void
*/
public function list_table_page()
{
if ( ! class_exists('WPCF7_ContactForm') ) {
wp_die( 'Please activate <a href="https://wordpress.org/plugins/contact-form-7/" target="_blank">contact form 7</a> plugin.' );
}
$fid = empty($_GET['fid']) ? 0 : (int) $_GET['fid'];
$ufid = empty($_GET['ufid']) ? 0 : (int) $_GET['ufid'];
if ( !empty($fid) && empty($_GET['ufid']) ) {
new CFDB7_Wp_Sub_Page();
return;
}
if( !empty($ufid) && !empty($fid) ){
new CFDB7_Form_Details();
return;
}
$ListTable = new CFDB7_Main_List_Table();
$ListTable->prepare_items();
?>
<div class="wrap">
<div id="icon-users" class="icon32"></div>
<h2><?php _e( 'Contact Forms', 'contact-form-cfdb7' ); ?></h2>
<?php $ListTable->display(); ?>
</div>
<?php
}
}
// WP_List_Table is not loaded automatically so we need to load it in our application
if( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
/**
* Create a new table class that will extend the WP_List_Table
*/
class CFDB7_Main_List_Table extends WP_List_Table
{
/**
* Prepare the items for the table to process
*
* @return Void
*/
public function prepare_items()
{
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$data = $this->table_data();
$perPage = 10;
$currentPage = $this->get_pagenum();
$count_forms = wp_count_posts('wpcf7_contact_form');
$totalItems = $count_forms->publish;
$this->set_pagination_args( array(
'total_items' => $totalItems,
'per_page' => $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()
{
$columns = array(
'name' => __( 'Name', 'contact-form-cfdb7' ),
'count'=> __( 'Count', 'contact-form-cfdb7' )
);
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;
$cfdb = apply_filters( 'cfdb7_database', $wpdb );
$data = array();
$table_name = $cfdb->prefix.'db7_forms';
$page = $this->get_pagenum();
$page = $page - 1;
$start = $page * 10;
$args = array(
'post_type'=> 'wpcf7_contact_form',
'order' => 'ASC',
'posts_per_page' => 10,
'offset' => $start
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
$form_post_id = get_the_id();
$totalItems = $cfdb->get_var("SELECT COUNT(*) FROM $table_name WHERE form_post_id = $form_post_id");
$title = get_the_title();
$link = "<a class='row-title' href=admin.php?page=cfdb7-list.php&fid=$form_post_id>%s</a>";
$data_value['name'] = sprintf( $link, $title );
$data_value['count'] = sprintf( $link, $totalItems );
$data[] = $data_value;
endwhile;
return $data;
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data
* @param String $column_name - Current column name
*
* @return Mixed
*/
public function column_default( $item, $column_name )
{
return $item[ $column_name ];
}
}
@@ -0,0 +1,477 @@
<?php
/**
* CFDB7 Admin subpage
*/
if (!defined( 'ABSPATH')) exit;
/**
* Cfdb7_Wp_List_Table class will create the page to load the table
*/
class CFDB7_Wp_Sub_Page
{
private $form_post_id;
/**
* Constructor start subpage
*/
public function __construct()
{
$this->form_post_id = (int) $_GET['fid'];
$this->list_table_page();
}
/**
* Display the list table page
*
* @return Void
*/
public function list_table_page()
{
$ListTable = new CFDB7_List_Table();
$ListTable->prepare_items();
?>
<div class="wrap">
<div id="icon-users" class="icon32"></div>
<h2><?php echo get_the_title( $this->form_post_id ); ?></h2>
<form method="post" action="">
<?php $ListTable->search_box(__( 'Search', 'contact-form-cfdb7' ), 'search'); ?>
<?php $ListTable->display(); ?>
</form>
</div>
<?php
}
}
// WP_List_Table is not loaded automatically so we need to load it in our application
if( ! class_exists( 'WP_List_Table' ) ) {
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' );
}
/**
* Create a new table class that will extend the WP_List_Table
*/
class CFDB7_List_Table extends WP_List_Table
{
private $form_post_id;
private $column_titles = array();
public function __construct() {
parent::__construct(
array(
'singular' => 'contact_form',
'plural' => 'contact_forms',
'ajax' => false
)
);
}
/**
* Prepare the items for the table to process
*
* @return Void
*/
public function prepare_items()
{
$this->form_post_id = (int) $_GET['fid'];
$search = empty( $_REQUEST['s'] ) ? false : esc_sql( $_REQUEST['s'] );
$form_post_id = $this->form_post_id;
global $wpdb;
$this->process_bulk_action();
$cfdb = apply_filters( 'cfdb7_database', $wpdb );
$table_name = $cfdb->prefix.'db7_forms';
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$data = $this->table_data();
//usort( $data, array( &$this, 'sort_data' ) );
$perPage = 100;
$currentPage = $this->get_pagenum();
if ( ! empty($search) ) {
$totalItems = $cfdb->get_var("SELECT COUNT(*) FROM $table_name WHERE form_value LIKE '%$search%' AND form_post_id = '$form_post_id' ");
}else{
$totalItems = $cfdb->get_var("SELECT COUNT(*) FROM $table_name WHERE form_post_id = '$form_post_id'");
}
$this->set_pagination_args( array(
'total_items' => $totalItems,
'per_page' => $perPage
) );
$this->_column_headers = array($columns, $hidden ,$sortable);
$this->items = $data;
}
/**
* Override the parent columns method. Defines the columns to use in your listing table
*
* @return Array
*/
public function get_columns()
{
$form_post_id = $this->form_post_id;
global $wpdb;
$cfdb = apply_filters( 'cfdb7_database', $wpdb );
$table_name = $cfdb->prefix.'db7_forms';
$results = $cfdb->get_results( "
SELECT * FROM $table_name
WHERE form_post_id = $form_post_id ORDER BY form_id DESC LIMIT 1", OBJECT
);
$first_row = isset($results[0]) ? unserialize( $results[0]->form_value, ['allowed_classes' => false] ): 0 ;
$columns = array();
$rm_underscore = apply_filters('cfdb7_remove_underscore_data', true);
if( !empty($first_row) ){
//$columns['form_id'] = $results[0]->form_id;
$columns['cb'] = '<input type="checkbox" />';
foreach ($first_row as $key => $value) {
$matches = array();
$key = esc_html( $key );
if ( $key == 'cfdb7_status' ) continue;
if( $rm_underscore ) preg_match('/^_.*$/m', $key, $matches);
if( ! empty($matches[0]) ) continue;
$key_val = str_replace( array('your-', 'cfdb7_file'), '', $key);
$key_val = str_replace( array('_', '-'), ' ', $key_val);
$columns[$key] = ucwords( $key_val );
$this->column_titles[] = $key_val;
if ( sizeof($columns) > 4) break;
}
$columns['form-date'] = 'Date';
}
return apply_filters('cfdb7_admin_subpage_columns', $columns, $form_post_id);
}
/**
* Define check box for bulk action (each row)
* @param $item
* @return checkbox
*/
public function column_cb($item){
return sprintf(
'<input type="checkbox" name="%1$s[]" value="%2$s" />',
$this->_args['singular'],
$item['form_id']
);
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns()
{
return array('form_id');
}
/**
* Define the sortable columns
*
* @return Array
*/
public function get_sortable_columns()
{
return array('form-date' => array('form-date', true));
}
/**
* Define bulk action
* @return Array
*/
public function get_bulk_actions() {
return array(
'read' => __( 'Read', 'contact-form-cfdb7' ),
'unread' => __( 'Unread', 'contact-form-cfdb7' ),
'delete' => __( 'Delete', 'contact-form-cfdb7' )
);
}
/**
* Get the table data
*
* @return Array
*/
private function table_data()
{
$data = array();
global $wpdb;
$cfdb = apply_filters( 'cfdb7_database', $wpdb );
$search = empty( $_REQUEST['s'] ) ? false : esc_sql( $_REQUEST['s'] );
$table_name = $cfdb->prefix.'db7_forms';
$page = $this->get_pagenum();
$page = $page - 1;
$start = $page * 100;
$form_post_id = $this->form_post_id;
$orderby = isset($_GET['orderby']) ? 'form_date' : 'form_id';
$order = isset($_GET['order']) && $_GET['order'] == 'asc' ? 'ASC' : 'DESC';
if ( ! empty($search) ) {
$results = $cfdb->get_results( "SELECT * FROM $table_name
WHERE form_value LIKE '%$search%'
AND form_post_id = '$form_post_id'
ORDER BY $orderby $order
LIMIT $start,100", OBJECT
);
}else{
$results = $cfdb->get_results( "SELECT * FROM $table_name
WHERE form_post_id = $form_post_id
ORDER BY $orderby $order
LIMIT $start,100", OBJECT
);
}
foreach ( $results as $result ) {
$form_value = unserialize( $result->form_value, ['allowed_classes' => false] );
$link = "<b><a href=admin.php?page=cfdb7-list.php&fid=%s&ufid=%s>%s</a></b>";
if(isset($form_value['cfdb7_status']) && ( $form_value['cfdb7_status'] === 'read' ) )
$link = "<a href=admin.php?page=cfdb7-list.php&fid=%s&ufid=%s>%s</a>";
$fid = $result->form_post_id;
$form_values['form_id'] = $result->form_id;
foreach ( $this->column_titles as $col_title) {
$form_value[ $col_title ] = isset( $form_value[ $col_title ] ) ?
$form_value[ $col_title ] : '';
}
foreach ($form_value as $k => $value) {
$ktmp = $k;
$can_foreach = is_array($value) || is_object($value);
if ( $can_foreach ) {
foreach ($value as $k_val => $val):
$val = esc_html( $val );
$form_values[$ktmp] = ( strlen($val) > 150 ) ? substr($val, 0, 150).'...': $val;
$form_values[$ktmp] = sprintf($link, $fid, $result->form_id, $form_values[$ktmp]);
endforeach;
}else{
$value = esc_html( $value );
$form_values[$ktmp] = ( strlen($value) > 150 ) ? substr($value, 0, 150).'...': $value;
$form_values[$ktmp] = sprintf($link, $fid, $result->form_id, $form_values[$ktmp]);
}
}
$form_values['form-date'] = sprintf($link, $fid, $result->form_id, $result->form_date );
$data[] = $form_values;
}
return $data;
}
/**
* Define bulk action
*
*/
public function process_bulk_action(){
global $wpdb;
$cfdb = apply_filters( 'cfdb7_database', $wpdb );
$table_name = $cfdb->prefix.'db7_forms';
$action = $this->current_action();
if ( !empty( $action ) ) {
$nonce = isset( $_REQUEST['_wpnonce'] ) ? $_REQUEST['_wpnonce'] : '';
$nonce_action = 'bulk-' . $this->_args['plural'];
if ( !wp_verify_nonce( $nonce, $nonce_action ) ){
wp_die( 'Not valid..!!' );
}
}
$form_ids = isset( $_POST['contact_form'] ) ? $_POST['contact_form'] : array();
if( 'delete' === $action ) {
foreach ($form_ids as $form_id):
$form_id = (int) $form_id;
$results = $cfdb->get_results( "SELECT * FROM $table_name WHERE form_id = '$form_id' LIMIT 1", OBJECT );
$result_value = $results[0]->form_value;
$result_values = unserialize($result_value, ['allowed_classes' => false]);
$upload_dir = wp_upload_dir();
$cfdb7_dirname = $upload_dir['basedir'].'/cfdb7_uploads';
foreach ($result_values as $key => $result) {
if ( ( strpos($key, 'cfdb7_file') !== false ) &&
! empty( $result ) &&
file_exists($cfdb7_dirname.'/'.$result) ) {
unlink($cfdb7_dirname.'/'.$result);
}
}
$cfdb->delete(
$table_name ,
array( 'form_id' => $form_id ),
array( '%d' )
);
endforeach;
}else if( 'read' === $action ){
foreach ($form_ids as $form_id):
$form_id = (int) $form_id;
$results = $cfdb->get_results( "SELECT * FROM $table_name WHERE form_id = '$form_id' LIMIT 1", OBJECT );
$result_value = $results[0]->form_value;
$result_values = unserialize( $result_value, ['allowed_classes' => false] );
$result_values['cfdb7_status'] = 'read';
$form_data = serialize( $result_values );
$sql = $cfdb->prepare(
"UPDATE {$table_name} SET form_value = %s WHERE form_id = %d",
$form_data,
$form_id
);
$cfdb->query( $sql );
endforeach;
}else if( 'unread' === $action ){
foreach ($form_ids as $form_id):
$form_id = (int) $form_id;
$results = $cfdb->get_results( "SELECT * FROM $table_name WHERE form_id = '$form_id' LIMIT 1", OBJECT );
$result_value = $results[0]->form_value;
$result_values = unserialize( $result_value, ['allowed_classes' => false] );
$result_values['cfdb7_status'] = 'unread';
$form_data = serialize( $result_values );
$sql = $cfdb->prepare(
"UPDATE {$table_name} SET form_value = %s WHERE form_id = %d",
$form_data,
$form_id
);
$cfdb->query( $sql );
endforeach;
}
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data
* @param String $column_name - Current column name
*
* @return Mixed
*/
public function column_default( $item, $column_name )
{
return isset( $item[ $column_name ] ) ? $item[ $column_name ]: '';
}
/**
* Allows you to sort the data by the variables set in the $_GET
*
* @return Mixed
*/
private function sort_data( $a, $b )
{
// Set defaults
$orderby = 'form_date';
$order = 'ASC';
// If orderby is set, use this as the sort column
if(!empty($_GET['orderby']))
{
$orderby = $_GET['orderby'] === 'form_id' ? 'form_id' : 'form_date';
}
// If order is set use this as the order
if(!empty($_GET['order']))
{
$order = $_GET['order'] === 'asc' ? 'ASC' : 'DESC';
}
$result = strcmp( $a[$orderby], $b[$orderby] );
if($order === 'asc')
{
return $result;
}
return -$result;
}
/**
* Display the bulk actions dropdown.
*
* @since 3.1.0
* @access protected
*
* @param string $which The location of the bulk actions: 'top' or 'bottom'.
* This is designated as optional for backward compatibility.
*/
protected function bulk_actions( $which = '' ) {
if ( is_null( $this->_actions ) ) {
$this->_actions = $this->get_bulk_actions();
/**
* Filters the list table Bulk Actions drop-down.
*
* The dynamic portion of the hook name, `$this->screen->id`, refers
* to the ID of the current screen, usually a string.
*
* This filter can currently only be used to remove bulk actions.
*
* @since 3.5.0
*
* @param array $actions An array of the available bulk actions.
*/
$this->_actions = apply_filters( "cfdb7_bulk_actions-{$this->screen->id}", $this->_actions );
$two = '';
} else {
$two = '2';
}
if ( empty( $this->_actions ) )
return;
echo '<label for="bulk-action-selector-' . esc_attr( $which ) . '" class="screen-reader-text">' . __( 'Select bulk action', 'contact-form-cfdb7' ) . '</label>';
echo '<select name="action' . $two . '" id="bulk-action-selector-' . esc_attr( $which ) . "\">\n";
echo '<option value="-1">' . __( 'Bulk Actions', 'contact-form-cfdb7' ) . "</option>\n";
foreach ( $this->_actions as $name => $title ) {
$class = 'edit' === $name ? ' class="hide-if-no-js"' : '';
echo "\t" . '<option value="' . $name . '"' . $class . '>' . $title . "</option>\n";
}
echo "</select>\n";
submit_button( __( 'Apply', 'contact-form-cfdb7' ), 'action', '', false, array( 'id' => "doaction$two" ) );
echo "\n";
$nonce = wp_create_nonce( 'dnonce' );
echo "<a href='".esc_html($_SERVER['REQUEST_URI'])."&csv=true&nonce=".$nonce."' style='float:right; margin:0;' class='button'>";
_e( 'Export CSV', 'contact-form-cfdb7' );
echo '</a>';
do_action('cfdb7_after_export_button');
}
}
@@ -0,0 +1,179 @@
<?php
/**
* CFDB7 csv
*/
if (!defined( 'ABSPATH')) exit;
class CFDB7_Export_CSV{
public function __construct(){
add_filter('cfdb7_csv_delimiter', array($this, 'set_delimiter'));
}
public function set_delimiter( $delimiter ){
$delimiter = get_option('cfdb7_csv_delimiter', ',');
return $delimiter;
}
/**
* Download csv file
* @param String $filename
* @return file
*/
public function download_send_headers( $filename ) {
// disable caching
$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");
header("Content-Type: text/csv; charset=UTF-8");
header("Content-Disposition: attachment;filename={$filename}");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
/**
* Convert array to csv format
* @param array &$array
*/
public function array2csv(array &$array, $df){
if (count($array) == 0) {
return null;
}
$array_keys = array_keys($array);
$heading = array();
$unwanted = array('cfdb7_file', 'cfdb7_', 'your-');
$delimiter = apply_filters('cfdb7_csv_delimiter', ',');
foreach ( $array_keys as $aKeys ) {
if( $aKeys == 'form_date' ) $aKeys = 'Date';
if( $aKeys == 'form_id' ) $aKeys = 'Id';
$tmp = str_replace( $unwanted, '', $aKeys );
$tmp = str_replace( array('-','_'), ' ', $tmp );
$heading[] = ucwords( $tmp );
}
fputs( $df, ( chr(0xEF) . chr(0xBB) . chr(0xBF) ) );
fputcsv( $df, $heading, $delimiter);
foreach ( $array['form_id'] as $line => $form_id ) {
$line_values = array();
foreach($array_keys as $array_key ) {
$val = isset( $array[ $array_key ][ $line ] ) ? $array[ $array_key ][ $line ] : '';
$line_values[ $array_key ] = $val;
}
fputcsv($df, $line_values, $delimiter);
}
}
/**
* Download file
* @return csv file
*/
public function download_csv_file(){
global $wpdb;
$cfdb = apply_filters( 'cfdb7_database', $wpdb );
$table_name = $cfdb->prefix.'db7_forms';
if( isset($_REQUEST['csv']) && isset( $_REQUEST['nonce'] ) ){
$nonce = $_REQUEST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'dnonce')) {
wp_die( 'Not Valid.. Download nonce..!! ' );
}
$fid = (int)$_REQUEST['fid'];
$heading_row = $cfdb->get_results("SELECT form_id, form_value, form_date FROM $table_name
WHERE form_post_id = '$fid' ORDER BY form_id DESC LIMIT 1",OBJECT);
$heading_row = reset( $heading_row );
$heading_row = unserialize( $heading_row->form_value, ['allowed_classes' => false] );
$heading_key = array_keys( $heading_row );
$rm_underscore = apply_filters('cfdb7_remove_underscore_data', true);
$total_rows = $cfdb->get_var("SELECT COUNT(*) FROM $table_name WHERE form_post_id = '$fid' ");
$per_query = 1000;
$total_query = ( $total_rows / $per_query );
$this->download_send_headers( "cfdb7-" . date("Y-m-d") . ".csv" );
$df = fopen("php://output", 'w');
ob_start();
for( $p = 0; $p <= $total_query; $p++ ){
$offset = $p * $per_query;
$results = $cfdb->get_results("SELECT form_id, form_value, form_date FROM $table_name
WHERE form_post_id = '$fid' ORDER BY form_id DESC LIMIT $offset, $per_query",OBJECT);
$data = array();
$i = 0;
foreach ($results as $result) :
$i++;
$data['form_id'][$i] = $result->form_id;
$data['form_date'][$i] = $result->form_date;
$resultTmp = unserialize( $result->form_value, ['allowed_classes' => false] );
$upload_dir = wp_upload_dir();
$cfdb7_dir_url = $upload_dir['baseurl'].'/cfdb7_uploads';
foreach ($resultTmp as $key => $value):
$matches = array();
if ( ! in_array( $key, $heading_key ) ) continue;
if( $rm_underscore ) preg_match('/^_.*$/m', $key, $matches);
if( ! empty($matches[0]) ) continue;
$value = str_replace(
array('&quot;','&#039;','&#047;','&#092;'),
array('"',"'",'/','\\'), $value
);
if (strpos($key, 'cfdb7_file') !== false ){
$data[$key][$i] = empty( $value ) ? '' : $cfdb7_dir_url.'/'.$value;
continue;
}
if ( is_array($value) ){
$data[$key][$i] = implode(', ', $value);
continue;
}
$data[$key][$i] = $value;
$data[$key][$i] = $this->escape_data( $data[$key][$i] );
endforeach;
endforeach;
echo $this->array2csv( $data, $df );
}
echo ob_get_clean();
fclose( $df );
die();
}
}
/**
* Escape a string to be used in a CSV context
* @param string $data CSV field to escape.
* @return string
*/
public function escape_data( $data ) {
$active_content_triggers = array( '=', '+', '-', '@', ';' );
if ( in_array( mb_substr( $data, 0, 1 ), $active_content_triggers, true ) ) {
$data = '"'. $data.'"';
}
return $data;
}
}
@@ -0,0 +1,230 @@
<?php
if (!defined('ABSPATH')) {
exit;
}
class CFDB7_Settings
{
private static $instance = null;
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct()
{
add_action('admin_menu', array($this, 'register_menu'));
add_action('admin_init', array($this, 'register_settings'));
add_action('admin_enqueue_scripts', array($this, 'admin_assets'));
}
public function admin_assets($hook)
{
if ($hook !== 'contact-forms_page_cfdb7-settings') {
return;
}
wp_enqueue_style(
'cfdb7-settings',
plugins_url('css/settings.css', CFDB7_PLUGIN_FILE),
array(),
'1.4.0'
);
}
public function register_menu()
{
add_submenu_page(
'cfdb7-list.php',
__('CFDB7 Settings', 'contact-form-cfdb7'),
__('Settings', 'contact-form-cfdb7'),
'manage_options',
'cfdb7-settings',
array($this, 'settings_page')
);
}
public function register_settings()
{
register_setting(
'cfdb7_settings',
'cfdb7_csv_delimiter',
array(
'type' => 'string',
'sanitize_callback' => array($this, 'sanitize_delimiter'),
'default' => ',',
)
);
add_settings_section(
'cfdb7_export_section',
__('CSV Export', 'contact-form-cfdb7'),
'__return_false',
'cfdb7_settings'
);
add_settings_field(
'cfdb7_csv_delimiter',
__('Delimiter', 'contact-form-cfdb7'),
array($this, 'delimiter_field'),
'cfdb7_settings',
'cfdb7_export_section'
);
/* Premium Features */
add_settings_section(
'cfdb7_premium_section',
__('Premium Features', 'contact-form-cfdb7'),
'__return_false',
'cfdb7_settings'
);
add_settings_field(
'cfdb7_entry_automation',
__('Entry Automation', 'contact-form-cfdb7'),
array($this, 'entry_automation_field'),
'cfdb7_settings',
'cfdb7_premium_section'
);
add_settings_field(
'cfdb7_pdf_export',
__('PDF Export', 'contact-form-cfdb7'),
array($this, 'pdf_export_field'),
'cfdb7_settings',
'cfdb7_premium_section'
);
add_settings_field(
'cfdb7_csv_import',
__('CSV Import', 'contact-form-cfdb7'),
array($this, 'csv_import_field'),
'cfdb7_settings',
'cfdb7_premium_section'
);
}
public function sanitize_delimiter($value)
{
$allowed = array(',', ';', '|', "\t");
return in_array($value, $allowed, true) ? $value : ',';
}
public function delimiter_field()
{
$value = get_option('cfdb7_csv_delimiter', ',');
$options = array(
',' => __('Comma (,)', 'contact-form-cfdb7'),
';' => __('Semicolon (;)', 'contact-form-cfdb7'),
'|' => __('Pipe (|)', 'contact-form-cfdb7'),
);
?>
<select name="cfdb7_csv_delimiter">
<?php foreach ($options as $delimiter => $label) : ?>
<option value="<?php echo esc_attr($delimiter); ?>" <?php selected($value, $delimiter); ?>>
<?php echo esc_html($label); ?>
</option>
<?php endforeach; ?>
</select>
<?php
}
public function settings_page()
{
?>
<div class="wrap">
<h1><?php esc_html_e('CFDB7 Settings', 'contact-form-cfdb7'); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields('cfdb7_settings');
do_settings_sections('cfdb7_settings');
submit_button();
?>
</form>
</div>
<?php
}
private function premium_feature($title, $description, $url, $installed = false){
?>
<fieldset class="cfdb7-premium-setting">
<label>
<input type="checkbox" disabled <?php checked($installed); ?> />
<strong><?php echo esc_html($title); ?></strong>
</label>
<p class="description">
<?php echo esc_html($description); ?>
</p>
<?php if (!$installed) : ?>
<p style="margin:8px 0;color:#d63638;">
🔒 <?php esc_html_e('Requires the premium add-on.', 'contact-form-cfdb7'); ?>
</p>
<a class="button button-secondary"
target="_blank"
href="<?php echo esc_url($url); ?>">
<?php esc_html_e('Install Add-on', 'contact-form-cfdb7'); ?>
</a>
<?php else : ?>
<span style="color:#00a32a;">
✓ <?php esc_html_e('Installed', 'contact-form-cfdb7'); ?>
</span>
<?php endif; ?>
</fieldset>
<?php
}
public function entry_automation_field(){
$installed = class_exists('CFDB7_Public_Export_CSV');
$this->premium_feature(
__('Automatically export entries', 'contact-form-cfdb7'),
__('Automatically export Contact Form 7 submissions to CSV on schedule or when new entries arrive.', 'contact-form-cfdb7'),
'https://ciphercoin.com/downloads/public-export-csv/',
$installed
);
}
public function pdf_export_field(){
$installed = function_exists('cfdb7_pdf_on_activate');
$this->premium_feature(
__('Export entries as PDF', 'contact-form-cfdb7'),
__('Generate professional PDF reports from Contact Form 7 submissions.', 'contact-form-cfdb7'),
'https://ciphercoin.com/downloads/cfdb7-export-pdf-addon/',
$installed
);
}
public function csv_import_field(){
$installed = class_exists('CFDB7_csv_import_page');
$this->premium_feature(
__('Import CSV into database', 'contact-form-cfdb7'),
__('Import existing CSV files directly into the Contact Form 7 database.', 'contact-form-cfdb7'),
'https://ciphercoin.com/downloads/cfdb7-import-csv-to-database/',
$installed
);
}
}