PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.2
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.2
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 All 96 releases
sureforms / admin / admin.php

admin.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 0.0.2, at admin/admin.php

514 lines 17.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin Class.
4 *
5 * @package sureforms.
6 */
7
8 namespace SRFM\Admin;
9
10 use SRFM\Inc\Traits\Get_Instance;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15 /**
16 * Admin handler class.
17 *
18 * @since 0.0.1
19 */
20 class Admin {
21
22 use Get_Instance;
23
24 /**
25 * Class constructor.
26 *
27 * @return void
28 * @since 0.0.1
29 */
30 public function __construct() {
31 add_action( 'admin_menu', [ $this, 'add_menu_page' ], 9 );
32 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
33 add_action( 'admin_menu', [ $this, 'settings_page' ] );
34 add_action( 'admin_menu', [ $this, 'add_new_form' ] );
35 add_filter( 'plugin_action_links', [ $this, 'add_settings_link' ], 10, 2 );
36 add_action( 'enqueue_block_assets', [ $this, 'enqueue_styles' ] );
37 add_action( 'admin_head', [ $this, 'enqueue_header_styles' ] );
38 add_action( 'admin_body_class', [ $this, 'admin_template_picker_body_class' ] );
39
40 // this action is used to restrict Spectra's quick action bar on SureForms CPTS.
41 add_action( 'uag_enable_quick_action_sidebar', [ $this, 'restrict_spectra_quick_action_bar' ] );
42 }
43
44 /**
45 * Sureforms editor header styles.
46 *
47 * @since 0.0.1
48 */
49 public function enqueue_header_styles() {
50 $current_screen = get_current_screen();
51 $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min';
52 $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified';
53
54 $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/';
55
56 /* RTL */
57 if ( is_rtl() ) {
58 $file_prefix .= '-rtl';
59 }
60
61 if ( 'sureforms_form' === $current_screen->id ) {
62 wp_enqueue_style( SRFM_SLUG . '-editor-header-styles', $css_uri . 'header-styles' . $file_prefix . '.css', [], SRFM_VER );
63 }
64 }
65
66 /**
67 * Add menu page.
68 *
69 * @return void
70 * @since 0.0.1
71 */
72 public function add_menu_page() {
73 if ( ! current_user_can( 'manage_options' ) ) {
74 return;
75 }
76
77 $capability = 'manage_options';
78 $menu_slug = 'sureforms_menu';
79
80 $logo = file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/icon.svg' );
81 add_menu_page(
82 __( 'SureForms', 'sureforms' ),
83 __( 'SureForms', 'sureforms' ),
84 'edit_others_posts',
85 $menu_slug,
86 function () {},
87 'data:image/svg+xml;base64,' . base64_encode( $logo ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
88 30
89 );
90
91 // Add the Dashboard Submenu.
92 add_submenu_page(
93 $menu_slug,
94 __( 'Dashboard', 'sureforms' ),
95 __( 'Dashboard', 'sureforms' ),
96 $capability,
97 $menu_slug,
98 [ $this, 'render_dashboard' ]
99 );
100 }
101
102 /**
103 * Add Settings page.
104 *
105 * @return void
106 * @since 0.0.1
107 */
108 public function settings_page() {
109 $callback = [ $this, 'settings_page_callback' ];
110 add_submenu_page(
111 'sureforms_menu',
112 __( 'Settings', 'sureforms' ),
113 __( 'Settings', 'sureforms' ),
114 'edit_others_posts',
115 'sureforms_form_settings',
116 $callback
117 );
118
119 // Get the current submenu page.
120 $submenu_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- $_GET['page'] does not provide nonce.
121
122 if ( ! isset( $_GET['tab'] ) && 'sureforms_form_settings' === $submenu_page ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- $_GET['page'] does not provide nonce.
123 wp_safe_redirect( admin_url( 'admin.php?page=sureforms_form_settings&tab=general-settings' ) );
124 exit;
125 }
126 }
127
128 /**
129 * Render Admin Dashboard.
130 *
131 * @return void
132 * @since 0.0.1
133 */
134 public function render_dashboard() {
135 echo '<div id="srfm-dashboard-container"></div>';
136 }
137
138 /**
139 * Settings page callback.
140 *
141 * @return void
142 * @since 0.0.1
143 */
144 public function settings_page_callback() {
145 echo '<div id="srfm-settings-container"></div>';
146 }
147
148 /**
149 * Add new form menu item.
150 *
151 * @return void
152 * @since 0.0.1
153 */
154 public function add_new_form() {
155 add_submenu_page(
156 'sureforms_menu',
157 __( 'New Form', 'sureforms' ),
158 __( 'New Form', 'sureforms' ),
159 'edit_others_posts',
160 'add-new-form',
161 [ $this, 'add_new_form_callback' ],
162 2
163 );
164 }
165
166 /**
167 * Add new form mentu item callback.
168 *
169 * @return void
170 * @since 0.0.1
171 */
172 public function add_new_form_callback() {
173 echo '<div id="srfm-add-new-form-container"></div>';
174 }
175
176 /**
177 * Adds a settings link to the plugin action links on the plugins page.
178 *
179 * @param array $links An array of plugin action links.
180 * @param string $file The plugin file path.
181 * @return array The updated array of plugin action links.
182 * @since 0.0.1
183 */
184 public function add_settings_link( $links, $file ) {
185 if ( 'sureforms/sureforms.php' === $file ) {
186 $settings_link = '<a href="' . esc_url( admin_url( 'admin.php?page=sureforms_form_settings&tab=general-settings' ) ) . '">' . esc_html__( 'Settings', 'sureforms' ) . '</a>';
187 array_push( $links, $settings_link );
188 }
189 return $links;
190 }
191
192 /**
193 * Sureforms block editor styles.
194 *
195 * @since 0.0.1
196 */
197 public function enqueue_styles() {
198 $current_screen = get_current_screen();
199
200 $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min';
201 $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified';
202
203 $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/';
204 $vendor_css_uri = SRFM_URL . 'assets/css/minified/deps/';
205
206 /* RTL */
207 if ( is_rtl() ) {
208 $file_prefix .= '-rtl';
209 }
210
211 // Enqueue editor styles for post and page.
212 if ( SRFM_FORMS_POST_TYPE === $current_screen->post_type ) {
213 wp_enqueue_style( SRFM_SLUG . '-editor', $css_uri . 'backend/editor' . $file_prefix . '.css', [], SRFM_VER );
214 wp_enqueue_style( SRFM_SLUG . '-backend-blocks', $css_uri . 'blocks/default/backend' . $file_prefix . '.css', [], SRFM_VER );
215 wp_enqueue_style( SRFM_SLUG . '-intl', $vendor_css_uri . 'intl/intlTelInput-backend.min.css', [], SRFM_VER );
216 wp_enqueue_style( SRFM_SLUG . '-common', $css_uri . 'common' . $file_prefix . '.css', [], SRFM_VER );
217 wp_enqueue_style( SRFM_SLUG . '-reactQuill', $vendor_css_uri . 'quill/quill.snow.css', [], SRFM_VER );
218 }
219
220 wp_enqueue_style( SRFM_SLUG . '-form-selector', $css_uri . 'srfm-form-selector' . $file_prefix . '.css', [], SRFM_VER );
221 wp_enqueue_style( SRFM_SLUG . '-common-editor', SRFM_URL . 'assets/build/common-editor.css', [], SRFM_VER, 'all' );
222 }
223
224 /**
225 * Get Breadcrumbs for current page.
226 *
227 * @since 0.0.1
228 * @return array Breadcrumbs Array.
229 */
230 public function get_breadcrumbs_for_current_page() {
231 global $post, $pagenow;
232 $breadcrumbs = [];
233
234 if ( 'admin.php' === $pagenow && isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
235 $page_title = get_admin_page_title();
236 $breadcrumbs[] = [
237 'title' => $page_title,
238 'link' => '',
239 ];
240 } elseif ( $post && in_array( $pagenow, [ 'post.php', 'post-new.php', 'edit.php' ], true ) ) {
241 $post_type_obj = get_post_type_object( get_post_type() );
242 if ( $post_type_obj ) {
243 $post_type_plural = $post_type_obj->labels->name;
244 $breadcrumbs[] = [
245 'title' => $post_type_plural,
246 'link' => admin_url( 'edit.php?post_type=' . $post_type_obj->name ),
247 ];
248
249 if ( 'edit.php' === $pagenow && ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
250 $breadcrumbs[ count( $breadcrumbs ) - 1 ]['link'] = '';
251 } else {
252 $breadcrumbs[] = [
253 /* Translators: Post Title. */
254 'title' => sprintf( __( 'Edit %1$s', 'sureforms' ), get_the_title() ),
255 'link' => get_edit_post_link( $post->ID ),
256 ];
257 }
258 }
259 } else {
260 $current_screen = get_current_screen();
261 if ( $current_screen && 'sureforms_form' === $current_screen->post_type ) {
262 $breadcrumbs[] = [
263 'title' => 'Forms',
264 'link' => '',
265 ];
266 } elseif ( $current_screen && 'sureforms_entry' === $current_screen->post_type ) {
267 $breadcrumbs[] = [
268 'title' => 'Entries',
269 'link' => '',
270 ];
271 } else {
272 $breadcrumbs[] = [
273 'title' => '',
274 'link' => '',
275 ];
276 }
277 }
278
279 return $breadcrumbs;
280 }
281
282
283 /**
284 * Enqueue Admin Scripts.
285 *
286 * @return void
287 * @since 0.0.1
288 */
289 public function enqueue_scripts() {
290 $current_screen = get_current_screen();
291
292 $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min';
293 $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified';
294 $js_uri = SRFM_URL . 'assets/js/' . $dir_name . '/';
295 $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/';
296
297 /* RTL */
298 if ( is_rtl() ) {
299 $file_prefix .= '-rtl';
300 }
301
302 if ( SRFM_FORMS_POST_TYPE === $current_screen->post_type || 'toplevel_page_sureforms_menu' === $current_screen->base || SRFM_ENTRIES_POST_TYPE === $current_screen->post_type
303 || 'sureforms_page_sureforms_form_settings' === $current_screen->id
304 ) {
305 $asset_handle = '-dashboard';
306
307 wp_enqueue_style( SRFM_SLUG . $asset_handle . '-font', 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500&display=swap', [], SRFM_VER );
308
309 $script_asset_path = SRFM_DIR . 'assets/build/dashboard.asset.php';
310 $script_info = file_exists( $script_asset_path )
311 ? include $script_asset_path
312 : [
313 'dependencies' => [],
314 'version' => SRFM_VER,
315 ];
316 wp_enqueue_script( SRFM_SLUG . $asset_handle, SRFM_URL . 'assets/build/dashboard.js', $script_info['dependencies'], SRFM_VER, true );
317
318 wp_localize_script( SRFM_SLUG . $asset_handle, 'scIcons', [ 'path' => SRFM_URL . 'assets/build/icon-assets' ] );
319 wp_localize_script(
320 SRFM_SLUG . $asset_handle,
321 SRFM_SLUG . '_admin',
322 apply_filters(
323 SRFM_SLUG . '_admin_filter',
324 [
325 'site_url' => get_site_url(),
326 'breadcrumbs' => $this->get_breadcrumbs_for_current_page(),
327 'sureforms_dashboard_url' => admin_url( '/admin.php?page=sureforms_menu' ),
328 'plugin_version' => SRFM_VER,
329 'global_settings_nonce' => ( current_user_can( 'manage_options' ) ) ? wp_create_nonce( 'wp_rest' ) : '',
330 'security_settings_url' => admin_url( '/admin.php?page=sureforms_form_settings&tab=security-settings' ),
331 ]
332 )
333 );
334 wp_enqueue_style( SRFM_SLUG . '-dashboard', SRFM_URL . 'assets/build/dashboard.css', [], SRFM_VER, 'all' );
335
336 }
337
338 if ( 'sureforms_page_sureforms_form_settings' === $current_screen->id ) {
339 wp_enqueue_style( SRFM_SLUG . '-settings', $css_uri . 'backend/settings' . $file_prefix . '.css', [], SRFM_VER );
340 }
341
342 // Admin Submenu Styles.
343 wp_enqueue_style( SRFM_SLUG . '-admin', $css_uri . 'backend/admin' . $file_prefix . '.css', [], SRFM_VER );
344 wp_enqueue_style( SRFM_SLUG . '-single-form-modal', $css_uri . 'single-form-setting' . $file_prefix . '.css', [], SRFM_VER );
345
346 if ( 'edit-' . SRFM_FORMS_POST_TYPE === $current_screen->id || 'edit-' . SRFM_ENTRIES_POST_TYPE === $current_screen->id ) {
347 $asset_handle = 'page_header';
348
349 $script_asset_path = SRFM_DIR . 'assets/build/' . $asset_handle . '.asset.php';
350 $script_info = file_exists( $script_asset_path )
351 ? include $script_asset_path
352 : [
353 'dependencies' => [],
354 'version' => SRFM_VER,
355 ];
356 wp_enqueue_script( SRFM_SLUG . '-form-page-header', SRFM_URL . 'assets/build/' . $asset_handle . '.js', $script_info['dependencies'], SRFM_VER, true );
357 wp_enqueue_style( SRFM_SLUG . '-form-archive-styles', $css_uri . 'form-archive-styles' . $file_prefix . '.css', [], SRFM_VER );
358 }
359 if ( 'sureforms_page_' . SRFM_FORMS_POST_TYPE . '_settings' === $current_screen->base ) {
360 $asset_handle = 'settings';
361
362 $script_asset_path = SRFM_DIR . 'assets/build/' . $asset_handle . '.asset.php';
363 $script_info = file_exists( $script_asset_path )
364 ? include $script_asset_path
365 : [
366 'dependencies' => [],
367 'version' => SRFM_VER,
368 ];
369 wp_enqueue_script( SRFM_SLUG . '-settings', SRFM_URL . 'assets/build/' . $asset_handle . '.js', $script_info['dependencies'], SRFM_VER, true );
370 wp_enqueue_style( SRFM_SLUG . '-setting-styles', SRFM_URL . 'assets/build/' . $asset_handle . '.css', [ 'wp-components' ], SRFM_VER, 'all' );
371 wp_localize_script(
372 SRFM_SLUG . '-settings',
373 SRFM_SLUG . '_admin',
374 [
375 'site_url' => get_site_url(),
376 'breadcrumbs' => $this->get_breadcrumbs_for_current_page(),
377 'sureforms_dashboard_url' => admin_url( '/admin.php?page=sureforms_menu' ),
378 'plugin_version' => SRFM_VER,
379 'global_settings_nonce' => current_user_can( 'manage_options' ) ? wp_create_nonce( 'wp_rest' ) : '',
380 ]
381 );
382 }
383 if ( 'edit-' . SRFM_FORMS_POST_TYPE === $current_screen->id ) {
384 wp_enqueue_script( SRFM_SLUG . '-form-archive', $js_uri . 'form-archive' . $file_prefix . '.js', [], SRFM_VER, true );
385 wp_enqueue_script( SRFM_SLUG . '-export', $js_uri . 'export' . $file_prefix . '.js', [], SRFM_VER, true );
386 wp_localize_script(
387 SRFM_SLUG . '-export',
388 SRFM_SLUG . '_export',
389 [
390 'ajaxurl' => admin_url( 'admin-ajax.php' ),
391 'srfm_export_nonce' => wp_create_nonce( 'export_form_nonce' ),
392 'site_url' => get_site_url(),
393 'srfm_import_endpoint' => '/wp-json/sureforms/v1/sureforms_import',
394 'import_form_nonce' => ( current_user_can( 'edit_posts' ) ) ? wp_create_nonce( 'wp_rest' ) : '',
395 ]
396 );
397
398 wp_enqueue_script( SRFM_SLUG . '-backend', $js_uri . 'backend' . $file_prefix . '.js', [], SRFM_VER, true );
399 wp_localize_script(
400 SRFM_SLUG . '-backend',
401 SRFM_SLUG . '_backend',
402 [
403 'site_url' => get_site_url(),
404 ]
405 );
406
407 }
408
409 if ( 'sureforms_page_add-new-form' === $current_screen->id ) {
410
411 $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min';
412 $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified';
413
414 $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/';
415
416 /* RTL */
417 if ( is_rtl() ) {
418 $file_prefix .= '-rtl';
419 }
420
421 wp_enqueue_style( SRFM_SLUG . '-template-picker', $css_uri . 'template-picker' . $file_prefix . '.css', [], SRFM_VER );
422
423 $sureforms_admin = 'templatePicker';
424
425 $script_asset_path = SRFM_DIR . 'assets/build/' . $sureforms_admin . '.asset.php';
426 $script_info = file_exists( $script_asset_path )
427 ? include $script_asset_path
428 : [
429 'dependencies' => [],
430 'version' => SRFM_VER,
431 ];
432 wp_enqueue_script( SRFM_SLUG . '-template-picker', SRFM_URL . 'assets/build/' . $sureforms_admin . '.js', $script_info['dependencies'], SRFM_VER, true );
433
434 wp_localize_script(
435 SRFM_SLUG . '-template-picker',
436 SRFM_SLUG . '_admin',
437 [
438 'site_url' => get_site_url(),
439 'plugin_url' => SRFM_URL,
440 'preview_images_url' => SRFM_URL . 'images/template-previews/',
441 'admin_url' => admin_url( 'admin.php' ),
442 'new_template_picker_base_url' => admin_url( 'post-new.php?post_type=sureforms_form' ),
443 'capability' => current_user_can( 'edit_posts' ),
444 'template_picker_nonce' => current_user_can( 'edit_posts' ) ? wp_create_nonce( 'wp_rest' ) : '',
445 'is_pro_active' => defined( 'SRFM_PRO_VER' ),
446 ]
447 );
448 }
449 // Quick action sidebar.
450 $default_allowed_quick_sidebar_blocks = apply_filters(
451 'srfm_quick_sidebar_allowed_blocks',
452 [
453 'srfm/input',
454 'srfm/email',
455 'srfm/textarea',
456 'srfm/number',
457 'srfm/address',
458 ]
459 );
460 if ( ! is_array( $default_allowed_quick_sidebar_blocks ) ) {
461 $default_allowed_quick_sidebar_blocks = [];
462 }
463
464 $srfm_enable_quick_action_sidebar = get_option( 'srfm_enable_quick_action_sidebar' );
465 if ( ! $srfm_enable_quick_action_sidebar ) {
466 $srfm_enable_quick_action_sidebar = 'disabled';
467 }
468 $quick_sidebar_allowed_blocks = get_option( 'srfm_quick_sidebar_allowed_blocks' );
469 $quick_sidebar_allowed_blocks = ! empty( $quick_sidebar_allowed_blocks ) && is_array( $quick_sidebar_allowed_blocks ) ? $quick_sidebar_allowed_blocks : $default_allowed_quick_sidebar_blocks;
470 $srfm_ajax_nonce = wp_create_nonce( 'srfm_ajax_nonce' );
471 wp_enqueue_script( SRFM_SLUG . '-quick-action-siderbar', SRFM_URL . 'assets/build/quickActionSidebar.js', [], SRFM_VER, true );
472 wp_localize_script(
473 SRFM_SLUG . '-quick-action-siderbar',
474 SRFM_SLUG . '_quick_sidebar_blocks',
475 [
476 'allowed_blocks' => $quick_sidebar_allowed_blocks,
477 'srfm_enable_quick_action_sidebar' => $srfm_enable_quick_action_sidebar,
478 'srfm_ajax_nonce' => $srfm_ajax_nonce,
479 'srfm_ajax_url' => admin_url( 'admin-ajax.php' ),
480 ]
481 );
482 }
483
484 /**
485 * Form Template Picker Admin Body Classes
486 *
487 * @since 0.0.1
488 * @param string $classes Space separated class string.
489 */
490 public function admin_template_picker_body_class( $classes = '' ) {
491 $theme_builder_class = isset( $_GET['page'] ) && 'add-new-form' === $_GET['page'] ? 'srfm-template-picker' : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Fetching a $_GET value, no nonce available to validate.
492 $classes .= ' ' . $theme_builder_class . ' ';
493
494 return $classes;
495
496 }
497
498 /**
499 * Disable spectra's quick action bar in sureforms CPT.
500 *
501 * @param string $status current status of the quick action bar.
502 * @since 0.0.2
503 * @return string
504 */
505 public function restrict_spectra_quick_action_bar( $status ) {
506 $screen = get_current_screen();
507 if ( 'disabled' !== $status && isset( $screen->id ) && 'sureforms_form' === $screen->id ) {
508 $status = 'disabled';
509 }
510
511 return $status;
512 }
513 }
514