| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The admin page handler class |
| 5 |
*/ |
| 6 |
class WeForms_Admin { |
| 7 |
|
| 8 |
public function __construct() { |
| 9 |
|
| 10 |
add_action( 'init', array( $this, 'register_post_type' ) ); |
| 11 |
|
| 12 |
add_action( 'admin_menu', array( $this, 'register_admin_menu' ) ); |
| 13 |
|
| 14 |
add_action( 'pre_update_option_wpuf_general', array( $this, 'watch_wpuf_settings' ) ); |
| 15 |
|
| 16 |
add_filter( 'admin_post_weforms_export_forms', array( $this, 'export_forms' ) ); |
| 17 |
add_filter( 'admin_post_weforms_export_form_entries', array( $this, 'export_form_entries' ) ); |
| 18 |
|
| 19 |
// load default settings tabs |
| 20 |
add_filter( 'weforms_settings_tabs', array( $this, 'set_default_settings' ), 5 ); |
| 21 |
add_action( 'weforms_settings_tab_content_general', array( $this, 'settings_tab_general' ) ); |
| 22 |
add_action( 'weforms_settings_tab_content_recaptcha', array( $this, 'settings_tab_recaptcha' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Register form post types |
| 27 |
* |
| 28 |
* @return void |
| 29 |
*/ |
| 30 |
public function register_post_type() { |
| 31 |
$capability = weforms_form_access_capability(); |
| 32 |
|
| 33 |
register_post_type( 'wpuf_contact_form', array( |
| 34 |
'label' => __( 'Contact Forms', 'weforms' ), |
| 35 |
'public' => false, |
| 36 |
'show_ui' => true, |
| 37 |
'show_in_menu' => false, |
| 38 |
'capability_type' => 'post', |
| 39 |
'hierarchical' => false, |
| 40 |
'query_var' => false, |
| 41 |
'supports' => array('title'), |
| 42 |
'capabilities' => array( |
| 43 |
'publish_posts' => $capability, |
| 44 |
'edit_posts' => $capability, |
| 45 |
'edit_others_posts' => $capability, |
| 46 |
'delete_posts' => $capability, |
| 47 |
'delete_others_posts' => $capability, |
| 48 |
'read_private_posts' => $capability, |
| 49 |
'edit_post' => $capability, |
| 50 |
'delete_post' => $capability, |
| 51 |
'read_post' => $capability, |
| 52 |
), |
| 53 |
'labels' => array( |
| 54 |
'name' => __( 'Forms', 'weforms' ), |
| 55 |
'singular_name' => __( 'Form', 'weforms' ), |
| 56 |
'menu_name' => __( 'Contact Forms', 'weforms' ), |
| 57 |
'add_new' => __( 'Add Form', 'weforms' ), |
| 58 |
'add_new_item' => __( 'Add New Form', 'weforms' ), |
| 59 |
'edit' => __( 'Edit', 'weforms' ), |
| 60 |
'edit_item' => __( 'Edit Form', 'weforms' ), |
| 61 |
'new_item' => __( 'New Form', 'weforms' ), |
| 62 |
'view' => __( 'View Form', 'weforms' ), |
| 63 |
'view_item' => __( 'View Form', 'weforms' ), |
| 64 |
'search_items' => __( 'Search Form', 'weforms' ), |
| 65 |
'not_found' => __( 'No Form Found', 'weforms' ), |
| 66 |
'not_found_in_trash' => __( 'No Form Found in Trash', 'weforms' ), |
| 67 |
'parent' => __( 'Parent Form', 'weforms' ), |
| 68 |
), |
| 69 |
) ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Register the admin menu |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function register_admin_menu() { |
| 78 |
global $submenu; |
| 79 |
|
| 80 |
$capability = weforms_form_access_capability(); |
| 81 |
|
| 82 |
$hook = add_menu_page( __( 'weForms - The Best Contact Form', 'weforms' ), 'weForms', $capability, 'weforms', array( $this, 'contact_form_page'), 'data:image/svg+xml;base64,' . base64_encode( '<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 300 300" style="enable-background:new 0 0 300 300;" xml:space="preserve"><g fill="#9ea3a8"><path d="M285.1,24.1C273.1,9.4,254.8,0,234.3,0H65.7C46.6,0,29.3,8.2,17.3,21.2C6.6,32.9,0,48.5,0,65.7v60.5v108.1 C0,270.6,29.4,300,65.7,300h140h28.6c15.3,0,29.5-5.3,40.7-14.1c15.2-12,25-30.7,25-51.6V65.7C300,49.9,294.4,35.4,285.1,24.1z M212.7,187L104.6,233c-11.1,4.8-24-0.3-28.7-11.4c-4.8-11.1,0.3-24,11.4-28.7l108.1-46.1c11.1-4.8,24,0.3,28.7,11.4 C228.9,169.3,223.8,182.2,212.7,187z M217.4,107.1L99.9,157.8c-11.1,4.8-24-0.3-28.7-11.4c-4.8-11.1,0.3-24,11.4-28.7L200.1,67 c11.1-4.8,24,0.3,28.7,11.4v0C233.6,89.5,228.5,102.3,217.4,107.1z"/></g></svg>' ), 56 ); |
| 83 |
|
| 84 |
if ( current_user_can( $capability ) ) { |
| 85 |
$submenu['weforms'][] = array( __( 'All Forms', 'weforms' ), $capability, 'admin.php?page=weforms#/' ); |
| 86 |
$submenu['weforms'][] = array( __( 'Entries', 'weforms' ), $capability, 'admin.php?page=weforms#/entries' ); |
| 87 |
$submenu['weforms'][] = array( __( 'Tools', 'weforms' ), $capability, 'admin.php?page=weforms#/tools' ); |
| 88 |
|
| 89 |
if ( class_exists( 'WeForms_Pro' ) ) { |
| 90 |
if ( current_user_can( 'manage_options' ) ) { |
| 91 |
$submenu['weforms'][] = array( __( 'Modules', 'weforms' ), $capability, 'admin.php?page=weforms#/modules' ); |
| 92 |
} |
| 93 |
} else { |
| 94 |
$submenu['weforms'][] = array( __( 'Premium', 'weforms' ), $capability, 'admin.php?page=weforms#/premium' ); |
| 95 |
} |
| 96 |
|
| 97 |
do_action( 'weforms-admin-menu', $hook, $capability ); |
| 98 |
|
| 99 |
$submenu['weforms'][] = array( __( 'Help', 'weforms' ), $capability, 'admin.php?page=weforms#/help' ); |
| 100 |
} |
| 101 |
|
| 102 |
// only admins should see the settings page |
| 103 |
if ( current_user_can( 'manage_options' ) ) { |
| 104 |
$submenu['weforms'][] = array( __( 'Settings', 'weforms' ), 'manage_options', 'admin.php?page=weforms#/settings' ); |
| 105 |
} |
| 106 |
|
| 107 |
add_action( 'load-'. $hook, array( $this, 'load_assets' ) ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Load the asset libraries |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
public function load_assets() { |
| 116 |
require_once dirname( __FILE__ ) . '/class-form-builder-assets.php'; |
| 117 |
new WeForms_Form_Builder_Assets(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* The contact form page handler |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
public function contact_form_page() { |
| 126 |
require_once dirname( __FILE__ ) . '/views/vue-index.php'; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Export forms to JSON |
| 131 |
* |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function export_forms() { |
| 135 |
|
| 136 |
check_admin_referer( 'weforms-export-forms' ); |
| 137 |
|
| 138 |
if ( ! isset( $_REQUEST['weforms_export_forms'] ) ) { |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
$export_type = isset( $_POST['export_type'] ) ? $_POST['export_type'] : 'all'; |
| 143 |
$selected = isset( $_POST['selected_forms'] ) ? array_map( 'absint', $_POST['selected_forms'] ) : array(); |
| 144 |
|
| 145 |
if ( ! class_exists( 'WeForms_Admin_Tools' ) ) { |
| 146 |
require_once dirname( __FILE__ ) . '/class-admin-tools.php'; |
| 147 |
} |
| 148 |
|
| 149 |
switch ($export_type) { |
| 150 |
case 'all': |
| 151 |
WeForms_Admin_Tools::export_to_json(); |
| 152 |
return; |
| 153 |
|
| 154 |
case 'selected': |
| 155 |
WeForms_Admin_Tools::export_to_json( $selected ); |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
exit; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Export form entries to CSV |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
public function export_form_entries() { |
| 168 |
|
| 169 |
$form_id = isset( $_REQUEST['selected_forms'] ) ? absint( $_REQUEST['selected_forms'] ) : 0; |
| 170 |
|
| 171 |
if ( ! $form_id ) { |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
$entry_array = []; |
| 176 |
$columns = weforms_get_entry_columns( $form_id, false ); |
| 177 |
$total_entries = weforms_count_form_entries( $form_id ); |
| 178 |
$entries = weforms_get_form_entries( $form_id, array( |
| 179 |
'number' => $total_entries, |
| 180 |
'offset' => 0 |
| 181 |
) ); |
| 182 |
$extra_columns = array( |
| 183 |
'ip_address' => __( 'IP Address', 'weforms' ), |
| 184 |
'created_at' => __( 'Date', 'weforms' ) |
| 185 |
); |
| 186 |
|
| 187 |
$columns = array_merge( array( 'id' => 'Entry ID' ), $columns, $extra_columns ); |
| 188 |
|
| 189 |
foreach ($entries as $entry) { |
| 190 |
$temp = array(); |
| 191 |
|
| 192 |
foreach ($columns as $column_id => $label) { |
| 193 |
switch ( $column_id ) { |
| 194 |
case 'id': |
| 195 |
$temp[ $column_id ] = $entry->id; |
| 196 |
break; |
| 197 |
|
| 198 |
case 'ip_address': |
| 199 |
$temp[ $column_id ] = $entry->ip_address; |
| 200 |
break; |
| 201 |
|
| 202 |
case 'created_at': |
| 203 |
$temp[ $column_id ] = $entry->created_at; |
| 204 |
break; |
| 205 |
|
| 206 |
default: |
| 207 |
$value = weforms_get_entry_meta( $entry->id, $column_id, true ); |
| 208 |
$temp[ $column_id ] = str_replace( WeForms::$field_separator, ' ', $value ); |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
$entry_array[] = $temp; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
error_reporting(0); |
| 218 |
|
| 219 |
if ( ob_get_contents() ) { |
| 220 |
ob_clean(); |
| 221 |
} |
| 222 |
|
| 223 |
$blogname = strtolower( str_replace( " ", "-", get_option( 'blogname' ) ) ); |
| 224 |
$file_name = $blogname . "-weforms-entries-" . time() . '.csv'; |
| 225 |
|
| 226 |
// force download |
| 227 |
header("Content-Type: application/force-download"); |
| 228 |
header("Content-Type: application/octet-stream"); |
| 229 |
header("Content-Type: application/download"); |
| 230 |
|
| 231 |
// disposition / encoding on response body |
| 232 |
header("Content-Disposition: attachment;filename={$file_name}"); |
| 233 |
header("Content-Transfer-Encoding: binary"); |
| 234 |
|
| 235 |
$handle = fopen("php://output", 'w'); |
| 236 |
|
| 237 |
// put the column headers |
| 238 |
fputcsv( $handle, array_values( $columns ) ); |
| 239 |
|
| 240 |
// put the entry values |
| 241 |
foreach ($entry_array as $row) { |
| 242 |
fputcsv( $handle, $row ); |
| 243 |
} |
| 244 |
|
| 245 |
fclose( $handle ); |
| 246 |
|
| 247 |
exit; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Watch the wpuf general settings and sync with weforms settings |
| 252 |
* |
| 253 |
* @param array $value |
| 254 |
* |
| 255 |
* @return array |
| 256 |
*/ |
| 257 |
public function watch_wpuf_settings( $settings ) { |
| 258 |
$merge_array = array(); |
| 259 |
|
| 260 |
if ( isset( $settings['gmap_api_key'] ) ) { |
| 261 |
$merge_array['gmap_api'] = $settings['gmap_api_key']; |
| 262 |
} |
| 263 |
|
| 264 |
if ( isset( $settings['recaptcha_public'] ) ) { |
| 265 |
$recaptcha = new StdClass; |
| 266 |
$recaptcha->key = $settings['recaptcha_public']; |
| 267 |
$recaptcha->secret = $settings['recaptcha_private']; |
| 268 |
|
| 269 |
$merge_array['recaptcha'] = $recaptcha; |
| 270 |
} |
| 271 |
|
| 272 |
if ( $merge_array ) { |
| 273 |
$weforms_settings = get_option( 'weforms_settings', array() ); |
| 274 |
$weforms_settings = array_merge( $weforms_settings, $merge_array ); |
| 275 |
|
| 276 |
update_option( 'weforms_settings', $weforms_settings ); |
| 277 |
} |
| 278 |
|
| 279 |
return $settings; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Set default settings tabs |
| 284 |
* |
| 285 |
* @param array $tabs |
| 286 |
* |
| 287 |
* @return array |
| 288 |
*/ |
| 289 |
function set_default_settings( $tabs = array() ) { |
| 290 |
|
| 291 |
$tabs['general'] = array( |
| 292 |
'label' => 'General Settings', |
| 293 |
'icon' => WEFORMS_ASSET_URI . '/images/integrations/general-setting.svg', |
| 294 |
); |
| 295 |
|
| 296 |
$tabs['recaptcha'] = array( |
| 297 |
'label' => 'reCaptcha', |
| 298 |
'icon' => WEFORMS_ASSET_URI . '/images/integrations/reCaptcha.svg', |
| 299 |
); |
| 300 |
|
| 301 |
return $tabs; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* General tab content |
| 306 |
* |
| 307 |
* @param array $tab |
| 308 |
* |
| 309 |
* @return void |
| 310 |
*/ |
| 311 |
function settings_tab_general( $tab ) { |
| 312 |
include dirname( __FILE__ ) . '/views/weforms-settings-general.php'; |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* recaptcha tab content |
| 317 |
* |
| 318 |
* @param array $tab |
| 319 |
* |
| 320 |
* @return void |
| 321 |
*/ |
| 322 |
function settings_tab_recaptcha( $tab ) { |
| 323 |
include dirname( __FILE__ ) . '/views/weforms-settings-recaptcha.php'; |
| 324 |
} |
| 325 |
} |
| 326 |
|