<?php
/**
 * Plugin Name: ShortPixel AI Alt Text Generator
 * Plugin URI: https://shortpixel.com/
 * Description: Generate AI-powered alt text and image SEO fields for your WordPress media library. Configure the plugin in Settings > ShortPixel AI Alt Text Generator.
 * Version: 1.0.0
 * Requires at least: 5.5
 * Author: ShortPixel
 * Author URI: https://shortpixel.com
 * GitHub Plugin URI: https://github.com/short-pixel-optimizer/shortpixel-ai-alt-text-generator
 * Text Domain: shortpixel-ai-alt-text-generator
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */


 if ( ! defined( 'ABSPATH' ) ) {
 	exit('No Direct Access'); // Exit if accessed directly.
 }

// Preventing double load crash.
if (function_exists('wpSPAATG'))
{
    add_action('admin_notices', function () {
      echo '<div class="error"><h4>';
      /* translators: %s: plugin file path. */
      printf(esc_html__('ShortPixel AI Alt Text Generator plugin already loaded. You might have two versions active. Not loaded: %s', 'shortpixel-ai-alt-text-generator'), esc_html(__FILE__));
      echo '</h4></div>';
    });
    return;
}

if (! defined('SPAATG_RESET_ON_ACTIVATE'))
  define('SPAATG_RESET_ON_ACTIVATE', false);

//define('SPAATG_DEBUG', true);
//define('SPAATG_DEBUG_TARGET', true);

define('SPAATG_PLUGIN_FILE', __FILE__);
define('SPAATG_PLUGIN_DIR', __DIR__);

define('SPAATG_IMAGE_OPTIMISER_VERSION', "1.0.0");

define('SPAATG_MAX_FAIL_RETRIES', 3);

define('SPAATG_API', 'api.shortpixel.com');

$spaatg_max_exec = intval(ini_get('max_execution_time'));
if ($spaatg_max_exec === 0) // max execution time of zero means infinite. Quantify.
  $spaatg_max_exec = 60;
elseif($spaatg_max_exec < 0) // some hosts like to set negative figures on this. Ignore that.
  $spaatg_max_exec = 30;
define('SPAATG_MAX_EXECUTION_TIME', $spaatg_max_exec);

// ** Load the modules */
require_once(SPAATG_PLUGIN_DIR . '/build/shortpixel/autoload.php');

$spaatg_uploads = wp_get_upload_dir();
$spaatg_uploads_base = isset($spaatg_uploads['basedir']) ? $spaatg_uploads['basedir'] : trailingslashit(WP_CONTENT_DIR) . 'uploads';
$spaatg_uploads_url = isset($spaatg_uploads['baseurl']) ? $spaatg_uploads['baseurl'] : content_url('uploads');

define('SPAATG_UPLOADS_BASE', untrailingslashit(wp_normalize_path($spaatg_uploads_base)));
define('SPAATG_UPLOADS_URL', untrailingslashit($spaatg_uploads_url));
define('SPAATG_UPLOADS_NAME', basename(SPAATG_UPLOADS_BASE));
define('SPAATG_STORAGE_FOLDER', SPAATG_UPLOADS_BASE . '/shortpixel-ai-alt-text-generator');

if (! function_exists('spaatg_has_optimizer_conflict'))
{
  function spaatg_has_optimizer_conflict()
  {
    if (! function_exists('is_plugin_active')) {
      include_once(ABSPATH . 'wp-admin/includes/plugin.php');
    }

    $optimizer_conflict_plugins = array(
      'shortpixel-image-optimiser/wp-shortpixel.php',
      'shortpixel-image-optimizer/wp-shortpixel.php',
    );

    foreach ($optimizer_conflict_plugins as $optimizer_conflict_plugin) {
      if (function_exists('is_plugin_active') && is_plugin_active($optimizer_conflict_plugin)) {
        return true;
      }

      if (function_exists('is_plugin_active_for_network') && is_plugin_active_for_network($optimizer_conflict_plugin)) {
        return true;
      }
    }

    return false;
  }
}

if (spaatg_has_optimizer_conflict())
{
  if (! function_exists('deactivate_plugins')) {
    include_once(ABSPATH . 'wp-admin/includes/plugin.php');
  }

  deactivate_plugins(plugin_basename(__FILE__));
  return;
}



//define('SPAATG_SILENT_MODE', true); // no global notifications. Can lead to data damage. After setting, reactivate plugin.
//define('SPAATG_TRUSTED_MODE', false); // doesn't do any file checks on the view-side of things.
// define('SPAATG_SKIP_FEEDBACK', true);

// Starting logging services, early as possible.
if (! defined('SPAATG_DEBUG'))
{
    define('SPAATG_DEBUG', false);
}


if (false === defined( 'WP_CLI' ) || false === WP_CLI)
{
	$spaatg_log = \SPAATG\ShortPixelLogger\ShortPixelLogger::getInstance();
	if (\SPAATG\ShortPixelLogger\ShortPixelLogger::debugIsActive() )
	{
		$spaatg_log->setLogPath(SPAATG_STORAGE_FOLDER . "/spaatg.log");
	}
}

/* Function to reach core function of ShortPixel
* Use to get plugin url, plugin path, or certain core controllers
*/

if (! function_exists("wpSPAATG"))	{
  // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- Public bootstrap helper kept for plugin backwards compatibility.
  function wpSPAATG()
  {
     return \SPAATG\ShortPixelPlugin::getInstance();
  }
}
// Start runtime here
require_once(SPAATG_PLUGIN_DIR . '/shortpixel-plugin.php'); // loads runtime and needed classes.

// PSR-4 package loader.
$spaatg_loader = new SPAATG\Build\PackageLoader();
$spaatg_loader->setComposerFile(SPAATG_PLUGIN_DIR . '/class/plugin.json');
$spaatg_loader->load(SPAATG_PLUGIN_DIR);

wpSPAATG(); // let's go!

// Activation / Deactivation services
register_activation_hook( __FILE__, array('\SPAATG\Helper\InstallHelper','activatePlugin') );
register_deactivation_hook( __FILE__,  array('\SPAATG\Helper\InstallHelper','deactivatePlugin') );
register_uninstall_hook(__FILE__,  array('\SPAATG\Helper\InstallHelper','uninstallPlugin') );
