| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify; |
| 5 |
|
| 6 |
use Imagify\Admin\AdminBar; |
| 7 |
use Imagify\Bulk\Bulk; |
| 8 |
use Imagify\CLI\{BulkOptimizeCommand, GenerateMissingNextgenCommand, RestoreCommand}; |
| 9 |
use Imagify\Dependencies\League\Container\Container; |
| 10 |
use Imagify\Dependencies\League\Container\ServiceProvider\ServiceProviderInterface; |
| 11 |
use Imagify\EventManagement\{EventManager, SubscriberInterface}; |
| 12 |
use Imagify\Notices\Notices; |
| 13 |
use Imagify_Filesystem; |
| 14 |
|
| 15 |
/** |
| 16 |
* Main plugin class. |
| 17 |
*/ |
| 18 |
class Plugin { |
| 19 |
/** |
| 20 |
* Container instance. |
| 21 |
* |
| 22 |
* @var Container |
| 23 |
*/ |
| 24 |
private $container; |
| 25 |
|
| 26 |
/** |
| 27 |
* Is the plugin loaded |
| 28 |
* |
| 29 |
* @var boolean |
| 30 |
*/ |
| 31 |
private $loaded = false; |
| 32 |
|
| 33 |
/** |
| 34 |
* Absolute path to the plugin (with trailing slash). |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
private $plugin_path; |
| 39 |
|
| 40 |
/** |
| 41 |
* Instantiate the class. |
| 42 |
* |
| 43 |
* @since 1.9 |
| 44 |
* |
| 45 |
* @param Container $container Instance of the container. |
| 46 |
* @param array $plugin_args { |
| 47 |
* An array of arguments. |
| 48 |
* |
| 49 |
* @type string $plugin_path Absolute path to the plugin (with trailing slash). |
| 50 |
* } |
| 51 |
*/ |
| 52 |
public function __construct( Container $container, $plugin_args ) { |
| 53 |
$this->container = $container; |
| 54 |
$this->plugin_path = $plugin_args['plugin_path']; |
| 55 |
|
| 56 |
add_filter( 'imagify_container', [ $this, 'get_container' ] ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns the container instance. |
| 61 |
* |
| 62 |
* @return Container |
| 63 |
*/ |
| 64 |
public function get_container() { |
| 65 |
return $this->container; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Checks if the plugin is loaded |
| 70 |
* |
| 71 |
* @return boolean |
| 72 |
*/ |
| 73 |
private function is_loaded(): bool { |
| 74 |
return $this->loaded; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Plugin init. |
| 79 |
* |
| 80 |
* @param array $providers Array of service providers. |
| 81 |
* |
| 82 |
* @since 1.9 |
| 83 |
*/ |
| 84 |
public function init( $providers ) { |
| 85 |
if ( $this->is_loaded() ) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
$this->container->addShared( |
| 90 |
'event_manager', |
| 91 |
function () { |
| 92 |
return new EventManager(); |
| 93 |
} |
| 94 |
); |
| 95 |
|
| 96 |
$this->container->addShared( |
| 97 |
'filesystem', |
| 98 |
function () { |
| 99 |
return new Imagify_Filesystem(); |
| 100 |
} |
| 101 |
); |
| 102 |
|
| 103 |
$this->include_files(); |
| 104 |
|
| 105 |
class_alias( '\\Imagify\\Traits\\InstanceGetterTrait', '\\Imagify\\Traits\\FakeSingletonTrait' ); |
| 106 |
|
| 107 |
\Imagify_Auto_Optimization::get_instance()->init(); |
| 108 |
\Imagify_Options::get_instance()->init(); |
| 109 |
\Imagify_Data::get_instance()->init(); |
| 110 |
\Imagify_Folders_DB::get_instance()->init(); |
| 111 |
\Imagify_Files_DB::get_instance()->init(); |
| 112 |
\Imagify_Cron_Library_Size::get_instance()->init(); |
| 113 |
\Imagify_Cron_Rating::get_instance()->init(); |
| 114 |
\Imagify_Cron_Sync_Files::get_instance()->init(); |
| 115 |
\Imagify\Auth\Basic::get_instance()->init(); |
| 116 |
\Imagify\Job\MediaOptimization::get_instance()->init(); |
| 117 |
Bulk::get_instance()->init(); |
| 118 |
|
| 119 |
if ( is_admin() ) { |
| 120 |
Notices::get_instance()->init(); |
| 121 |
\Imagify_Admin_Ajax_Post::get_instance()->init(); |
| 122 |
\Imagify_Settings::get_instance()->init(); |
| 123 |
\Imagify_Views::get_instance()->init(); |
| 124 |
\Imagify\Imagifybeat\Core::get_instance()->init(); |
| 125 |
\Imagify\Imagifybeat\Actions::get_instance()->init(); |
| 126 |
} |
| 127 |
|
| 128 |
if ( ! wp_doing_ajax() ) { |
| 129 |
\Imagify_Assets::get_instance()->init(); |
| 130 |
} |
| 131 |
|
| 132 |
add_action( 'init', [ $this, 'maybe_activate' ] ); |
| 133 |
|
| 134 |
imagify_add_command( new BulkOptimizeCommand() ); |
| 135 |
imagify_add_command( new RestoreCommand() ); |
| 136 |
imagify_add_command( new GenerateMissingNextgenCommand() ); |
| 137 |
|
| 138 |
foreach ( $providers as $service_provider ) { |
| 139 |
$provider_instance = new $service_provider(); |
| 140 |
$this->container->addServiceProvider( $provider_instance ); |
| 141 |
|
| 142 |
// Load each service provider's subscribers if found. |
| 143 |
$this->load_subscribers( $provider_instance ); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Fires when Imagify is fully loaded. |
| 148 |
* |
| 149 |
* @since 1.0 |
| 150 |
* @since 1.9 Added the class instance as parameter. |
| 151 |
* |
| 152 |
* @param \Imagify_Plugin $plugin Instance of this class. |
| 153 |
*/ |
| 154 |
do_action( 'imagify_loaded', $this ); |
| 155 |
|
| 156 |
$this->loaded = true; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Include plugin files. |
| 161 |
* |
| 162 |
* @since 1.9 |
| 163 |
*/ |
| 164 |
public function include_files() { |
| 165 |
$instance_getter_path = $this->plugin_path . 'classes/Traits/InstanceGetterTrait.php'; |
| 166 |
|
| 167 |
if ( file_exists( $instance_getter_path . '.suspected' ) && ! file_exists( $instance_getter_path ) ) { |
| 168 |
// Trolling greedy antiviruses. |
| 169 |
require_once $instance_getter_path . '.suspected'; |
| 170 |
} |
| 171 |
|
| 172 |
$inc_path = $this->plugin_path . 'inc/'; |
| 173 |
|
| 174 |
require_once $inc_path . '/Dependencies/ActionScheduler/action-scheduler.php'; |
| 175 |
require_once $inc_path . 'deprecated/deprecated.php'; |
| 176 |
require_once $inc_path . 'deprecated/3rd-party.php'; |
| 177 |
require_once $inc_path . 'functions/common.php'; |
| 178 |
require_once $inc_path . 'functions/options.php'; |
| 179 |
require_once $inc_path . 'functions/formatting.php'; |
| 180 |
require_once $inc_path . 'functions/admin.php'; |
| 181 |
require_once $inc_path . 'functions/api.php'; |
| 182 |
require_once $inc_path . 'functions/media.php'; |
| 183 |
require_once $inc_path . 'functions/attachments.php'; |
| 184 |
require_once $inc_path . 'functions/process.php'; |
| 185 |
require_once $inc_path . 'functions/admin-ui.php'; |
| 186 |
require_once $inc_path . 'functions/admin-stats.php'; |
| 187 |
require_once $inc_path . 'functions/i18n.php'; |
| 188 |
require_once $inc_path . 'functions/partners.php'; |
| 189 |
require_once $inc_path . 'common/attachments.php'; |
| 190 |
require_once $inc_path . 'common/partners.php'; |
| 191 |
require_once $inc_path . '3rd-party/3rd-party.php'; |
| 192 |
|
| 193 |
if ( ! is_admin() ) { |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
require_once $inc_path . 'admin/upgrader.php'; |
| 198 |
require_once $inc_path . 'admin/upload.php'; |
| 199 |
require_once $inc_path . 'admin/media.php'; |
| 200 |
require_once $inc_path . 'admin/meta-boxes.php'; |
| 201 |
require_once $inc_path . 'admin/custom-folders.php'; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Trigger a hook on plugin activation after the plugin is loaded. |
| 206 |
* |
| 207 |
* @since 1.9 |
| 208 |
* @see imagify_set_activation() |
| 209 |
*/ |
| 210 |
public function maybe_activate() { |
| 211 |
if ( imagify_is_active_for_network() ) { |
| 212 |
$user_id = get_site_transient( 'imagify_activation' ); |
| 213 |
} else { |
| 214 |
$user_id = get_transient( 'imagify_activation' ); |
| 215 |
} |
| 216 |
|
| 217 |
if ( ! is_numeric( $user_id ) ) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
if ( imagify_is_active_for_network() ) { |
| 222 |
delete_site_transient( 'imagify_activation' ); |
| 223 |
} else { |
| 224 |
delete_transient( 'imagify_activation' ); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Imagify activation. |
| 229 |
* |
| 230 |
* @since 1.9 |
| 231 |
* |
| 232 |
* @param int $user_id ID of the user activating the plugin. |
| 233 |
*/ |
| 234 |
do_action( 'imagify_activation', (int) $user_id ); |
| 235 |
} |
| 236 |
|
| 237 |
/** |
| 238 |
* Load list of event subscribers from service provider. |
| 239 |
* |
| 240 |
* @param ServiceProviderInterface $service_provider Instance of service provider. |
| 241 |
* |
| 242 |
* @return void |
| 243 |
*/ |
| 244 |
private function load_subscribers( ServiceProviderInterface $service_provider ) { |
| 245 |
if ( empty( $service_provider->get_subscribers() ) ) { |
| 246 |
return; |
| 247 |
} |
| 248 |
|
| 249 |
foreach ( $service_provider->get_subscribers() as $subscriber ) { |
| 250 |
$subscriber_object = $this->container->get( $subscriber ); |
| 251 |
|
| 252 |
if ( $subscriber_object instanceof SubscriberInterface ) { |
| 253 |
$this->container->get( 'event_manager' )->add_subscriber( $subscriber_object ); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|