<?php

/**
 * Plugin Name: Content Performance Analyzer
 * Plugin URI: https://cpanalyzer.com/
 * Description: Your AI content strategist: connects GA4 and Search Console, finds pages losing traffic or conversions, and writes a specific fix plan for each page.
 * Version: 1.5.1
 * Author: Wajahat Mubashir
 * Author URI: https://wajahatmubashir.netlify.app/
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: content-performance-analyzer
 * Domain Path: /languages
 * Requires at least: 6.2
 * Tested up to: 7.0.2
 * Requires PHP: 7.4
 *
 */
// Prevent direct access
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
// The free and paid packages can briefly coexist during an in-dashboard
// upgrade. Let the SDK that loaded first own the shared integration and stop
// this copy before any plugin classes or global functions are declared.
if ( function_exists( 'cpanalyzer_fs' ) ) {
    cpanalyzer_fs()->set_basename( false, __FILE__ );
    return;
}
if ( !function_exists( 'cpanalyzer_fs' ) ) {
    /**
     * Return this product's Freemius SDK instance.
     *
     * The product secret key must never be placed here. Development and
     * sandbox credentials belong in the local site's wp-config.php only.
     *
     * @return Freemius
     */
    function cpanalyzer_fs() {
        global $cpanalyzer_fs;
        if ( !isset( $cpanalyzer_fs ) ) {
            require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
            $cpanalyzer_fs = fs_dynamic_init( array(
                'id'               => '36608',
                'slug'             => 'content-performance-analyzer',
                'type'             => 'plugin',
                'public_key'       => 'pk_b84c53b5d1cb19fea942445fc1b85',
                'is_premium'       => false,
                'premium_suffix'   => 'Pro',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'trial'            => array(
                    'days'               => 7,
                    'is_require_payment' => true,
                ),
                'menu'             => array(
                    'slug'   => 'cpanalyzer-settings',
                    'parent' => array(
                        'slug' => 'content-insights',
                    ),
                ),
                'is_live'          => true,
            ) );
        }
        return $cpanalyzer_fs;
    }

    // Initialize before the rest of the plugin so licensing, updates, and
    // the account/upgrade pages are available during normal WordPress boot.
    cpanalyzer_fs();
    do_action( 'cpanalyzer_fs_loaded' );
    // Data cleanup runs on the SDK's uninstall action, not an uninstall.php
    // file — Freemius rejects packages containing one, and its hook already
    // skips the delete when the other package is still active.
    require_once dirname( __FILE__ ) . '/includes/cpa-uninstall.php';
    cpanalyzer_fs()->add_action( 'after_uninstall', 'cpanalyzer_run_uninstall_cleanup' );
}
// Define plugin constants
define( 'CPANALYZER_VERSION', '1.5.1' );
define( 'CPANALYZER_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'CPANALYZER_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'CPANALYZER_PLUGIN_FILE', __FILE__ );
// Include required files — order matters: helpers first, then classes, then dependents
require_once CPANALYZER_PLUGIN_DIR . 'includes/cpa-functions.php';
require_once CPANALYZER_PLUGIN_DIR . 'includes/class-cpa-google-oauth.php';
require_once CPANALYZER_PLUGIN_DIR . 'includes/class-cpa-core.php';
require_once CPANALYZER_PLUGIN_DIR . 'includes/class-cpa-content-scraper.php';
require_once CPANALYZER_PLUGIN_DIR . 'includes/class-cpa-ai-processor.php';
require_once CPANALYZER_PLUGIN_DIR . 'includes/class-cpa-pagespeed-collector.php';
require_once CPANALYZER_PLUGIN_DIR . 'includes/class-cpa-data-collector.php';
require_once CPANALYZER_PLUGIN_DIR . 'includes/class-cpa-batch-processor.php';
require_once CPANALYZER_PLUGIN_DIR . 'includes/class-cpa-internal-links.php';
require_once CPANALYZER_PLUGIN_DIR . 'includes/class-cpa-interlinking.php';
require_once CPANALYZER_PLUGIN_DIR . 'includes/class-cpa-snapshots.php';
require_once CPANALYZER_PLUGIN_DIR . 'includes/class-cpa-admin.php';
// Text domain and translations are loaded automatically by WordPress 4.6+
// when Text Domain and Domain Path are declared in plugin header
// Initialize the plugin
function cpanalyzer_init() {
    CPAP_Core::get_instance()->init();
    // Smart Interlinking's AJAX endpoints are only registered when its page
    // is actually reachable. They used to register unconditionally, so the
    // handlers stayed callable even though the menu entry is removed —
    // meaning a hidden feature was not actually gated. The same filter
    // controls both, so visibility and access can never drift apart.
    if ( apply_filters( 'cpanalyzer_show_smart_interlinking_menu', false ) ) {
        CPAP_Interlinking::init();
    }
}

add_action( 'plugins_loaded', 'cpanalyzer_init' );
// Activation hook
register_activation_hook( __FILE__, array('CPAP_Core', 'activate') );
// Deactivation hook
register_deactivation_hook( __FILE__, array('CPAP_Core', 'deactivate') );
// Add settings link to plugins page
function cpanalyzer_add_settings_link(  $links  ) {
    $settings_link = sprintf( '<a href="%s">%s</a>', esc_url( admin_url( 'admin.php?page=cpanalyzer-settings' ) ), esc_html__( 'Settings', 'content-performance-analyzer' ) );
    array_unshift( $links, $settings_link );
    // Also add insights link
    $insights_link = sprintf( '<a href="%s" style="color: #0073aa; font-weight: 600;">%s</a>', esc_url( admin_url( 'admin.php?page=content-insights' ) ), esc_html__( 'View Insights', 'content-performance-analyzer' ) );
    array_unshift( $links, $insights_link );
    return $links;
}

add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'cpanalyzer_add_settings_link' );