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

521 lines 17.2 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 wp_enqueue_style( SRFM_SLUG . '-single-form-modal', $css_uri . 'single-form-setting' . $file_prefix . '.css', [], SRFM_VER );
219 }
220
221 wp_enqueue_style( SRFM_SLUG . '-form-selector', $css_uri . 'srfm-form-selector' . $file_prefix . '.css', [], SRFM_VER );
222 wp_enqueue_style( SRFM_SLUG . '-common-editor', SRFM_URL . 'assets/build/common-editor.css', [], SRFM_VER, 'all' );
223 }
224
225 /**
226 * Get Breadcrumbs for current page.
227 *
228 * @since 0.0.1
229 * @return array Breadcrumbs Array.
230 */
231 public function get_breadcrumbs_for_current_page() {
232 global $post, $pagenow;
233 $breadcrumbs = [];
234
235 if ( 'admin.php' === $pagenow && isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
236 $page_title = get_admin_page_title();
237 $breadcrumbs[] = [
238 'title' => $page_title,
239 'link' => '',
240 ];
241 } elseif ( $post && in_array( $pagenow, [ 'post.php', 'post-new.php', 'edit.php' ], true ) ) {
242 $post_type_obj = get_post_type_object( get_post_type() );
243 if ( $post_type_obj ) {
244 $post_type_plural = $post_type_obj->labels->name;
245 $breadcrumbs[] = [
246 'title' => $post_type_plural,
247 'link' => admin_url( 'edit.php?post_type=' . $post_type_obj->name ),
248 ];
249
250 if ( 'edit.php' === $pagenow && ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
251 $breadcrumbs[ count( $breadcrumbs ) - 1 ]['link'] = '';
252 } else {
253 $breadcrumbs[] = [
254 /* Translators: Post Title. */
255 'title' => sprintf( __( 'Edit %1$s', 'sureforms' ), get_the_title() ),
256 'link' => get_edit_post_link( $post->ID ),
257 ];
258 }
259 }
260 } else {
261 $current_screen = get_current_screen();
262 if ( $current_screen && 'sureforms_form' === $current_screen->post_type ) {
263 $breadcrumbs[] = [
264 'title' => 'Forms',
265 'link' => '',
266 ];
267 } elseif ( $current_screen && 'sureforms_entry' === $current_screen->post_type ) {
268 $breadcrumbs[] = [
269 'title' => 'Entries',
270 'link' => '',
271 ];
272 } else {
273 $breadcrumbs[] = [
274 'title' => '',
275 'link' => '',
276 ];
277 }
278 }
279
280 return $breadcrumbs;
281 }
282
283
284 /**
285 * Enqueue Admin Scripts.
286 *
287 * @return void
288 * @since 0.0.1
289 */
290 public function enqueue_scripts() {
291 $current_screen = get_current_screen();
292
293 $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min';
294 $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified';
295 $js_uri = SRFM_URL . 'assets/js/' . $dir_name . '/';
296 $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/';
297
298 /* RTL */
299 if ( is_rtl() ) {
300 $file_prefix .= '-rtl';
301 }
302
303 $localization_data = [
304 'site_url' => get_site_url(),
305 'breadcrumbs' => $this->get_breadcrumbs_for_current_page(),
306 'sureforms_dashboard_url' => admin_url( '/admin.php?page=sureforms_menu' ),
307 'plugin_version' => SRFM_VER,
308 'global_settings_nonce' => current_user_can( 'manage_options' ) ? wp_create_nonce( 'wp_rest' ) : '',
309 'is_pro_active' => defined( 'SRFM_PRO_VER' ),
310 'pro_plugin_version' => defined( 'SRFM_PRO_VER' ) ? SRFM_PRO_VER : '',
311 ];
312
313 if ( class_exists( 'SRFM_PRO\Admin\Licensing' ) ) {
314 $license_active = \SRFM_PRO\Admin\Licensing::is_license_active();
315 $localization_data['is_license_active'] = $license_active;
316 }
317
318 if ( SRFM_FORMS_POST_TYPE === $current_screen->post_type || 'toplevel_page_sureforms_menu' === $current_screen->base || SRFM_ENTRIES_POST_TYPE === $current_screen->post_type
319 || 'sureforms_page_sureforms_form_settings' === $current_screen->id
320 ) {
321 $asset_handle = '-dashboard';
322
323 wp_enqueue_style( SRFM_SLUG . $asset_handle . '-font', 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500&display=swap', [], SRFM_VER );
324
325 $script_asset_path = SRFM_DIR . 'assets/build/dashboard.asset.php';
326 $script_info = file_exists( $script_asset_path )
327 ? include $script_asset_path
328 : [
329 'dependencies' => [],
330 'version' => SRFM_VER,
331 ];
332 wp_enqueue_script( SRFM_SLUG . $asset_handle, SRFM_URL . 'assets/build/dashboard.js', $script_info['dependencies'], SRFM_VER, true );
333
334 wp_localize_script( SRFM_SLUG . $asset_handle, 'scIcons', [ 'path' => SRFM_URL . 'assets/build/icon-assets' ] );
335
336 $localization_data['security_settings_url'] = admin_url( '/admin.php?page=sureforms_form_settings&tab=security-settings' );
337 wp_localize_script(
338 SRFM_SLUG . $asset_handle,
339 SRFM_SLUG . '_admin',
340 apply_filters(
341 SRFM_SLUG . '_admin_filter',
342 $localization_data
343 )
344 );
345 wp_enqueue_style( SRFM_SLUG . '-dashboard', SRFM_URL . 'assets/build/dashboard.css', [], SRFM_VER, 'all' );
346
347 }
348
349 if ( 'sureforms_page_sureforms_form_settings' === $current_screen->id ) {
350 wp_enqueue_style( SRFM_SLUG . '-settings', $css_uri . 'backend/settings' . $file_prefix . '.css', [], SRFM_VER );
351 }
352
353 // Admin Submenu Styles.
354 wp_enqueue_style( SRFM_SLUG . '-admin', $css_uri . 'backend/admin' . $file_prefix . '.css', [], SRFM_VER );
355
356 if ( 'edit-' . SRFM_FORMS_POST_TYPE === $current_screen->id || 'edit-' . SRFM_ENTRIES_POST_TYPE === $current_screen->id ) {
357 $asset_handle = 'page_header';
358
359 $script_asset_path = SRFM_DIR . 'assets/build/' . $asset_handle . '.asset.php';
360 $script_info = file_exists( $script_asset_path )
361 ? include $script_asset_path
362 : [
363 'dependencies' => [],
364 'version' => SRFM_VER,
365 ];
366 wp_enqueue_script( SRFM_SLUG . '-form-page-header', SRFM_URL . 'assets/build/' . $asset_handle . '.js', $script_info['dependencies'], SRFM_VER, true );
367 wp_enqueue_style( SRFM_SLUG . '-form-archive-styles', $css_uri . 'form-archive-styles' . $file_prefix . '.css', [], SRFM_VER );
368 }
369 if ( 'sureforms_page_' . SRFM_FORMS_POST_TYPE . '_settings' === $current_screen->base ) {
370 $asset_handle = 'settings';
371
372 $script_asset_path = SRFM_DIR . 'assets/build/' . $asset_handle . '.asset.php';
373 $script_info = file_exists( $script_asset_path )
374 ? include $script_asset_path
375 : [
376 'dependencies' => [],
377 'version' => SRFM_VER,
378 ];
379
380 wp_enqueue_script( SRFM_SLUG . '-settings', SRFM_URL . 'assets/build/' . $asset_handle . '.js', $script_info['dependencies'], SRFM_VER, true );
381 wp_enqueue_style( SRFM_SLUG . '-setting-styles', SRFM_URL . 'assets/build/' . $asset_handle . '.css', [ 'wp-components' ], SRFM_VER, 'all' );
382 wp_localize_script(
383 SRFM_SLUG . '-settings',
384 SRFM_SLUG . '_admin',
385 $localization_data
386 );
387 }
388 if ( 'edit-' . SRFM_FORMS_POST_TYPE === $current_screen->id ) {
389 wp_enqueue_script( SRFM_SLUG . '-form-archive', $js_uri . 'form-archive' . $file_prefix . '.js', [], SRFM_VER, true );
390 wp_enqueue_script( SRFM_SLUG . '-export', $js_uri . 'export' . $file_prefix . '.js', [], SRFM_VER, true );
391 wp_localize_script(
392 SRFM_SLUG . '-export',
393 SRFM_SLUG . '_export',
394 [
395 'ajaxurl' => admin_url( 'admin-ajax.php' ),
396 'srfm_export_nonce' => wp_create_nonce( 'export_form_nonce' ),
397 'site_url' => get_site_url(),
398 'srfm_import_endpoint' => '/wp-json/sureforms/v1/sureforms_import',
399 'import_form_nonce' => ( current_user_can( 'edit_posts' ) ) ? wp_create_nonce( 'wp_rest' ) : '',
400 ]
401 );
402
403 wp_enqueue_script( SRFM_SLUG . '-backend', $js_uri . 'backend' . $file_prefix . '.js', [], SRFM_VER, true );
404 wp_localize_script(
405 SRFM_SLUG . '-backend',
406 SRFM_SLUG . '_backend',
407 [
408 'site_url' => get_site_url(),
409 ]
410 );
411
412 }
413
414 if ( 'sureforms_page_add-new-form' === $current_screen->id ) {
415
416 $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min';
417 $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified';
418
419 $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/';
420
421 /* RTL */
422 if ( is_rtl() ) {
423 $file_prefix .= '-rtl';
424 }
425
426 wp_enqueue_style( SRFM_SLUG . '-template-picker', $css_uri . 'template-picker' . $file_prefix . '.css', [], SRFM_VER );
427
428 $sureforms_admin = 'templatePicker';
429
430 $script_asset_path = SRFM_DIR . 'assets/build/' . $sureforms_admin . '.asset.php';
431 $script_info = file_exists( $script_asset_path )
432 ? include $script_asset_path
433 : [
434 'dependencies' => [],
435 'version' => SRFM_VER,
436 ];
437 wp_enqueue_script( SRFM_SLUG . '-template-picker', SRFM_URL . 'assets/build/' . $sureforms_admin . '.js', $script_info['dependencies'], SRFM_VER, true );
438
439 wp_localize_script(
440 SRFM_SLUG . '-template-picker',
441 SRFM_SLUG . '_admin',
442 [
443 'site_url' => get_site_url(),
444 'plugin_url' => SRFM_URL,
445 'preview_images_url' => SRFM_URL . 'images/template-previews/',
446 'admin_url' => admin_url( 'admin.php' ),
447 'new_template_picker_base_url' => admin_url( 'post-new.php?post_type=sureforms_form' ),
448 'capability' => current_user_can( 'edit_posts' ),
449 'template_picker_nonce' => current_user_can( 'edit_posts' ) ? wp_create_nonce( 'wp_rest' ) : '',
450 'is_pro_active' => defined( 'SRFM_PRO_VER' ),
451 ]
452 );
453 }
454 // Quick action sidebar.
455 $default_allowed_quick_sidebar_blocks = apply_filters(
456 'srfm_quick_sidebar_allowed_blocks',
457 [
458 'srfm/input',
459 'srfm/email',
460 'srfm/textarea',
461 'srfm/checkbox',
462 'srfm/number',
463 'srfm/inline-button',
464 'srfm/advanced-heading',
465 ]
466 );
467 if ( ! is_array( $default_allowed_quick_sidebar_blocks ) ) {
468 $default_allowed_quick_sidebar_blocks = [];
469 }
470
471 $srfm_enable_quick_action_sidebar = get_option( 'srfm_enable_quick_action_sidebar' );
472 if ( ! $srfm_enable_quick_action_sidebar ) {
473 $srfm_enable_quick_action_sidebar = 'disabled';
474 }
475 $quick_sidebar_allowed_blocks = get_option( 'srfm_quick_sidebar_allowed_blocks' );
476 $quick_sidebar_allowed_blocks = ! empty( $quick_sidebar_allowed_blocks ) && is_array( $quick_sidebar_allowed_blocks ) ? $quick_sidebar_allowed_blocks : $default_allowed_quick_sidebar_blocks;
477 $srfm_ajax_nonce = wp_create_nonce( 'srfm_ajax_nonce' );
478 wp_enqueue_script( SRFM_SLUG . '-quick-action-siderbar', SRFM_URL . 'assets/build/quickActionSidebar.js', [], SRFM_VER, true );
479 wp_localize_script(
480 SRFM_SLUG . '-quick-action-siderbar',
481 SRFM_SLUG . '_quick_sidebar_blocks',
482 [
483 'allowed_blocks' => $quick_sidebar_allowed_blocks,
484 'srfm_enable_quick_action_sidebar' => $srfm_enable_quick_action_sidebar,
485 'srfm_ajax_nonce' => $srfm_ajax_nonce,
486 'srfm_ajax_url' => admin_url( 'admin-ajax.php' ),
487 ]
488 );
489 }
490
491 /**
492 * Form Template Picker Admin Body Classes
493 *
494 * @since 0.0.1
495 * @param string $classes Space separated class string.
496 */
497 public function admin_template_picker_body_class( $classes = '' ) {
498 $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.
499 $classes .= ' ' . $theme_builder_class . ' ';
500
501 return $classes;
502
503 }
504
505 /**
506 * Disable spectra's quick action bar in sureforms CPT.
507 *
508 * @param string $status current status of the quick action bar.
509 * @since 0.0.2
510 * @return string
511 */
512 public function restrict_spectra_quick_action_bar( $status ) {
513 $screen = get_current_screen();
514 if ( 'disabled' !== $status && isset( $screen->id ) && 'sureforms_form' === $screen->id ) {
515 $status = 'disabled';
516 }
517
518 return $status;
519 }
520 }
521