<?php
/**
 * Plugin Name: Pipeline Buster
 * Plugin URI:  https://pipelinebuster.com
 * Description: AI-powered WooCommerce product onboarding. Drop in product images and manual photos — AI reads them, generates all content, and publishes a complete product listing + How To post in one pass.
 * Version:     1.8.14
 * Author:      ZOA
 * Author URI:  https://zoa.captelek.net
 * License:     GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: pipeline-buster
 * Requires at least: 6.0
 * Requires PHP: 8.0
 * WC requires at least: 7.0
 */

defined( 'ABSPATH' ) || exit;

// ── Freemius SDK ──────────────────────────────────────────────────────────────
if ( ! function_exists( 'pipebu_fs' ) ) {
    function pipebu_fs() {
        global $pipebu_fs;
        if ( ! isset( $pipebu_fs ) ) {
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
            $pipebu_fs = fs_dynamic_init( array(
                'id'               => '27716',
                'slug'             => 'pipeline-buster',
                'type'             => 'plugin',
                'public_key'       => 'pk_92b6ca744140fad6acbc921530960',
                'is_premium'       => false,
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_live'          => true,
                'is_org_compliant' => true,
                'menu'             => array(
                    'support' => false,
                ),
            ) );
        }
        return $pipebu_fs;
    }
    pipebu_fs();
    do_action( 'pipebu_fs_loaded' );
}

// Bridge Freemius license state → quota gate.
add_filter( 'pipebu_has_paid_license', function() {
    return pipebu_fs()->is_paying() || pipebu_fs()->is_trial();
} );

// Credit grant on license activation — idempotent via license key dedup in PIPEBU_Quota.
// after_license_activation fires when a paid license is activated (manual key entry or checkout redirect).
// after_install_activation fires on initial opt-in — also hooked as fallback for checkout-redirect flow.
$pb_credit_grant = function() {
    $fs = pipebu_fs();
    if ( ! $fs->is_paying() ) {
        return;
    }
    $plan_id     = (int) $fs->get_plan_id();
    $license     = $fs->get_license();
    $license_key = $license ? (string) $license->secret_key : '';
    $credits     = PIPEBU_Quota::credits_for_plan( $plan_id );
    if ( $credits > 0 && $license_key !== '' ) {
        PIPEBU_Quota::add_credits( $credits, $license_key );
        PIPEBU_Quota::set_plan_id( $plan_id );
    }
};
pipebu_fs()->add_action( 'after_license_activation', $pb_credit_grant );
pipebu_fs()->add_action( 'after_install_activation',  $pb_credit_grant );
// ─────────────────────────────────────────────────────────────────────────────

define( 'PIPEBU_VERSION',    '1.8.14' );
define( 'PIPEBU_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'PIPEBU_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'PIPEBU_OPTION_KEY', 'pipeline_buster_settings' );
define( 'PIPEBU_JOB_META',   'pipebu_generate_job' );
define( 'PIPEBU_MAX_IMAGES', 10 );
define( 'PIPEBU_MAX_MB',     20 );

// One-time opcache reset for v1.5.0.
if ( ! get_transient( 'pipebu_cache_150' ) ) {
    if ( function_exists( 'opcache_reset' ) ) { opcache_reset(); }
    set_transient( 'pipebu_cache_150', 1, HOUR_IN_SECONDS );
}

// One-time migration: rename pb_ option keys → pipebu_ (v1.8.0 prefix compliance).
if ( ! get_option( 'pipebu_migrated_180' ) ) {
    foreach ( [
        'pb_s1' => 'pipebu_s1', 'pb_s1h' => 'pipebu_s1h',
        'pb_s2' => 'pipebu_s2', 'pb_s5'  => 'pipebu_s5',
        'pb_s4' => 'pipebu_s4', 'pb_s4h' => 'pipebu_s4h',
        'pb_seclog' => 'pipebu_seclog',
        'pb_cr'  => 'pipebu_cr',  'pb_crh'  => 'pipebu_crh',
        'pb_crt' => 'pipebu_crt', 'pb_crth' => 'pipebu_crth',
        'pb_crl' => 'pipebu_crl', 'pb_pid'  => 'pipebu_pid',
        'pb_wf'  => 'pipebu_wf',  'pb_wfh'  => 'pipebu_wfh',
        'pb_sale' => 'pipebu_sale', 'pb_gsc_data' => 'pipebu_gsc_data',
    ] as $old => $new ) {
        $val = get_option( $old );
        if ( $val !== false ) {
            update_option( $new, $val, false );
            delete_option( $old );
        }
    }
    update_option( 'pipebu_migrated_180', 1 );
}

require_once PIPEBU_PLUGIN_DIR . 'includes/class-pb-quota.php';
require_once PIPEBU_PLUGIN_DIR . 'includes/class-pb-settings.php';
require_once PIPEBU_PLUGIN_DIR . 'includes/class-pb-gsc.php';
require_once PIPEBU_PLUGIN_DIR . 'includes/class-pb-ai.php';
require_once PIPEBU_PLUGIN_DIR . 'includes/class-pb-generator.php';
require_once PIPEBU_PLUGIN_DIR . 'includes/class-pb-color.php';
require_once PIPEBU_PLUGIN_DIR . 'includes/class-pb-publisher.php';
require_once PIPEBU_PLUGIN_DIR . 'includes/class-pb-job.php';
require_once PIPEBU_PLUGIN_DIR . 'includes/class-pb-admin-2.php';

add_action( 'plugins_loaded', [ 'PIPEBU_Admin', 'init' ] );
add_action( 'pipebu_process_job', [ 'PIPEBU_Job', 'process' ] );
