<?php
/**
 * Plugin Name:       Flipa — PDF Flipbook
 * Plugin URI:        https://flipa.pro
 * Description:       Turn any PDF into a fast, interactive page-flip book with full right-to-left (Hebrew/Arabic) support and a multilingual viewer. Create unlimited flipbooks and embed them anywhere with a shortcode or block.
 * Version:           0.12.2
 * Requires at least: 5.8
 * Requires PHP:      7.4
 * Author:            Flipa
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       flipa-pdf-flipbook
 * Domain Path:       /languages
 *
 * Engine: StPageFlip 2.0.7 (MIT) + PDF.js (Apache-2.0) + QRCode.js (MIT) + Poppins (OFL-1.1) — all GPL-compatible.
 */

if ( ! defined( 'ABSPATH' ) ) { exit; } // no direct access

define( 'FLIPA_VER',  '0.12.2' );   // kept in sync with the Version: header by build-flipa.sh
define( 'FLIPA_FILE', __FILE__ );
define( 'FLIPA_DIR',  plugin_dir_path( __FILE__ ) );      // .../flipa/
define( 'FLIPA_URL',  plugin_dir_url( __FILE__ ) );       // https://site/wp-content/plugins/flipa/
define( 'FLIPA_CPT',  'flipa_book' );
// Media hotspots (M8) are hidden for now — the feature isn't stable yet. Flip to true to re-enable
// the Media tab + media rendering once it's finished. All the code stays in place either way.
if ( ! defined( 'FLIPA_MEDIA_ENABLED' ) ) { define( 'FLIPA_MEDIA_ENABLED', false ); }

/* -------------------------------------------------------------------------
 * Freemius bootstrap (guarded)
 * In production, drop the Freemius SDK into /freemius and fill in the IDs
 * from your Freemius dashboard. Until then, a stub keeps the plugin fully
 * functional in development (all features unlocked).
 * ---------------------------------------------------------------------- */
if ( ! function_exists( 'flipa_fs' ) ) {
	function flipa_fs() {
		global $flipa_fs;
		if ( isset( $flipa_fs ) ) { return $flipa_fs; }

		$sdk = FLIPA_DIR . 'freemius/start.php';
		// tolerate a nested extraction (e.g. freemius/wordpress-sdk-master/start.php)
		if ( ! file_exists( $sdk ) ) {
			$alt = glob( FLIPA_DIR . 'freemius/*/start.php' );
			if ( ! empty( $alt ) ) { $sdk = $alt[0]; }
		}
		if ( file_exists( $sdk ) ) {
			require_once $sdk;
			$flipa_fs = fs_dynamic_init( array(
				'id'                => '32006',
				'slug'              => 'flipa',
				'type'              => 'plugin',
				'public_key'        => 'pk_0130adbcfdfe0c54effcc16da9fba',   // public — safe to embed
				'is_premium'         => false,           // this uploaded build is the PREMIUM build; Freemius auto-generates the free build (is_premium=false) by stripping __premium_only code
				'premium_slug'       => 'flipa-premium',    // matches the "Paid version slug" in the Freemius dashboard
				'has_premium_version'=> true,
				'has_addons'        => false,
				'has_paid_plans'    => true,    // Pro plan exists in Freemius (1/5/unlimited site tiers + lifetime + trial)
				'is_org_compliant'  => true,
				'menu'              => array(
					'slug'       => 'edit.php?post_type=' . FLIPA_CPT,
					// where Freemius redirects AFTER opt-in / license activation. Without this,
					// a CPT-anchored menu falls back to the non-existent top-level admin.php?page=flipa
					// and WordPress throws "Sorry, you are not allowed to access this page".
					'first-path' => 'edit.php?post_type=' . FLIPA_CPT,
					'account'    => true,
					'contact'    => true,
					'support'    => false,
				),
			) );
		} else {
			// ---- dev stub: behaves like a licensed Pro install ----
			$flipa_fs = new Flipa_FS_Stub();
		}
		return $flipa_fs;
	}

	// Init Freemius (or the stub) and let other files hook in.
	flipa_fs();
	do_action( 'flipa_fs_loaded' );

	// Freemius requires uninstall cleanup via its hook (no uninstall.php in the plugin).
	flipa_fs()->add_action( 'after_uninstall', 'flipa_fs_uninstall_cleanup' );

	// Force the bundled plugin icon on every Freemius UI surface (opt-in/connect screen,
	// account page, plugins list). On a production site Freemius does NOT download the remote
	// icon (that only runs on localhost) and its local-path guessing can miss our file, so it
	// falls back to the generic gray "plug". Handing it the absolute path via the slug-scoped
	// 'fs_plugin_icon_flipa' filter is deterministic and fixes the gray plug on all installs.
	add_filter( 'fs_plugin_icon_flipa', 'flipa_fs_icon_path' );
}

/** Absolute path to the plugin icon for Freemius (false → let Freemius fall back). */
function flipa_fs_icon_path() {
	$icon = FLIPA_DIR . 'assets/icon.png';
	return file_exists( $icon ) ? $icon : false;
}

/**
 * Uninstall cleanup (runs via Freemius `after_uninstall`). By default we KEEP the user's
 * flipbooks; only purge if the site owner opted in via the `flipa_delete_data` option.
 */
function flipa_fs_uninstall_cleanup() {
	if ( ! get_option( 'flipa_delete_data' ) ) { return; }   // keep flipbooks + settings
	$books = get_posts( array(
		'post_type'   => FLIPA_CPT,
		'post_status' => 'any',
		'numberposts' => -1,
		'fields'      => 'ids',
	) );
	foreach ( $books as $book_id ) { wp_delete_post( $book_id, true ); }
	delete_option( 'flipa_delete_data' );
}

/**
 * Minimal Freemius stand-in so the plugin runs without the SDK during dev.
 * Mirrors only the methods Flipa calls.
 */
class Flipa_FS_Stub {
	public function is_paying() { return true; }
	public function is_free_plan() { return false; }
	public function can_use_premium_code() { return true; }
	public function __call( $name, $args ) { return true; } // any other gate → allow
}

/* ---- includes ---- */
require_once FLIPA_DIR . 'includes/class-flipa-license.php';
require_once FLIPA_DIR . 'includes/class-flipa-cpt.php';
require_once FLIPA_DIR . 'includes/class-flipa-admin.php';
require_once FLIPA_DIR . 'includes/class-flipa-frontend.php';
require_once FLIPA_DIR . 'includes/class-flipa-render.php';
require_once FLIPA_DIR . 'includes/class-flipa-block.php';
require_once FLIPA_DIR . 'includes/class-flipa-classic.php';

// ---- Pro-only integrations: these files carry the "__premium_only" suffix so Freemius strips them
// from the generated free (WordPress.org) build. The require block below is also auto-removed from the
// free build (it's inside an is__premium_only() guard), so the free build never references missing files. ----


/* ---- boot ---- */
add_action( 'plugins_loaded', function () {
	// (Translations auto-load on WordPress.org-hosted plugins since WP 4.6 — no manual textdomain loading needed.)
	Flipa_CPT::init();
	Flipa_Admin::init();
	Flipa_Frontend::init();
	Flipa_Render::init();
	Flipa_Block::init();   // Gutenberg — free
	Flipa_Classic::init(); // Classic "Add Flipbook" — free

	// ---- Pro-only surfaces: these classes only exist in the premium build (stripped from the free build),
	// so their init() calls live inside is__premium_only() guards (also auto-removed from the free build).
	// The bookshelf shortcode registers in premium only; the Elementor/WPBakery/WooCommerce surfaces also
	// require an active license at runtime. ----
	
} );

/* ---- activation / deactivation: CPT rewrite rules ---- */
register_activation_hook( __FILE__, function () {
	Flipa_CPT::register();      // ensure the CPT exists before flushing
	flush_rewrite_rules();
} );
register_deactivation_hook( __FILE__, function () {
	flush_rewrite_rules();
} );
