| 1 |
<?php |
| 2 |
/** |
| 3 |
* The core plugin class. |
| 4 |
* |
| 5 |
* @since 1.6.0 |
| 6 |
* |
| 7 |
* @package woo-additional-terms |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Woo_Additional_Terms; |
| 11 |
|
| 12 |
/** |
| 13 |
* The plugin class. |
| 14 |
*/ |
| 15 |
class Plugin extends Vendor\Pimple\Container { |
| 16 |
|
| 17 |
/** |
| 18 |
* The plugin version. |
| 19 |
* |
| 20 |
* @since 1.6.0 |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
private $version; |
| 25 |
|
| 26 |
/** |
| 27 |
* Constructor. |
| 28 |
* |
| 29 |
* @since 1.6.0 |
| 30 |
* |
| 31 |
* @param string $version The plugin version. |
| 32 |
* @param string $file The plugin file. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function __construct( $version, $file ) { |
| 37 |
|
| 38 |
// Set the version. |
| 39 |
$this->version = $version; |
| 40 |
|
| 41 |
// Pimple Container construct. |
| 42 |
parent::__construct(); |
| 43 |
|
| 44 |
// Register the file service. |
| 45 |
$this['file'] = fn() => new File( $file ); |
| 46 |
|
| 47 |
// Register services early. |
| 48 |
$this->register_services(); |
| 49 |
|
| 50 |
// Hooks. |
| 51 |
$this->hooks(); |
| 52 |
|
| 53 |
// Load the plugin. |
| 54 |
$this->load(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Register services. |
| 59 |
* |
| 60 |
* @since 1.6.0 |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
private function register_services() { |
| 65 |
|
| 66 |
$provider = new PluginServiceProvider(); |
| 67 |
$provider->register( $this ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Register assets. |
| 72 |
* |
| 73 |
* @since 1.6.5 |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
private function hooks() { |
| 78 |
|
| 79 |
add_action( 'before_woocommerce_init', array( __NAMESPACE__ . '\\I18n', 'textdomain' ) ); |
| 80 |
add_action( 'enqueue_block_editor_assets', array( __NAMESPACE__ . '\\Assets', 'enqueue_editor' ) ); |
| 81 |
add_action( 'admin_enqueue_scripts', array( __NAMESPACE__ . '\\Assets', 'enqueue_admin' ) ); |
| 82 |
add_action( 'wp_enqueue_scripts', array( __NAMESPACE__ . '\\Assets', 'enqueue_frontend' ) ); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get a service by given key. |
| 87 |
* |
| 88 |
* @since 1.6.0 |
| 89 |
* |
| 90 |
* @param string $key The service key. |
| 91 |
* |
| 92 |
* @return mixed |
| 93 |
*/ |
| 94 |
public function service( $key ) { |
| 95 |
|
| 96 |
return $this[ $key ]; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get the plugin slug. |
| 101 |
* |
| 102 |
* @since 1.6.0 |
| 103 |
* |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
public function get_slug() { |
| 107 |
|
| 108 |
return 'woo-additional-terms'; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Get the plugin version. |
| 113 |
* |
| 114 |
* @since 1.6.0 |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
public function get_version() { |
| 119 |
|
| 120 |
return $this->version; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Start loading classes on `woocommerce_loaded`, priority 20. |
| 125 |
* |
| 126 |
* @since 1.6.0 |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
private function load() { |
| 131 |
|
| 132 |
// Iterate through the classes and initialize them. |
| 133 |
foreach ( $this->get_classes() as $class => $args ) { |
| 134 |
|
| 135 |
// Skip if the condition is not met. |
| 136 |
if ( isset( $args['condition'] ) && ! $args['condition'] ) { |
| 137 |
continue; |
| 138 |
} |
| 139 |
|
| 140 |
// Initialize the class with parameters. |
| 141 |
if ( isset( $args['params'] ) ) { |
| 142 |
( new $class() )->setup( ...$args['params'] ); |
| 143 |
continue; |
| 144 |
} |
| 145 |
|
| 146 |
// Initialize the class. |
| 147 |
( new $class() )->setup(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get the classes to load. |
| 153 |
* |
| 154 |
* @since 1.6.0 |
| 155 |
* |
| 156 |
* @return array |
| 157 |
*/ |
| 158 |
private function get_classes() { |
| 159 |
|
| 160 |
$is_ajax = wp_doing_ajax(); |
| 161 |
$is_admin = is_admin(); |
| 162 |
$is_frontend = ! $is_admin; |
| 163 |
$classes = array( |
| 164 |
'Admin\\Order' => array( |
| 165 |
'condition' => $is_admin, |
| 166 |
), |
| 167 |
'Ajax\\OnBoarding' => array( |
| 168 |
'condition' => $is_ajax, |
| 169 |
), |
| 170 |
'Ajax\\Rate' => array( |
| 171 |
'condition' => $is_ajax, |
| 172 |
), |
| 173 |
'Ajax\\Rated' => array( |
| 174 |
'condition' => $is_ajax, |
| 175 |
), |
| 176 |
'Compatibility\\WooCommerce' => array( |
| 177 |
'condition' => $is_admin && class_exists( 'Automattic\\WooCommerce\\Utilities\\FeaturesUtil' ), |
| 178 |
), |
| 179 |
'Enhancements\\Docs' => array( |
| 180 |
'condition' => $is_admin, |
| 181 |
), |
| 182 |
'Enhancements\\Meta' => array( |
| 183 |
'condition' => $is_admin, |
| 184 |
), |
| 185 |
'Enhancements\\Notices' => array( |
| 186 |
'condition' => $is_admin, |
| 187 |
), |
| 188 |
'Enhancements\\OnBoarding' => array( |
| 189 |
'condition' => $is_admin, |
| 190 |
), |
| 191 |
'Enhancements\\Rate' => array( |
| 192 |
'condition' => $is_admin, |
| 193 |
), |
| 194 |
'Enhancements\\Upsell' => array( |
| 195 |
'condition' => $is_admin, |
| 196 |
), |
| 197 |
'Migration\\Migration160' => array( |
| 198 |
'condition' => $is_admin, |
| 199 |
), |
| 200 |
'Settings\\Register' => array( |
| 201 |
'condition' => $is_admin, |
| 202 |
), |
| 203 |
'WooCommerce\\Checkout' => array( |
| 204 |
'condition' => $is_frontend, |
| 205 |
), |
| 206 |
'WooCommerce\\Block\\Checkout' => array(), |
| 207 |
'WooCommerce\\Block\\Register' => array(), |
| 208 |
); |
| 209 |
|
| 210 |
return array_combine( |
| 211 |
array_map( |
| 212 |
fn ( $key ) => __NAMESPACE__ . '\\' . $key, |
| 213 |
array_keys( $classes ) |
| 214 |
), |
| 215 |
$classes |
| 216 |
); |
| 217 |
} |
| 218 |
} |
| 219 |
|