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

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