.js|.asset.php|.css|-rtl.css`) under the * `foldivo-*` handles and injects `window.foldivoBoot` before `foldivo-core`. * * Registration is lazy: in the admin on `admin_enqueue_scripts`, elsewhere (front-end page * builders) only when an `enqueue_*()` helper runs, so ordinary front-end pages never pay for it. * * The boot data and the REST preload (`GET /foldivo/v1/folders?space=…` for every space a * screen asked for) are added at print time, right before the footer scripts are printed, so * every space requested during the page render is included. */ final class Assets { /** * Build entries => script handles, in registration order (core first). * * @var array */ const HANDLES = array( 'core' => 'foldivo-core', 'media' => 'foldivo-media', 'list' => 'foldivo-list', 'dataviews' => 'foldivo-dataviews', 'settings' => 'foldivo-settings', 'uploader' => 'foldivo-uploader', ); /** * Dependencies that the bundles use through globals (not detected by the build). * * @var array */ const EXTRA_DEPENDENCIES = array( 'media' => array( 'media-views' ), 'uploader' => array( 'plupload-handlers' ), ); /** * Handle of the inline-only admin stylesheet (Foldivo screens rendered in PHP). * * @var string */ const ADMIN_STYLE = 'foldivo-admin'; /** * Fallback instance, when the module was not booted by the plugin. * * @var Assets|null */ private static $fallback = null; /** * Whether the bundles were registered. * * @var bool */ private $registered = false; /** * Whether the boot data was added to `foldivo-core`. * * @var bool */ private $boot_added = false; /** * Spaces requested by the screen (boot `space` + preloaded trees), in request order. * * @var string[] */ private $spaces = array(); /** * Extra REST paths to preload. * * @var string[] */ private $preload_paths = array(); /** * Directory of the compiled files. * * @var string */ private $build_dir; /** * URL of the compiled files. * * @var string */ private $build_url; /** * Constructor. * * @param string $build_dir Optional build directory (tests). * @param string $build_url Optional build URL (tests). */ public function __construct( $build_dir = '', $build_url = '' ) { $this->build_dir = '' !== $build_dir ? trailingslashit( $build_dir ) : FOLDIVO_PATH . 'build/'; $this->build_url = '' !== $build_url ? trailingslashit( $build_url ) : FOLDIVO_URL . 'build/'; } /** * Returns the booted module (or a registered fallback instance). * * @return Assets */ public static function get() { $module = Plugin::instance()->get_module( __CLASS__ ); if ( $module instanceof self ) { return $module; } if ( null === self::$fallback ) { self::$fallback = new self(); self::$fallback->register(); } return self::$fallback; } /** * Registers the hooks. * * @return void */ public function register() { add_action( 'admin_enqueue_scripts', array( $this, 'register_assets' ), 1 ); foreach ( array( 'admin_print_footer_scripts', 'wp_print_footer_scripts', 'customize_controls_print_footer_scripts' ) as $hook ) { add_action( $hook, array( $this, 'add_boot_data' ), 1 ); } } /** * Registers every available bundle once. * * @return void */ public function register_assets() { if ( $this->registered ) { return; } $this->registered = true; foreach ( self::HANDLES as $entry => $handle ) { $this->register_entry( $entry, $handle ); } // Inline-only stylesheet for the PHP-rendered Foldivo screens (no file, no request). if ( ! wp_style_is( self::ADMIN_STYLE, 'registered' ) ) { wp_register_style( self::ADMIN_STYLE, false, array(), FOLDIVO_VERSION ); } /** * Fires after the Foldivo bundles are registered (extensions may register theirs). * * @param Assets $assets Assets service. */ do_action( 'foldivo_assets_registered', $this ); } /** * Registers the script, translations and styles of one entry. * * @param string $entry Entry name (`core`, `media`, …). * @param string $handle Script handle. * @return bool Whether the script is registered. */ private function register_entry( string $entry, string $handle ): bool { if ( wp_script_is( $handle, 'registered' ) ) { return true; } $asset_file = $this->build_dir . $entry . '.asset.php'; if ( ! is_readable( $asset_file ) ) { return false; } $asset = include $asset_file; $asset = is_array( $asset ) ? $asset : array(); $deps = isset( $asset['dependencies'] ) ? (array) $asset['dependencies'] : array(); $version = isset( $asset['version'] ) ? (string) $asset['version'] : FOLDIVO_VERSION; if ( isset( self::EXTRA_DEPENDENCIES[ $entry ] ) ) { $deps = array_merge( $deps, self::EXTRA_DEPENDENCIES[ $entry ] ); } if ( 'core' !== $entry && ! in_array( 'foldivo-core', $deps, true ) ) { $deps[] = 'foldivo-core'; } /** * Filters the dependencies of a Foldivo script. * * @param string[] $deps Script handles. * @param string $entry Build entry. * @param string $handle Script handle. */ $deps = (array) apply_filters( 'foldivo_script_dependencies', array_values( array_unique( $deps ) ), $entry, $handle ); wp_register_script( $handle, $this->build_url . $entry . '.js', $deps, $version, array( 'in_footer' => true ) ); wp_set_script_translations( $handle, 'foldivo', FOLDIVO_PATH . 'languages' ); if ( is_readable( $this->build_dir . $entry . '.css' ) ) { if ( 'core' === $entry ) { $style_deps = array( 'wp-components' ); } else { $style_deps = wp_style_is( 'foldivo-core', 'registered' ) ? array( 'foldivo-core' ) : array(); } wp_register_style( $handle, $this->build_url . $entry . '.css', $style_deps, $version ); if ( is_readable( $this->build_dir . $entry . '-rtl.css' ) ) { wp_style_add_data( $handle, 'rtl', 'replace' ); } } return true; } /** * Enqueues `foldivo-core` (script + style) and records the screen space. * * @param Space|string $space Optional space (its tree is preloaded). * @return bool False when the bundle is not available (not built). */ public function enqueue_core( $space = '' ): bool { $this->register_assets(); if ( ! wp_script_is( 'foldivo-core', 'registered' ) ) { return false; } $this->add_space( $space ); wp_enqueue_script( 'foldivo-core' ); if ( wp_style_is( 'foldivo-core', 'registered' ) ) { wp_enqueue_style( 'foldivo-core' ); } return true; } /** * Media library bridge (grid, every `wp.media` modal, page builders). * * @return bool */ public function enqueue_media_integration(): bool { return $this->enqueue_entry( 'media', 'attachment' ); } /** * List screens (`upload.php` list mode, `edit.php` of a space). * * @param Space|string $space Space. * @return bool */ public function enqueue_list_integration( $space ): bool { return $this->enqueue_entry( 'list', $space ); } /** * Settings application (Media → Foldivo). * * @return bool */ public function enqueue_settings(): bool { $this->add_preload_path( '/' . Plugin::REST_NAMESPACE . '/settings' ); return $this->enqueue_entry( 'settings', '' ); } /** * DataViews media modal integration (block editor). * * @return bool */ public function enqueue_dataviews(): bool { return $this->enqueue_entry( 'dataviews', 'attachment' ); } /** * Folder selector of media-new.php. * * @return bool */ public function enqueue_uploader(): bool { return $this->enqueue_entry( 'uploader', 'attachment' ); } /** * Enqueues the inline-only admin stylesheet with its CSS. * * @param string $css CSS to add (once per call site). * @return void */ public function enqueue_admin_style( string $css ) { $this->register_assets(); wp_enqueue_style( self::ADMIN_STYLE ); if ( '' !== $css ) { wp_add_inline_style( self::ADMIN_STYLE, $css ); } } /** * Adds a REST path to preload on this page (GET only). * * @param string $path Path, e.g. `/foldivo/v1/settings`. * @return void */ public function add_preload_path( string $path ) { $path = '/' . ltrim( $path, '/' ); if ( ! in_array( $path, $this->preload_paths, true ) ) { $this->preload_paths[] = $path; } } /** * Spaces requested by the current screen. * * @return string[] */ public function get_requested_spaces(): array { return $this->spaces; } /** * Boot data (`window.foldivoBoot`), filtered by `foldivo_boot_data`. * * @param string $space_id Main space of the screen ('' when none). * @return array */ public function get_boot_data( string $space_id = '' ): array { $spaces = array(); $preferences = array(); foreach ( Plugin::instance()->spaces()->visible() as $id => $space ) { $object = get_post_type_object( $space->post_type ); $spaces[ $id ] = array_merge( $space->to_array(), array( // Plural name of the post type ("Media", "Pages"…), for settings and headings. 'label' => $object && ! empty( $object->labels->name ) ? (string) $object->labels->name : (string) $id, 'canManage' => Capabilities::user_can( 'manage', $space ), 'canAssign' => Capabilities::user_can( 'assign', $space ), ) ); $preferences[ $id ] = Preferences::get( $space ); } $is_pro = foldivo_is_pro_active(); $data = array( 'version' => FOLDIVO_VERSION, 'apiVersion' => (int) FOLDIVO_API_VERSION, 'spaces' => $spaces, 'settings' => Settings::get_public(), 'preferences' => $preferences, 'isPro' => $is_pro, 'proUrl' => $is_pro ? '' : Upsell::pro_url( 'app' ), // Foldivo Pro suggestions (tips, menu item, footer): `show` is false with Pro or when turned off. 'pro' => Pro_Mentions::boot_data(), 'adminUrl' => admin_url(), 'currentScreen' => $this->current_screen_id(), 'space' => $space_id, 'settingsUrl' => current_user_can( 'manage_options' ) ? Settings_Page::url() : '', ); /** * Filters the boot data injected before `foldivo-core` (`window.foldivoBoot`). * * @param array $data Boot data. * @param string $space_id Main space of the screen ('' when none). */ $data = apply_filters( 'foldivo_boot_data', $data, $space_id ); return is_array( $data ) ? $data : array(); } /** * REST preload data for the requested spaces and paths (`rest_preload_api_request` format). * * @return array */ public function get_preload_data(): array { $paths = $this->preload_paths; foreach ( $this->spaces as $space_id ) { $space = Plugin::instance()->spaces()->get( $space_id ); if ( $space && Capabilities::user_can( 'view', $space ) ) { // Exactly the path built by the JS store: addQueryArgs( '/foldivo/v1/folders', { space } ). $paths[] = '/' . Plugin::REST_NAMESPACE . '/folders?space=' . rawurlencode( $space_id ); } } /** * Filters the REST paths preloaded for the Foldivo scripts. * * @param string[] $paths Paths. */ $paths = array_values( array_unique( array_filter( array_map( 'strval', (array) apply_filters( 'foldivo_preload_paths', $paths ) ) ) ) ); if ( ! $paths || ! function_exists( 'rest_preload_api_request' ) || ! is_user_logged_in() ) { return array(); } return array_reduce( $paths, 'rest_preload_api_request', array() ); } /** * Inline script printed before `foldivo-core`: boot data, then the preloading middleware. * * @return string */ public function get_inline_script(): string { $main = isset( $this->spaces[0] ) ? $this->spaces[0] : ''; $boot = $this->get_boot_data( $main ); // Maps indexed by space id must stay JSON objects, even when empty. foreach ( array( 'spaces', 'preferences' ) as $key ) { if ( isset( $boot[ $key ] ) && is_array( $boot[ $key ] ) && ! $boot[ $key ] ) { $boot[ $key ] = new \stdClass(); } } $script = 'window.foldivoBoot = ' . wp_json_encode( $boot, JSON_HEX_TAG | JSON_HEX_AMP ) . ';'; $preload = $this->get_preload_data(); if ( $preload ) { $script .= "\nif ( window.wp && wp.apiFetch && wp.apiFetch.createPreloadingMiddleware ) { wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( " . wp_json_encode( $preload, JSON_HEX_TAG | JSON_HEX_AMP ) . ' ) ); }'; } return $script; } /** * Print-time hook: adds the boot data once, if `foldivo-core` is about to be printed. * * @return void */ public function add_boot_data() { if ( $this->boot_added || ! wp_script_is( 'foldivo-core', 'registered' ) ) { return; } if ( ! wp_script_is( 'foldivo-core', 'enqueued' ) || wp_script_is( 'foldivo-core', 'done' ) ) { return; } $this->boot_added = true; wp_add_inline_script( 'foldivo-core', $this->get_inline_script(), 'before' ); } /** * Enqueues an entry (and the core). * * @param string $entry Entry name. * @param Space|string $space Screen space. * @return bool */ private function enqueue_entry( string $entry, $space ): bool { if ( ! $this->enqueue_core( $space ) ) { return false; } $handle = self::HANDLES[ $entry ]; if ( ! wp_script_is( $handle, 'registered' ) ) { return false; } wp_enqueue_script( $handle ); if ( wp_style_is( $handle, 'registered' ) ) { wp_enqueue_style( $handle ); } return true; } /** * Records a requested space. * * @param Space|string $space Space or space id. * @return void */ private function add_space( $space ) { $id = $space instanceof Space ? $space->id : sanitize_key( (string) $space ); if ( '' !== $id && ! in_array( $id, $this->spaces, true ) ) { $this->spaces[] = $id; } } /** * Current screen id (`upload`, `edit-post`, `media_page_foldivo`…), `front` outside the admin. * * @return string */ private function current_screen_id(): string { if ( ! is_admin() ) { return 'front'; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; return $screen ? (string) $screen->id : ''; } }