print_scripts()` */ class Component implements Component_Interface, Templating_Component_Interface { /** * Associative array of JavaScript files, as $handle => $data pairs. * $data must be an array with keys: * - 'file' (file path relative to 'assets/js' directory) - required * - 'global' (whether the file should immediately be enqueued instead of just being registered) * - 'loading' (whether the file should be loaded 'async' or 'defer') * - 'footer' (whether the file should be loaded in the footer) * - 'deps' (array of dependencies) * - 'localize' (array of variables to inject with wp_localize_scripts) * * Not currently implemented * 'preload_callback' * (callback function determining whether the file should be preloaded for the current request). * * Do not access this property directly, instead use the `get_js_files()` method. * * @var array */ protected $js_files; /** * Gets the unique identifier for the theme component. * * @return string Component slug. */ public function get_slug() : string { return 'scripts'; } /** * Adds the action and filter hooks to integrate with WordPress. */ public function initialize() { add_action( 'wp_head', array( $this, 'action_print_remove_no_js' ), 0 ); add_action( 'wp_enqueue_scripts', array( $this, 'action_enqueue_scripts' ) ); } /** * Gets template tags to expose as methods on the Template_Tags class instance, accessible through `ampy()`. * * @return array Associative array of $method_name => $callback_info pairs. Each $callback_info must either be * a callable or an array with key 'callable'. This approach is used to reserve the possibility of * adding support for further arguments in the future. */ public function template_tags() : array { return array( 'print_scripts' => array( $this, 'print_scripts' ), ); } /** * Remove DOM elements with "no-js" class. */ public function action_print_remove_no_js() { if ( ! ampy()->is_amp() ) { echo ''; } } /** * Registers or enqueues JavaScript files. * * JavaScript files that are global are enqueued. All other JavaScript files are only registered, to be enqueued later. */ public function action_enqueue_scripts() { $js_uri = get_theme_file_uri( '/assets/js/' ); $js_dir = get_theme_file_path( '/assets/js/' ); $js_files = $this->get_js_files(); foreach ( $js_files as $handle => $data ) { $src = $js_uri . $data['file']; $version = ampy()->get_asset_version( $js_dir . $data['file'] ); /* * Enqueue global JavaScript files immediately and register the other ones for later use. */ if ( $data['global'] ) { wp_enqueue_script( $handle, $src, $data['deps'], $version, $data['footer'] ); } else { wp_register_script( $handle, $src, $data['deps'], $version, $data['footer'] ); } /** * Set async and deferred attributes. */ if ( 'async' === $data['loading'] ) { wp_script_add_data( $handle, 'async', true ); } if ( 'defer' === $data['loading'] ) { wp_script_add_data( $handle, 'defer', true ); } /** * Uses wp_localize_scripts */ if ( $data['localize'] ) { foreach ( $data['localize'] as $object => $vars ) { wp_localize_script( $handle, $object, $vars ); } } } } /** * Prints JavaScript