<?php

/**
 * Plugin Name: Plugixa Recipe
 * Plugin URI: https://plugixa.com/recipe
 * Description: A modern recipe management plugin with React admin panel, nutrition facts, user reviews, collections, meal planning, AI generation, and multiple display layouts.
 * Version: 1.0.0
 * Author: Plugixa
 * Author URI: https://plugixa.com
 * Copyright (C) 2026 Plugixa (https://plugixa.com)
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: plugixarecipe
 * Requires at least: 6.5
 * Requires PHP: 8.0
 *
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// Plugin constants
define( 'PLUGIXARECIPE_VERSION', '1.0.0' );
define( 'PLUGIXARECIPE_PATH', plugin_dir_path( __FILE__ ) );
define( 'PLUGIXARECIPE_URL', plugin_dir_url( __FILE__ ) );
define( 'PLUGIXARECIPE_BASENAME', plugin_basename( __FILE__ ) );
// Core Composer autoloader.
if ( file_exists( PLUGIXARECIPE_PATH . 'vendor/autoload.php' ) ) {
    require_once PLUGIXARECIPE_PATH . 'vendor/autoload.php';
}
// Premium dependencies (PDF Parser, used by the document importer) live in pro/vendor
// and are absent from the free build. Load them only when the overlay is present.
if ( file_exists( PLUGIXARECIPE_PATH . 'pro/vendor/autoload.php' ) ) {
    require_once PLUGIXARECIPE_PATH . 'pro/vendor/autoload.php';
}
// --- Freemius SDK bootstrap --------------------------------------------------
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals -- Freemius SDK helper function, global, and `*_loaded` hook use the SDK's mandated `pr_fs_pxr` naming.
if ( !function_exists( 'pr_fs_pxr' ) ) {
    // Create a helper function for easy SDK access.
    function pr_fs_pxr() {
        global $pr_fs_pxr;
        if ( !isset( $pr_fs_pxr ) ) {
            // Activate multisite network integration.
            if ( !defined( 'WP_FS__PRODUCT_27466_MULTISITE' ) ) {
                define( 'WP_FS__PRODUCT_27466_MULTISITE', true );
            }
            // Include Freemius SDK.
            require_once PLUGIXARECIPE_PATH . 'vendor/freemius/wordpress-sdk/start.php';
            $pr_fs_pxr = fs_dynamic_init( array(
                'id'               => '27466',
                'slug'             => 'plugixa-recipe',
                'type'             => 'plugin',
                'public_key'       => 'pk_d4efae3d93c40d63f87132ce4f363',
                'is_premium'       => false,
                'premium_suffix'   => 'Pro',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => array(
                    'slug'    => 'plugixa-recipe',
                    'support' => false,
                ),
                'is_live'          => true,
            ) );
            // No trial is offered. Suppress every Freemius trial promotion - the
            // "Start free trial" admin banner, the "Start Trial" menu item, and the
            // trial pricing CTA - so the wp.org free build shows only "Upgrade".
            $pr_fs_pxr->add_filter( 'show_trial', '__return_false' );
        }
        return $pr_fs_pxr;
    }

    // Init Freemius.
    pr_fs_pxr();
    // Signal that SDK was initiated.
    do_action( 'pr_fs_pxr_loaded' );
}
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals
/**
 * PSR-4 style autoloader for plugin classes.
 * Resolves from pro/includes/ first (when shipped), falls back to includes/.
 */
spl_autoload_register( function ( $class ) {
    $prefix = 'PlugixaRecipe\\';
    $len = strlen( $prefix );
    if ( strncmp( $prefix, $class, $len ) !== 0 ) {
        return;
    }
    $relative_class = substr( $class, $len );
    $parts = explode( '\\', $relative_class );
    $class_name = array_pop( $parts );
    // Convert CamelCase to kebab-case: RecipesController -> recipes-controller
    $file_name = 'class-' . strtolower( preg_replace( '/([a-z])([A-Z])/', '$1-$2', $class_name ) ) . '.php';
    $dir_path = '';
    if ( !empty( $parts ) ) {
        $dir_path = strtolower( implode( '/', $parts ) ) . '/';
    }
    foreach ( [PLUGIXARECIPE_PATH . 'pro/includes/', PLUGIXARECIPE_PATH . 'includes/'] as $base_dir ) {
        $file = $base_dir . $dir_path . $file_name;
        if ( file_exists( $file ) ) {
            require $file;
            return;
        }
    }
} );
/**
 * Plugin activation hook
 */
register_activation_hook( __FILE__, function () {
    \PlugixaRecipe\Activator::activate();
} );
/**
 * Plugin deactivation hook
 */
register_deactivation_hook( __FILE__, function () {
    \PlugixaRecipe\Deactivator::deactivate();
} );
/**
 * Plugin uninstall cleanup.
 *
 * Freemius ships its own uninstall.php and forbids the plugin from bundling
 * one, so the cleanup runs via the Freemius `after_uninstall` action instead.
 */
pr_fs_pxr()->add_action( 'after_uninstall', function () {
    \PlugixaRecipe\Uninstaller::uninstall();
} );
/**
 * Initialize the plugin
 */
add_action( 'plugins_loaded', function () {
    \PlugixaRecipe\Plugin::instance();
} );