PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.3.0
HTML Forms – Simple WordPress Forms Plugin v1.3.0
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 / Admin.php

Admin.php in HTML Forms – Simple WordPress Forms Plugin 1.3.0, at src/Admin/Admin.php

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