.js with the dependencies listed in .asset.php). * * @package Waysill */ defined( 'ABSPATH' ) || exit; /** * Data every Waysill script reads from `window.WAYSILL`. * * @param WP_Post|null $post The edited post, when there is one. * @return array */ function waysill_script_data( $post = null ): array { $data = array( 'axeUrl' => WAYSILL_URL . 'assets/vendor/axe/axe.js', 'settings' => waysill_core_settings(), 'canSeeResults' => waysill_user_can_see_results(), 'checkOnSave' => waysill_user_check_on_save( get_current_user_id() ), 'settingsUrl' => current_user_can( 'manage_options' ) ? admin_url( 'options-general.php?page=waysill' ) : '', ); if ( $post instanceof WP_Post ) { $data['postId'] = $post->ID; $data['previewUrl'] = (string) get_preview_post_link( $post ); $data['highlightNonce'] = wp_create_nonce( 'waysill_highlight_' . $post->ID ); } return $data; } /** * Registers and enqueues a built script with its asset file, preceded by `window.WAYSILL`. * * @param string $entry Entry name: assets/js/.js. * @param array $extra Values added to `window.WAYSILL`. * @return string Script handle. */ function waysill_enqueue_entry( string $entry, array $extra = array() ): string { $handle = 'waysill-' . $entry; $asset = array( 'dependencies' => array(), 'version' => WAYSILL_VERSION, ); $file = WAYSILL_DIR . 'assets/js/' . $entry . '.asset.php'; if ( is_readable( $file ) ) { $asset = require $file; } wp_enqueue_script( $handle, WAYSILL_URL . 'assets/js/' . $entry . '.js', $asset['dependencies'], $asset['version'], true ); wp_add_inline_script( $handle, 'window.WAYSILL = ' . wp_json_encode( array_merge( waysill_script_data( get_post() ), $extra ) ) . ';', 'before' ); if ( in_array( 'wp-i18n', $asset['dependencies'], true ) ) { wp_set_script_translations( $handle, 'waysill' ); } return $handle; } /** Styles shared by the sidebar and the meta box. */ function waysill_enqueue_editor_style(): void { wp_enqueue_style( 'waysill-editor', WAYSILL_URL . 'assets/css/editor.css', array(), WAYSILL_VERSION ); } /** * Whether Waysill works on this post: its type has public previews and the user may edit it and see results. * * @param WP_Post|null $post Post. */ function waysill_editor_enabled( $post ): bool { return $post instanceof WP_Post && is_post_type_viewable( $post->post_type ) && current_user_can( 'edit_post', $post->ID ) && waysill_user_can_see_results(); } /** Block editor: the Accessibility sidebar. */ function waysill_enqueue_block_editor(): void { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; // The hook also fires in the site and widget editors, which have no post preview to check. if ( ! $screen || 'post' !== $screen->base || ! waysill_editor_enabled( get_post() ) ) { return; } waysill_enqueue_entry( 'editor' ); waysill_enqueue_editor_style(); } add_action( 'enqueue_block_editor_assets', 'waysill_enqueue_block_editor' ); /** * Classic editor: the Accessibility meta box. The block editor hides it and shows the sidebar instead. * * @param string $post_type Post type. * @param WP_Post $post Post. */ function waysill_add_meta_box( $post_type, $post ): void { if ( ! waysill_editor_enabled( $post ) ) { return; } add_meta_box( 'waysill', __( 'Accessibility', 'waysill' ), 'waysill_render_meta_box', null, 'side', 'default', array( '__back_compat_meta_box' => true ) ); } add_action( 'add_meta_boxes', 'waysill_add_meta_box', 10, 2 ); /** Meta box body; the script renders the results into it. */ function waysill_render_meta_box(): void { echo '

' . esc_html__( 'Loading…', 'waysill' ) . '

'; } /** * Loads the meta box script on classic post edit screens. * * @param string $hook_suffix Admin page. */ function waysill_enqueue_classic_editor( $hook_suffix ): void { if ( 'post.php' !== $hook_suffix && 'post-new.php' !== $hook_suffix ) { return; } $post = get_post(); if ( ! waysill_editor_enabled( $post ) || use_block_editor_for_post( $post ) ) { return; } waysill_enqueue_entry( 'classic' ); waysill_enqueue_editor_style(); } add_action( 'admin_enqueue_scripts', 'waysill_enqueue_classic_editor' );