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

917 lines 32.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\Admin\Views\Entries_List_Table;
11 use SRFM\Admin\Views\Single_Entry;
12 use SRFM\Inc\AI_Form_Builder\AI_Helper;
13 use SRFM\Inc\Database\Tables\Entries;
14 use SRFM\Inc\Helper;
15 use SRFM\Inc\Post_Types;
16 use SRFM\Inc\Traits\Get_Instance;
17
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit; // Exit if accessed directly.
20 }
21 /**
22 * Admin handler class.
23 *
24 * @since 0.0.1
25 */
26 class Admin {
27 use Get_Instance;
28
29 /**
30 * Class constructor.
31 *
32 * @return void
33 * @since 0.0.1
34 */
35 public function __construct() {
36 add_action( 'admin_menu', [ $this, 'add_menu_page' ], 9 );
37 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
38 add_action( 'admin_menu', [ $this, 'settings_page' ] );
39 add_action( 'admin_menu', [ $this, 'add_new_form' ] );
40 if ( ! defined( 'SRFM_PRO_VER' ) ) {
41 add_action( 'admin_menu', [ $this, 'add_upgrade_to_pro' ] );
42 add_action( 'admin_footer', [ $this, 'add_upgrade_to_pro_target_attr' ] );
43 }
44
45 add_filter( 'plugin_action_links', [ $this, 'add_settings_link' ], 10, 2 );
46 add_action( 'enqueue_block_assets', [ $this, 'enqueue_styles' ] );
47 add_action( 'admin_head', [ $this, 'enqueue_header_styles' ] );
48 add_filter( 'admin_body_class', [ $this, 'admin_template_picker_body_class' ] );
49
50 // this action is used to restrict Spectra's quick action bar on SureForms CPTS.
51 add_action( 'uag_enable_quick_action_sidebar', [ $this, 'restrict_spectra_quick_action_bar' ] );
52
53 add_action( 'current_screen', [ $this, 'enable_gutenberg_for_sureforms' ], 100 );
54 add_action( 'admin_notices', [ $this, 'srfm_pro_version_compatibility' ] );
55
56 // Handle entry actions.
57 add_action( 'admin_init', [ $this, 'handle_entry_actions' ] );
58 add_action( 'admin_notices', [ Entries_List_Table::class, 'display_bulk_action_notice' ] );
59
60 // This action enqueues translations for NPS Survey library.
61 // A better solution will be required from library to resolve plugin conflict.
62 add_action(
63 'admin_footer',
64 static function() {
65 Helper::register_script_translations( 'nps-survey-script' );
66 },
67 1000
68 );
69 // Enfold theme compatibility to enable block editor for SureForms post type.
70 add_filter( 'avf_use_block_editor_for_post', [ $this, 'enable_block_editor_in_enfold_theme' ] );
71
72 // Add action links to the plugin page.
73 add_filter( 'plugin_action_links_' . SRFM_BASENAME, [ $this, 'add_action_links' ] );
74 add_filter( 'wpforms_current_user_can', [ $this, 'disable_wpforms_capabilities' ], 10, 3 );
75 }
76
77 /**
78 * Show action on plugin page.
79 *
80 * @param array $links links.
81 * @return array
82 * @since 1.4.2
83 */
84 public function add_action_links( $links ) {
85 if ( ! defined( 'SRFM_PRO_FILE' ) && ! file_exists( WP_PLUGIN_DIR . '/sureforms-pro/sureforms-pro.php' ) ) {
86 // Display upsell link if SureForms Pro is not installed.
87 $upsell_link = add_query_arg(
88 [
89 'utm_medium' => 'plugin-list',
90 ],
91 Helper::get_sureforms_website_url( 'pricing' )
92 );
93 $links[] = '<a href="' . esc_url( $upsell_link ) . '" target="_blank" rel="noreferrer" class="sureforms-plugins-go-pro">' . esc_html__( 'Get SureForms Pro', 'sureforms' ) . '</a>';
94 }
95
96 return $links;
97 }
98
99 /**
100 * Enable block editor in Enfold theme for SureForms post type.
101 *
102 * @param bool $use_block_editor Whether to use block editor.
103 * @since 1.3.1
104 */
105 public function enable_block_editor_in_enfold_theme( $use_block_editor ) {
106 // if SureForms form post type then return true.
107 if ( SRFM_FORMS_POST_TYPE === get_current_screen()->post_type ) {
108 return true;
109 }
110 return $use_block_editor;
111 }
112
113 /**
114 * Enable Gutenberg for SureForms associated post types.
115 *
116 * @since 0.0.10
117 */
118 public function enable_gutenberg_for_sureforms() {
119 /**
120 * Check if the classic editor is enabled from Classic Editor plugin settings or Divi settings.
121 */
122 if ( 'block' === get_option( 'classic-editor-replace' ) || 'on' === get_option( 'et_enable_classic_editor' ) ) {
123 return;
124 }
125
126 $srfm_post_types = apply_filters( 'srfm_enable_gutenberg_post_types', [ SRFM_FORMS_POST_TYPE ] );
127
128 if ( in_array( get_current_screen()->post_type, $srfm_post_types, true ) ) {
129 add_filter( 'use_block_editor_for_post_type', '__return_true', 110 );
130 add_filter( 'gutenberg_can_edit_post_type', '__return_true', 110 );
131 }
132 }
133
134 /**
135 * Sureforms editor header styles.
136 *
137 * @since 0.0.1
138 */
139 public function enqueue_header_styles() {
140 $current_screen = get_current_screen();
141 $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min';
142 $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified';
143
144 $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/';
145
146 /* RTL */
147 if ( is_rtl() ) {
148 $file_prefix .= '-rtl';
149 }
150
151 if ( 'sureforms_form' === $current_screen->id ) {
152 wp_enqueue_style( SRFM_SLUG . '-editor-header-styles', $css_uri . 'header-styles' . $file_prefix . '.css', [], SRFM_VER );
153 }
154 }
155
156 /**
157 * Add menu page.
158 *
159 * @return void
160 * @since 0.0.1
161 */
162 public function add_menu_page() {
163 if ( ! current_user_can( 'manage_options' ) ) {
164 return;
165 }
166
167 $capability = 'manage_options';
168 $menu_slug = 'sureforms_menu';
169
170 $logo = file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/icon.svg' );
171 add_menu_page(
172 __( 'SureForms', 'sureforms' ),
173 __( 'SureForms', 'sureforms' ),
174 'edit_others_posts',
175 $menu_slug,
176 static function () {
177 },
178 'data:image/svg+xml;base64,' . base64_encode( $logo ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
179 30
180 );
181
182 // Add the Dashboard Submenu.
183 add_submenu_page(
184 $menu_slug,
185 __( 'Dashboard', 'sureforms' ),
186 __( 'Dashboard', 'sureforms' ),
187 $capability,
188 $menu_slug,
189 [ $this, 'render_dashboard' ]
190 );
191 }
192
193 /**
194 * Add Settings page.
195 *
196 * @return void
197 * @since 0.0.1
198 */
199 public function settings_page() {
200 $callback = [ $this, 'settings_page_callback' ];
201 add_submenu_page(
202 'sureforms_menu',
203 __( 'Settings', 'sureforms' ),
204 __( 'Settings', 'sureforms' ),
205 'edit_others_posts',
206 'sureforms_form_settings',
207 $callback
208 );
209
210 // Get the current submenu page.
211 $submenu_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- $_GET['page'] does not provide nonce.
212
213 if ( ! isset( $_GET['tab'] ) && 'sureforms_form_settings' === $submenu_page ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- $_GET['page'] does not provide nonce.
214 wp_safe_redirect( admin_url( 'admin.php?page=sureforms_form_settings&tab=general-settings' ) );
215 exit;
216 }
217 }
218
219 /**
220 * Open to Upgrade to Pro submenu link in new tab.
221 *
222 * @return void
223 * @since 1.6.1
224 */
225 public function add_upgrade_to_pro_target_attr() {
226 ?>
227 <script type="text/javascript">
228 document.addEventListener('DOMContentLoaded', function () {
229 // Upgrade link handler.
230 // IMPORTANT: If this URL changes, also update it in the `add_upgrade_to_pro` function.
231 const upgradeLink = document.querySelector('a[href*="https://sureforms.com/upgrade"]');
232 if (upgradeLink) {
233 upgradeLink.addEventListener('click', e => {
234 e.preventDefault();
235 window.open(upgradeLink.href, '_blank');
236 });
237 }
238 });
239 </script>
240 <?php
241 }
242
243 /**
244 * Add Upgrade to pro menu item.
245 *
246 * @return void
247 * @since 1.6.1
248 */
249 public function add_upgrade_to_pro() {
250 // The url used here is used as a selector for css to style the upgrade to pro submenu.
251 // If you are changing this url, please make sure to update the css as well.
252 $upgrade_url = add_query_arg(
253 [
254 'utm_medium' => 'submenu_link_upgrade',
255 ],
256 Helper::get_sureforms_website_url( 'upgrade' )
257 );
258
259 add_submenu_page(
260 'sureforms_menu',
261 __( 'Upgrade', 'sureforms' ),
262 __( 'Upgrade', 'sureforms' ),
263 'edit_others_posts',
264 $upgrade_url
265 );
266 }
267
268 /**
269 * Render Admin Dashboard.
270 *
271 * @return void
272 * @since 0.0.1
273 */
274 public function render_dashboard() {
275 echo '<div id="srfm-dashboard-container" class="srfm-admin-wrapper"></div>';
276 }
277
278 /**
279 * Settings page callback.
280 *
281 * @return void
282 * @since 0.0.1
283 */
284 public function settings_page_callback() {
285 echo '<div id="srfm-settings-container" class="srfm-admin-wrapper"></div>';
286 }
287
288 /**
289 * Add new form menu item.
290 *
291 * @return void
292 * @since 0.0.1
293 */
294 public function add_new_form() {
295 add_submenu_page(
296 'sureforms_menu',
297 __( 'New Form', 'sureforms' ),
298 __( 'New Form', 'sureforms' ),
299 'edit_others_posts',
300 'add-new-form',
301 [ $this, 'add_new_form_callback' ],
302 2
303 );
304 add_submenu_page(
305 'sureforms_menu',
306 __( 'Entries', 'sureforms' ),
307 __( 'Entries', 'sureforms' ),
308 'edit_others_posts',
309 SRFM_ENTRIES,
310 [ $this, 'render_entries' ],
311 3
312 );
313 }
314
315 /**
316 * Add new form mentu item callback.
317 *
318 * @return void
319 * @since 0.0.1
320 */
321 public function add_new_form_callback() {
322 echo '<div id="srfm-add-new-form-container" class="srfm-admin-wrapper"></div>';
323 }
324
325 /**
326 * Entries page callback.
327 *
328 * @since 0.0.13
329 * @return void
330 */
331 public function render_entries() {
332 // Render single entry view.
333 // Adding the phpcs ignore nonce verification as no database operations are performed in this function, it is used to display the single entry view.
334 if ( isset( $_GET['entry_id'] ) && is_numeric( $_GET['entry_id'] ) && isset( $_GET['view'] ) && 'details' === $_GET['view'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verification is not needed here and explained in the comments above as well.
335 $single_entry_view = new Single_Entry();
336 $single_entry_view->render();
337 return;
338 }
339
340 // Render all entries view.
341 $entries_table = new Entries_List_Table();
342 $entries_table->prepare_items();
343 echo '<div class="wrap"><h1 class="wp-heading-inline">' . esc_html__( 'Entries', 'sureforms' ) . '</h1>';
344 if ( empty( $entries_table->all_entries_count ) && empty( $entries_table->trash_entries_count ) ) {
345 $instance = Post_Types::get_instance();
346 $instance->sureforms_render_blank_state( SRFM_ENTRIES );
347 $instance->get_blank_state_styles();
348 return;
349 }
350 echo '<form method="get">';
351 echo '<input type="hidden" name="page" value="sureforms_entries">';
352 $entries_table->display();
353 echo '</form>';
354 echo '</div>';
355 }
356
357 /**
358 * Adds a settings link to the plugin action links on the plugins page.
359 *
360 * @param array $links An array of plugin action links.
361 * @param string $file The plugin file path.
362 * @return array The updated array of plugin action links.
363 * @since 0.0.1
364 */
365 public function add_settings_link( $links, $file ) {
366 if ( 'sureforms/sureforms.php' === $file ) {
367 $plugin_links = apply_filters(
368 'sureforms_plugin_action_links',
369 [
370 'sureforms_settings' => '<a href="' . esc_url( admin_url( 'admin.php?page=sureforms_form_settings&tab=general-settings' ) ) . '">' . esc_html__( 'Settings', 'sureforms' ) . '</a>',
371 ]
372 );
373 $links = array_merge( $plugin_links, $links );
374 }
375 return $links;
376 }
377
378 /**
379 * Sureforms block editor styles.
380 *
381 * @since 0.0.1
382 */
383 public function enqueue_styles() {
384 $current_screen = get_current_screen();
385 global $wp_version;
386
387 $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min';
388 $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified';
389
390 $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/';
391 $vendor_css_uri = SRFM_URL . 'assets/css/minified/deps/';
392
393 /* RTL */
394 if ( is_rtl() ) {
395 $file_prefix .= '-rtl';
396 }
397
398 // Enqueue editor styles for post and page.
399 if ( SRFM_FORMS_POST_TYPE === $current_screen->post_type ) {
400 wp_enqueue_style( SRFM_SLUG . '-editor', $css_uri . 'backend/editor' . $file_prefix . '.css', [], SRFM_VER );
401 wp_enqueue_style( SRFM_SLUG . '-backend-blocks', $css_uri . 'blocks/default/backend' . $file_prefix . '.css', [], SRFM_VER );
402 wp_enqueue_style( SRFM_SLUG . '-intl', $vendor_css_uri . 'intl/intlTelInput-backend.min.css', [], SRFM_VER );
403 wp_enqueue_style( SRFM_SLUG . '-common', $css_uri . 'common' . $file_prefix . '.css', [], SRFM_VER );
404 wp_enqueue_style( SRFM_SLUG . '-reactQuill', $vendor_css_uri . 'quill/quill.snow.css', [], SRFM_VER );
405 wp_enqueue_style( SRFM_SLUG . '-single-form-modal', $css_uri . 'single-form-setting' . $file_prefix . '.css', [], SRFM_VER );
406
407 // if version is equal to or lower than 6.6.2 then add compatibility css.
408 if ( version_compare( $wp_version, '6.6.2', '<=' ) ) {
409 $srfm_inline_css = '.srfm-settings-modal .srfm-setting-modal-container .components-toggle-control .components-base-control__help{
410 margin-left: 4em;
411 }';
412 wp_add_inline_style( SRFM_SLUG . '-single-form-modal', $srfm_inline_css );
413 }
414 }
415
416 wp_enqueue_style( SRFM_SLUG . '-form-selector', $css_uri . 'srfm-form-selector' . $file_prefix . '.css', [], SRFM_VER );
417 wp_enqueue_style( SRFM_SLUG . '-common-editor', SRFM_URL . 'assets/build/common-editor.css', [], SRFM_VER, 'all' );
418 }
419
420 /**
421 * Get Breadcrumbs for current page.
422 *
423 * @since 0.0.1
424 * @return array Breadcrumbs Array.
425 */
426 public function get_breadcrumbs_for_current_page() {
427 global $post, $pagenow;
428 $breadcrumbs = [];
429
430 if ( 'admin.php' === $pagenow && isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- We don't need nonce verification here.
431 $page_title = get_admin_page_title();
432 $breadcrumbs[] = [
433 'title' => $page_title,
434 'link' => '',
435 ];
436 } elseif ( $post && in_array( $pagenow, [ 'post.php', 'post-new.php', 'edit.php' ], true ) ) {
437 $post_type_obj = get_post_type_object( get_post_type() );
438 if ( $post_type_obj ) {
439 $post_type_plural = $post_type_obj->labels->name;
440 $breadcrumbs[] = [
441 'title' => $post_type_plural,
442 'link' => admin_url( 'edit.php?post_type=' . $post_type_obj->name ),
443 ];
444
445 if ( 'edit.php' === $pagenow && ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- We don't need nonce verification here.
446 $breadcrumbs[ count( $breadcrumbs ) - 1 ]['link'] = '';
447 } else {
448 $breadcrumbs[] = [
449 /* Translators: Post Title. */
450 'title' => sprintf( __( 'Edit %1$s', 'sureforms' ), get_the_title() ),
451 'link' => get_edit_post_link( $post->ID ),
452 ];
453 }
454 }
455 } else {
456 $current_screen = get_current_screen();
457 if ( $current_screen && 'sureforms_form' === $current_screen->post_type ) {
458 $breadcrumbs[] = [
459 'title' => 'Forms',
460 'link' => '',
461 ];
462 } else {
463 $breadcrumbs[] = [
464 'title' => '',
465 'link' => '',
466 ];
467 }
468 }
469
470 return $breadcrumbs;
471 }
472
473 /**
474 * Enqueue Admin Scripts.
475 *
476 * @return void
477 * @since 0.0.1
478 */
479 public function enqueue_scripts() {
480 $current_screen = get_current_screen();
481 global $wp_version;
482
483 $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min';
484 $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified';
485 $js_uri = SRFM_URL . 'assets/js/' . $dir_name . '/';
486 $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/';
487 $is_rtl = is_rtl();
488 $rtl = $is_rtl ? '-rtl' : '';
489
490 /**
491 * List of the handles in which we need to add translation compatibility.
492 */
493 $script_translations_handlers = [];
494
495 $localization_data = [
496 'site_url' => get_site_url(),
497 'breadcrumbs' => $this->get_breadcrumbs_for_current_page(),
498 'sureforms_dashboard_url' => admin_url( '/admin.php?page=sureforms_menu' ),
499 'plugin_version' => SRFM_VER,
500 'global_settings_nonce' => current_user_can( 'manage_options' ) ? wp_create_nonce( 'wp_rest' ) : '',
501 'is_pro_active' => defined( 'SRFM_PRO_VER' ),
502 'pro_plugin_version' => defined( 'SRFM_PRO_VER' ) ? SRFM_PRO_VER : '',
503 'pro_plugin_name' => defined( 'SRFM_PRO_VER' ) && defined( 'SRFM_PRO_PRODUCT' ) ? SRFM_PRO_PRODUCT : 'SureForms Pro',
504 'sureforms_pricing_page' => Helper::get_sureforms_website_url( 'pricing' ),
505 'field_spacing_vars' => Helper::get_css_vars(),
506 'is_ver_lower_than_6_7' => version_compare( $wp_version, '6.6.2', '<=' ),
507 'integrations' => Helper::sureforms_get_integration(),
508 'ajax_url' => admin_url( 'admin-ajax.php' ),
509 'sf_plugin_manager_nonce' => wp_create_nonce( 'sf_plugin_manager_nonce' ),
510 'plugin_installer_nonce' => wp_create_nonce( 'updates' ),
511 'plugin_activating_text' => __( 'Activating...', 'sureforms' ),
512 'plugin_activated_text' => __( 'Activated', 'sureforms' ),
513 'plugin_activate_text' => __( 'Activate', 'sureforms' ),
514 'plugin_installing_text' => __( 'Installing...', 'sureforms' ),
515 'plugin_installed_text' => __( 'Installed', 'sureforms' ),
516 'is_rtl' => $is_rtl,
517 ];
518
519 $is_screen_sureforms_menu = Helper::validate_request_context( 'sureforms_menu', 'page' );
520 $is_screen_add_new_form = Helper::validate_request_context( 'add-new-form', 'page' );
521 $is_screen_sureforms_form_settings = Helper::validate_request_context( 'sureforms_form_settings', 'page' );
522 $is_screen_sureforms_entries = Helper::validate_request_context( SRFM_ENTRIES, 'page' );
523 $is_post_type_sureforms_form = SRFM_FORMS_POST_TYPE === $current_screen->post_type;
524
525 if ( $is_screen_sureforms_menu || $is_post_type_sureforms_form || $is_screen_add_new_form || $is_screen_sureforms_form_settings || $is_screen_sureforms_entries ) {
526 $asset_handle = '-dashboard';
527
528 wp_enqueue_style( SRFM_SLUG . $asset_handle . '-font', 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500&display=swap', [], SRFM_VER );
529
530 $script_asset_path = SRFM_DIR . 'assets/build/dashboard.asset.php';
531 $script_info = file_exists( $script_asset_path )
532 ? include $script_asset_path
533 : [
534 'dependencies' => [],
535 'version' => SRFM_VER,
536 ];
537 wp_enqueue_script( SRFM_SLUG . $asset_handle, SRFM_URL . 'assets/build/dashboard.js', $script_info['dependencies'], SRFM_VER, true );
538
539 wp_localize_script( SRFM_SLUG . $asset_handle, 'scIcons', [ 'path' => SRFM_URL . 'assets/build/icon-assets' ] );
540
541 $script_translations_handlers[] = SRFM_SLUG . $asset_handle;
542
543 if ( class_exists( 'SRFM_PRO\Admin\Licensing' ) ) {
544 $license_active = \SRFM_PRO\Admin\Licensing::is_license_active();
545 $localization_data['is_license_active'] = $license_active;
546
547 // Updating current licensing status.
548 $srfm_pro_license_status = get_option( 'srfm_pro_license_status', '' );
549 $current_license_status = $license_active ? 'licensed' : 'unlicensed';
550 if ( $current_license_status !== $srfm_pro_license_status ) {
551 update_option( 'srfm_pro_license_status', $current_license_status );
552 }
553 }
554
555 $localization_data['security_settings_url'] = admin_url( '/admin.php?page=sureforms_form_settings&tab=security-settings' );
556 $localization_data['integration_settings_url'] = admin_url( '/admin.php?page=sureforms_form_settings&tab=integration-settings' );
557 wp_localize_script(
558 SRFM_SLUG . $asset_handle,
559 SRFM_SLUG . '_admin',
560 apply_filters(
561 SRFM_SLUG . '_admin_filter',
562 $localization_data
563 )
564 );
565 wp_enqueue_style( SRFM_SLUG . '-dashboard', SRFM_URL . 'assets/build/dashboard.css', [], SRFM_VER, 'all' );
566
567 }
568
569 if ( $is_screen_sureforms_form_settings ) {
570 wp_enqueue_style( SRFM_SLUG . '-settings', $css_uri . 'backend/settings' . $file_prefix . $rtl . '.css', [], SRFM_VER );
571 }
572
573 // Enqueue styles for the entries page.
574 if ( $is_screen_sureforms_entries ) {
575 $asset_handle = '-entries';
576 wp_enqueue_script( SRFM_SLUG . $asset_handle, SRFM_URL . 'assets/build/entries.js', $script_info['dependencies'], SRFM_VER, true );
577
578 $script_translations_handlers[] = SRFM_SLUG . $asset_handle;
579 }
580
581 // Admin Submenu Styles.
582 wp_enqueue_style( SRFM_SLUG . '-admin', $css_uri . 'backend/admin' . $file_prefix . $rtl . '.css', [], SRFM_VER );
583
584 if ( 'edit-' . SRFM_FORMS_POST_TYPE === $current_screen->id ) {
585 $asset_handle = 'page_header';
586
587 $script_asset_path = SRFM_DIR . 'assets/build/' . $asset_handle . '.asset.php';
588 $script_info = file_exists( $script_asset_path )
589 ? include $script_asset_path
590 : [
591 'dependencies' => [],
592 'version' => SRFM_VER,
593 ];
594 wp_enqueue_script( SRFM_SLUG . '-form-page-header', SRFM_URL . 'assets/build/' . $asset_handle . '.js', $script_info['dependencies'], SRFM_VER, true );
595 wp_enqueue_style( SRFM_SLUG . '-form-archive-styles', $css_uri . 'form-archive-styles' . $file_prefix . $rtl . '.css', [], SRFM_VER );
596
597 $script_translations_handlers[] = SRFM_SLUG . '-form-page-header';
598 }
599
600 if ( $is_screen_sureforms_form_settings ) {
601 $asset_handle = 'settings';
602
603 $script_asset_path = SRFM_DIR . 'assets/build/' . $asset_handle . '.asset.php';
604 $script_info = file_exists( $script_asset_path )
605 ? include $script_asset_path
606 : [
607 'dependencies' => [],
608 'version' => SRFM_VER,
609 ];
610
611 wp_enqueue_script( SRFM_SLUG . '-settings', SRFM_URL . 'assets/build/' . $asset_handle . '.js', $script_info['dependencies'], SRFM_VER, true );
612 wp_enqueue_style( SRFM_SLUG . '-setting-styles', SRFM_URL . 'assets/build/' . $asset_handle . '.css', [ 'wp-components' ], SRFM_VER, 'all' );
613 wp_localize_script(
614 SRFM_SLUG . '-settings',
615 SRFM_SLUG . '_admin',
616 $localization_data
617 );
618
619 $script_translations_handlers[] = SRFM_SLUG . '-settings';
620 }
621 if ( 'edit-' . SRFM_FORMS_POST_TYPE === $current_screen->id ) {
622 wp_enqueue_script( SRFM_SLUG . '-form-archive', $js_uri . 'form-archive' . $file_prefix . '.js', [], SRFM_VER, true );
623 wp_enqueue_script( SRFM_SLUG . '-export', $js_uri . 'export' . $file_prefix . '.js', [ 'wp-i18n' ], SRFM_VER, true );
624 wp_localize_script(
625 SRFM_SLUG . '-export',
626 SRFM_SLUG . '_export',
627 [
628 'ajaxurl' => admin_url( 'admin-ajax.php' ),
629 'srfm_export_nonce' => wp_create_nonce( 'export_form_nonce' ),
630 'site_url' => get_site_url(),
631 'import_form_nonce' => current_user_can( 'edit_posts' ) ? wp_create_nonce( 'wp_rest' ) : '',
632 'import_btn_string' => __( 'Import Form', 'sureforms' ),
633 ]
634 );
635
636 wp_enqueue_script( SRFM_SLUG . '-backend', $js_uri . 'backend' . $file_prefix . '.js', [], SRFM_VER, true );
637 wp_localize_script(
638 SRFM_SLUG . '-backend',
639 SRFM_SLUG . '_backend',
640 [
641 'site_url' => get_site_url(),
642 ]
643 );
644
645 $script_translations_handlers[] = SRFM_SLUG . '-form-archive';
646 $script_translations_handlers[] = SRFM_SLUG . '-export';
647 $script_translations_handlers[] = SRFM_SLUG . '-backend';
648 }
649
650 if ( $is_screen_add_new_form ) {
651 wp_enqueue_style( SRFM_SLUG . '-template-picker', $css_uri . 'template-picker' . $file_prefix . $rtl . '.css', [], SRFM_VER );
652
653 $sureforms_admin = 'templatePicker';
654
655 $script_asset_path = SRFM_DIR . 'assets/build/' . $sureforms_admin . '.asset.php';
656 $script_info = file_exists( $script_asset_path )
657 ? include $script_asset_path
658 : [
659 'dependencies' => [],
660 'version' => SRFM_VER,
661 ];
662 wp_enqueue_script( SRFM_SLUG . '-template-picker', SRFM_URL . 'assets/build/' . $sureforms_admin . '.js', $script_info['dependencies'], SRFM_VER, true );
663
664 wp_localize_script(
665 SRFM_SLUG . '-template-picker',
666 SRFM_SLUG . '_admin',
667 [
668 'site_url' => get_site_url(),
669 'plugin_url' => SRFM_URL,
670 'preview_images_url' => SRFM_URL . 'images/template-previews/',
671 'admin_url' => admin_url( 'admin.php' ),
672 'new_template_picker_base_url' => admin_url( 'post-new.php?post_type=sureforms_form' ),
673 'capability' => current_user_can( 'edit_posts' ),
674 'template_picker_nonce' => current_user_can( 'edit_posts' ) ? wp_create_nonce( 'wp_rest' ) : '',
675 'is_pro_active' => defined( 'SRFM_PRO_VER' ),
676 'srfm_ai_usage_details' => AI_Helper::get_current_usage_details(),
677 'is_pro_license_active' => AI_Helper::is_pro_license_active(),
678 'srfm_ai_auth_user_email' => get_option( 'srfm_ai_auth_user_email' ),
679 'pricing_page_url' => Helper::get_sureforms_website_url( 'pricing' ),
680 ]
681 );
682
683 $script_translations_handlers[] = SRFM_SLUG . '-template-picker';
684 }
685 // Quick action sidebar.
686 $default_allowed_quick_sidebar_blocks = apply_filters(
687 'srfm_quick_sidebar_allowed_blocks',
688 [
689 'srfm/input',
690 'srfm/email',
691 'srfm/textarea',
692 'srfm/checkbox',
693 'srfm/number',
694 'srfm/inline-button',
695 'srfm/advanced-heading',
696 ]
697 );
698 if ( ! is_array( $default_allowed_quick_sidebar_blocks ) ) {
699 $default_allowed_quick_sidebar_blocks = [];
700 }
701
702 $srfm_enable_quick_action_sidebar = get_option( 'srfm_enable_quick_action_sidebar' );
703 if ( ! $srfm_enable_quick_action_sidebar ) {
704 $srfm_enable_quick_action_sidebar = 'disabled';
705 }
706 $quick_sidebar_allowed_blocks = get_option( 'srfm_quick_sidebar_allowed_blocks' );
707 $quick_sidebar_allowed_blocks = ! empty( $quick_sidebar_allowed_blocks ) && is_array( $quick_sidebar_allowed_blocks ) ? $quick_sidebar_allowed_blocks : $default_allowed_quick_sidebar_blocks;
708 $srfm_ajax_nonce = wp_create_nonce( 'srfm_ajax_nonce' );
709 wp_enqueue_script( SRFM_SLUG . '-quick-action-siderbar', SRFM_URL . 'assets/build/quickActionSidebar.js', [], SRFM_VER, true );
710 wp_localize_script(
711 SRFM_SLUG . '-quick-action-siderbar',
712 SRFM_SLUG . '_quick_sidebar_blocks',
713 [
714 'allowed_blocks' => $quick_sidebar_allowed_blocks,
715 'srfm_enable_quick_action_sidebar' => $srfm_enable_quick_action_sidebar,
716 'srfm_ajax_nonce' => $srfm_ajax_nonce,
717 'srfm_ajax_url' => admin_url( 'admin-ajax.php' ),
718 ]
719 );
720
721 $script_translations_handlers[] = SRFM_SLUG . '-quick-action-siderbar';
722
723 /**
724 * Enqueuing SureTriggers Integration script.
725 * This script loads suretriggers iframe in Intergations tab.
726 */
727 if ( $is_post_type_sureforms_form ) {
728 wp_enqueue_script( SRFM_SLUG . '-suretriggers-integration', SRFM_SURETRIGGERS_INTEGRATION_BASE_URL . 'js/v2/embed.js', [], SRFM_VER, true );
729 }
730
731 // Check $script_translations_handlers is not empty before calling the function.
732 if ( ! empty( $script_translations_handlers ) ) {
733 // Remove duplicates values from the array.
734 $script_translations_handlers = array_unique( $script_translations_handlers );
735
736 foreach ( $script_translations_handlers as $script_handle ) {
737 Helper::register_script_translations( $script_handle );
738 }
739 }
740 }
741
742 /**
743 * Form Template Picker Admin Body Classes
744 * WordPress sometimes translates class names in the admin body tag, which can result in
745 * incorrect or missing class names when rendering the admin pages. This function ensures
746 * that essential class names are manually added to the body tag to maintain proper functionality.
747 *
748 * @since 0.0.1
749 * @param string $classes Space separated class string.
750 */
751 public function admin_template_picker_body_class( $classes = '' ) {
752 // Define an associative array of class names and their corresponding conditions.
753 // Each condition checks whether a specific request context matches.
754 $srfm_classes = [
755 'sureforms_page_sureforms_entries' => Helper::validate_request_context( SRFM_ENTRIES, 'page' ),
756 'sureforms_page_sureforms_form_settings' => Helper::validate_request_context( 'sureforms_form_settings', 'page' ),
757 'srfm-template-picker' => Helper::validate_request_context( 'add-new-form', 'page' ),
758 ];
759
760 $add_srfm_classes = '';
761
762 // Loop through the defined classes and conditions.
763 foreach ( $srfm_classes as $class => $condition ) {
764 // Check if the condition evaluates to true.
765 if ( $condition ) {
766 // Append the class to the existing classes string, followed by a space.
767 $add_srfm_classes .= empty( $add_srfm_classes ) ? $class : ' ' . $class;
768 }
769 }
770
771 // Append the new classes to the existing classes string.
772 if ( ! empty( $add_srfm_classes ) ) {
773 $classes .= ' ' . $add_srfm_classes;
774 }
775
776 // Return the updated list of classes.
777 return $classes;
778 }
779
780 /**
781 * Disable spectra's quick action bar in sureforms CPT.
782 *
783 * @param string $status current status of the quick action bar.
784 * @since 0.0.2
785 * @return string
786 */
787 public function restrict_spectra_quick_action_bar( $status ) {
788 $screen = get_current_screen();
789 if ( 'disabled' !== $status && isset( $screen->id ) && 'sureforms_form' === $screen->id ) {
790 $status = 'disabled';
791 }
792
793 return $status;
794 }
795
796 // Entries methods.
797
798 /**
799 * Handle entry actions.
800 *
801 * @since 0.0.13
802 * @return void
803 */
804 public function handle_entry_actions() {
805 Entries_List_Table::process_bulk_actions();
806
807 if ( ! isset( $_GET['page'] ) || SRFM_ENTRIES !== $_GET['page'] ) {
808 return;
809 }
810 if ( ! isset( $_GET['entry_id'] ) || ! isset( $_GET['action'] ) ) {
811 return;
812 }
813 if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'srfm_entries_action' ) ) {
814 wp_die( esc_html__( 'Nonce verification failed.', 'sureforms' ) );
815 }
816 $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : '';
817 $entry_id = Helper::get_integer_value( sanitize_text_field( wp_unslash( $_GET['entry_id'] ) ) );
818 $view = isset( $_GET['view'] ) ? sanitize_text_field( wp_unslash( $_GET['view'] ) ) : '';
819 if ( $entry_id > 0 ) {
820 if ( 'read' === $action && 'details' === $view ) {
821 $entry_status = Entries::get( $entry_id )['status'];
822 if ( 'trash' === $entry_status ) {
823 wp_die( esc_html__( 'You cannot view this entry because it is in trash.', 'sureforms' ) );
824 }
825 }
826 Entries_List_Table::handle_entry_status( $entry_id, $action, $view );
827 }
828 }
829
830 /**
831 * Admin Notice Callback if sureforms pro is out of date.
832 *
833 * Hooked - admin_notices
834 *
835 * @return void
836 * @since 1.0.4
837 */
838 public function srfm_pro_version_compatibility() {
839 $plugin_file = 'sureforms-pro/sureforms-pro.php';
840 if ( ! is_plugin_active( $plugin_file ) || ! defined( 'SRFM_PRO_VER' ) ) {
841 return;
842 }
843
844 if ( empty( get_current_screen() ) ) {
845 return;
846 }
847
848 if ( ! current_user_can( 'update_plugins' ) ) {
849 return;
850 }
851
852 $srfm_pro_license_status = get_option( 'srfm_pro_license_status', '' );
853 /**
854 * If the license status is not set then get the license status and update the option accordingly.
855 * This will be executed only once. Subsequently, the option status is updated by the licensing class on license activation or deactivation.
856 */
857 if ( empty( $srfm_pro_license_status ) && class_exists( 'SRFM_PRO\Admin\Licensing' ) ) {
858 $srfm_pro_license_status = \SRFM_PRO\Admin\Licensing::is_license_active() ? 'licensed' : 'unlicensed';
859 update_option( 'srfm_pro_license_status', $srfm_pro_license_status );
860 }
861
862 $pro_plugin_name = defined( 'SRFM_PRO_PRODUCT' ) ? SRFM_PRO_PRODUCT : 'SureForms Pro';
863 $message = '';
864 $url = admin_url( 'admin.php?page=sureforms_form_settings&tab=account-settings' );
865 if ( 'unlicensed' === $srfm_pro_license_status ) {
866 $message = '<p>' . sprintf(
867 // translators: %1$s: Opening anchor tag with URL, %2$s: Closing anchor tag, %3$s: SureForms Pro Plugin Name.
868 esc_html__( 'Please %1$sactivate%2$s your copy of %3$s to get new features, access support, receive update notifications, and more.', 'sureforms' ),
869 '<a href="' . esc_url( $url ) . '">',
870 '</a>',
871 '<i>' . esc_html( $pro_plugin_name ) . '</i>'
872 ) . '</p>';
873 }
874
875 if ( ! version_compare( SRFM_PRO_VER, SRFM_PRO_RECOMMENDED_VER, '>=' ) ) {
876 $message .= '<p>' . sprintf(
877 // translators: %1$s: SureForms version, %2$s: SureForms Pro Plugin Name, %3$s: SureForms Pro Version, %4$s: Anchor tag open, %5$s: Closing anchor tag.
878 esc_html__( 'SureForms %1$s requires minimum %2$s %3$s to work properly. Please update to the latest version from %4$shere%5$s.', 'sureforms' ),
879 esc_html( SRFM_VER ),
880 esc_html( $pro_plugin_name ),
881 esc_html( SRFM_PRO_RECOMMENDED_VER ),
882 '<a href=' . esc_url( admin_url( 'update-core.php' ) ) . '>',
883 '</a>'
884 ) . '</p>';
885 }
886
887 if ( ! empty( $message ) ) {
888 // Phpcs ignore comment is required as $message variable is already escaped.
889 echo '<div class="notice notice-warning">' . wp_kses_post( $message ) . '</div>';
890 }
891 }
892
893 /**
894 * Disables the capabilities for WPForms to avoid conflicts when enqueueing
895 * scripts and styles for WPForms.
896 *
897 * This function is intended to prevent any potential conflicts that may arise
898 * when WPForms scripts and styles are enqueued. By disabling certain capabilities,
899 * it ensures that WPForms does not interfere with other functionalities.
900 *
901 * @param bool $user_can A boolean indicating whether the user has the capability.
902 * @return bool Returns true if the capabilities are successfully disabled, false otherwise.
903 * @since 1.4.2
904 */
905 public function disable_wpforms_capabilities( $user_can ) {
906 // Note: Nonce verification is intentionally omitted here as no database operations are performed.
907 // The values of the $_REQUEST variables are strictly validated, ensuring security without the need for nonce verification.
908
909 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
910 $post_id = ! empty( $_REQUEST['post'] ) && ! empty( $_REQUEST['action'] ) ? absint( $_REQUEST['post'] ) : 0;
911
912 // phpcs:ignore WordPress.Security.NonceVerification.Recommended
913 $post_type = $post_id ? get_post_type( $post_id ) : sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ?? '' ) );
914 return SRFM_FORMS_POST_TYPE === $post_type ? false : $user_can;
915 }
916 }
917