<?php
/**
 * Plugin Name: Easy Digital Downloads - Zapier
 * Plugin URI: https://easydigitaldownloads.com/downloads/zapier/
 * Description: Adds Zapier integration for your EDD Shop. Trigger actions based on new customers and orders. Send sample data to Zapier via Downloads > Settings > Extensions.
 * Version: 1.4.2
 * Requires at least: 5.8
 * Requires PHP: 7.4
 * Author: Easy Digital Downloads
 * Author URI: https://easydigitaldownloads.com
 * License: GPL2
 * Text Domain: edd-zapier
 * Domain Path: /languages
 *
 * @package EDD_Zapier_Integration
 */

/*
Copyright 2021

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

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

/**
 * Main plugin instantiation class.
 *
 * @since 1.0.0
 */
class EDD_Zapier_Integration {

	/**
	 * The plugin basename.
	 */
	public $basename;

	/**
	 * The plugin directory path.
	 */
	public $directory_path;

	/**
	 * The plugin directory URL.
	 */
	public $directory_url;

	/**
	 * Fire up the engines.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {

		// Define plugin constants.
		$this->basename       = plugin_basename( __FILE__ );
		$this->directory_path = plugin_dir_path( __FILE__ );
		$this->directory_url  = plugin_dir_url( __FILE__ );

		// Basic setup.
		add_action( 'init', array( $this, 'register_cpt' ) );
		// License setup.
		add_action( 'edd_extension_license_init', array( $this, 'licensed_updates' ) );

		$this->i18n();
		$this->includes();
	} /* __construct() */

	/**
	 * Output error message and disable plugin if requirements are not met.
	 *
	 * This fires on admin_notices.
	 *
	 * @deprecated 1.3.11 due to using ExtensionLoader.
	 * @since 1.0.0
	 */
	public function maybe_disable_plugin() {}

	/**
	 * Register EDD License
	 *
	 * @since 1.0.0
	 *
	 * @param \EDD\Extensions\ExtensionRegistry $registry Extension registry.
	 */
	public function licensed_updates( \EDD\Extensions\ExtensionRegistry $registry ) {
		$registry->addExtension( __FILE__, 'Zapier', 319476, '1.4.2', null );
	} /* licensed_updates() */

	/**
	 * Load localization.
	 *
	 * @since 1.0.0
	 */
	public function i18n() {
		load_plugin_textdomain( 'edd-zapier', false, $this->directory_path . '/languages/' );
	} /* i18n() */

	/**
	 * Register EDD Zapier Subscription post type.
	 *
	 * @since  1.0.0
	 */
	public function register_cpt() {
		register_post_type(
			'edd-zapier-sub',
			array(
				'description' => __( 'Used for storing EDD Zapier subscriptions', 'edd-zapier' ),
				'public'      => false,
				'rewrite'     => false,
				'query_var'   => false,
			)
		);
	} /* register_cpt() */

	/**
	 * Include file dependencies.
	 *
	 * @since 1.0.0
	 */
	public function includes() {
		require_once $this->directory_path . 'includes/class.EDD_Zapier_Customer.php';
		require_once $this->directory_path . 'includes/class.EDD_Zapier_Subscription_Factory.php';
		require_once $this->directory_path . 'includes/class.EDD_Zapier_Connection.php';
		require_once $this->directory_path . 'includes/subscription_hooks.php';
		require_once $this->directory_path . 'includes/settings.php';
	}
}

require_once __DIR__ . '/vendor/autoload.php';
\EDD\ExtensionUtils\v1\ExtensionLoader::loadOrQuit(
	__FILE__,
	function () {
		$zapier                            = new EDD_Zapier_Integration();
		$GLOBALS['edd_zapier_integration'] = $zapier;
	},
	array(
		'php'                    => '7.4',
		'wp'                     => '5.8',
		'easy-digital-downloads' => '3.2.0',
	)
);
