PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.4.5
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.4.5
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / admin / class-admin.php

class-admin.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.4.5, at includes/admin/class-admin.php

376 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The admin page handler class
5 */
6 class WeForms_Admin {
7
8 public function __construct() {
9 add_action( 'init', [ $this, 'register_post_type' ] );
10 add_action( 'admin_menu', [ $this, 'register_admin_menu' ] );
11 add_action( 'pre_update_option_wpuf_general', [ $this, 'watch_wpuf_settings' ] );
12
13 add_filter( 'admin_footer_text', [ $this, 'admin_footer_text' ] );
14 add_filter( 'admin_post_weforms_export_forms', [ $this, 'export_forms' ] );
15 add_filter( 'admin_post_weforms_export_form_entries', [ $this, 'export_form_entries' ] );
16
17 // load default settings tabs
18 add_filter( 'weforms_settings_tabs', [ $this, 'set_default_settings' ], 5 );
19 add_action( 'weforms_settings_tab_content_general', [ $this, 'settings_tab_general' ] );
20 add_action( 'weforms_settings_tab_content_recaptcha', [ $this, 'settings_tab_recaptcha' ] );
21 add_action( 'weforms_settings_tab_content_privacy', [ $this, 'settings_tab_privacy' ] );
22 }
23
24 /**
25 * Register form post types
26 *
27 * @return void
28 */
29 public function register_post_type() {
30 $capability = weforms_form_access_capability();
31
32 register_post_type( 'wpuf_contact_form', [
33 'label' => __( 'Contact Forms', 'weforms' ),
34 'public' => false,
35 'show_ui' => true,
36 'show_in_menu' => false,
37 'capability_type' => 'post',
38 'hierarchical' => false,
39 'query_var' => false,
40 'supports' => ['title'],
41 'capabilities' => [
42 'publish_posts' => $capability,
43 'edit_posts' => $capability,
44 'edit_others_posts' => $capability,
45 'delete_posts' => $capability,
46 'delete_others_posts' => $capability,
47 'read_private_posts' => $capability,
48 'edit_post' => $capability,
49 'delete_post' => $capability,
50 'read_post' => $capability,
51 ],
52 'labels' => [
53 'name' => __( 'Forms', 'weforms' ),
54 'singular_name' => __( 'Form', 'weforms' ),
55 'menu_name' => __( 'Contact Forms', 'weforms' ),
56 'add_new' => __( 'Add Form', 'weforms' ),
57 'add_new_item' => __( 'Add New Form', 'weforms' ),
58 'edit' => __( 'Edit', 'weforms' ),
59 'edit_item' => __( 'Edit Form', 'weforms' ),
60 'new_item' => __( 'New Form', 'weforms' ),
61 'view' => __( 'View Form', 'weforms' ),
62 'view_item' => __( 'View Form', 'weforms' ),
63 'search_items' => __( 'Search Form', 'weforms' ),
64 'not_found' => __( 'No Form Found', 'weforms' ),
65 'not_found_in_trash' => __( 'No Form Found in Trash', 'weforms' ),
66 'parent' => __( 'Parent Form', 'weforms' ),
67 ],
68 ] );
69 }
70
71 /**
72 * Register the admin menu
73 *
74 * @return void
75 */
76 public function register_admin_menu() {
77 global $submenu;
78
79 $capability = weforms_form_access_capability();
80
81 $hook = add_menu_page( __( 'weForms - The Best Contact Form', 'weforms' ), 'weForms', $capability, 'weforms', [ $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 );
82
83 if ( current_user_can( $capability ) ) {
84 $submenu['weforms'][] = [ __( 'All Forms', 'weforms' ), $capability, 'admin.php?page=weforms#/' ];
85 $submenu['weforms'][] = [ __( 'Entries', 'weforms' ), $capability, 'admin.php?page=weforms#/entries' ];
86 $submenu['weforms'][] = [ __( 'Tools', 'weforms' ), $capability, 'admin.php?page=weforms#/tools' ];
87
88 if ( class_exists( 'WeForms_Pro' ) ) {
89 if ( current_user_can( 'manage_options' ) ) {
90 $submenu['weforms'][] = [ __( 'Modules', 'weforms' ), $capability, 'admin.php?page=weforms#/modules' ];
91 }
92 } else {
93 $submenu['weforms'][] = [ __( 'Premium', 'weforms' ), $capability, 'admin.php?page=weforms#/premium' ];
94 }
95
96 do_action( 'weforms-admin-menu', $hook, $capability );
97
98 $submenu['weforms'][] = [ __( '<span style="color:#f18500">Help</span>', 'weforms' ), $capability, 'admin.php?page=weforms#/help' ];
99 }
100
101 // only admins should see the settings page
102 if ( current_user_can( 'manage_options' ) ) {
103 $submenu['weforms'][] = [ __( 'Settings', 'weforms' ), 'manage_options', 'admin.php?page=weforms#/settings' ];
104 }
105
106 add_action( 'load-' . $hook, [ $this, 'load_assets' ] );
107 }
108
109 /**
110 * Load the asset libraries
111 *
112 * @return void
113 */
114 public function load_assets() {
115 require_once __DIR__ . '/class-form-builder-assets.php';
116 new WeForms_Form_Builder_Assets();
117 }
118
119 /**
120 * The contact form page handler
121 *
122 * @return void
123 */
124 public function contact_form_page() {
125 require_once __DIR__ . '/views/vue-index.php';
126 }
127
128 /**
129 * Export forms to JSON
130 *
131 * @return void
132 */
133 public function export_forms() {
134 check_admin_referer( 'weforms-export-forms' );
135
136 if ( !isset( $_REQUEST['weforms_export_forms'] ) ) {
137 return;
138 }
139
140 $export_type = isset( $_POST['export_type'] ) ? sanitize_text_field( wp_unslash( $_POST['export_type'] ) ) : 'all';
141 $selected = isset( $_POST['selected_forms'] ) ? array_map( 'absint', $_POST['selected_forms'] ) : array();
142
143 if ( !class_exists( 'WeForms_Admin_Tools' ) ) {
144 require_once __DIR__ . '/class-admin-tools.php';
145 }
146
147 switch ( $export_type ) {
148 case 'all':
149 WeForms_Admin_Tools::export_to_json();
150
151 return;
152
153 case 'selected':
154 WeForms_Admin_Tools::export_to_json( $selected );
155
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 $form_id = isset( $_REQUEST['selected_forms'] ) ? absint( $_REQUEST['selected_forms'] ) : 0;
169
170 if ( !$form_id ) {
171 return;
172 }
173
174 $entry_array = [];
175 $columns = weforms_get_entry_columns( $form_id, false );
176 $total_entries = weforms_count_form_entries( $form_id );
177 $entries = weforms_get_form_entries( $form_id, [
178 'number' => $total_entries,
179 'offset' => 0,
180 ] );
181 $extra_columns = [
182 'ip_address' => __( 'IP Address', 'weforms' ),
183 'created_at' => __( 'Date', 'weforms' ),
184 ];
185
186 $columns = array_merge( [ 'id' => 'Entry ID' ], $columns, $extra_columns );
187
188 foreach ( $entries as $entry ) {
189 $temp = [];
190
191 foreach ( $columns as $column_id => $label ) {
192 switch ( $column_id ) {
193 case 'id':
194 $temp[ $column_id ] = $entry->id;
195 break;
196
197 case 'ip_address':
198 $temp[ $column_id ] = $entry->ip_address;
199 break;
200
201 case 'created_at':
202 $temp[ $column_id ] = $entry->created_at;
203 break;
204
205 default:
206
207 $value = weforms_get_entry_meta( $entry->id, $column_id, true );
208 $value = weforms_get_pain_text( $value );
209 $temp[ $column_id ] = str_replace( WeForms::$field_separator, ' ', $value );
210
211 break;
212 }
213 }
214
215 $entry_array[] = $temp;
216 }
217
218 error_reporting( 0 );
219
220 if ( ob_get_contents() ) {
221 ob_clean();
222 }
223
224 $blogname = sanitize_title( strtolower( str_replace( ' ', '-', get_option( 'blogname' ) ) ) );
225 $file_name = $blogname . '-weforms-entries-' . time() . '.csv';
226
227 // force download
228 header( 'Content-Type: application/force-download' );
229 header( 'Content-Type: application/octet-stream' );
230 header( 'Content-Type: application/download' );
231
232 // disposition / encoding on response body
233 header( "Content-Disposition: attachment;filename={$file_name}" );
234 header( 'Content-Transfer-Encoding: binary' );
235
236 $handle = fopen( 'php://output', 'w' );
237
238 //handle UTF-8 chars conversion for CSV
239 fprintf( $handle, chr( 0xEF ) . chr( 0xBB ) . chr( 0xBF ) );
240
241 // put the column headers
242 fputcsv( $handle, array_values( $columns ) );
243
244 // put the entry values
245 foreach ( $entry_array as $row ) {
246 fputcsv( $handle, $row );
247 }
248
249 fclose( $handle );
250
251 exit;
252 }
253
254 /**
255 * Watch the wpuf general settings and sync with weforms settings
256 *
257 * @param array $value
258 *
259 * @return array
260 */
261 public function watch_wpuf_settings( $settings ) {
262 $merge_array = [];
263
264 if ( isset( $settings['gmap_api_key'] ) ) {
265 $merge_array['gmap_api'] = $settings['gmap_api_key'];
266 }
267
268 if ( isset( $settings['recaptcha_public'] ) ) {
269 $recaptcha = new StdClass();
270 $recaptcha->key = $settings['recaptcha_public'];
271 $recaptcha->secret = $settings['recaptcha_private'];
272 $recaptcha->type = $settings['recaptcha_type'];
273
274 $merge_array['recaptcha'] = $recaptcha;
275 }
276
277 if ( $merge_array ) {
278 $weforms_settings = get_option( 'weforms_settings', [] );
279 $weforms_settings = array_merge( $weforms_settings, $merge_array );
280
281 update_option( 'weforms_settings', $weforms_settings );
282 }
283
284 return $settings;
285 }
286
287 /**
288 * Admin footer text.
289 *
290 * Fired by `admin_footer_text` filter.
291 *
292 * @since 1.3.5
293 *
294 * @param string $footer_text the content that will be printed
295 *
296 * @return string the content that will be printed
297 **/
298 public function admin_footer_text( $footer_text ) {
299 $current_screen = get_current_screen();
300 $is_weforms_screen = ( $current_screen && false !== strpos( $current_screen->id, 'weforms' ) );
301
302 if ( $is_weforms_screen ) {
303 $footer_text = sprintf(
304 __( 'If you like %1$s please leave us a %2$s rating. A huge thank you from %3$s in advance!', 'weforms' ),
305 '<strong>' . __( 'weForms', 'weforms' ) . '</strong>',
306 '<a href="https://wordpress.org/support/plugin/weforms/reviews/" target="_blank">&#9733;&#9733;&#9733;&#9733;&#9733;</a>',
307 '<strong>weDevs</strong>'
308 );
309 }
310
311 return $footer_text;
312 }
313
314 /**
315 * Set default settings tabs
316 *
317 * @param array $tabs
318 *
319 * @return array
320 */
321 public function set_default_settings( $tabs = [] ) {
322 $tabs['general'] = [
323 'label' => __( 'General Settings', 'weforms' ),
324 'icon' => WEFORMS_ASSET_URI . '/images/integrations/general-setting.svg',
325 ];
326
327 $tabs['recaptcha'] = [
328 'label' => __( 'reCaptcha', 'weforms' ),
329 'icon' => WEFORMS_ASSET_URI . '/images/integrations/reCaptcha.svg',
330 ];
331
332 /* TODO: Refactor this block when more options are added in privacy settings*/
333 if ( class_exists( 'WeForms_Pro' ) ) {
334 $tabs['privacy'] = [
335 'label' => __( 'Privacy', 'weforms' ),
336 'icon' => WEFORMS_ASSET_URI . '/images/privacy.svg',
337 ];
338 }
339
340 return $tabs;
341 }
342
343 /**
344 * General tab content
345 *
346 * @param array $tab
347 *
348 * @return void
349 */
350 public function settings_tab_general( $tab ) {
351 include __DIR__ . '/views/weforms-settings-general.php';
352 }
353
354 /**
355 * recaptcha tab content
356 *
357 * @param array $tab
358 *
359 * @return void
360 */
361 public function settings_tab_recaptcha( $tab ) {
362 include __DIR__ . '/views/weforms-settings-recaptcha.php';
363 }
364
365 /**
366 * Privacy tab content
367 *
368 * @param array $tab
369 *
370 * @return void
371 */
372 public function settings_tab_privacy( $tab ) {
373 include __DIR__ . '/views/weforms-settings-privacy.php';
374 }
375 }
376