renderer = $renderer; $this->feedRepo = $feedRepo; } /** * Registers WordPress hooks. */ public function register(): void { add_action( 'init', [ $this, 'registerBlock' ] ); } /** * Registers the block type from block.json. */ public function registerBlock(): void { $blockJsonPath = FEEDNETIC_DIR . 'build/block.json'; if ( ! file_exists( $blockJsonPath ) ) { return; } // block.json's static "version" would be the ver of the editor style // (file:./style-frontend.css); use the file's content hash instead. The // editor script already gets the hash from block.asset.php. $withVersion = static function ( $metadata ) { if ( is_array( $metadata ) && 'feednetic/feed' === ( $metadata['name'] ?? '' ) ) { $metadata['version'] = \Feednetic\AssetVersion::of( 'build/style-frontend.css' ); } return $metadata; }; add_filter( 'block_type_metadata', $withVersion ); register_block_type( $blockJsonPath, [ 'render_callback' => [ $this, 'render' ], ] ); remove_filter( 'block_type_metadata', $withVersion ); // Provide the admin URL so the block JS can link to the feed editor. wp_localize_script( 'feednetic-feed-editor-script', 'feedneticBlock', [ 'adminUrl' => admin_url( 'admin.php?page=feednetic' ), ] ); } /** * Server-side render callback. * * @param array $attributes Block attributes. * @return string */ public function render( array $attributes ): string { $feedId = isset( $attributes['feedId'] ) ? (int) $attributes['feedId'] : 0; if ( $feedId <= 0 ) { return $this->renderPlaceholder( __( 'Select a feed in the block settings.', 'feednetic' ) ); } // Allow draft feeds in the block editor preview (REST_REQUEST is true // when ServerSideRender triggers this callback). $allowDraft = defined( 'REST_REQUEST' ) && REST_REQUEST; $html = $this->renderer->render( $feedId, [], $allowDraft ); if ( '' === $html ) { return $this->renderPlaceholder( __( 'Feed not found or is not published.', 'feednetic' ) ); } // Record page context so CachePurger can invalidate the right URLs. // The filter lets tests override the is_admin() guard (it is always true // in WP-CLI integration tests because WP_ADMIN is defined as a constant). $postId = get_the_ID(); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- slash namespace is intentional $shouldRecord = apply_filters( 'feednetic/record_page_for_feed', ! is_admin(), $feedId ); if ( $shouldRecord ) { if ( $postId ) { // Standard post/page: track the post ID so CachePurger can purge it. $this->recordPageForFeed( $feedId, $postId ); } else { // Block-theme template part: get_the_ID() returns 0 — no post context. // Flag this feed so CachePurger falls back to a full-domain purge. // Stored in the feed record (not a transient) so object-cache eviction // never loses the flag. $this->feedRepo->markHeadless( $feedId ); } } return $html; } // ── Private helpers ──────────────────────────────────────────────────────── /** * Stores a feedId → pageId mapping in a transient so CachePurger can find * all pages without a post_content scan on each purge. */ private function recordPageForFeed( int $feedId, int $postId ): void { $key = 'feednetic_feed_pages_' . $feedId; $existing = get_transient( $key ); $pageIds = is_array( $existing ) ? $existing : []; if ( ! in_array( $postId, $pageIds, true ) ) { $pageIds[] = $postId; // 30-day expiry; rebuilt on every new page render. set_transient( $key, $pageIds, 30 * DAY_IN_SECONDS ); } } /** * Returns a safe, editor-visible placeholder paragraph. */ private function renderPlaceholder( string $message ): string { return '

' . esc_html( $message ) . '

'; } }