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

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