summaryrefslogtreecommitdiffstats
path: root/cepis-woocommerce.php
blob: e4930081290054dd51df93b6b67b62abcd73c502 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
 * Plugin Name:       CEP Autofill for WooCommerce
 * Plugin URI:        https://cep.is
 * Description:       Fills the WooCommerce checkout address from the CEP the shopper types, with official Correios data (DNE) from cep.is.
 * Version:           0.2.0
 * Requires at least: 6.9
 * Tested up to:      7.0
 * Requires PHP:      8.1
 * Requires Plugins:  woocommerce
 * Author:            Murilo Ijanc
 * Author URI:        https://ijanc.org
 * License:           ISC
 * License URI:       https://opensource.org/licenses/ISC
 * Text Domain:       cepis-woocommerce
 * Domain Path:       /languages
 *
 * @package CepisWooCommerce
 */

declare(strict_types=1);

defined( 'ABSPATH' ) || exit;

define( 'CEPIS_WOO_VERSION', '0.2.0' );
define( 'CEPIS_WOO_FILE', __FILE__ );
define( 'CEPIS_WOO_DIR', plugin_dir_path( __FILE__ ) );
define( 'CEPIS_WOO_URL', plugin_dir_url( __FILE__ ) );

/**
 * Autoload the bundled `Cepis\` PHP client.
 *
 * The library is vendored under `lib/cepis/` rather than installed with
 * Composer, because a WordPress site is deployed by copying files and never
 * runs `composer install`. Registration is appended, so a Composer autoloader
 * already in play on the site keeps precedence and only one copy of the
 * classes is ever declared.
 *
 * @param string $class_name Fully qualified class name being resolved.
 */
function cepis_woo_autoload( string $class_name ): void {
	if ( ! str_starts_with( $class_name, 'Cepis\\' ) ) {
		return;
	}

	$relative = substr( $class_name, 6 );
	if ( str_contains( $relative, '\\' ) || ! preg_match( '/^[A-Za-z0-9_]+$/', $relative ) ) {
		return;
	}

	$file = CEPIS_WOO_DIR . 'lib/cepis/' . $relative . '.php';
	if ( is_readable( $file ) ) {
		require_once $file;
	}
}

spl_autoload_register( 'cepis_woo_autoload' );

require_once CEPIS_WOO_DIR . 'includes/class-cepis-woo-options.php';
require_once CEPIS_WOO_DIR . 'includes/class-cepis-woo-messages.php';
require_once CEPIS_WOO_DIR . 'includes/class-cepis-woo-token.php';
require_once CEPIS_WOO_DIR . 'includes/class-cepis-woo-transport.php';
require_once CEPIS_WOO_DIR . 'includes/class-cepis-woo-lookup.php';
require_once CEPIS_WOO_DIR . 'includes/class-cepis-woo-rest.php';
require_once CEPIS_WOO_DIR . 'includes/class-cepis-woo-assets.php';
require_once CEPIS_WOO_DIR . 'includes/class-cepis-woo-admin.php';

/**
 * Boot the plugin once WordPress has loaded its own translations.
 *
 * Every hook registered here fires at or after `init`, which is what keeps
 * translation calls out of the `_load_textdomain_just_in_time` notice.
 */
function cepis_woo_init(): void {
	load_plugin_textdomain( 'cepis-woocommerce', false, dirname( plugin_basename( CEPIS_WOO_FILE ) ) . '/languages' );

	Cepis_Woo_Assets::init();
	Cepis_Woo_Rest::init();

	if ( is_admin() ) {
		Cepis_Woo_Admin::init();
	}
}

add_action( 'init', 'cepis_woo_init' );

/**
 * Declare compatibility with WooCommerce High-Performance Order Storage.
 *
 * The plugin never reads or writes an order, so the declaration is honest and
 * keeps the store owner from seeing an "incompatible plugin" warning.
 */
function cepis_woo_declare_hpos(): void {
	if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', CEPIS_WOO_FILE, true );
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', CEPIS_WOO_FILE, true );
	}
}

add_action( 'before_woocommerce_init', 'cepis_woo_declare_hpos' );