PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.1.4
HTML Forms – Simple WordPress Forms Plugin v1.1.4
1.7.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 All 67 releases
html-forms / src / Admin / Admin.php

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

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