| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @subpackage Core |
| 7 |
* @copyright Copyright (c) 2022, Sandhills Development, LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 4.4.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SimplePay\Core; |
| 13 |
|
| 14 |
use Exception; |
| 15 |
use SimplePay\Core\EventManagement\EventManager; |
| 16 |
use SimplePay\Core\EventManagement\SubscriberInterface; |
| 17 |
|
| 18 |
/** |
| 19 |
* Plugin class. |
| 20 |
* |
| 21 |
* @since 4.4.0 |
| 22 |
*/ |
| 23 |
final class Plugin { |
| 24 |
|
| 25 |
/** |
| 26 |
* Plugin base filename with full path. |
| 27 |
* |
| 28 |
* @since 4.4.0 |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public $file; |
| 32 |
|
| 33 |
/** |
| 34 |
* Plugin container. |
| 35 |
* |
| 36 |
* @since 4.4.0 |
| 37 |
* @var \SimplePay\Vendor\League\Container\Container |
| 38 |
*/ |
| 39 |
protected $container; |
| 40 |
|
| 41 |
/** |
| 42 |
* Plugin. |
| 43 |
* |
| 44 |
* @since 4.4.0 |
| 45 |
* |
| 46 |
* @param string $file Plugin base filename with full path. |
| 47 |
*/ |
| 48 |
public function __construct( $file ) { |
| 49 |
$this->file = $file; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Loads the plugin on the plugins_loaded hook. |
| 54 |
* |
| 55 |
* @since 4.4.0 |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function load() { |
| 60 |
// Load Action Scheduler before plugins loaded. |
| 61 |
// https://actionscheduler.org/usage/#loading-action-scheduler |
| 62 |
if ( |
| 63 |
! defined( 'SIMPAY_SCHEDULER_WP_CRON' ) || |
| 64 |
false === SIMPAY_SCHEDULER_WP_CRON |
| 65 |
) { |
| 66 |
require_once dirname( $this->file ) . '/lib/woocommerce/action-scheduler/action-scheduler.php'; |
| 67 |
} |
| 68 |
|
| 69 |
// Run slightly early to gain access to legacy registries. |
| 70 |
add_action( 'plugins_loaded', array( $this, 'register' ), 5 ); // @phpstan-ignore-line |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Sets up and returns the basic container instance. |
| 75 |
* |
| 76 |
* This is separate from the registration of service providers and subscribers |
| 77 |
* to allow modifications to the container to be made before said items are executed. |
| 78 |
* |
| 79 |
* @since 4.4.0 |
| 80 |
* |
| 81 |
* @return \SimplePay\Core\PluginContainer |
| 82 |
*/ |
| 83 |
public function setup_container() { |
| 84 |
$this->container = new PluginContainer; |
| 85 |
|
| 86 |
// Event management. |
| 87 |
$this->container->share( |
| 88 |
'event-manager', |
| 89 |
EventManagement\EventManager::class |
| 90 |
); |
| 91 |
|
| 92 |
// Scheduler. |
| 93 |
$this->container->share( |
| 94 |
'scheduler', |
| 95 |
function() { |
| 96 |
$events = $this->container->get( 'event-manager' ); |
| 97 |
|
| 98 |
if ( ! $events instanceof EventManager ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
// Allow fallback to default WordPress cron. |
| 103 |
// @todo abstract as a more robust factory? |
| 104 |
if ( |
| 105 |
defined( 'SIMPAY_SCHEDULER_WP_CRON' ) && |
| 106 |
true === SIMPAY_SCHEDULER_WP_CRON |
| 107 |
) { |
| 108 |
return new Scheduler\WpCronScheduler( $events ); |
| 109 |
} |
| 110 |
|
| 111 |
return new Scheduler\ActionScheduler; |
| 112 |
} |
| 113 |
); |
| 114 |
|
| 115 |
return $this->container; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Registers the plugin's service providers and subscribers. |
| 120 |
* |
| 121 |
* @since 4.4.0 |
| 122 |
* |
| 123 |
* @return \SimplePay\Vendor\League\Container\Container |
| 124 |
*/ |
| 125 |
public function register() { |
| 126 |
if ( ! $this->container instanceof PluginContainer ) { |
| 127 |
$this->container = $this->setup_container(); |
| 128 |
} |
| 129 |
|
| 130 |
// Attach service provider subscribers to the event manager. |
| 131 |
$this->add_service_providers( $this->get_service_providers() ); |
| 132 |
|
| 133 |
return $this->container; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Registers service providers (and their child subscribers) with the container. |
| 138 |
* |
| 139 |
* @since 4.4.3 |
| 140 |
* |
| 141 |
* @param array<\SimplePay\Vendor\League\Container\ServiceProvider\ServiceProviderInterface> $service_providers |
| 142 |
* Service providers. |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
private function add_service_providers( $service_providers ) { |
| 146 |
$events = $this->container->get( 'event-manager' ); |
| 147 |
|
| 148 |
if ( ! $events instanceof EventManager ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
// Register service providers. |
| 153 |
foreach ( $service_providers as $service_provider ) { |
| 154 |
if ( ! $service_provider instanceof AbstractPluginServiceProvider ) { |
| 155 |
continue; |
| 156 |
} |
| 157 |
|
| 158 |
$this->container->addServiceProvider( $service_provider ); |
| 159 |
} |
| 160 |
|
| 161 |
// Attach subscribers. |
| 162 |
foreach ( $service_providers as $service_provider ) { |
| 163 |
if ( ! $service_provider instanceof AbstractPluginServiceProvider ) { |
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
/** @var \SimplePay\Core\AbstractPluginServiceProvider $service_provider */ |
| 168 |
$subscribers = $service_provider->get_subscribers(); |
| 169 |
|
| 170 |
foreach ( $subscribers as $subscriber_id ) { |
| 171 |
try { |
| 172 |
$subscriber = $this->container->get( $subscriber_id ); |
| 173 |
|
| 174 |
if ( $subscriber instanceof SubscriberInterface ) { |
| 175 |
$events->add_subscriber( $subscriber ); |
| 176 |
} |
| 177 |
} catch ( Exception $e ) { |
| 178 |
// Do not subscribe. |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
/** @var array<\SimplePay\Vendor\League\Container\ServiceProvider\ServiceProviderInterface> $child_service_providers */ |
| 183 |
$child_service_providers = $service_provider->get_service_providers(); |
| 184 |
|
| 185 |
// Walk through child service providers. |
| 186 |
if ( ! empty( $child_service_providers ) ) { |
| 187 |
$this->add_service_providers( $child_service_providers ); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Retrieves service providers for the derived context. |
| 194 |
* |
| 195 |
* @since 4.4.0 |
| 196 |
* |
| 197 |
* @return array<\SimplePay\Vendor\League\Container\ServiceProvider\ServiceProviderInterface> |
| 198 |
*/ |
| 199 |
private function get_service_providers() { |
| 200 |
global $wp_version; |
| 201 |
|
| 202 |
$service_providers = array( |
| 203 |
new AdminBar\AdminBarServiceProvider, |
| 204 |
new AntiSpam\AntiSpamServiceProvider, |
| 205 |
new CustomerSuccess\CustomerSuccessServiceProvider, |
| 206 |
new Emails\EmailServiceProvider, |
| 207 |
new FormPreview\FormPreviewServiceProvider, |
| 208 |
new Integration\IntegrationServiceProvider, |
| 209 |
new License\LicenseServiceProvider, |
| 210 |
new Connect\ConnectServiceProvider, |
| 211 |
new PaymentPage\PaymentPageServiceProvider, |
| 212 |
new Report\ReportServiceProvider, |
| 213 |
new RestApi\RestApiServiceProvider, |
| 214 |
new StripeConnect\StripeConnectServiceProvider, |
| 215 |
new Transaction\TransactionServiceProvider, |
| 216 |
new Webhook\WebhookServiceProvider, |
| 217 |
); |
| 218 |
|
| 219 |
if ( version_compare( $wp_version, '5.6', '>=' ) ) { |
| 220 |
$service_providers[] = new Block\BlockServiceProvider; |
| 221 |
} |
| 222 |
|
| 223 |
if ( version_compare( $wp_version, '5.7', '>=' ) ) { |
| 224 |
$service_providers[] = new Help\HelpServiceProvider; |
| 225 |
$service_providers[] = new NotificationInbox\NotificationInboxServiceProvider; |
| 226 |
} |
| 227 |
|
| 228 |
if ( is_admin() ) { |
| 229 |
global $wp_version; |
| 230 |
|
| 231 |
$admin_service_providers = array( |
| 232 |
new Admin\AdminServiceProvider, |
| 233 |
new Admin\Addon\AddonServiceProvider, |
| 234 |
new Admin\DashboardWidget\DashboardWidgetServiceProvider, |
| 235 |
new Admin\Education\EducationServiceProvider, |
| 236 |
new Admin\SiteHealth\SiteHealthServiceProvider, |
| 237 |
); |
| 238 |
|
| 239 |
if ( version_compare( $wp_version, '5.5', '>=' ) ) { |
| 240 |
$admin_service_providers[] = |
| 241 |
new Admin\SetupWizard\SetupWizardServiceProvider; |
| 242 |
} |
| 243 |
|
| 244 |
if ( version_compare( $wp_version, '5.6', '>=' ) ) { |
| 245 |
$admin_service_providers[] = |
| 246 |
new Admin\FormBuilder\FormBuilderServiceProvider; |
| 247 |
} |
| 248 |
|
| 249 |
return array_merge( $admin_service_providers, $service_providers ); |
| 250 |
} |
| 251 |
|
| 252 |
return $service_providers; |
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|