<?php

/*
 * Plugin Name: Assistini - WP AI Text Writer, Content Generator, Auto Image Generator and SEO Optimizer with ChatGPT
 * Plugin URI: https://www.assistini.com
 * Description: Assistini: Create, edit and SEO-optimize WordPress posts and generate images automatically with ChatGPT, Gemini, Claude.ai
 * Author: Assistini
 * Text Domain: assistini
 * Version: 2.4.1
 * Requires at least: 6.2
 * Requires PHP: 8.0
 * Tested up to: 6.9
 * License: GPLv3
 */

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

define('ASSI_PLUGIN_VERSION', '241');
define('ASSI_PLUGIN_VERSION_DISPLAY', 'Version 2.4.1');
define('ASSI_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('ASSI_PLUGIN_URL', plugin_dir_url(__FILE__));
define('ASSI_PLUGIN_HOOK', basename(dirname(__FILE__)) . '/' . basename(__FILE__));
define('ASSI_PLUGIN_FILE', __FILE__);
define('ASSI_PLUGIN_BASENAME', plugin_basename(__FILE__));

define('ASSI_PLUGIN_API_WP_ENDPOINT', 'https://apiwp.assistini.com/rest/v1/');
define('ASSI_PLUGIN_API_ENDPOINT', 'https://api.assistini.com/rest/v1/');
define('ASSI_PLUGIN_APP_ENDPOINT', 'https://app.assistini.com/');
define('ASSI_PLUGIN_OPEN_AI_STATUS_ENDPOINT', 'https://status.openai.com/api/v2/summary.json');

//ASSLoader
require_once(ASSI_PLUGIN_DIR . 'includes/Loader.php');
$assiLoad = new ASSI_Loader();
register_uninstall_hook(ASSI_PLUGIN_FILE, 'assiUninstallPlugin');
register_activation_hook(ASSI_PLUGIN_FILE, array($assiLoad, 'activatePlugin'));

function assiUninstallPlugin() {
    global $wpdb;

    $args = array(
        'method' => 'POST',
        'body' => array(
            'blog_url' => get_option('home'),
            'access_token' => get_option('ASSI_PLUGIN_BLOG_ACCESS_TOKEN'),
        ),
        'timeout' => 30,
        'redirection' => '5',
    );
    wp_remote_post(ASSI_PLUGIN_API_WP_ENDPOINT . 'plugin/uninstall', $args);

    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
    $options = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE %s",
            $wpdb->esc_like('ASSI_PLUGIN') . '%'
        )
    );
    foreach ($options as $option) {
        delete_option($option->option_name);
    }

    $tables = [
        "{$wpdb->prefix}assi_user",
        "{$wpdb->prefix}assi_user_notifications",
        "{$wpdb->prefix}assi_user_templates",
        "{$wpdb->prefix}assi_user_history",
    ];
    foreach ($tables as $table) {
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.DirectDatabaseQuery.SchemaChange
        $wpdb->query($wpdb->prepare("DROP TABLE IF EXISTS %i", $table));
    }
}