admin.php
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Class. |
| 4 | * |
| 5 | * @package sureforms. |
| 6 | */ |
| 7 | |
| 8 | namespace SRFM\Admin; |
| 9 | |
| 10 | use SRFM\Inc\Traits\Get_Instance; |
| 11 | use SRFM\Inc\AI_Form_Builder\AI_Helper; |
| 12 | use SRFM\Inc\Helper; |
| 13 | use SRFM\Admin\Views\Single_Entry; |
| 14 | use SRFM\Admin\Views\Entries_List_Table; |
| 15 | use SRFM\Inc\Post_Types; |
| 16 | use SRFM\Inc\Database\Tables\Entries; |
| 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 | |
| 28 | use Get_Instance; |
| 29 | |
| 30 | /** |
| 31 | * Class constructor. |
| 32 | * |
| 33 | * @return void |
| 34 | * @since 0.0.1 |
| 35 | */ |
| 36 | public function __construct() { |
| 37 | add_action( 'admin_menu', [ $this, 'add_menu_page' ], 9 ); |
| 38 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 39 | add_action( 'admin_menu', [ $this, 'settings_page' ] ); |
| 40 | add_action( 'admin_menu', [ $this, 'add_new_form' ] ); |
| 41 | add_filter( 'plugin_action_links', [ $this, 'add_settings_link' ], 10, 2 ); |
| 42 | add_action( 'enqueue_block_assets', [ $this, 'enqueue_styles' ] ); |
| 43 | add_action( 'admin_head', [ $this, 'enqueue_header_styles' ] ); |
| 44 | add_action( 'admin_body_class', [ $this, 'admin_template_picker_body_class' ] ); |
| 45 | |
| 46 | // this action is used to restrict Spectra's quick action bar on SureForms CPTS. |
| 47 | add_action( 'uag_enable_quick_action_sidebar', [ $this, 'restrict_spectra_quick_action_bar' ] ); |
| 48 | |
| 49 | add_action( 'current_screen', [ $this, 'enable_gutenberg_for_sureforms' ], 100 ); |
| 50 | |
| 51 | // Handle entry actions. |
| 52 | add_action( 'admin_init', [ $this, 'handle_entry_actions' ] ); |
| 53 | add_action( 'admin_notices', [ $this, 'entries_migration_notice' ] ); |
| 54 | add_action( 'admin_notices', [ Entries_List_Table::class, 'display_bulk_action_notice' ] ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Print notice to inform users about entries database migration. |
| 59 | * phpcs:ignore -- TODO - Remove this notice after three major releases. |
| 60 | * |
| 61 | * @since 0.0.13 |
| 62 | * @return void |
| 63 | */ |
| 64 | public function entries_migration_notice() { |
| 65 | $dismiss = get_option( 'srfm_dismiss_entries_migration_notice' ); |
| 66 | if ( 'hide' === $dismiss ) { |
| 67 | // If we are here then it means user has dismissed the notice 'hide'. |
| 68 | return; |
| 69 | } elseif ( ! $dismiss ) { |
| 70 | // If we are here then it means user don't have version saved in the db initially so we need to proceed with notice accordingly. |
| 71 | if ( empty( get_posts( [ 'post_type' => 'sureforms_entry' ] ) ) ) { |
| 72 | // If we are here then we are certain that this is a fresh setup without legacy entries so we can hide the notice. |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // From below, display notice for those users who are directly upgrading from version before v0.0.12. |
| 77 | } |
| 78 | |
| 79 | // Show notice for users coming from a version lower than 0.0.13 and have legacy entries. |
| 80 | |
| 81 | $ajaxurl = add_query_arg( |
| 82 | [ |
| 83 | 'action' => 'sureforms_dismiss_plugin_notice', |
| 84 | 'security' => wp_create_nonce( 'srfm_notice_dismiss_nonce' ), |
| 85 | ], |
| 86 | admin_url( 'admin-ajax.php' ) |
| 87 | ); |
| 88 | ?> |
| 89 | <!-- Adding this internal style to maintain notice styling without worrying about conditional loading of the css files. --> |
| 90 | <style> |
| 91 | .srfm-plugin-notice-container { |
| 92 | display: flex; |
| 93 | align-items: center; |
| 94 | gap: 20px; |
| 95 | padding: 10px 0; |
| 96 | } |
| 97 | .srfm-plugin-notice--logo svg { |
| 98 | width: 60px; |
| 99 | height: 60px; |
| 100 | } |
| 101 | .srfm-plugin-notice--content .srfm-plugin-notice--title { |
| 102 | margin: 0; |
| 103 | font-size: 20px; |
| 104 | line-height: 1.5; |
| 105 | } |
| 106 | .srfm-plugin-notice--content .srfm-plugin-notice--message { |
| 107 | margin-top: 4px; |
| 108 | padding: 0; |
| 109 | } |
| 110 | </style> |
| 111 | <div class="notice notice-warning is-dismissible srfm-plugin-notice"> |
| 112 | <div class="srfm-plugin-notice-container"> |
| 113 | <div class="srfm-plugin-notice--logo"> |
| 114 | <svg width="500" height="500" viewBox="0 0 500 500" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 115 | <path fill-rule="evenodd" clip-rule="evenodd" d="M250 500C388.072 500 500 388.071 500 250C500 111.929 388.072 0 250 0C111.929 0 0 111.929 0 250C0 388.071 111.929 500 250 500ZM251.076 125C231.002 125 203.224 136.48 189.028 150.641L150.477 189.103H342.635L406.887 125H251.076ZM310.648 349.359C296.454 363.52 268.673 375 248.599 375H92.7892L157.043 310.897H349.199L310.648 349.359ZM373.1 221.154H118.42L106.39 233.173C77.9043 258.814 86.3526 278.846 126.246 278.846H381.615L393.649 266.827C421.859 241.336 412.993 221.154 373.1 221.154Z" fill="#D54407"/> |
| 116 | </svg> |
| 117 | </div> |
| 118 | <div class="srfm-plugin-notice--content"> |
| 119 | <h3 class="srfm-plugin-notice--title"><?php esc_html_e( 'SureForms - Important Update Notice', 'sureforms' ); ?></h3> |
| 120 | <p class="srfm-plugin-notice--message"><strong><?php esc_html_e( "From version 0.0.13 we're migrating to a custom database to enhance SureForms' performance and features.", 'sureforms' ); ?></strong></p> |
| 121 | <p class="srfm-plugin-notice--message"><?php esc_html_e( 'This step is necessary and irreversible and your current existing entries will be lost. Thank you for your understanding!', 'sureforms' ); ?></p> |
| 122 | </div> |
| 123 | </div> |
| 124 | </div> |
| 125 | <script> |
| 126 | (function() { |
| 127 | window.addEventListener('load', function() { |
| 128 | const dismissNotice = document.querySelector('.is-dismissible.srfm-plugin-notice .notice-dismiss'); |
| 129 | |
| 130 | dismissNotice.addEventListener('click', function() { |
| 131 | fetch('<?php echo esc_url_raw( $ajaxurl ); ?>'); |
| 132 | }); |
| 133 | }); |
| 134 | }()); |
| 135 | </script> |
| 136 | <?php |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Enable Gutenberg for SureForms associated post types. |
| 141 | * |
| 142 | * @since 0.0.10 |
| 143 | */ |
| 144 | public function enable_gutenberg_for_sureforms() { |
| 145 | |
| 146 | // Check if the Classic Editor plugin is active and if it is set to replace the block editor. |
| 147 | if ( ! class_exists( 'Classic_Editor' ) || 'block' === get_option( 'classic-editor-replace' ) ) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | $srfm_post_types = apply_filters( 'srfm_enable_gutenberg_post_types', [ SRFM_FORMS_POST_TYPE ] ); |
| 152 | |
| 153 | if ( in_array( get_current_screen()->post_type, $srfm_post_types, true ) ) { |
| 154 | add_filter( 'use_block_editor_for_post_type', '__return_true', 110 ); |
| 155 | add_filter( 'gutenberg_can_edit_post_type', '__return_true', 110 ); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /** |
| 161 | * Sureforms editor header styles. |
| 162 | * |
| 163 | * @since 0.0.1 |
| 164 | */ |
| 165 | public function enqueue_header_styles() { |
| 166 | $current_screen = get_current_screen(); |
| 167 | $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min'; |
| 168 | $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified'; |
| 169 | |
| 170 | $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/'; |
| 171 | |
| 172 | /* RTL */ |
| 173 | if ( is_rtl() ) { |
| 174 | $file_prefix .= '-rtl'; |
| 175 | } |
| 176 | |
| 177 | if ( 'sureforms_form' === $current_screen->id ) { |
| 178 | wp_enqueue_style( SRFM_SLUG . '-editor-header-styles', $css_uri . 'header-styles' . $file_prefix . '.css', [], SRFM_VER ); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Add menu page. |
| 184 | * |
| 185 | * @return void |
| 186 | * @since 0.0.1 |
| 187 | */ |
| 188 | public function add_menu_page() { |
| 189 | if ( ! current_user_can( 'manage_options' ) ) { |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | $capability = 'manage_options'; |
| 194 | $menu_slug = 'sureforms_menu'; |
| 195 | |
| 196 | $logo = file_get_contents( plugin_dir_path( SRFM_FILE ) . 'images/icon.svg' ); |
| 197 | add_menu_page( |
| 198 | __( 'SureForms', 'sureforms' ), |
| 199 | __( 'SureForms', 'sureforms' ), |
| 200 | 'edit_others_posts', |
| 201 | $menu_slug, |
| 202 | function () {}, |
| 203 | 'data:image/svg+xml;base64,' . base64_encode( $logo ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode |
| 204 | 30 |
| 205 | ); |
| 206 | |
| 207 | // Add the Dashboard Submenu. |
| 208 | add_submenu_page( |
| 209 | $menu_slug, |
| 210 | __( 'Dashboard', 'sureforms' ), |
| 211 | __( 'Dashboard', 'sureforms' ), |
| 212 | $capability, |
| 213 | $menu_slug, |
| 214 | [ $this, 'render_dashboard' ] |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Add Settings page. |
| 220 | * |
| 221 | * @return void |
| 222 | * @since 0.0.1 |
| 223 | */ |
| 224 | public function settings_page() { |
| 225 | $callback = [ $this, 'settings_page_callback' ]; |
| 226 | add_submenu_page( |
| 227 | 'sureforms_menu', |
| 228 | __( 'Settings', 'sureforms' ), |
| 229 | __( 'Settings', 'sureforms' ), |
| 230 | 'edit_others_posts', |
| 231 | 'sureforms_form_settings', |
| 232 | $callback |
| 233 | ); |
| 234 | |
| 235 | // Get the current submenu page. |
| 236 | $submenu_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- $_GET['page'] does not provide nonce. |
| 237 | |
| 238 | if ( ! isset( $_GET['tab'] ) && 'sureforms_form_settings' === $submenu_page ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- $_GET['page'] does not provide nonce. |
| 239 | wp_safe_redirect( admin_url( 'admin.php?page=sureforms_form_settings&tab=general-settings' ) ); |
| 240 | exit; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Render Admin Dashboard. |
| 246 | * |
| 247 | * @return void |
| 248 | * @since 0.0.1 |
| 249 | */ |
| 250 | public function render_dashboard() { |
| 251 | echo '<div id="srfm-dashboard-container"></div>'; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Settings page callback. |
| 256 | * |
| 257 | * @return void |
| 258 | * @since 0.0.1 |
| 259 | */ |
| 260 | public function settings_page_callback() { |
| 261 | echo '<div id="srfm-settings-container"></div>'; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Add new form menu item. |
| 266 | * |
| 267 | * @return void |
| 268 | * @since 0.0.1 |
| 269 | */ |
| 270 | public function add_new_form() { |
| 271 | add_submenu_page( |
| 272 | 'sureforms_menu', |
| 273 | __( 'New Form', 'sureforms' ), |
| 274 | __( 'New Form', 'sureforms' ), |
| 275 | 'edit_others_posts', |
| 276 | 'add-new-form', |
| 277 | [ $this, 'add_new_form_callback' ], |
| 278 | 2 |
| 279 | ); |
| 280 | add_submenu_page( |
| 281 | 'sureforms_menu', |
| 282 | __( 'Entries', 'sureforms' ), |
| 283 | __( 'Entries', 'sureforms' ), |
| 284 | 'edit_others_posts', |
| 285 | SRFM_ENTRIES, |
| 286 | [ $this, 'render_entries' ], |
| 287 | 3 |
| 288 | ); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Add new form mentu item callback. |
| 293 | * |
| 294 | * @return void |
| 295 | * @since 0.0.1 |
| 296 | */ |
| 297 | public function add_new_form_callback() { |
| 298 | echo '<div id="srfm-add-new-form-container"></div>'; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Entries page callback. |
| 303 | * |
| 304 | * @since 0.0.13 |
| 305 | * @return void |
| 306 | */ |
| 307 | public function render_entries() { |
| 308 | // Render single entry view. |
| 309 | // Adding the phpcs ignore nonce verification as no database operations are performed in this function, it is used to display the single entry view. |
| 310 | if ( isset( $_GET['entry_id'] ) && is_numeric( $_GET['entry_id'] ) && isset( $_GET['view'] ) && 'details' === $_GET['view'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 311 | $single_entry_view = new Single_Entry(); |
| 312 | $single_entry_view->render(); |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | // Render all entries view. |
| 317 | $entries_table = new Entries_List_Table(); |
| 318 | $entries_table->prepare_items(); |
| 319 | echo '<div class="wrap"><h1 class="wp-heading-inline">Entries</h1>'; |
| 320 | if ( 0 >= $entries_table->all_entries_count && 0 >= $entries_table->trash_count ) { |
| 321 | $instance = Post_Types::get_instance(); |
| 322 | $instance->sureforms_render_blank_state( SRFM_ENTRIES ); |
| 323 | $instance->get_blank_state_styles(); |
| 324 | return; |
| 325 | } |
| 326 | echo '<form method="get">'; |
| 327 | echo '<input type="hidden" name="page" value="sureforms_entries">'; |
| 328 | $entries_table->display(); |
| 329 | echo '</form>'; |
| 330 | echo '</div>'; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Adds a settings link to the plugin action links on the plugins page. |
| 335 | * |
| 336 | * @param array $links An array of plugin action links. |
| 337 | * @param string $file The plugin file path. |
| 338 | * @return array The updated array of plugin action links. |
| 339 | * @since 0.0.1 |
| 340 | */ |
| 341 | public function add_settings_link( $links, $file ) { |
| 342 | if ( 'sureforms/sureforms.php' === $file ) { |
| 343 | $plugin_links = apply_filters( |
| 344 | 'sureforms_plugin_action_links', |
| 345 | [ |
| 346 | 'sureforms_settings' => '<a href="' . esc_url( admin_url( 'admin.php?page=sureforms_form_settings&tab=general-settings' ) ) . '">' . esc_html__( 'Settings', 'sureforms' ) . '</a>', |
| 347 | ] |
| 348 | ); |
| 349 | $links = array_merge( $plugin_links, $links ); |
| 350 | } |
| 351 | return $links; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Sureforms block editor styles. |
| 356 | * |
| 357 | * @since 0.0.1 |
| 358 | */ |
| 359 | public function enqueue_styles() { |
| 360 | $current_screen = get_current_screen(); |
| 361 | |
| 362 | $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min'; |
| 363 | $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified'; |
| 364 | |
| 365 | $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/'; |
| 366 | $vendor_css_uri = SRFM_URL . 'assets/css/minified/deps/'; |
| 367 | |
| 368 | /* RTL */ |
| 369 | if ( is_rtl() ) { |
| 370 | $file_prefix .= '-rtl'; |
| 371 | } |
| 372 | |
| 373 | // Enqueue editor styles for post and page. |
| 374 | if ( SRFM_FORMS_POST_TYPE === $current_screen->post_type ) { |
| 375 | wp_enqueue_style( SRFM_SLUG . '-editor', $css_uri . 'backend/editor' . $file_prefix . '.css', [], SRFM_VER ); |
| 376 | wp_enqueue_style( SRFM_SLUG . '-backend-blocks', $css_uri . 'blocks/default/backend' . $file_prefix . '.css', [], SRFM_VER ); |
| 377 | wp_enqueue_style( SRFM_SLUG . '-intl', $vendor_css_uri . 'intl/intlTelInput-backend.min.css', [], SRFM_VER ); |
| 378 | wp_enqueue_style( SRFM_SLUG . '-common', $css_uri . 'common' . $file_prefix . '.css', [], SRFM_VER ); |
| 379 | wp_enqueue_style( SRFM_SLUG . '-reactQuill', $vendor_css_uri . 'quill/quill.snow.css', [], SRFM_VER ); |
| 380 | wp_enqueue_style( SRFM_SLUG . '-single-form-modal', $css_uri . 'single-form-setting' . $file_prefix . '.css', [], SRFM_VER ); |
| 381 | } |
| 382 | |
| 383 | wp_enqueue_style( SRFM_SLUG . '-form-selector', $css_uri . 'srfm-form-selector' . $file_prefix . '.css', [], SRFM_VER ); |
| 384 | wp_enqueue_style( SRFM_SLUG . '-common-editor', SRFM_URL . 'assets/build/common-editor.css', [], SRFM_VER, 'all' ); |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Get Breadcrumbs for current page. |
| 389 | * |
| 390 | * @since 0.0.1 |
| 391 | * @return array Breadcrumbs Array. |
| 392 | */ |
| 393 | public function get_breadcrumbs_for_current_page() { |
| 394 | global $post, $pagenow; |
| 395 | $breadcrumbs = []; |
| 396 | |
| 397 | if ( 'admin.php' === $pagenow && isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 398 | $page_title = get_admin_page_title(); |
| 399 | $breadcrumbs[] = [ |
| 400 | 'title' => $page_title, |
| 401 | 'link' => '', |
| 402 | ]; |
| 403 | } elseif ( $post && in_array( $pagenow, [ 'post.php', 'post-new.php', 'edit.php' ], true ) ) { |
| 404 | $post_type_obj = get_post_type_object( get_post_type() ); |
| 405 | if ( $post_type_obj ) { |
| 406 | $post_type_plural = $post_type_obj->labels->name; |
| 407 | $breadcrumbs[] = [ |
| 408 | 'title' => $post_type_plural, |
| 409 | 'link' => admin_url( 'edit.php?post_type=' . $post_type_obj->name ), |
| 410 | ]; |
| 411 | |
| 412 | if ( 'edit.php' === $pagenow && ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 413 | $breadcrumbs[ count( $breadcrumbs ) - 1 ]['link'] = ''; |
| 414 | } else { |
| 415 | $breadcrumbs[] = [ |
| 416 | /* Translators: Post Title. */ |
| 417 | 'title' => sprintf( __( 'Edit %1$s', 'sureforms' ), get_the_title() ), |
| 418 | 'link' => get_edit_post_link( $post->ID ), |
| 419 | ]; |
| 420 | } |
| 421 | } |
| 422 | } else { |
| 423 | $current_screen = get_current_screen(); |
| 424 | if ( $current_screen && 'sureforms_form' === $current_screen->post_type ) { |
| 425 | $breadcrumbs[] = [ |
| 426 | 'title' => 'Forms', |
| 427 | 'link' => '', |
| 428 | ]; |
| 429 | } else { |
| 430 | $breadcrumbs[] = [ |
| 431 | 'title' => '', |
| 432 | 'link' => '', |
| 433 | ]; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | return $breadcrumbs; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | /** |
| 442 | * Enqueue Admin Scripts. |
| 443 | * |
| 444 | * @return void |
| 445 | * @since 0.0.1 |
| 446 | */ |
| 447 | public function enqueue_scripts() { |
| 448 | $current_screen = get_current_screen(); |
| 449 | |
| 450 | $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min'; |
| 451 | $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified'; |
| 452 | $js_uri = SRFM_URL . 'assets/js/' . $dir_name . '/'; |
| 453 | $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/'; |
| 454 | |
| 455 | /* RTL */ |
| 456 | if ( is_rtl() ) { |
| 457 | $file_prefix .= '-rtl'; |
| 458 | } |
| 459 | |
| 460 | $localization_data = [ |
| 461 | 'site_url' => get_site_url(), |
| 462 | 'breadcrumbs' => $this->get_breadcrumbs_for_current_page(), |
| 463 | 'sureforms_dashboard_url' => admin_url( '/admin.php?page=sureforms_menu' ), |
| 464 | 'plugin_version' => SRFM_VER, |
| 465 | 'global_settings_nonce' => current_user_can( 'manage_options' ) ? wp_create_nonce( 'wp_rest' ) : '', |
| 466 | 'is_pro_active' => defined( 'SRFM_PRO_VER' ), |
| 467 | 'pro_plugin_version' => defined( 'SRFM_PRO_VER' ) ? SRFM_PRO_VER : '', |
| 468 | 'pro_plugin_name' => defined( 'SRFM_PRO_VER' ) && defined( 'SRFM_PRO_PRODUCT' ) ? SRFM_PRO_PRODUCT : 'SureForms Pro', |
| 469 | 'sureforms_pricing_page' => $this->get_sureforms_website_url( 'pricing' ), |
| 470 | 'field_spacing_vars' => Helper::get_css_vars(), |
| 471 | ]; |
| 472 | |
| 473 | if ( class_exists( 'SRFM_PRO\Admin\Licensing' ) ) { |
| 474 | $license_active = \SRFM_PRO\Admin\Licensing::is_license_active(); |
| 475 | $localization_data['is_license_active'] = $license_active; |
| 476 | } |
| 477 | |
| 478 | if ( SRFM_FORMS_POST_TYPE === $current_screen->post_type || 'toplevel_page_sureforms_menu' === $current_screen->base || 'sureforms_page_sureforms_form_settings' === $current_screen->id || 'sureforms_page_' . SRFM_ENTRIES === $current_screen->id ) { |
| 479 | $asset_handle = '-dashboard'; |
| 480 | |
| 481 | wp_enqueue_style( SRFM_SLUG . $asset_handle . '-font', 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500&display=swap', [], SRFM_VER ); |
| 482 | |
| 483 | $script_asset_path = SRFM_DIR . 'assets/build/dashboard.asset.php'; |
| 484 | $script_info = file_exists( $script_asset_path ) |
| 485 | ? include $script_asset_path |
| 486 | : [ |
| 487 | 'dependencies' => [], |
| 488 | 'version' => SRFM_VER, |
| 489 | ]; |
| 490 | wp_enqueue_script( SRFM_SLUG . $asset_handle, SRFM_URL . 'assets/build/dashboard.js', $script_info['dependencies'], SRFM_VER, true ); |
| 491 | |
| 492 | wp_localize_script( SRFM_SLUG . $asset_handle, 'scIcons', [ 'path' => SRFM_URL . 'assets/build/icon-assets' ] ); |
| 493 | |
| 494 | $localization_data['security_settings_url'] = admin_url( '/admin.php?page=sureforms_form_settings&tab=security-settings' ); |
| 495 | wp_localize_script( |
| 496 | SRFM_SLUG . $asset_handle, |
| 497 | SRFM_SLUG . '_admin', |
| 498 | apply_filters( |
| 499 | SRFM_SLUG . '_admin_filter', |
| 500 | $localization_data |
| 501 | ) |
| 502 | ); |
| 503 | wp_enqueue_style( SRFM_SLUG . '-dashboard', SRFM_URL . 'assets/build/dashboard.css', [], SRFM_VER, 'all' ); |
| 504 | |
| 505 | } |
| 506 | |
| 507 | if ( 'sureforms_page_sureforms_form_settings' === $current_screen->id ) { |
| 508 | wp_enqueue_style( SRFM_SLUG . '-settings', $css_uri . 'backend/settings' . $file_prefix . '.css', [], SRFM_VER ); |
| 509 | } |
| 510 | |
| 511 | // Enqueue styles for the entries page. |
| 512 | if ( 'sureforms_page_' . SRFM_ENTRIES === $current_screen->id ) { |
| 513 | $asset_handle = '-entries'; |
| 514 | wp_enqueue_style( SRFM_SLUG . $asset_handle, $css_uri . 'backend/entries' . $file_prefix . '.css', [], SRFM_VER ); |
| 515 | wp_enqueue_script( SRFM_SLUG . $asset_handle, SRFM_URL . 'assets/build/entries.js', $script_info['dependencies'], SRFM_VER, true ); |
| 516 | } |
| 517 | |
| 518 | // Admin Submenu Styles. |
| 519 | wp_enqueue_style( SRFM_SLUG . '-admin', $css_uri . 'backend/admin' . $file_prefix . '.css', [], SRFM_VER ); |
| 520 | |
| 521 | if ( 'edit-' . SRFM_FORMS_POST_TYPE === $current_screen->id ) { |
| 522 | $asset_handle = 'page_header'; |
| 523 | |
| 524 | $script_asset_path = SRFM_DIR . 'assets/build/' . $asset_handle . '.asset.php'; |
| 525 | $script_info = file_exists( $script_asset_path ) |
| 526 | ? include $script_asset_path |
| 527 | : [ |
| 528 | 'dependencies' => [], |
| 529 | 'version' => SRFM_VER, |
| 530 | ]; |
| 531 | wp_enqueue_script( SRFM_SLUG . '-form-page-header', SRFM_URL . 'assets/build/' . $asset_handle . '.js', $script_info['dependencies'], SRFM_VER, true ); |
| 532 | wp_enqueue_style( SRFM_SLUG . '-form-archive-styles', $css_uri . 'form-archive-styles' . $file_prefix . '.css', [], SRFM_VER ); |
| 533 | } |
| 534 | if ( 'sureforms_page_' . SRFM_FORMS_POST_TYPE . '_settings' === $current_screen->base ) { |
| 535 | $asset_handle = 'settings'; |
| 536 | |
| 537 | $script_asset_path = SRFM_DIR . 'assets/build/' . $asset_handle . '.asset.php'; |
| 538 | $script_info = file_exists( $script_asset_path ) |
| 539 | ? include $script_asset_path |
| 540 | : [ |
| 541 | 'dependencies' => [], |
| 542 | 'version' => SRFM_VER, |
| 543 | ]; |
| 544 | |
| 545 | wp_enqueue_script( SRFM_SLUG . '-settings', SRFM_URL . 'assets/build/' . $asset_handle . '.js', $script_info['dependencies'], SRFM_VER, true ); |
| 546 | wp_enqueue_style( SRFM_SLUG . '-setting-styles', SRFM_URL . 'assets/build/' . $asset_handle . '.css', [ 'wp-components' ], SRFM_VER, 'all' ); |
| 547 | wp_localize_script( |
| 548 | SRFM_SLUG . '-settings', |
| 549 | SRFM_SLUG . '_admin', |
| 550 | $localization_data |
| 551 | ); |
| 552 | } |
| 553 | if ( 'edit-' . SRFM_FORMS_POST_TYPE === $current_screen->id ) { |
| 554 | wp_enqueue_script( SRFM_SLUG . '-form-archive', $js_uri . 'form-archive' . $file_prefix . '.js', [], SRFM_VER, true ); |
| 555 | wp_enqueue_script( SRFM_SLUG . '-export', $js_uri . 'export' . $file_prefix . '.js', [ 'wp-i18n' ], SRFM_VER, true ); |
| 556 | wp_localize_script( |
| 557 | SRFM_SLUG . '-export', |
| 558 | SRFM_SLUG . '_export', |
| 559 | [ |
| 560 | 'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 561 | 'srfm_export_nonce' => wp_create_nonce( 'export_form_nonce' ), |
| 562 | 'site_url' => get_site_url(), |
| 563 | 'srfm_import_endpoint' => '/wp-json/sureforms/v1/sureforms_import', |
| 564 | 'import_form_nonce' => ( current_user_can( 'edit_posts' ) ) ? wp_create_nonce( 'wp_rest' ) : '', |
| 565 | ] |
| 566 | ); |
| 567 | |
| 568 | wp_enqueue_script( SRFM_SLUG . '-backend', $js_uri . 'backend' . $file_prefix . '.js', [], SRFM_VER, true ); |
| 569 | wp_localize_script( |
| 570 | SRFM_SLUG . '-backend', |
| 571 | SRFM_SLUG . '_backend', |
| 572 | [ |
| 573 | 'site_url' => get_site_url(), |
| 574 | ] |
| 575 | ); |
| 576 | |
| 577 | } |
| 578 | |
| 579 | if ( 'sureforms_page_add-new-form' === $current_screen->id ) { |
| 580 | |
| 581 | $file_prefix = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? '' : '.min'; |
| 582 | $dir_name = defined( 'SRFM_DEBUG' ) && SRFM_DEBUG ? 'unminified' : 'minified'; |
| 583 | |
| 584 | $css_uri = SRFM_URL . 'assets/css/' . $dir_name . '/'; |
| 585 | |
| 586 | /* RTL */ |
| 587 | if ( is_rtl() ) { |
| 588 | $file_prefix .= '-rtl'; |
| 589 | } |
| 590 | |
| 591 | wp_enqueue_style( SRFM_SLUG . '-template-picker', $css_uri . 'template-picker' . $file_prefix . '.css', [], SRFM_VER ); |
| 592 | |
| 593 | $sureforms_admin = 'templatePicker'; |
| 594 | |
| 595 | $script_asset_path = SRFM_DIR . 'assets/build/' . $sureforms_admin . '.asset.php'; |
| 596 | $script_info = file_exists( $script_asset_path ) |
| 597 | ? include $script_asset_path |
| 598 | : [ |
| 599 | 'dependencies' => [], |
| 600 | 'version' => SRFM_VER, |
| 601 | ]; |
| 602 | wp_enqueue_script( SRFM_SLUG . '-template-picker', SRFM_URL . 'assets/build/' . $sureforms_admin . '.js', $script_info['dependencies'], SRFM_VER, true ); |
| 603 | |
| 604 | wp_localize_script( |
| 605 | SRFM_SLUG . '-template-picker', |
| 606 | SRFM_SLUG . '_admin', |
| 607 | [ |
| 608 | 'site_url' => get_site_url(), |
| 609 | 'plugin_url' => SRFM_URL, |
| 610 | 'preview_images_url' => SRFM_URL . 'images/template-previews/', |
| 611 | 'admin_url' => admin_url( 'admin.php' ), |
| 612 | 'new_template_picker_base_url' => admin_url( 'post-new.php?post_type=sureforms_form' ), |
| 613 | 'capability' => current_user_can( 'edit_posts' ), |
| 614 | 'template_picker_nonce' => current_user_can( 'edit_posts' ) ? wp_create_nonce( 'wp_rest' ) : '', |
| 615 | 'is_pro_active' => defined( 'SRFM_PRO_VER' ), |
| 616 | 'srfm_ai_usage_details' => AI_Helper::get_current_usage_details(), |
| 617 | 'is_pro_license_active' => AI_Helper::is_pro_license_active(), |
| 618 | 'srfm_ai_auth_user_email' => get_option( 'srfm_ai_auth_user_email' ), |
| 619 | 'pricing_page_url' => $this->get_sureforms_website_url( 'pricing' ), |
| 620 | ] |
| 621 | ); |
| 622 | } |
| 623 | // Quick action sidebar. |
| 624 | $default_allowed_quick_sidebar_blocks = apply_filters( |
| 625 | 'srfm_quick_sidebar_allowed_blocks', |
| 626 | [ |
| 627 | 'srfm/input', |
| 628 | 'srfm/email', |
| 629 | 'srfm/textarea', |
| 630 | 'srfm/checkbox', |
| 631 | 'srfm/number', |
| 632 | 'srfm/inline-button', |
| 633 | 'srfm/advanced-heading', |
| 634 | ] |
| 635 | ); |
| 636 | if ( ! is_array( $default_allowed_quick_sidebar_blocks ) ) { |
| 637 | $default_allowed_quick_sidebar_blocks = []; |
| 638 | } |
| 639 | |
| 640 | $srfm_enable_quick_action_sidebar = get_option( 'srfm_enable_quick_action_sidebar' ); |
| 641 | if ( ! $srfm_enable_quick_action_sidebar ) { |
| 642 | $srfm_enable_quick_action_sidebar = 'disabled'; |
| 643 | } |
| 644 | $quick_sidebar_allowed_blocks = get_option( 'srfm_quick_sidebar_allowed_blocks' ); |
| 645 | $quick_sidebar_allowed_blocks = ! empty( $quick_sidebar_allowed_blocks ) && is_array( $quick_sidebar_allowed_blocks ) ? $quick_sidebar_allowed_blocks : $default_allowed_quick_sidebar_blocks; |
| 646 | $srfm_ajax_nonce = wp_create_nonce( 'srfm_ajax_nonce' ); |
| 647 | wp_enqueue_script( SRFM_SLUG . '-quick-action-siderbar', SRFM_URL . 'assets/build/quickActionSidebar.js', [], SRFM_VER, true ); |
| 648 | wp_localize_script( |
| 649 | SRFM_SLUG . '-quick-action-siderbar', |
| 650 | SRFM_SLUG . '_quick_sidebar_blocks', |
| 651 | [ |
| 652 | 'allowed_blocks' => $quick_sidebar_allowed_blocks, |
| 653 | 'srfm_enable_quick_action_sidebar' => $srfm_enable_quick_action_sidebar, |
| 654 | 'srfm_ajax_nonce' => $srfm_ajax_nonce, |
| 655 | 'srfm_ajax_url' => admin_url( 'admin-ajax.php' ), |
| 656 | ] |
| 657 | ); |
| 658 | |
| 659 | /** |
| 660 | * Enqueuing SureTriggers Integration script. |
| 661 | * This script loads suretriggers iframe in Intergations tab. |
| 662 | */ |
| 663 | if ( SRFM_FORMS_POST_TYPE === $current_screen->post_type ) { |
| 664 | wp_enqueue_script( SRFM_SLUG . '-suretriggers-integration', SRFM_SURETRIGGERS_INTERGATION_BASE_URL . 'js/v2/embed.js', [], SRFM_VER, true ); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * Form Template Picker Admin Body Classes |
| 670 | * |
| 671 | * @since 0.0.1 |
| 672 | * @param string $classes Space separated class string. |
| 673 | */ |
| 674 | public function admin_template_picker_body_class( $classes = '' ) { |
| 675 | $theme_builder_class = isset( $_GET['page'] ) && 'add-new-form' === $_GET['page'] ? 'srfm-template-picker' : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Fetching a $_GET value, no nonce available to validate. |
| 676 | $classes .= ' ' . $theme_builder_class . ' '; |
| 677 | |
| 678 | return $classes; |
| 679 | |
| 680 | } |
| 681 | |
| 682 | /** |
| 683 | * Disable spectra's quick action bar in sureforms CPT. |
| 684 | * |
| 685 | * @param string $status current status of the quick action bar. |
| 686 | * @since 0.0.2 |
| 687 | * @return string |
| 688 | */ |
| 689 | public function restrict_spectra_quick_action_bar( $status ) { |
| 690 | $screen = get_current_screen(); |
| 691 | if ( 'disabled' !== $status && isset( $screen->id ) && 'sureforms_form' === $screen->id ) { |
| 692 | $status = 'disabled'; |
| 693 | } |
| 694 | |
| 695 | return $status; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * Get SureForms Website URL. |
| 700 | * |
| 701 | * @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. |
| 702 | * @since 0.0.7 |
| 703 | * @return string |
| 704 | */ |
| 705 | public static function get_sureforms_website_url( $trail ) { |
| 706 | $url = SRFM_WEBSITE; |
| 707 | if ( ! empty( $trail ) && is_string( $trail ) ) { |
| 708 | $url = SRFM_WEBSITE . $trail; |
| 709 | } |
| 710 | |
| 711 | return esc_url( $url ); |
| 712 | } |
| 713 | |
| 714 | // Entries methods. |
| 715 | |
| 716 | /** |
| 717 | * Handle entry actions. |
| 718 | * |
| 719 | * @since 0.0.13 |
| 720 | * @return void |
| 721 | */ |
| 722 | public function handle_entry_actions() { |
| 723 | if ( isset( $_GET['entry'] ) && isset( $_GET['action'] ) ) { |
| 724 | Entries_List_Table::process_bulk_actions(); |
| 725 | return; |
| 726 | } |
| 727 | if ( ! isset( $_GET['page'] ) || SRFM_ENTRIES !== $_GET['page'] ) { |
| 728 | return; |
| 729 | } |
| 730 | if ( ! isset( $_GET['entry_id'] ) || ! isset( $_GET['action'] ) ) { |
| 731 | return; |
| 732 | } |
| 733 | if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'srfm_entries_action' ) ) { |
| 734 | wp_die( esc_html__( 'Nonce verification failed.', 'sureforms' ) ); |
| 735 | } |
| 736 | $action = isset( $_GET['action'] ) ? sanitize_text_field( wp_unslash( $_GET['action'] ) ) : ''; |
| 737 | $entry_id = Helper::get_integer_value( sanitize_text_field( wp_unslash( $_GET['entry_id'] ) ) ); |
| 738 | $view = isset( $_GET['view'] ) ? sanitize_text_field( wp_unslash( $_GET['view'] ) ) : ''; |
| 739 | if ( $entry_id > 0 ) { |
| 740 | if ( 'read' === $action && 'details' === $view ) { |
| 741 | $entry_status = Entries::get( $entry_id )['status']; |
| 742 | if ( 'trash' === $entry_status ) { |
| 743 | wp_die( esc_html__( 'You cannot view this entry because it is in trash.', 'sureforms' ) ); |
| 744 | } |
| 745 | } |
| 746 | Entries_List_Table::handle_entry_status( $entry_id, $action, $view ); |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | } |
| 751 |