on the pages that matter, prescan() enqueues it on `wp` * for a singular view whose post holds a form (also through a synced pattern it references) * or when an active block widget holds one. A form in a template, part or archive loop * prints its stylesheet when it renders. * - `general.load_styles` off: no Formwright front-end CSS anywhere. The handle stays * registered (the editor needs it) and is dropped from every front-end print. * - Editor: the editor script (toolbar toggle, notices) and the editor stylesheet are * attached with editorScript / editorStyle. */ class Assets { /** * Front-end stylesheet handle (also loaded in the editor). */ const STYLE = 'formwright-form'; /** * Editor-only stylesheet handle. */ const EDITOR_STYLE = 'formwright-editor'; /** * Editor script handle. */ const EDITOR_SCRIPT = 'formwright-editor'; /** * View script module ID. */ const VIEW_MODULE = '@formwright/view'; /** * Most synced patterns the prescan looks into. */ const MAX_PATTERN_REFS = 10; /** * Hook the assets. * * @return void */ public static function init() { // Before the block builder registers the blocks (init 5/10): the blocks only name these handles. add_action( 'init', [ self::class, 'register' ], 4 ); add_action( 'wp', [ self::class, 'prescan' ] ); add_filter( 'print_styles_array', [ self::class, 'filter_print_styles' ] ); } /** * Register the styles, the editor script and the view module. * * @return void */ public static function register() { $form = self::asset_data( 'style/form' ); wp_register_style( self::STYLE, self::url( 'style/form.css' ), [], $form['version'] ); wp_style_add_data( self::STYLE, 'rtl', 'replace' ); $editor_style = self::asset_data( 'style/editor' ); wp_register_style( self::EDITOR_STYLE, self::url( 'style/editor.css' ), [ self::STYLE ], $editor_style['version'] ); wp_style_add_data( self::EDITOR_STYLE, 'rtl', 'replace' ); $script = self::asset_data( 'editor/index' ); wp_register_script( self::EDITOR_SCRIPT, self::url( 'editor/index.js' ), $script['dependencies'], $script['version'], true ); wp_set_script_translations( self::EDITOR_SCRIPT, 'formwright', plugin_dir_path( FORMWRIGHT_FILE ) . 'languages' ); $view = self::asset_data( 'view/index' ); wp_register_script_module( self::VIEW_MODULE, self::url( 'view/index.js' ), $view['dependencies'], $view['version'] ); } /** * Enqueue the stylesheet early (in ) when the request will show a form. * * @return void */ public static function prescan() { if ( is_admin() || ! self::styles_enabled() ) { return; } if ( self::queried_post_has_form() || self::widgets_have_form() ) { wp_enqueue_style( self::STYLE ); } } /** * Drop the front-end stylesheet from a front-end print when the site switched it off. * * @param string[] $handles Handles about to print. * @return string[] */ public static function filter_print_styles( $handles ) { if ( is_admin() || self::styles_enabled() || ! is_array( $handles ) ) { return $handles; } return array_values( array_diff( $handles, [ self::STYLE ] ) ); } /** * Whether the site wants Formwright' front-end CSS (Settings: general.load_styles). * * @return bool */ public static function styles_enabled() { return false !== Settings::get_value( 'general.load_styles', true ); } /** * Whether the queried post, or a synced pattern it references, contains a form. * * @return bool */ private static function queried_post_has_form() { if ( ! is_singular() ) { return false; } $post = get_queried_object(); if ( ! $post instanceof \WP_Post || '' === $post->post_content ) { return false; } if ( has_block( Field_Types::FORM_BLOCK, $post->post_content ) ) { return true; } // Synced patterns: , one level deep. if ( false === strpos( $post->post_content, '