| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit( 'Direct\'s not allowed' ); |
| 4 |
} |
| 5 |
/* |
| 6 |
* Support functions |
| 7 |
*/ |
| 8 |
if ( ! function_exists( 'cf7d_get_posted_data' ) ) { |
| 9 |
function cf7d_get_posted_data( $cf7 ) { |
| 10 |
if ( ! isset( $cf7->posted_data ) && class_exists( 'WPCF7_Submission' ) ) { |
| 11 |
// Contact Form 7 version 3.9 removed $cf7->posted_data and now |
| 12 |
// we have to retrieve it from an API |
| 13 |
$submission = WPCF7_Submission::get_instance(); |
| 14 |
if ( $submission ) { |
| 15 |
$data = array(); |
| 16 |
$data['title'] = $cf7->title(); |
| 17 |
$data['posted_data'] = $submission->get_posted_data(); |
| 18 |
$data['uploaded_files'] = $submission->uploaded_files(); |
| 19 |
$data['WPCF7_ContactForm'] = $cf7; |
| 20 |
$cf7 = (object) $data; |
| 21 |
} |
| 22 |
} |
| 23 |
return $cf7; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
if ( ! function_exists( 'cf7d_no_save_fields' ) ) { |
| 28 |
function cf7d_no_save_fields() { |
| 29 |
$cf7d_no_save_fields = array( '_wpcf7', '_wpcf7_version', '_wpcf7_locale', '_wpcf7_unit_tag', '_wpcf7_is_ajax_call', '_wpcf7_container_post', '_wpcf7_posted_data_hash' ); |
| 30 |
return apply_filters( 'cf7d_no_save_fields', $cf7d_no_save_fields ); |
| 31 |
} |
| 32 |
} |
| 33 |
if ( ! function_exists( 'cf7d_add_more_fields' ) ) { |
| 34 |
function cf7d_add_more_fields( $cf7 ) { |
| 35 |
$submission = WPCF7_Submission::get_instance(); |
| 36 |
// time |
| 37 |
// $cf7->posted_data['submit_time'] = date_i18n('Y-m-d H:i:s'); |
| 38 |
$cf7->posted_data['submit_time'] = get_date_from_gmt( gmdate( 'Y-m-d H:i:s', $submission->get_meta( 'timestamp' ) ), 'Y-m-d H:i:s' ); |
| 39 |
// ip |
| 40 |
$cf7->posted_data['submit_ip'] = $submission->get_meta( 'remote_ip' ); |
| 41 |
// user id |
| 42 |
$cf7->posted_data['submit_user_id'] = 0; |
| 43 |
if ( function_exists( 'is_user_logged_in' ) && is_user_logged_in() ) { |
| 44 |
$current_user = wp_get_current_user(); // WP_User |
| 45 |
$cf7->posted_data['submit_user_id'] = $current_user->ID; |
| 46 |
} |
| 47 |
return $cf7; |
| 48 |
} |
| 49 |
} |
| 50 |
/* |
| 51 |
* $data: rows from database |
| 52 |
* $fid: form id |
| 53 |
*/ |
| 54 |
if ( ! function_exists( 'cf7d_sortdata' ) ) { |
| 55 |
function cf7d_sortdata( $data, $entry = true ) { |
| 56 |
$data_sorted = array(); |
| 57 |
foreach ( $data as $k => $v ) { |
| 58 |
if ( ! isset( $data_sorted[ $v->data_id ] ) ) { |
| 59 |
$data_sorted[ $v->data_id ] = array(); |
| 60 |
} |
| 61 |
if ( ! $entry ) { |
| 62 |
$data_sorted[ $v->data_id ][ $v->name ] = apply_filters( 'cf7d_entry_value', $v->value, $v->name ); |
| 63 |
} else { |
| 64 |
$data_sorted[ $v->data_id ][ $v->name ] = $v->value; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return $data_sorted; |
| 69 |
} |
| 70 |
} |
| 71 |
if ( ! function_exists( 'cf7d_get_db_fields' ) ) { |
| 72 |
function cf7d_get_db_fields( $fid, $filter = true ) { |
| 73 |
global $wpdb; |
| 74 |
$sql = sprintf( 'SELECT `name` FROM `' . $wpdb->prefix . 'cf7_data_entry` WHERE cf7_id = %d GROUP BY `name`', $fid ); |
| 75 |
$data = $wpdb->get_results( $sql ); |
| 76 |
|
| 77 |
$fields = array(); |
| 78 |
foreach ( $data as $k => $v ) { |
| 79 |
$fields[ $v->name ] = $v->name; |
| 80 |
} |
| 81 |
if ( $fields ) { |
| 82 |
$fields = apply_filters( 'cf7d_admin_fields', $fields, $fid ); |
| 83 |
} |
| 84 |
return $fields; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if ( ! function_exists( 'cf7d_get_entrys' ) ) { |
| 89 |
function cf7d_get_entrys( $fid, $entry_ids = '', $cf7d_entry_order_by = '' ) { |
| 90 |
global $wpdb; |
| 91 |
if ( empty( $cf7d_entry_order_by ) ) { |
| 92 |
$cf7d_entry_order_by = '`data_id` DESC'; |
| 93 |
} |
| 94 |
$query = sprintf( 'SELECT * FROM `' . $wpdb->prefix . 'cf7_data_entry` WHERE `cf7_id` = %d AND data_id IN(SELECT * FROM (SELECT data_id FROM `' . $wpdb->prefix . 'cf7_data_entry` WHERE 1 = 1 ' . ( ( ! empty( $entry_ids ) ) ? 'AND `data_id` IN (' . $entry_ids . ')' : '' ) . ' GROUP BY `data_id` ORDER BY ' . $cf7d_entry_order_by . ') temp_table) ORDER BY ' . $cf7d_entry_order_by, $fid ); |
| 95 |
$data = $wpdb->get_results( $query ); |
| 96 |
foreach ( $data as $k => $v ) { |
| 97 |
foreach ( $v as $k2 => $v2 ) { |
| 98 |
$data[ $k ]->{$k2} = esc_html( $v2 ); |
| 99 |
} |
| 100 |
} |
| 101 |
return $data; |
| 102 |
} |
| 103 |
} |
| 104 |
if ( ! function_exists( 'cf7d_upload_folder' ) ) { |
| 105 |
function cf7d_upload_folder() { |
| 106 |
return apply_filters( 'cf7d_upload_folder', 'cf7-database' ); |
| 107 |
} |
| 108 |
} |
| 109 |
if ( ! function_exists( 'cf7d_sanitize_arr' ) ) { |
| 110 |
function cf7d_sanitize_arr( $arr ) { |
| 111 |
return is_array( $arr ) ? array_map( 'cf7d_sanitize_arr', $arr ) : sanitize_text_field( $arr ); |
| 112 |
} |
| 113 |
} |
| 114 |
if ( ! function_exists( 'cf7d_esc_sorted_data' ) ) { |
| 115 |
function cf7d_esc_sorted_data( $data_sorted ) { |
| 116 |
foreach ( $data_sorted as $k => $d ) { |
| 117 |
foreach ( $d as $k2 => $v2 ) { |
| 118 |
if ( esc_url_raw( $v2 ) === $v2 ) { |
| 119 |
$data_sorted[ $k ][ $k2 ] = '<a href="' . esc_url( $v2 ) . '" target="_blank" rel="nofollow noopener noreferrer">' . $v2 . '</a>'; |
| 120 |
} else { |
| 121 |
$data_sorted[ $k ][ $k2 ] = esc_html( $v2 ); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
return $data_sorted; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
add_filter( 'cf7d_admin_fields', 'cf7d_admin_fields_cb', 10, 2 ); |
| 130 |
if ( ! function_exists( 'cf7d_admin_fields_cb' ) ) { |
| 131 |
function cf7d_admin_fields_cb( $fields, $fid ) { |
| 132 |
$return = array(); |
| 133 |
$field_settings = get_option( 'cf7d_settings_field_' . $fid, array() ); |
| 134 |
if ( empty( $field_settings ) ) { |
| 135 |
$field_settings = array(); |
| 136 |
} |
| 137 |
if ( count( $field_settings ) == 0 ) { // no settings found |
| 138 |
$return = $fields; |
| 139 |
} else { |
| 140 |
foreach ( $field_settings as $k => $v ) { |
| 141 |
if ( isset( $fields[ $k ] ) ) { |
| 142 |
$show = $field_settings[ $k ]['show']; |
| 143 |
if ( 1 == $show ) { |
| 144 |
$label = $field_settings[ $k ]['label']; |
| 145 |
$return[ $k ] = $label; |
| 146 |
} |
| 147 |
unset( $fields[ $k ] ); |
| 148 |
} |
| 149 |
} |
| 150 |
if ( count( $fields ) > 0 ) { |
| 151 |
foreach ( $fields as $k => $v ) { |
| 152 |
$return[ $k ] = $v; |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
return $return; |
| 157 |
} |
| 158 |
} |
| 159 |
|