PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.27
HTML Forms – Simple WordPress Forms Plugin v1.3.27
trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.3.0 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 All 66 releases
html-forms / src / admin / class-admin.php

class-admin.php in HTML Forms – Simple WordPress Forms Plugin 1.3.27, at src/admin/class-admin.php

466 lines 13.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace HTML_Forms\Admin;
4
5 use HTML_Forms\Form;
6 use HTML_Forms\Submission;
7
8 class Admin {
9
10 /**
11 * @var string
12 */
13 private $plugin_file;
14
15 /**
16 * Admin constructor.
17 *
18 * @param string $plugin_file
19 */
20 public function __construct( $plugin_file ) {
21 $this->plugin_file = $plugin_file;
22 }
23
24 public function hook() {
25 add_action( 'admin_menu', array( $this, 'menu' ) );
26 add_action( 'init', array( $this, 'register_settings' ) );
27 add_action( 'admin_init', array( $this, 'run_migrations' ) );
28 add_action( 'admin_init', array( $this, 'listen' ) );
29 add_action( 'admin_print_styles', array( $this, 'assets' ) );
30 add_action( 'admin_head', array( $this, 'add_screen_options' ) );
31 add_action( 'hf_admin_action_create_form', array( $this, 'process_create_form' ) );
32 add_action( 'hf_admin_action_save_form', array( $this, 'process_save_form' ) );
33 add_action( 'hf_admin_action_bulk_delete_submissions', array( $this, 'process_bulk_delete_submissions' ) );
34
35 add_action( 'hf_admin_output_form_tab_fields', array( $this, 'tab_fields' ) );
36 add_action( 'hf_admin_output_form_tab_messages', array( $this, 'tab_messages' ) );
37 add_action( 'hf_admin_output_form_tab_settings', array( $this, 'tab_settings' ) );
38 add_action( 'hf_admin_output_form_tab_actions', array( $this, 'tab_actions' ) );
39 add_action( 'hf_admin_output_form_tab_submissions', array( $this, 'tab_submissions_list' ) );
40 add_action( 'hf_admin_output_form_tab_submissions', array( $this, 'tab_submissions_detail' ) );
41 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_gutenberg_assets' ) );
42 }
43
44 public function enqueue_gutenberg_assets() {
45 wp_enqueue_script(
46 'html-forms-block',
47 plugins_url( 'assets/js/gutenberg-block.js', $this->plugin_file ),
48 array(
49 'wp-blocks',
50 'wp-i18n',
51 'wp-element',
52 'wp-components',
53 )
54 );
55 $forms = hf_get_forms();
56 $data = array();
57 foreach ( $forms as $form ) {
58 $data[] = array(
59 'title' => $form->title,
60 'slug' => $form->slug,
61 'id' => $form->ID,
62 );
63 }
64 wp_localize_script( 'html-forms-block', 'html_forms', $data );
65 }
66
67 public function register_settings() {
68 // register settings
69 register_setting( 'hf_settings', 'hf_settings', array( $this, 'sanitize_settings' ) );
70 }
71
72 public function run_migrations() {
73 $version_from = get_option( 'hf_version', '0.0' );
74 $version_to = HTML_FORMS_VERSION;
75
76 if ( version_compare( $version_from, $version_to, '>=' ) ) {
77 return;
78 }
79
80 $migrations = new Migrations( $version_from, $version_to, dirname( $this->plugin_file ) . '/migrations' );
81 $migrations->run();
82 update_option( 'hf_version', HTML_FORMS_VERSION );
83 }
84
85 /**
86 * @param array $dirty
87 *
88 * @return array
89 */
90 public function sanitize_settings( $dirty ) {
91 return $dirty;
92 }
93
94 public function listen() {
95 if ( isset( $_GET['_hf_admin_action'] ) ) {
96 $action = (string) $_GET['_hf_admin_action'];
97 } elseif ( isset( $_POST['_hf_admin_action'] ) ) {
98 $action = (string) $_POST['_hf_admin_action'];
99 } else {
100 return;
101 }
102
103 // verify nonce
104 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], '_hf_admin_action' ) ) {
105 wp_nonce_ays( $action );
106 exit;
107 }
108
109 // do nothing if logged in user is not of role administrator
110 if ( ! current_user_can( 'edit_forms' ) ) {
111 return;
112 }
113
114 /**
115 * Allows you to hook into requests containing `_hf_admin_action` => action name.
116 *
117 * The dynamic portion of the hook name, `$action`, refers to the action name.
118 *
119 * By the time this hook is fired, the user is already authorized. After processing all the registered hooks,
120 * the request is redirected back to the referring URL.
121 *
122 * @since 3.0
123 */
124 do_action( 'hf_admin_action_' . $action );
125
126 // redirect back to where we came from
127 $redirect_url = ! empty( $_REQUEST['_redirect_to'] ) ? $_REQUEST['_redirect_to'] : remove_query_arg( '_hf_admin_action' );
128 wp_safe_redirect( $redirect_url );
129 exit;
130 }
131
132 public function assets() {
133 if ( empty( $_GET['page'] ) || strpos( $_GET['page'], 'html-forms' ) !== 0 ) {
134 return;
135 }
136
137 wp_enqueue_style( 'html-forms-admin', plugins_url( 'assets/css/admin.css', $this->plugin_file ), array(), HTML_FORMS_VERSION );
138 wp_enqueue_script( 'html-forms-admin', plugins_url( 'assets/js/admin.js', $this->plugin_file ), array(), HTML_FORMS_VERSION, true );
139 wp_localize_script(
140 'html-forms-admin',
141 'hf_options',
142 array(
143 'page' => $_GET['page'],
144 'view' => empty( $_GET['view'] ) ? '' : $_GET['view'],
145 'form_id' => empty( $_GET['form_id'] ) ? 0 : (int) $_GET['form_id'],
146 )
147 );
148 }
149
150 public function menu() {
151 $capability = 'edit_forms';
152 $svg_icon = '<svg version="1.0" xmlns="http://www.w3.org/2000/svg" width="256.000000pt" height="256.000000pt" viewBox="0 0 256.000000 256.000000" preserveAspectRatio="xMidYMid meet"><g transform="translate(0.000000,256.000000) scale(0.100000,-0.100000)"
153 fill="#000000" stroke="none"><path d="M0 1280 l0 -1280 1280 0 1280 0 0 1280 0 1280 -1280 0 -1280 0 0 -1280z m2031 593 c8 -8 9 -34 4 -78 -6 -56 -9 -65 -23 -60 -43 16 -98 15 -132 -2 -50 -26 -72 -72 -78 -159 l-5 -74 92 0 91 0 0 -70 0 -70 -90 0 -90 0 0 -345 0 -345 -90 0 -90 0 0 345 0 345 -55 0 -55 0 0 70 0 70 55 0 55 0 0 38 c0 63 20 153 45 202 54 105 141 152 273 147 45 -2 87 -8 93 -14z m-1291 -288 l0 -235 230 0 230 0 0 235 0 235 90 0 90 0 0 -575 0 -575 -90 0 -90 0 0 260 0 260 -230 0 -230 0 0 -260 0 -260 -90 0 -90 0 0 575 0 575 90 0 90 0 0 -235z"/></g></svg>';
154 add_menu_page(
155 'HTML Forms',
156 'HTML Forms',
157 $capability,
158 'html-forms',
159 array(
160 $this,
161 'page_overview',
162 ),
163 'data:image/svg+xml;base64,' . base64_encode( $svg_icon ),
164 '99.88491'
165 );
166 add_submenu_page(
167 'html-forms',
168 __( 'Forms', 'html-forms' ),
169 __( 'All Forms', 'html-forms' ),
170 $capability,
171 'html-forms',
172 array(
173 $this,
174 'page_overview',
175 )
176 );
177 add_submenu_page(
178 'html-forms',
179 __( 'Add new form', 'html-forms' ),
180 __( 'Add New', 'html-forms' ),
181 $capability,
182 'html-forms-add-form',
183 array(
184 $this,
185 'page_new_form',
186 )
187 );
188 add_submenu_page(
189 'html-forms',
190 __( 'Settings', 'html-forms' ),
191 __( 'Settings', 'html-forms' ),
192 $capability,
193 'html-forms-settings',
194 array(
195 $this,
196 'page_settings',
197 )
198 );
199
200 if ( ! defined( 'HF_PREMIUM_VERSION' ) ) {
201 add_submenu_page(
202 'html-forms',
203 'Premium',
204 '<span style="color: #ea6ea6;">Premium</span>',
205 $capability,
206 'html-forms-premium',
207 array(
208 $this,
209 'page_premium',
210 )
211 );
212 }
213 }
214
215 public function add_screen_options() {
216 // only run on the submissions overview page (not detail)
217 if ( empty( $_GET['page'] ) || $_GET['page'] !== 'html-forms' || empty( $_GET['view'] ) || $_GET['view'] !== 'edit' || empty( $_GET['form_id'] ) || ! empty( $_GET['submission_id'] ) ) {
218 return;
219 }
220
221 // don't run if form does not have submissions enabled
222 $form = hf_get_form( $_GET['form_id'] );
223 if ( ! $form->settings['save_submissions'] ) {
224 return;
225 }
226
227 // tell screen options to show columns option
228 $submissions = hf_get_form_submissions( $_GET['form_id'] );
229 $columns = $this->get_submission_columns( $submissions );
230 add_filter(
231 'manage_toplevel_page_html-forms_columns',
232 function ( $unused ) use ( $columns ) {
233 return $columns;
234 }
235 );
236 add_screen_option( 'layout_columns' );
237 }
238
239 public function page_overview() {
240 if ( ! empty( $_GET['view'] ) && $_GET['view'] === 'edit' ) {
241 $this->page_edit_form();
242
243 return;
244 }
245
246 $settings = hf_get_settings();
247
248 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
249 $table = new Table( $settings );
250
251 require dirname( $this->plugin_file ) . '/views/page-overview.php';
252 }
253
254 public function page_new_form() {
255 require dirname( $this->plugin_file ) . '/views/page-add-form.php';
256 }
257
258 public function page_settings() {
259 $settings = hf_get_settings();
260 require dirname( $this->plugin_file ) . '/views/page-global-settings.php';
261 }
262
263 public function page_premium() {
264 require dirname( $this->plugin_file ) . '/views/page-premium.php';
265 }
266
267 public function page_edit_form() {
268 $active_tab = ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'fields';
269 $form_id = (int) $_GET['form_id'];
270 $form = hf_get_form( $form_id );
271 $settings = hf_get_settings();
272 require dirname( $this->plugin_file ) . '/views/page-edit-form.php';
273 }
274
275 public function tab_fields( Form $form ) {
276 $form_preview_url = add_query_arg(
277 array(
278 'hf_preview_form' => $form->ID,
279 ),
280 site_url( '/', 'admin' )
281 );
282 require dirname( $this->plugin_file ) . '/views/tab-fields.php';
283 }
284
285 public function tab_messages( Form $form ) {
286 require dirname( $this->plugin_file ) . '/views/tab-messages.php';
287 }
288
289
290 public function tab_settings( Form $form ) {
291 require dirname( $this->plugin_file ) . '/views/tab-settings.php';
292 }
293
294
295 public function tab_actions( Form $form ) {
296 require dirname( $this->plugin_file ) . '/views/tab-actions.php';
297 }
298
299 public function get_submission_columns( array $submissions ) {
300 $columns = array();
301 foreach ( $submissions as $s ) {
302 if ( ! is_array( $s->data ) ) {
303 continue;
304 }
305
306 foreach ( $s->data as $field => $value ) {
307 if ( ! isset( $columns[ $field ] ) ) {
308 $columns[ $field ] = esc_html( ucfirst( strtolower( str_replace( '_', ' ', $field ) ) ) );
309 }
310 }
311 }
312
313 return $columns;
314 }
315
316 public function tab_submissions_list( Form $form ) {
317 if ( ! empty( $_GET['submission_id'] ) ) {
318 return;
319 }
320
321 $items_per_page = 500;
322 $total_items = hf_count_form_submissions( $form->ID );
323 $total_pages = max( 1, ceil( $total_items / $items_per_page ) );
324 $current_page = isset( $_GET['paged'] ) ? intval( $_GET['paged'] ) : 1;
325 $current_page = max( 1, $current_page );
326 $current_page = min( $total_pages, $current_page );
327 $submissions = hf_get_form_submissions(
328 $form->ID,
329 array(
330 'limit' => $items_per_page,
331 'offset' => ( $current_page - 1 ) * $items_per_page,
332 )
333 );
334 $columns = $this->get_submission_columns( $submissions );
335 $hidden_columns = get_hidden_columns( get_current_screen() );
336
337 require dirname( $this->plugin_file ) . '/views/tab-submissions-list.php';
338 }
339
340 public function tab_submissions_detail( Form $form ) {
341 if ( empty( $_GET['submission_id'] ) ) {
342 return;
343 }
344
345 $submission = hf_get_form_submission( (int) $_GET['submission_id'] );
346 require dirname( $this->plugin_file ) . '/views/tab-submissions-detail.php';
347 }
348
349
350 public function process_create_form() {
351 // Fix for MultiSite stripping KSES for roles other than administrator
352 remove_all_filters( 'content_save_pre' );
353
354 $data = $_POST['form'];
355 $form_title = sanitize_text_field( $data['title'] );
356 $form_id = wp_insert_post(
357 array(
358 'post_type' => 'html-form',
359 'post_status' => 'publish',
360 'post_title' => $form_title,
361 'post_content' => $this->get_default_form_content(),
362 )
363 );
364
365 wp_safe_redirect( admin_url( 'admin.php?page=html-forms&view=edit&form_id=' . $form_id ) );
366 exit;
367 }
368
369 public function process_save_form() {
370 $form_id = (int) $_POST['form_id'];
371 $form = hf_get_form( $form_id );
372 $data = $_POST['form'];
373
374 // Fix for MultiSite stripping KSES for roles other than administrator
375 remove_all_filters( 'content_save_pre' );
376
377 // strip <form> tag from markup
378 $data['markup'] = preg_replace( '/<\/?form(.|\s)*?>/i', '', $data['markup'] );
379
380 $form_id = wp_insert_post(
381 array(
382 'ID' => $form_id,
383 'post_type' => 'html-form',
384 'post_status' => 'publish',
385 'post_title' => sanitize_text_field( $data['title'] ),
386 'post_content' => $data['markup'],
387 'post_name' => sanitize_title_with_dashes( $data['slug'] ),
388 )
389 );
390
391 if ( ! empty( $data['settings'] ) ) {
392 update_post_meta( $form_id, '_hf_settings', $data['settings'] );
393 }
394
395 // save form messages in individual meta keys
396 foreach ( $data['messages'] as $key => $message ) {
397 update_post_meta( $form_id, 'hf_message_' . $key, $message );
398 }
399
400 $redirect_url_args = array(
401 'form_id' => $form_id,
402 'saved' => 1,
403 );
404 $redirect_url = add_query_arg( $redirect_url_args, admin_url( 'admin.php?page=html-forms&view=edit' ) );
405 wp_safe_redirect( $redirect_url );
406 exit;
407 }
408
409 /**
410 * Get URL for a tab on the current page.
411 *
412 * @param $tab
413 *
414 * @return string
415 * @since 3.0
416 * @internal
417 */
418 public function get_tab_url( $tab ) {
419 return add_query_arg( array( 'tab' => $tab ), remove_query_arg( 'tab' ) );
420 }
421
422 /**
423 * @return array
424 */
425 public function get_available_form_actions() {
426 $actions = array();
427
428 /**
429 * Filters the available form actions
430 *
431 * @param array $actions
432 */
433 $actions = apply_filters( 'hf_available_form_actions', $actions );
434
435 return $actions;
436 }
437
438 public function process_bulk_delete_submissions() {
439 global $wpdb;
440
441 if ( empty( $_POST['id'] ) ) {
442 return;
443 }
444
445 $args = array_map( 'intval', $_POST['id'] );
446 $table = $wpdb->prefix . 'hf_submissions';
447 $placeholders = rtrim( str_repeat( '%d,', count( $args ) ), ',' );
448 $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE id IN( {$placeholders} );", $args ) );
449
450 $args[] = '_hf_%%';
451 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN ( {$placeholders} ) AND meta_key LIKE %s;", $args ) );
452 }
453
454 private function get_default_form_content() {
455 $html = '';
456 $html .= sprintf( "<p>\n\t<label>%1\$s</label>\n\t<input type=\"text\" name=\"NAME\" placeholder=\"%1\$s\" required />\n</p>", __( 'Your name', 'html-forms' ) ) . PHP_EOL;
457 $html .= sprintf( "<p>\n\t<label>%1\$s</label>\n\t<input type=\"email\" name=\"EMAIL\" placeholder=\"%1\$s\" required />\n</p>", __( 'Your email', 'html-forms' ) ) . PHP_EOL;
458 $html .= sprintf( "<p>\n\t<label>%1\$s</label>\n\t<input type=\"text\" name=\"SUBJECT\" placeholder=\"%1\$s\" required />\n</p>", __( 'Subject', 'html-forms' ) ) . PHP_EOL;
459 $html .= sprintf( "<p>\n\t<label>%1\$s</label>\n\t<textarea name=\"MESSAGE\" placeholder=\"%1\$s\" required></textarea>\n</p>", __( 'Message', 'html-forms' ) ) . PHP_EOL;
460 $html .= sprintf( "<p>\n\t<input type=\"submit\" value=\"%s\" />\n</p>", __( 'Send', 'html-forms' ) );
461
462 return $html;
463 }
464
465 }
466