settings = $settings ?? new SettingsStore(); $this->enableMap = $enableMap; $this->generator = $generator; $this->injectedContext = $context; $this->injectedQuery = $query; } /** * Get module id. * * @return string The result. */ public function getId(): string { return 'schema'; } /** * Get human-readable name. * * @return string The result. */ public function getName(): string { return __( 'Schema', 'rankkernel' ); } /** * Module priority, after metadata. * * @return int The result. */ public function getPriority(): int { return 30; } /** * Dependencies, needs the metadata Context. * * @return string[] The result. */ public function dependsOn(): array { return [ 'metadata' ]; } /** * Whether the module is enabled (delegates to shared map if injected). * * @return bool The result. */ public function isEnabled(): bool { if ( null !== $this->enabledCache ) { return $this->enabledCache; } if ( null !== $this->enableMap ) { $this->enabledCache = $this->enableMap->isEnabled( 'schema' ); return $this->enabledCache; } $map = get_option( 'rankkernel_modules', [] ); if ( ! is_array( $map ) ) { $map = []; } // Support both associative map and indexed list (activation seed is list). if ( array_key_exists( 'schema', $map ) ) { $this->enabledCache = (bool) $map['schema']; } else { $this->enabledCache = in_array( 'schema', $map, true ); } return $this->enabledCache; } /** * Wire services (no hooks yet). */ public function register(): void { } /** * Boot hooks (only if enabled, caller enforces). * * Registers the after tags render hook, the single shared block * category, plus the FAQ and HowTo blocks. Boot itself fires on * init, so the block registration inside already happens on init * and stays behind the enable map gate. */ public function boot(): void { add_action( 'rankkernel/head/after_tags', [ $this, 'render' ], 10 ); add_filter( 'block_categories_all', [ $this, 'addCategory' ] ); ( new FaqBlock() )->register(); ( new HowtoBlock() )->register(); } /** * Append the single shared RankKernel block category. * * Central home for the slug and icon so the FAQ and HowTo blocks * stop registering divergent copies. The per block addCategory * methods stay until the rebuild pass removes them, and each one * skips when this slug already exists, so registration order * decides nothing and the icon stays single. * * @param mixed $categories Registered categories. * @return array> */ public function addCategory( mixed $categories ): array { $out = is_array( $categories ) ? array_values( $categories ) : []; foreach ( $out as $existing ) { $slug = is_array( $existing ) && isset( $existing['slug'] ) ? (string) $existing['slug'] : ''; if ( self::BLOCK_CATEGORY_SLUG === $slug ) { return $out; } } $out[] = [ 'slug' => self::BLOCK_CATEGORY_SLUG, 'title' => 'RankKernel', 'icon' => 'editor-ul', ]; return $out; } /** * Render the @graph as one JSON-LD script tag. * * Receives the HeadRenderer Context through the after_tags slot, so the * request keeps its single Context build and never builds two. * * @param mixed $ctx Context passed by the after_tags action. */ public function render( mixed $ctx = null ): void { $context = $ctx instanceof Context ? $ctx : $this->getContext(); $data = $this->getGenerator()->generate( $context ); $graph = $data['@graph'] ?? []; if ( ! is_array( $graph ) || [] === $graph ) { return; } $flags = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG; $flags = $flags | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP; $json = wp_json_encode( $data, $flags ); if ( ! is_string( $json ) ) { return; } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- JSON-LD already encoded via wp_json_encode. echo '' . "\n"; } /** * Get the generator, built lazily with the default pieces. * * @return Generator The result. */ public function getGenerator(): Generator { if ( null !== $this->generator ) { return $this->generator; } $generator = new Generator(); $generator->register( new OrganizationPiece( $this->settings ) ); $generator->register( new WebsitePiece( $this->settings ) ); $generator->register( new WebpagePiece( $this->settings ) ); $generator->register( new BreadcrumbPiece( $this->settings ) ); $generator->register( new PersonPiece( $this->settings ) ); $generator->register( new ArticlePiece( $this->settings ) ); $generator->register( new FaqPiece() ); $generator->register( new HowtoPiece() ); $generator->register( new ProductPiece( $this->settings ) ); $generator->register( new RecipePiece( $this->settings ) ); $generator->register( new EventPiece( $this->settings ) ); $generator->register( new ServicePiece( $this->settings ) ); $generator->register( new VideoPiece( $this->settings ) ); $generator->register( new BookPiece( $this->settings ) ); $generator->register( new CoursePiece( $this->settings ) ); $generator->register( new JobPostingPiece( $this->settings ) ); $generator->register( new SoftwarePiece( $this->settings ) ); $generator->register( new MusicPiece( $this->settings ) ); $generator->register( new MoviePiece( $this->settings ) ); $generator->register( new ClaimReviewPiece( $this->settings ) ); $generator->register( new DatasetPiece( $this->settings ) ); $generator->register( new PodcastEpisodePiece( $this->settings ) ); $generator->register( new CarouselPiece() ); $generator->register( new QaPagePiece( $this->settings ) ); $generator->register( new ItemListPiece( $this->settings ) ); $generator->register( new LocalBusinessPiece( $this->settings ) ); $generator->register( new ReviewPiece( $this->settings ) ); $generator->register( new ImageObjectPiece( $this->settings ) ); $generator->register( new CustomJsonPiece() ); $this->generator = $generator; return $generator; } /** * Get or build context (once per request, reused on every render call). * * @return Context The result. */ private function getContext(): Context { if ( null !== $this->injectedContext ) { return $this->injectedContext; } if ( null !== $this->context ) { return $this->context; } $query = $this->injectedQuery; if ( null === $query ) { global $wp_query; if ( isset( $wp_query ) && $wp_query instanceof WP_Query ) { $query = $wp_query; } else { $query = new WP_Query(); } } $this->context = new Context( $query, $this->settings, new TagsReplacer() ); return $this->context; } }