settings = $settings; $this->fields = $fields; } /** * Registers the hooks. * * @since 1.0.0 * * @return void */ public function hooks(): void { add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_editor' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin' ) ); } /** * Loads the sidebar in the block editor. * * @since 1.0.0 * * @return void */ public function enqueue_block_editor(): void { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( null === $screen || 'post' !== $screen->base || ! $screen->is_block_editor() || ! $this->applies( $screen->post_type ) ) { return; } if ( Build_Assets::enqueue( 'editor', self::BLOCK_HANDLE ) ) { $this->print_config( self::BLOCK_HANDLE ); } } /** * Loads the classic editor box scripts and the post list scripts. * * @since 1.0.0 * * @param string $hook_suffix Current admin page. * @return void */ public function enqueue_admin( $hook_suffix ): void { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( null === $screen || ! $this->applies( $screen->post_type ) ) { return; } if ( in_array( $hook_suffix, array( 'post.php', 'post-new.php' ), true ) && ! $screen->is_block_editor() ) { if ( Build_Assets::enqueue( 'classic', self::CLASSIC_HANDLE ) ) { $this->print_config( self::CLASSIC_HANDLE ); // The social image fields open the media library. wp_enqueue_media(); } return; } if ( 'edit.php' === $hook_suffix ) { Build_Assets::enqueue( 'list-table', self::LIST_HANDLE, array( 'inline-edit-post' ), array() ); } } /** * The configuration the scripts read from `window.elevenrankEditor`. * * @since 1.0.0 * * @return array */ public function config(): array { $titles = $this->settings->get( 'titles' ); $resolver = new Resolver( $this->settings, new Variable_Parser() ); $min_words = array(); $templates = array(); foreach ( $this->fields->post_types() as $type ) { $entry = isset( $titles['post_types'][ $type ] ) && is_array( $titles['post_types'][ $type ] ) ? $titles['post_types'][ $type ] : array(); $configured = isset( $entry['min_words'] ) ? (int) $entry['min_words'] : 0; $min_words[ $type ] = $configured > 0 ? $configured : ( 'post' === $type ? self::POST_MIN_WORDS : self::DEFAULT_MIN_WORDS ); $templates[ $type ] = array( 'title' => ! empty( $entry['title'] ) ? (string) $entry['title'] : Defaults::title( 'singular' ), 'description' => ! empty( $entry['description'] ) ? (string) $entry['description'] : Defaults::description( 'singular' ), ); } $config = array( 'version' => ELEVENRANK_VERSION, 'postTypes' => $this->fields->post_types(), 'minWords' => $min_words, 'templates' => $templates, 'variables' => ( new Variable_Parser() )->definitions(), 'locale' => get_locale(), 'canAdvanced' => current_user_can( Capabilities::EDIT_POST_ADVANCED ), 'site' => array( 'name' => wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), 'description' => wp_specialchars_decode( get_bloginfo( 'description' ), ENT_QUOTES ), 'separator' => $resolver->separator(), 'url' => home_url( '/' ), ), ); /** * Filters the configuration the editor scripts read from `window.elevenrankEditor`. * * @since 1.0.0 * * @param array $config Configuration. */ $filtered = apply_filters( 'elevenrank_editor_config', $config ); return is_array( $filtered ) ? $filtered : $config; } /** * Whether the SEO tools apply to a post type for the current user. * * @since 1.0.0 * * @param string $post_type Post type. * @return bool */ private function applies( string $post_type ): bool { return in_array( $post_type, $this->fields->post_types(), true ) && current_user_can( Capabilities::EDIT_POST_SEO ); } /** * Prints the configuration before a script. * * @since 1.0.0 * * @param string $handle Script handle. * @return void */ private function print_config( string $handle ): void { wp_add_inline_script( $handle, 'window.elevenrankEditor = ' . wp_json_encode( $this->config() ) . ';', 'before' ); } }