<?php
/**
 * Plugin Name: IGIT Related Posts With Thumb
 * Plugin URI:  https://github.com/igit-rpwt
 * Description: Display related posts with thumbnails after your post content. Supports grid/list layouts, shortcode, Gutenberg block, and transient caching.
 * Version:     5.0
 * Author:      Ankur Gandhi (modernised)
 * License:     GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: igit-related-posts-with-thumb-images-after-posts
 * Domain Path: /languages
 *
 * @package IGIT_RPWT
 */

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

// ── Constants ────────────────────────────────────────────────────────────────

define( 'IGIT_RPWT_VERSION',          '5.0' );
define( 'IGIT_RPWT_PLUGIN_DIR',       plugin_dir_path( __FILE__ ) );
define( 'IGIT_RPWT_PLUGIN_URL',       plugin_dir_url( __FILE__ ) );
define( 'IGIT_RPWT_BASENAME',         plugin_basename( __FILE__ ) );
define( 'IGIT_RPWT_PRO_MONTHLY_PRICE', '$6.99' );

// ── Freemius SDK ─────────────────────────────────────────────────────────────
if ( ! function_exists( 'igit_rpwt_fs' ) ) {
	// Create a helper function for easy SDK access.
	function igit_rpwt_fs() {
		global $igit_rpwt_fs;

		if ( ! isset( $igit_rpwt_fs ) ) {
			// Include Freemius SDK.
			require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';

			$igit_rpwt_fs = fs_dynamic_init( array(
				'id'                  => '25804',
				'slug'                => 'igit-related-posts-with-thumb-images-after-posts',
				'premium_slug'        => 'igit-related-pro',
				'type'                => 'plugin',
				'public_key'          => 'pk_a734c9a4205e24ea288ba4b84e9c9',
				'is_premium'          => false,
				'premium_suffix'      => 'Pro',
				'has_premium_version' => true,
				'has_addons'          => false,
				'has_paid_plans'      => true,
				'is_org_compliant'    => true,
				'menu'                => array(
					'slug'       => 'igit-rpwt',
					'first-path' => 'admin.php?page=igit-rpwt',
					'support'    => false,
				),
			) );
		}

		return $igit_rpwt_fs;
	}

	// Init Freemius.
	igit_rpwt_fs();
	// Signal that SDK was initiated.
	do_action( 'igit_rpwt_fs_loaded' );
}

// ── Pro version check ─────────────────────────────────────────────────────────
// Returns true when a valid Freemius license is active for the Pro version.
if ( ! function_exists( 'igit_rpwt_is_pro' ) ) {
	/**
	 * True only when the Pro add-on plugin is physically active (IGIT_RPWT_PRO
	 * constant is defined at boot) AND the Freemius license is valid/not expired.
	 * Requiring the constant prevents stale DB license data from enabling Pro
	 * features when the Pro plugin has been deactivated.
	 */
	function igit_rpwt_is_pro() {
		return defined( 'IGIT_RPWT_PRO' ) && igit_rpwt_fs()->can_use_premium_code();
	}
}

// ── Autoload classes ─────────────────────────────────────────────────────────

require_once IGIT_RPWT_PLUGIN_DIR . 'includes/class-settings.php';
require_once IGIT_RPWT_PLUGIN_DIR . 'includes/class-image-helper.php';
require_once IGIT_RPWT_PLUGIN_DIR . 'includes/class-query.php';
require_once IGIT_RPWT_PLUGIN_DIR . 'includes/class-renderer.php';
require_once IGIT_RPWT_PLUGIN_DIR . 'includes/class-series.php';
require_once IGIT_RPWT_PLUGIN_DIR . 'includes/class-widget.php';
require_once IGIT_RPWT_PLUGIN_DIR . 'includes/class-plugin.php';
require_once IGIT_RPWT_PLUGIN_DIR . 'public/class-frontend.php';

// WP-CLI commands — only loaded when the CLI runner is active.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
	require_once IGIT_RPWT_PLUGIN_DIR . 'includes/class-cli.php';
	WP_CLI::add_command( 'igit-rpwt', 'IGIT_RPWT_CLI' );
}

if ( is_admin() ) {
	require_once IGIT_RPWT_PLUGIN_DIR . 'admin/class-admin.php';
	require_once IGIT_RPWT_PLUGIN_DIR . 'admin/class-metabox.php';
}

// ── Boot ─────────────────────────────────────────────────────────────────────
// Defer boot until plugins_loaded so Freemius and all plugins are fully initialised.
add_action( 'plugins_loaded', function () {
	new IGIT_RPWT_Plugin();
}, 1 );