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

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