<?php

/**
 *
 * Plugin Name:				SDP Custom Order Status for WooCommerce
 * Description:				Create unlimited WooCommerce custom order statuses, send automated email notifications to customers and admins, and manage your order workflow easily
 * Plugin URI:				https://simpledirectplugins.com/sdp-custom-order-status-pro/
 * Author:					Simple Direct Plugins
 * Author URI:				https://simpledirectplugins.com/
 * Version:					6.1.7
 * Text Domain:				sdp-custom-order-status-for-woocommerce
 * Domain Path:       		/lang/
 * Requires at least:   	6.0
 * Tested up to:      		7.1
 * Requires PHP:      		7.4
 * WC requires at least:	9.0
 * WC tested up to:     	10.9
 * Requires Plugins:    	woocommerce
 * License:					GPLv2 or later
 * License URI:				http://www.gnu.org/licenses/gpl-2.0.html
 * 
 * This plugin bundles a third-party component: Advanced Custom Fields (ACF).
 * ACF is distributed under the GPL-2.0-or-later license.
 * Source: https://wordpress.org/plugins/advanced-custom-fields/
 *
 * @author          		  Simple Direct Plugins
 * @link          			  https://simpledirectplugins.com/
 * @since           		  1.0.0
 * @package         		  SCOSWoo
 *
 */

use Automattic\WooCommerce\Utilities\OrderUtil;

defined ('ABSPATH') or exit;
define  ('SCOS_ASSETS', plugins_url ('assets/', __FILE__));
define  ('SCOS_NAME', 'SDP Custom Order Status for WooCommerce');
define  ('SCOS_PRO_LINK', 'https://simpledirectplugins.com/sdp-custom-order-status-pro/');

if (!class_exists ('SCOS_Custom_Order_Status')) :

	Class SCOS_Custom_Order_Status {

		private array $files     = ['cpt', 'core', 'deactivation', 'email', 'fields', 'review', 'editor'];
		private array $classes   = ['SCOS_CPT', 'SCOS_Statuses', 'SCOS_Emails', 'SCOS_Deactivation', 'SCOS_Fields', 'SCOS_Review', 'SCOS_Editor'];
		
		private string $domain   = 'sdp-custom-order-status-for-woocommerce';
		private string $gestor;
		private string $dirname;
		private string $basename;
		private static $instance;

		private function __construct () {

			$this->dirname  = dirname (__FILE__);
			$this->basename = plugin_basename ( __FILE__ );

			$this->load_files();

			$this->gestor   = 'edit.php?post_type=' . SCOS_CPT::CPT;

			register_activation_hook (__FILE__, function () {

				set_transient ('scos-activado', true, 60);
				add_option ('scos_activation', current_time ('timestamp'));
				}, 10);

			add_action ('init', [$this, 'plugin_init'], 10);
			add_action ('admin_notices' , [$this, 'show_notices'], 10);
			add_action ('before_woocommerce_init', [$this, 'hpos_compatibility'], 10);

			add_filter ('load_textdomain_mofile', [$this, 'load_translations'], 10, 2);
			add_filter ('plugin_action_links', [$this, 'action_links'], 10, 2);
			add_filter ('plugin_row_meta', [$this, 'meta_links'], 10, 2);
			}

		public function __clone () {

			/* translators: this class */
			_doing_it_wrong (__FUNCTION__, sprintf (esc_html__('You cannot clone instances of %s.', 'sdp-custom-order-status-for-woocommerce'), esc_attr (get_class ($this))), '1.0.0');
			}

		private function load_files () {

			foreach ($this->files as $file)
				require (sprintf ('%s/src/%s.php', $this->dirname, $file));

			if (!class_exists ('acf')) {

				/**
				 * To disable the ACF_LITE constant:
				 *
				 * add_filter ('sdp_acf_lite', '__return_false');
				 *
				 */
				if (!defined ('ACF_LITE'))
				add_action ('after_setup_theme', function () {
					define ('ACF_LITE', apply_filters ('sdp_acf_lite', true)); //phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound	
					}, 10);

				require ($this->dirname . '/vendor/acf/acf.php');
				}
			}

		public function plugin_init () {

			/**
			 * Let's check if HPOS is active.
			 * Used on:
			 *    estados.php: SCOS_Statuses::devuelve_post_id()
			 *    cpt.php: SCOS_CPT::metabox_devinfo_html()
			 *    notificacion.php: SCOS_Notification::crea_tabla_pedido()
			 * 
			 * @since   5.0.0
			 * 
			 */
			define ('SCOS_HPOS_ACTIVE', Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled());

			foreach ($this->classes as $class)
				new $class;
			}

		public function hpos_compatibility () {

			if (class_exists (\Automattic\WooCommerce\Utilities\FeaturesUtil::class))
				\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
			}

		public function show_notices () {

			if (get_transient ('scos-activado')) {

				echo '<div class="updated notice is-dismissible woocommerce-message"><p>';
				/* translators: the name of the plugin */
				printf (esc_html__('Thank you for using %s! You can start by creating a custom order status.', 'sdp-custom-order-status-for-woocommerce'), esc_html (SCOS_NAME));
				printf ('</p><p><a href="%s" class="button button-primary">%s</a></p></div>', esc_url ($this->gestor), esc_html__('Manage order statuses', 'sdp-custom-order-status-for-woocommerce'));

				delete_transient ('scos-activado');
				}
			}

		public function load_translations ($mofile, $domain) {

			if ($domain !== $this->domain)
				return $mofile;

			if (is_string ($mofile) && $mofile !== '' && strpos ($mofile, WP_LANG_DIR . '/plugins/') !== false ) {

				$locale   = determine_locale();
				$mymofile = $this->dirname . '/lang/' . $domain . '-' . $locale . '.mo';

				if (file_exists ($mymofile))
					return $mymofile;

				return $mofile;
				}

			return $mofile;
			}

		public function action_links ($links, $plugin) {

			if ($plugin === $this->basename) {

				$sdp   = [];
				$sdp[] = sprintf ('<b><a target="_blank" href="%s">%s</a></b>', SCOS_PRO_LINK, esc_html__('Go Pro', 'sdp-custom-order-status-for-woocommerce'));
				$sdp[] = sprintf ('<a href="%s">%s</a>', esc_url ($this->gestor), esc_html__('Manage Statuses', 'sdp-custom-order-status-for-woocommerce'));
						$sdp[] = sprintf ('<a target="_blank" href="https://simpledirectplugins.com/docs/sdp-custom-order-status-pro-documentation/">%s</a>', esc_html__('User guide', 'sdp-custom-order-status-for-woocommerce'));

				$links = array_merge ($sdp, $links);
				}
			
			return $links;
			}

		public function meta_links ($links, $plugin) {

			if ($plugin === $this->basename) {
				
				$links[] = sprintf ('<a target="_blank" href="https://wordpress.org/support/plugin/sdp-custom-order-status-for-woocommerce/"><i class="fa fa-solid fa-life-ring"></i> %s</a>', esc_html__('Need Help?', 'sdp-custom-order-status-for-woocommerce'));
				$links[] = sprintf ('<a target="_blank" href="https://wordpress.org/support/plugin/sdp-custom-order-status-for-woocommerce/reviews/#new-post">%s <span>★★★★★</span></a>', esc_html__('Rate Us', 'sdp-custom-order-status-for-woocommerce'));
				}
			
			return $links;
			}

		public static function instance () {

			if (null === self::$instance)
				self::$instance = new self();

			return self::$instance;
			}

		}

endif;

SCOS_Custom_Order_Status::instance();