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

389 lines 13.3 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 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
128
129 wp_enqueue_style( 'html-forms-admin', plugins_url( 'assets/css/admin' . $suffix . '.css', $this->plugin_file ), array(), HTML_FORMS_VERSION );
130 wp_enqueue_script( 'html-forms-admin', plugins_url( 'assets/js/admin' . $suffix . '.js', $this->plugin_file ), array(), HTML_FORMS_VERSION, true );
131 wp_localize_script(
132 'html-forms-admin',
133 'hf_options',
134 array(
135 'page' => $_GET['page'],
136 'view' => empty( $_GET['view'] ) ? '' : $_GET['view'],
137 'form_id' => empty( $_GET['form_id'] ) ? 0 : (int) $_GET['form_id'],
138 )
139 );
140 }
141
142 public function menu() {
143 $capability = 'edit_forms';
144 $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)"
145 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>';
146 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' );
147 add_submenu_page( 'html-forms', __( 'Forms', 'html-forms' ), __( 'All Forms', 'html-forms' ), $capability, 'html-forms', array( $this, 'page_overview' ) );
148 add_submenu_page( 'html-forms', __( 'Add new form', 'html-forms' ), __( 'Add New', 'html-forms' ), $capability, 'html-forms-add-form', array( $this, 'page_new_form' ) );
149 add_submenu_page( 'html-forms', __( 'Settings', 'html-forms' ), __( 'Settings', 'html-forms' ), $capability, 'html-forms-settings', array( $this, 'page_settings' ) );
150
151 if ( ! defined( 'HF_PREMIUM_VERSION' ) ) {
152 add_submenu_page( 'html-forms', 'Premium', '<span style="color: #ea6ea6;">Premium</span>', $capability, 'html-forms-premium', array( $this, 'page_premium' ) );
153 }
154 }
155
156 public function add_screen_options() {
157 // only run on the submissions overview page (not detail)
158 if ( empty( $_GET['page'] ) || $_GET['page'] !== 'html-forms' || empty( $_GET['view'] ) || $_GET['view'] !== 'edit' || empty( $_GET['form_id'] ) || ! empty( $_GET['submission_id'] ) ) {
159 return;
160 }
161
162 // don't run if form does not have submissions enabled
163 $form = hf_get_form( $_GET['form_id'] );
164 if ( ! $form->settings['save_submissions'] ) {
165 return;
166 }
167
168 // tell screen options to show columns option
169 $submissions = hf_get_form_submissions( $_GET['form_id'] );
170 $columns = $this->get_submission_columns( $submissions );
171 add_filter(
172 'manage_toplevel_page_html-forms_columns',
173 function( $unused ) use ( $columns ) {
174 return $columns;
175 }
176 );
177 add_screen_option( 'layout_columns' );
178 }
179
180 public function page_overview() {
181 if ( ! empty( $_GET['view'] ) && $_GET['view'] === 'edit' ) {
182 $this->page_edit_form();
183 return;
184 }
185
186 $settings = hf_get_settings();
187
188 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
189 $table = new Table( $settings );
190
191 require dirname( $this->plugin_file ) . '/views/page-overview.php';
192 }
193
194 public function page_new_form() {
195 require dirname( $this->plugin_file ) . '/views/page-add-form.php';
196 }
197
198 public function page_settings() {
199 $settings = hf_get_settings();
200 require dirname( $this->plugin_file ) . '/views/page-global-settings.php';
201 }
202
203 public function page_premium() {
204 require dirname( $this->plugin_file ) . '/views/page-premium.php';
205 }
206
207 public function page_edit_form() {
208 $active_tab = ! empty( $_GET['tab'] ) ? $_GET['tab'] : 'fields';
209 $form_id = (int) $_GET['form_id'];
210 $form = hf_get_form( $form_id );
211 $settings = hf_get_settings();
212 require dirname( $this->plugin_file ) . '/views/page-edit-form.php';
213 }
214
215 public function tab_fields( Form $form ) {
216 $form_preview_url = add_query_arg(
217 array(
218 'hf_preview_form' => $form->ID,
219 ),
220 site_url( '/', 'admin' )
221 );
222 require dirname( $this->plugin_file ) . '/views/tab-fields.php';
223 }
224
225 public function tab_messages( Form $form ) {
226 require dirname( $this->plugin_file ) . '/views/tab-messages.php';
227 }
228
229
230 public function tab_settings( Form $form ) {
231 require dirname( $this->plugin_file ) . '/views/tab-settings.php';
232 }
233
234
235 public function tab_actions( Form $form ) {
236 require dirname( $this->plugin_file ) . '/views/tab-actions.php';
237 }
238
239 public function get_submission_columns( array $submissions ) {
240 $columns = array();
241 foreach ( $submissions as $s ) {
242 if ( ! is_array( $s->data ) ) {
243 continue;
244 }
245
246 foreach ( $s->data as $field => $value ) {
247 if ( ! isset( $columns[ $field ] ) ) {
248 $columns[ $field ] = esc_html( ucfirst( strtolower( str_replace( '_', ' ', $field ) ) ) );
249 }
250 }
251 }
252 return $columns;
253 }
254
255 public function tab_submissions_list( Form $form ) {
256 if ( ! empty( $_GET['submission_id'] ) ) {
257 return;
258 }
259
260 $submissions = hf_get_form_submissions( $form->ID );
261 $columns = $this->get_submission_columns( $submissions );
262 $hidden_columns = get_hidden_columns( get_current_screen() );
263
264 require dirname( $this->plugin_file ) . '/views/tab-submissions-list.php';
265 }
266
267 public function tab_submissions_detail( Form $form ) {
268 if ( empty( $_GET['submission_id'] ) ) {
269 return;
270 }
271
272 $submission = hf_get_form_submission( (int) $_GET['submission_id'] );
273 require dirname( $this->plugin_file ) . '/views/tab-submissions-detail.php';
274 }
275
276
277 public function process_create_form() {
278 // Fix for MultiSite stripping KSES for roles other than administrator
279 remove_all_filters( 'content_save_pre' );
280
281 $data = $_POST['form'];
282 $form_title = sanitize_text_field( $data['title'] );
283 $form_id = wp_insert_post(
284 array(
285 'post_type' => 'html-form',
286 'post_status' => 'publish',
287 'post_title' => $form_title,
288 'post_content' => $this->get_default_form_content(),
289 )
290 );
291
292 wp_safe_redirect( admin_url( 'admin.php?page=html-forms&view=edit&form_id=' . $form_id ) );
293 exit;
294 }
295
296 public function process_save_form() {
297 $form_id = (int) $_POST['form_id'];
298 $form = hf_get_form( $form_id );
299 $data = $_POST['form'];
300
301 // Fix for MultiSite stripping KSES for roles other than administrator
302 remove_all_filters( 'content_save_pre' );
303
304 // strip <form> tag from markup
305 $data['markup'] = preg_replace( '/<\/?form(.|\s)*?>/i', '', $data['markup'] );
306
307 $form_id = wp_insert_post(
308 array(
309 'ID' => $form_id,
310 'post_type' => 'html-form',
311 'post_status' => 'publish',
312 'post_title' => sanitize_text_field( $data['title'] ),
313 'post_content' => $data['markup'],
314 'post_name' => sanitize_title_with_dashes( $data['slug'] ),
315 )
316 );
317
318 if ( ! empty( $data['settings'] ) ) {
319 update_post_meta( $form_id, '_hf_settings', $data['settings'] );
320 }
321
322 // save form messages in individual meta keys
323 foreach ( $data['messages'] as $key => $message ) {
324 update_post_meta( $form_id, 'hf_message_' . $key, $message );
325 }
326
327 $redirect_url_args = array(
328 'form_id' => $form_id,
329 'saved' => 1,
330 );
331 $redirect_url = add_query_arg( $redirect_url_args, admin_url( 'admin.php?page=html-forms&view=edit' ) );
332 wp_safe_redirect( $redirect_url );
333 exit;
334 }
335
336 /**
337 * Get URL for a tab on the current page.
338 *
339 * @since 3.0
340 * @internal
341 * @param $tab
342 * @return string
343 */
344 public function get_tab_url( $tab ) {
345 return add_query_arg( array( 'tab' => $tab ), remove_query_arg( 'tab' ) );
346 }
347
348 /**
349 * @return array
350 */
351 public function get_available_form_actions() {
352 $actions = array();
353
354 /**
355 * Filters the available form actions
356 *
357 * @param array $actions
358 */
359 $actions = apply_filters( 'hf_available_form_actions', $actions );
360
361 return $actions;
362 }
363
364 public function process_bulk_delete_submissions() {
365 global $wpdb;
366
367 if ( empty( $_POST['id'] ) ) {
368 return;
369 }
370
371 $ids = $_POST['id'];
372 $table = $wpdb->prefix . 'hf_submissions';
373 $ids = join( ',', array_map( 'esc_sql', $ids ) );
374 $wpdb->query( sprintf( "DELETE FROM {$table} WHERE id IN( %s );", $ids ) );
375 $wpdb->query( sprintf( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN ( %s ) AND meta_key LIKE '_hf_%%';", $ids ) );
376 }
377
378 private function get_default_form_content() {
379 $html = '';
380 $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;
381 $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;
382 $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;
383 $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;
384 $html .= sprintf( "<p>\n\t<input type=\"submit\" value=\"%s\" />\n</p>", __( 'Send', 'html-forms' ) );
385 return $html;
386 }
387
388 }
389