<?php
/**
 * Article Repository
 *
 * @package Wisewand
 * @since 1.6.0
 *
 * phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- False positive from Plugin Checker
 * All queries use $wpdb->prepare() with proper placeholders. The checker incorrectly flags
 * the prepare() method calls themselves. Known PHPCS limitation.
 *
 * phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery -- Repository pattern requirement
 * phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching -- Custom table, no object cache
 */

namespace Wisewand\Repositories;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Article_Repository
 */
class Article_Repository extends Base_Repository {

	/**
	 * Get WordPress Post ID by Wisewand External ID.
	 *
	 * @param string $external_id Wisewand External ID.
	 * @return int|null Post ID or null if not found.
	 */
	public function get_post_id_by_external_id( $external_id ) {
		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
		$post_id = $this->wpdb->get_var(
			$this->wpdb->prepare(
				"SELECT post_id FROM {$this->wpdb->postmeta}
				 WHERE meta_key = '_wisewand_external_id'
				 AND meta_value = %s
				 LIMIT 1",
				$external_id
			)
		);

		return $post_id ? (int) $post_id : null;
	}
}
