get_dist_file( 'advanced-sidebar-menu-block-editor', 'js' ), [ 'jquery', 'lodash', 'react', 'react-dom', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-hooks', 'wp-html-entities', 'wp-i18n', 'wp-server-side-render', 'wp-url', ], ADVANCED_SIDEBAR_MENU_BASIC_VERSION, true ); if ( ! $this->is_webpack_enabled() ) { wp_register_style( static::GUTENBERG_CSS_HANDLE, $this->get_dist_file( 'advanced-sidebar-menu-block-editor', 'css' ), [ 'dashicons', ], ADVANCED_SIDEBAR_MENU_BASIC_VERSION ); } wp_set_script_translations( static::GUTENBERG_HANDLE, 'advanced-sidebar-menu', ADVANCED_SIDEBAR_MENU_DIR . 'languages' ); /** * Load separately because `$this->js_config()` is heavy, and * the block scripts must be registered before we have * access to `wp_should_load_block_editor_scripts_and_styles`. */ add_action( 'enqueue_block_editor_assets', function() { wp_localize_script( self::GUTENBERG_HANDLE, 'ADVANCED_SIDEBAR_MENU', $this->js_config() ); }, 1 ); } /** * Add JS and CSS to the admin and in specific cases the front-end. * * @return void */ public function admin_scripts() { wp_enqueue_script( static::ADMIN_SCRIPT, $this->get_dist_file( 'advanced-sidebar-menu-admin', 'js' ), [ 'jquery', ], ADVANCED_SIDEBAR_MENU_BASIC_VERSION, false ); if ( ! $this->is_webpack_enabled() ) { wp_enqueue_style( static::ADMIN_STYLE, $this->get_dist_file( 'advanced-sidebar-menu-admin', 'css' ), [], ADVANCED_SIDEBAR_MENU_BASIC_VERSION ); } /** * Fire action when admin scripts are being loaded. * Simply loading additional scripts such as "runtime" in any context * the admin scripts are used. * * @since 9.3.1 * * @param Scripts $scripts - The Scripts instance. */ do_action( 'advanced-sidebar-menu/scripts/admin-scripts', $this ); } /** * Is SCRIPT_DEBUG enabled or passed via URL argument. * * @since 9.0.0 * * @return bool */ public function is_script_debug_enabled() { if ( SCRIPT_DEBUG ) { return true; } //phpcs:ignore -- Not using nonce because users enter manually in url. if ( isset( $_GET['script-debug'] ) && 'true' === $_GET['script-debug'] ) { return true; } return false; } /** * Are we currently developing locally with Webpack enabled? * * Provides a consistent interface for determining considerations * when Webpack is enabled. * * @since 9.0.0 * * @return bool */ public function is_webpack_enabled() { return SCRIPT_DEBUG && file_exists( ADVANCED_SIDEBAR_MENU_DIR . '/js/dist/.running' ); } /** * Use the development version of React to improve our ErrorBoundary data. * * Provides more useful debugging information. * * - Do not change if we are already on SCRIPT_DEBUG. * - Do not change if our custom `$_GET['script-debug']` is not available. * - Only available in the context of Gutenberg. * * @since 9.0.10 * * @return void */ public function use_development_version_of_react() { if ( SCRIPT_DEBUG || ! $this->is_script_debug_enabled() ) { return; } $script = wp_scripts()->query( 'react', 'scripts' ); if ( is_a( $script, \_WP_Dependency::class ) ) { $script->src = \str_replace( wp_scripts_get_suffix(), '', $script->src ); } $script = wp_scripts()->query( 'react-dom', 'scripts' ); if ( is_a( $script, \_WP_Dependency::class ) ) { $script->src = \str_replace( wp_scripts_get_suffix(), '', $script->src ); } } /** * Configuration passed from PHP to JavaScript. * * @return array */ public function js_config() { return apply_filters( 'advanced-sidebar-menu/scripts/js-config', [ 'categories' => [ 'displayEach' => Category::get_display_each_options(), ], 'currentScreen' => is_admin() ? get_current_screen()->base : '', 'docs' => [ 'page' => Core::instance()->get_documentation_url( Page::NAME ), 'category' => Core::instance()->get_documentation_url( Category::NAME ), ], 'error' => apply_filters( 'advanced-sidebar-menu/scripts/js-config/error', '' ), 'features' => Notice::instance()->get_features(), 'isPostEdit' => ! empty( $GLOBALS['pagenow'] ) && 'post.php' === $GLOBALS['pagenow'], 'isPro' => false, 'isWidgets' => ! empty( $GLOBALS['pagenow'] ) && 'widgets.php' === $GLOBALS['pagenow'], 'pages' => [ 'orderBy' => Page::get_order_by_options(), ], 'siteInfo' => Debug::instance()->get_site_info(), 'support' => 'https://wordpress.org/support/plugin/advanced-sidebar-menu/#new-topic-0', ] ); } /** * Trigger any JS needed by the widgets. * This is outputted into the markup for each widget, so it may be * trigger whether the widget is loaded on the front-end by * page builders or the backend by standard WordPress or * really anywhere. * * @notice Does not work in Gutenberg as widget's markup is loaded * via the REST API and React. * * @return void */ public function init_widget_js() { if ( WP_DEBUG ) { ?> is_script_debug_enabled() ? $file_slug : "{$file_slug}.min"; return "{$js_dir}{$file_slug}.{$extension}"; } }