is_script_debug_enabled() ? 'admin' : 'admin.min'; wp_register_script( self::GUTENBERG_HANDLE, "{$js_dir}{$file}.js", [ 'jquery', 'react', 'react-dom', 'wp-url', ], ADVANCED_SIDEBAR_BASIC_VERSION, true ); // Must register here because used as a dependency of the Gutenberg styles. wp_register_style( self::ADMIN_STYLE, trailingslashit( (string) ADVANCED_SIDEBAR_MENU_URL ) . 'resources/css/advanced-sidebar-menu.css', [], ADVANCED_SIDEBAR_BASIC_VERSION ); if ( ! $this->is_webpack_enabled() ) { wp_register_style( self::GUTENBERG_CSS_HANDLE, "{$js_dir}{$file}.css", [ self::ADMIN_STYLE, 'dashicons', ], ADVANCED_SIDEBAR_BASIC_VERSION ); } wp_set_script_translations( self::GUTENBERG_HANDLE, 'advanced-sidebar-menu', ADVANCED_SIDEBAR_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. * * @action admin_enqueue_scripts 10 0 * * @return void */ public function admin_scripts() { wp_enqueue_script( self::ADMIN_SCRIPT, trailingslashit( (string) ADVANCED_SIDEBAR_MENU_URL ) . 'resources/js/advanced-sidebar-menu.js', [ 'jquery' ], ADVANCED_SIDEBAR_BASIC_VERSION, false ); wp_enqueue_style( self::ADMIN_STYLE ); } /** * Is SCRIPT_DEBUG enabled or passed via URL argument. * * @since 9.0.0 * * @return bool */ public function is_script_debug_enabled() { //phpcs:ignore WordPress.Security.NonceVerification.Recommended return ( \defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) || ! empty( $_GET['script-debug'] ); } /** * 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 && has_filter( 'advanced-sidebar-menu/js-dir' ); } /** * 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' => [ 'basic' => ADVANCED_SIDEBAR_BASIC_VERSION, 'pro' => false, 'scriptDebug' => $this->is_script_debug_enabled(), 'wordpress' => get_bloginfo( 'version' ), ], '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 ) { ?>