PluginProbe
HTML Forms – Simple WordPress Forms Plugin / 1.1.2
HTML Forms – Simple WordPress Forms Plugin v1.1.2
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.1.2, at src/Admin/Admin.php

315 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( 'init', array( $this, 'listen' ) );
28 add_action( 'init', array( $this, 'run_migrations' ) );
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 'p' => $form->ID,
156 ), get_option( 'home' ) );
157 require dirname( $this->plugin_file ) . '/views/tab-fields.php';
158 }
159
160
161 public function tab_messages( Form $form ) {
162 require dirname( $this->plugin_file ) . '/views/tab-messages.php';
163 }
164
165
166 public function tab_settings( Form $form ) {
167 require dirname( $this->plugin_file ) . '/views/tab-settings.php';
168 }
169
170
171 public function tab_actions( Form $form ) {
172 require dirname( $this->plugin_file ) . '/views/tab-actions.php';
173 }
174
175
176 public function tab_submissions_list( Form $form ) {
177 if( ! empty( $_GET['submission_id'] ) ) {
178 return;
179 }
180
181 $submissions = hf_get_form_submissions( $form->ID );
182
183 // create array of columns for submissions tab
184 $columns = array();
185 foreach( $submissions as $s ) {
186 if( ! is_array( $s->data ) ) {
187 continue;
188 }
189
190 foreach( $s->data as $field => $value ) {
191 if (!array_key_exists($field, $columns)) {
192 $columns[$field] = true;
193 }
194 }
195 }
196 $columns = array_keys( $columns );
197
198 require dirname( $this->plugin_file ) . '/views/tab-submissions-list.php';
199 }
200
201 public function tab_submissions_detail( Form $form ) {
202 if( empty( $_GET['submission_id'] ) ) {
203 return;
204 }
205
206 $submission = hf_get_form_submission( (int) $_GET['submission_id'] );
207 require dirname( $this->plugin_file ) . '/views/tab-submissions-detail.php';
208 }
209
210
211 public function process_create_form() {
212 // Fix for MultiSite stripping KSES for roles other than administrator
213 remove_all_filters( 'content_save_pre' );
214
215 $data = $_POST['form'];
216 $form_title = sanitize_text_field( $data['title'] );
217 $form_id = wp_insert_post(
218 array(
219 'post_type' => 'html-form',
220 'post_status' => 'publish',
221 'post_title' => $form_title,
222 'post_content' => $this->get_default_form_content(),
223 )
224 );
225
226 wp_safe_redirect( admin_url( 'admin.php?page=html-forms&view=edit&form_id=' . $form_id ));
227 exit;
228 }
229
230 public function process_save_form() {
231 $form_id = (int) $_POST['form_id'];
232 $form = hf_get_form( $form_id );
233 $data = $_POST['form'];
234
235 // Fix for MultiSite stripping KSES for roles other than administrator
236 remove_all_filters( 'content_save_pre' );
237
238 // strip <form> tag from markup
239 $data['markup'] = preg_replace( '/<\/?form(.|\s)*?>/i', '', $data['markup'] );
240
241 $form_id = wp_insert_post( array(
242 'ID' => $form_id,
243 'post_type' => 'html-form',
244 'post_status' => 'publish',
245 'post_title' => sanitize_text_field( $data['title'] ),
246 'post_content' => $data['markup'],
247 'post_name' => sanitize_title_with_dashes( $data['slug'] ),
248 ) );
249
250 if( ! empty( $data['settings'] ) ) {
251 update_post_meta( $form_id, '_hf_settings', $data['settings'] );
252 }
253
254 // save form messages in individual meta keys
255 foreach( $data['messages'] as $key => $message ) {
256 update_post_meta( $form_id, 'hf_message_' . $key, $message );
257 }
258
259 $redirect_url = add_query_arg( array( 'form_id' => $form_id, 'saved' => 1 ), admin_url ('admin.php?page=html-forms&view=edit' ) );
260 wp_safe_redirect( $redirect_url );
261 exit;
262 }
263
264 /**
265 * Get URL for a tab on the current page.
266 *
267 * @since 3.0
268 * @internal
269 * @param $tab
270 * @return string
271 */
272 public function get_tab_url( $tab ) {
273 return add_query_arg( array( 'tab' => $tab ), remove_query_arg( 'tab' ) );
274 }
275
276 /**
277 * @return array
278 */
279 public function get_available_form_actions() {
280 $actions = array();
281
282 /**
283 * Filters the available form actions
284 *
285 * @param array $actions
286 */
287 $actions = apply_filters( 'hf_available_form_actions', $actions );
288
289 return $actions;
290 }
291
292 public function process_bulk_delete_submissions() {
293 global $wpdb;
294
295 if( empty( $_POST['id'] ) ) {
296 return;
297 }
298
299 $table = $wpdb->prefix .'hf_submissions';
300 $ids = join( ',', array_map( 'esc_sql', $_POST['id'] ) );
301 $wpdb->query( sprintf( "DELETE FROM {$table} WHERE id IN( %s );", $ids ) );
302 }
303
304 private function get_default_form_content() {
305 $html = '';
306 $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;
307 $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;
308 $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;
309 $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;
310 $html .= sprintf( "<p>\n\t<input type=\"submit\" value=\"%s\" />\n</p>", __( 'Send', 'html-forms' ) );
311 return $html;
312 }
313
314 }
315