diff --git a/includes/core/class-pr-core-related-posts-section.php b/includes/core/class-pr-core-related-posts-section.php new file mode 100644 index 0000000..6d65ada --- /dev/null +++ b/includes/core/class-pr-core-related-posts-section.php @@ -0,0 +1,126 @@ +provider = $provider; + } + + /** + * Register the action hook. + * + * @return void + */ + public function register_hooks(): void { + add_action( 'pr_core_after_peptide_content', [ $this, 'render' ] ); + add_action( 'save_post_post', [ $this, 'invalidate_caches' ] ); + } + + /** + * Render the related posts section. + * + * Called via do_action( 'pr_core_after_peptide_content', $peptide_id ) in templates. + * If feature disabled or no posts found, renders nothing. + * + * @param int $peptide_id The peptide post ID. + * @return void + */ + public function render( int $peptide_id ): void { + $enabled = (bool) get_option( 'pr_core_related_posts_enabled', true ); + if ( ! $enabled ) { + return; + } + + $limit = (int) get_option( 'pr_core_related_posts_limit', 3 ); + $limit = min( 6, max( 1, $limit ) ); + + $posts = $this->provider->get_posts( $peptide_id, $limit + 5 ); + if ( empty( $posts ) ) { + return; + } + + $peptide = get_post( $peptide_id ); + ?> + + get_col( + "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE 'pr_core_related_%'" + ); + foreach ( $cache_keys as $key ) { + delete_transient( str_replace( '_transient_', '', $key ) ); + } + } +}