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

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