PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.23
HTML Forms – Simple WordPress Forms Plugin v1.3.23
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.23, at src/admin/class-admin.php

387 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 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( 'html-forms-block', plugins_url( 'assets/js/gutenberg-block.js', $this->plugin_file ), array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components' ) );
46 $forms = hf_get_forms();
47 $data = array();
48 foreach ( $forms as $form ) {
49 $data[] = array(
50 'title' => $form->title,
51 'slug' => $form->slug,
52 'id' => $form->ID,
53 );
54 }
55 wp_localize_script( 'html-forms-block', 'html_forms', $data );
56 }
57
58 public function register_settings() {
59 // register settings
60 register_setting( 'hf_settings', 'hf_settings', array( $this, 'sanitize_settings' ) );
61 }
62
63 public function run_migrations() {
64 $version_from = get_option( 'hf_version', '0.0' );
65 $version_to = HTML_FORMS_VERSION;
66
67 if ( version_compare( $version_from, $version_to, '>=' ) ) {
68 return;
69 }
70
71 $migrations = new Migrations( $version_from, $version_to, dirname( $this->plugin_file ) . '/migrations' );
72 $migrations->run();
73 update_option( 'hf_version', HTML_FORMS_VERSION );
74 }
75
76 /**
77 * @param array $dirty
78 * @return array
79 */
80 public function sanitize_settings( $dirty ) {
81 return $dirty;
82 }
83
84 public function listen() {
85 if ( isset( $_GET['_hf_admin_action'] ) ) {
86 $action = (string) $_GET['_hf_admin_action'];
87 } elseif ( isset( $_POST['_hf_admin_action'] ) ) {
88 $action = (string) $_POST['_hf_admin_action'];
89 } else {
90 return;
91 }
92
93 // verify nonce
94 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], '_hf_admin_action' ) ) {
95 wp_nonce_ays( $action );
96 exit;
97 }
98
99 // do nothing if logged in user is not of role administrator
100 if ( ! current_user_can( 'edit_forms' ) ) {
101 return;
102 }
103
104 /**
105 * Allows you to hook into requests containing `_hf_admin_action` => action name.
106 *
107 * The dynamic portion of the hook name, `$action`, refers to the action name.
108 *
109 * By the time this hook is fired, the user is already authorized. After processing all the registered hooks,
110 * the request is redirected back to the referring URL.
111 *
112 * @since 3.0
113 */
114 do_action( 'hf_admin_action_' . $action );
115
116 // redirect back to where we came from
117 $redirect_url = ! empty( $_REQUEST['_redirect_to'] ) ? $_REQUEST['_redirect_to'] : remove_query_arg( '_hf_admin_action' );
118 wp_safe_redirect( $redirect_url );
119 exit;
120 }
121
122 public function assets() {
123 if ( empty( $_GET['page'] ) || strpos( $_GET['page'], 'html-forms' ) !== 0 ) {
124 return;
125 }
126
127 wp_enqueue_style( 'html-forms-admin', plugins_url( 'assets/css/admin.css', $this->plugin_file ), array(), HTML_FORMS_VERSION );
128 wp_enqueue_script( 'html-forms-admin', plugins_url( 'assets/js/admin.js', $this->plugin_file ), array(), HTML_FORMS_VERSION, true );
129 wp_localize_script(
130 'html-forms-admin',
131 'hf_options',
132 array(
133 'page' => $_GET['page'],
134 'view' => empty( $_GET['view'] ) ? '' : $_GET['view'],
135 'form_id' => empty( $_GET['form_id'] ) ? 0 : (int) $_GET['form_id'],
136 )
137 );
138 }
139
140 public function menu() {
141 $capability = 'edit_forms';
142 $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)"
143 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>';
144 add_menu_page( 'HTML Forms', 'HTML Forms', $capability, 'html-forms', array( $this, 'page_overview' ), 'data:image/svg+xml;base64,' . base64_encode( $svg_icon ), '99.88491' );
145 add_submenu_page( 'html-forms', __( 'Forms', 'html-forms' ), __( 'All Forms', 'html-forms' ), $capability, 'html-forms', array( $this, 'page_overview' ) );
146 add_submenu_page( 'html-forms', __( 'Add new form', 'html-forms' ), __( 'Add New', 'html-forms' ), $capability, 'html-forms-add-form', array( $this, 'page_new_form' ) );
147 add_submenu_page( 'html-forms', __( 'Settings', 'html-forms' ), __( 'Settings', 'html-forms' ), $capability, 'html-forms-settings', array( $this, 'page_settings' ) );
148
149 if ( ! defined( 'HF_PREMIUM_VERSION' ) ) {
150 add_submenu_page( 'html-forms', 'Premium', '<span style="color: #ea6ea6;">Premium</span>', $capability, 'html-forms-premium', array( $this, 'page_premium' ) );
151 }
152 }
153
154 public function add_screen_options() {
155 // only run on the submissions overview page (not detail)
156 if ( empty( $_GET['page'] ) || $_GET['page'] !== 'html-forms' || empty( $_GET['view'] ) || $_GET['view'] !== 'edit' || empty( $_GET['form_id'] ) || ! empty( $_GET['submission_id'] ) ) {
157 return;
158 }
159
160 // don't run if form does not have submissions enabled
161 $form = hf_get_form( $_GET['form_id'] );
162 if ( ! $form->settings['save_submissions'] ) {
163 return;
164 }
165
166 // tell screen options to show columns option
167 $submissions = hf_get_form_submissions( $_GET['form_id'] );
168 $columns = $this->get_submission_columns( $submissions );
169 add_filter(
170 'manage_toplevel_page_html-forms_columns',
171 function( $unused ) use ( $columns ) {
172 return $columns;
173 }
174 );
175 add_screen_option( 'layout_columns' );
176 }
177
178 public function page_overview() {
179 if ( ! empty( $_GET['view'] ) && $_GET['view'] === 'edit' ) {
180 $this->page_edit_form();
181 return;
182 }
183
184 $settings = hf_get_settings();
185
186 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
187 $table = new Table( $settings );
188
189 require dirname( $this->plugin_file ) . '/views/page-overview.php';
190 }
191
192 public function page_new_form() {
193 require dirname( $this->plugin_file ) . '/views/page-add-form.php';
194 }
195
196 public function page_settings() {
197 $settings = hf_get_settings();
198 require dirname( $this->plugin_file ) . '/views/page-global-settings.php';
199 }
200
201 public function page_premium() {
202 require dirname( $this->plugin_file ) . '/views/page-premium.php';
203 }
204
205 public function page_edit_form() {
206 $active_tab = ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'fields';
207 $form_id = (int) $_GET['form_id'];
208 $form = hf_get_form( $form_id );
209 $settings = hf_get_settings();
210 require dirname( $this->plugin_file ) . '/views/page-edit-form.php';
211 }
212
213 public function tab_fields( Form $form ) {
214 $form_preview_url = add_query_arg(
215 array(
216 'hf_preview_form' => $form->ID,
217 ),
218 site_url( '/', 'admin' )
219 );
220 require dirname( $this->plugin_file ) . '/views/tab-fields.php';
221 }
222
223 public function tab_messages( Form $form ) {
224 require dirname( $this->plugin_file ) . '/views/tab-messages.php';
225 }
226
227
228 public function tab_settings( Form $form ) {
229 require dirname( $this->plugin_file ) . '/views/tab-settings.php';
230 }
231
232
233 public function tab_actions( Form $form ) {
234 require dirname( $this->plugin_file ) . '/views/tab-actions.php';
235 }
236
237 public function get_submission_columns( array $submissions ) {
238 $columns = array();
239 foreach ( $submissions as $s ) {
240 if ( ! is_array( $s->data ) ) {
241 continue;
242 }
243
244 foreach ( $s->data as $field => $value ) {
245 if ( ! isset( $columns[ $field ] ) ) {
246 $columns[ $field ] = esc_html( ucfirst( strtolower( str_replace( '_', ' ', $field ) ) ) );
247 }
248 }
249 }
250 return $columns;
251 }
252
253 public function tab_submissions_list( Form $form ) {
254 if ( ! empty( $_GET['submission_id'] ) ) {
255 return;
256 }
257
258 $submissions = hf_get_form_submissions( $form->ID );
259 $columns = $this->get_submission_columns( $submissions );
260 $hidden_columns = get_hidden_columns( get_current_screen() );
261
262 require dirname( $this->plugin_file ) . '/views/tab-submissions-list.php';
263 }
264
265 public function tab_submissions_detail( Form $form ) {
266 if ( empty( $_GET['submission_id'] ) ) {
267 return;
268 }
269
270 $submission = hf_get_form_submission( (int) $_GET['submission_id'] );
271 require dirname( $this->plugin_file ) . '/views/tab-submissions-detail.php';
272 }
273
274
275 public function process_create_form() {
276 // Fix for MultiSite stripping KSES for roles other than administrator
277 remove_all_filters( 'content_save_pre' );
278
279 $data = $_POST['form'];
280 $form_title = sanitize_text_field( $data['title'] );
281 $form_id = wp_insert_post(
282 array(
283 'post_type' => 'html-form',
284 'post_status' => 'publish',
285 'post_title' => $form_title,
286 'post_content' => $this->get_default_form_content(),
287 )
288 );
289
290 wp_safe_redirect( admin_url( 'admin.php?page=html-forms&view=edit&form_id=' . $form_id ) );
291 exit;
292 }
293
294 public function process_save_form() {
295 $form_id = (int) $_POST['form_id'];
296 $form = hf_get_form( $form_id );
297 $data = $_POST['form'];
298
299 // Fix for MultiSite stripping KSES for roles other than administrator
300 remove_all_filters( 'content_save_pre' );
301
302 // strip <form> tag from markup
303 $data['markup'] = preg_replace( '/<\/?form(.|\s)*?>/i', '', $data['markup'] );
304
305 $form_id = wp_insert_post(
306 array(
307 'ID' => $form_id,
308 'post_type' => 'html-form',
309 'post_status' => 'publish',
310 'post_title' => sanitize_text_field( $data['title'] ),
311 'post_content' => $data['markup'],
312 'post_name' => sanitize_title_with_dashes( $data['slug'] ),
313 )
314 );
315
316 if ( ! empty( $data['settings'] ) ) {
317 update_post_meta( $form_id, '_hf_settings', $data['settings'] );
318 }
319
320 // save form messages in individual meta keys
321 foreach ( $data['messages'] as $key => $message ) {
322 update_post_meta( $form_id, 'hf_message_' . $key, $message );
323 }
324
325 $redirect_url_args = array(
326 'form_id' => $form_id,
327 'saved' => 1,
328 );
329 $redirect_url = add_query_arg( $redirect_url_args, admin_url( 'admin.php?page=html-forms&view=edit' ) );
330 wp_safe_redirect( $redirect_url );
331 exit;
332 }
333
334 /**
335 * Get URL for a tab on the current page.
336 *
337 * @since 3.0
338 * @internal
339 * @param $tab
340 * @return string
341 */
342 public function get_tab_url( $tab ) {
343 return add_query_arg( array( 'tab' => $tab ), remove_query_arg( 'tab' ) );
344 }
345
346 /**
347 * @return array
348 */
349 public function get_available_form_actions() {
350 $actions = array();
351
352 /**
353 * Filters the available form actions
354 *
355 * @param array $actions
356 */
357 $actions = apply_filters( 'hf_available_form_actions', $actions );
358
359 return $actions;
360 }
361
362 public function process_bulk_delete_submissions() {
363 global $wpdb;
364
365 if ( empty( $_POST['id'] ) ) {
366 return;
367 }
368
369 $ids = $_POST['id'];
370 $table = $wpdb->prefix . 'hf_submissions';
371 $ids = join( ',', array_map( 'esc_sql', $ids ) );
372 $wpdb->query( sprintf( "DELETE FROM {$table} WHERE id IN( %s );", $ids ) );
373 $wpdb->query( sprintf( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN ( %s ) AND meta_key LIKE '_hf_%%';", $ids ) );
374 }
375
376 private function get_default_form_content() {
377 $html = '';
378 $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;
379 $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;
380 $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;
381 $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;
382 $html .= sprintf( "<p>\n\t<input type=\"submit\" value=\"%s\" />\n</p>", __( 'Send', 'html-forms' ) );
383 return $html;
384 }
385
386 }
387