PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.7
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.7
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.7, at admin/admin.php

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