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