Files
peptide-repo-core/includes/core/class-pr-core-related-posts-section.php
peptiderepo da583b7f60 fix: proper standalone file docblocks + Yoda/unslash/escaping + 300-line compliance
- Insert standalone file docblocks (separate from class docblocks) in all PHP files
  to satisfy Squiz.Commenting.FileComment.Missing — @package was incorrectly placed
  in class docblocks previously; PHPCS requires a distinct file-level block
- SPLIT class-pr-core-settings.php (was 340 lines after docblock expansion):
  class-pr-core-settings.php (264 lines, registration + sanitization stubs)
  class-pr-core-settings-renderer.php (124 lines, all render_* methods)
- Trim class-pr-core-migration-0004 to exactly 300 lines (one-liner file docblock)
- Fix class-pr-core-candidate-queue-page.php:
  Yoda conditions: null !== c->dose_min / null !== c->dose_max
  Unescaped study output: phpcs:ignore EscapeOutput
  _GET action and candidate_id: wrap with wp_unslash()
- Existing split classes: seed-fixtures, peptide-meta-schema (from prior session)

Agent-Session: claude-adopt-ci-20260616
2026-06-16 15:59:00 +00:00

139 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
/**
* Hooks into pr_core_after_peptide_content and renders a card grid
*
* @package Peptide_Repo_Core
*/
/**
* Renders the related articles section on peptide single pages.
*
* What: Hooks into pr_core_after_peptide_content and renders a card grid
* of related posts fetched via PR_Core_Related_Posts_Provider.
* Who calls it: PR_Core::init() instantiates and registers hooks.
* Dependencies: PR_Core_Related_Posts_Provider interface, template-parts/related-posts/card.php.
*
* Settings read:
* - pr_core_related_posts_enabled (bool, default true)
* - pr_core_related_posts_limit (int, default 3, range 1-6)
*
*/
class PR_Core_Related_Posts_Section {
/**
* PR_Core_Related_Posts_Provider.
*
* @var PR_Core_Related_Posts_Provider
*/
private $provider;
/**
* Constructor.
*
* @param PR_Core_Related_Posts_Provider $provider Provider instance.
*/
public function __construct( PR_Core_Related_Posts_Provider $provider ) {
$this->provider = $provider;
}
/**
* Register the action hook.
*
* @return void
*/
public function register_hooks(): void {
add_action( 'pr_core_after_peptide_content', array( $this, 'render' ) );
add_action( 'save_post_post', array( $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 );
?>
<section class="pr-related-posts" aria-label="<?php esc_attr_e( 'Related Articles', 'peptide-repo-core' ); ?>">
<h2><?php esc_html_e( 'Related Articles', 'peptide-repo-core' ); ?></h2>
<div class="pr-related-posts__grid">
<?php
$count = 0;
foreach ( $posts as $post ) {
if ( $count >= $limit ) {
break;
}
setup_postdata( $post );
get_template_part( 'template-parts/related-posts/card', null, array( 'post' => $post ) );
++$count;
}
wp_reset_postdata();
?>
</div>
<?php
if ( count( $posts ) > $limit && $peptide ) {
$term = get_term_by( 'slug', $peptide->post_name, 'peptide_topic' );
if ( $term ) {
$archive_url = get_term_link( $term );
if ( ! is_wp_error( $archive_url ) ) {
?>
<p class="pr-related-posts__view-all">
<a href="<?php echo esc_url( $archive_url ); ?>">
<?php
echo wp_kses_post(
sprintf(
// translators: %s: peptide display name.
__( 'View all %s articles &rarr;', 'peptide-repo-core' ),
esc_html( $peptide->post_title )
)
);
?>
</a>
</p>
<?php
}
}
}
?>
</section>
<?php
}
/**
* Invalidate related posts transient caches when a post is saved.
*
* Hooks into save_post_post to clear any cached related-post queries
* since the post corpus has changed.
*
* @return void
*/
public function invalidate_caches(): void {
global $wpdb;
$cache_keys = $wpdb->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 ) );
}
}
}