<?php
/**
 * This file is part of the Wisewand WordPress plugin.
 *
 * @package Wisewand
 * @license GPL-2.0-or-later
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

/**
 * 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;
	}
}
