<?php
/**
 * Plugin Name:       Neutrope Japanese Invoice PDFs for WooCommerce
 * Plugin URI:        https://neutrope.biz/neutrope-japanese-invoice-pdfs-for-woocommerce/
 * Description:        Generate Japanese-style invoice PDFs from WooCommerce orders. Output from admin and customer My Account, with configurable company name, address, and logo. Free MVP edition.
 * Version:           1.0.1
 * Requires at least: 6.0
 * Requires PHP:      7.4
 * Requires Plugins:  woocommerce
 * Author:            Neutrope Inc.
 * Author URI:        https://neutrope.biz/
 * License:           GPL-2.0-or-later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       neutrope-japanese-invoice-pdfs-for-woocommerce
 * Domain Path:       /languages
 * WC requires at least: 7.0
 * WC tested up to:   9.4
 *
 * @package Neutrope_Japanese_Invoice_PDFs_For_WooCommerce
 */

defined( 'ABSPATH' ) || exit;

if ( defined( 'WCJBP_VERSION' ) ) {
	return;
}

define( 'WCJBP_VERSION', '1.0.1' );
define( 'WCJBP_PLUGIN_FILE', __FILE__ );
define( 'WCJBP_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'WCJBP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'WCJBP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

/**
 * Composer オートローダ（mPDF など）を読み込む。
 *
 * vendor/ が無い環境でも致命的エラーにはせず、PDF生成時に
 * 利用者へ案内する（class-pdf.php 側で判定）。
 */
if ( is_readable( WCJBP_PLUGIN_DIR . 'vendor/autoload.php' ) ) {
	require_once WCJBP_PLUGIN_DIR . 'vendor/autoload.php';
}

/**
 * HPOS（高性能注文ストレージ）互換を宣言する。
 *
 * 本プラグインは注文データに WC_Order API のみを使用するため互換。
 */
add_action(
	'before_woocommerce_init',
	function () {
		if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
				'custom_order_tables',
				WCJBP_PLUGIN_FILE,
				true
			);
		}
	}
);

/**
 * WooCommerce 依存チェックののち本体を起動する。
 */
add_action(
	'plugins_loaded',
	function () {
		if ( ! class_exists( 'WooCommerce' ) ) {
			add_action(
				'admin_notices',
				function () {
					if ( ! current_user_can( 'activate_plugins' ) ) {
						return;
					}
					echo '<div class="notice notice-error"><p>';
					echo esc_html__(
						'"Neutrope Japanese Invoice PDFs for WooCommerce" requires WooCommerce to be installed and active.',
						'neutrope-japanese-invoice-pdfs-for-woocommerce'
					);
					echo '</p></div>';
				}
			);
			return;
		}

		require_once WCJBP_PLUGIN_DIR . 'includes/helpers.php';
		require_once WCJBP_PLUGIN_DIR . 'includes/class-pro-gate.php';
		require_once WCJBP_PLUGIN_DIR . 'includes/class-pdf.php';
		require_once WCJBP_PLUGIN_DIR . 'includes/class-admin.php';
		require_once WCJBP_PLUGIN_DIR . 'includes/class-order.php';
		require_once WCJBP_PLUGIN_DIR . 'includes/class-myaccount.php';
		require_once WCJBP_PLUGIN_DIR . 'includes/class-plugin.php';

		WCJBP_Plugin::instance();
	}
);
