PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.3.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.3.1
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / Plugin.php

Plugin.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2.3.1, at classes/Plugin.php

260 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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};
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->share(
90 'event_manager',
91 function () {
92 return new EventManager();
93 }
94 );
95
96 $this->container->share(
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 // Load plugin translations.
135 imagify_load_translations();
136
137 imagify_add_command( new BulkOptimizeCommand() );
138 imagify_add_command( new GenerateMissingNextgenCommand() );
139
140 foreach ( $providers as $service_provider ) {
141 $provider_instance = new $service_provider();
142 $this->container->addServiceProvider( $provider_instance );
143
144 // Load each service provider's subscribers if found.
145 $this->load_subscribers( $provider_instance );
146 }
147
148 /**
149 * Fires when Imagify is fully loaded.
150 *
151 * @since 1.0
152 * @since 1.9 Added the class instance as parameter.
153 *
154 * @param \Imagify_Plugin $plugin Instance of this class.
155 */
156 do_action( 'imagify_loaded', $this );
157
158 $this->loaded = true;
159 }
160
161 /**
162 * Include plugin files.
163 *
164 * @since 1.9
165 */
166 public function include_files() {
167 $instance_getter_path = $this->plugin_path . 'classes/Traits/InstanceGetterTrait.php';
168
169 if ( file_exists( $instance_getter_path . '.suspected' ) && ! file_exists( $instance_getter_path ) ) {
170 // Trolling greedy antiviruses.
171 require_once $instance_getter_path . '.suspected';
172 }
173
174 $inc_path = $this->plugin_path . 'inc/';
175
176 require_once $inc_path . '/Dependencies/ActionScheduler/action-scheduler.php';
177 require_once $inc_path . 'deprecated/deprecated.php';
178 require_once $inc_path . 'deprecated/3rd-party.php';
179 require_once $inc_path . 'functions/common.php';
180 require_once $inc_path . 'functions/options.php';
181 require_once $inc_path . 'functions/formatting.php';
182 require_once $inc_path . 'functions/admin.php';
183 require_once $inc_path . 'functions/api.php';
184 require_once $inc_path . 'functions/media.php';
185 require_once $inc_path . 'functions/attachments.php';
186 require_once $inc_path . 'functions/process.php';
187 require_once $inc_path . 'functions/admin-ui.php';
188 require_once $inc_path . 'functions/admin-stats.php';
189 require_once $inc_path . 'functions/i18n.php';
190 require_once $inc_path . 'functions/partners.php';
191 require_once $inc_path . 'common/attachments.php';
192 require_once $inc_path . 'common/partners.php';
193 require_once $inc_path . '3rd-party/3rd-party.php';
194
195 if ( ! is_admin() ) {
196 return;
197 }
198
199 require_once $inc_path . 'admin/upgrader.php';
200 require_once $inc_path . 'admin/upload.php';
201 require_once $inc_path . 'admin/media.php';
202 require_once $inc_path . 'admin/meta-boxes.php';
203 require_once $inc_path . 'admin/custom-folders.php';
204 }
205
206 /**
207 * Trigger a hook on plugin activation after the plugin is loaded.
208 *
209 * @since 1.9
210 * @see imagify_set_activation()
211 */
212 public function maybe_activate() {
213 if ( imagify_is_active_for_network() ) {
214 $user_id = get_site_transient( 'imagify_activation' );
215 } else {
216 $user_id = get_transient( 'imagify_activation' );
217 }
218
219 if ( ! is_numeric( $user_id ) ) {
220 return;
221 }
222
223 if ( imagify_is_active_for_network() ) {
224 delete_site_transient( 'imagify_activation' );
225 } else {
226 delete_transient( 'imagify_activation' );
227 }
228
229 /**
230 * Imagify activation.
231 *
232 * @since 1.9
233 *
234 * @param int $user_id ID of the user activating the plugin.
235 */
236 do_action( 'imagify_activation', (int) $user_id );
237 }
238
239 /**
240 * Load list of event subscribers from service provider.
241 *
242 * @param ServiceProviderInterface $service_provider Instance of service provider.
243 *
244 * @return void
245 */
246 private function load_subscribers( ServiceProviderInterface $service_provider ) {
247 if ( empty( $service_provider->get_subscribers() ) ) {
248 return;
249 }
250
251 foreach ( $service_provider->get_subscribers() as $subscriber ) {
252 $subscriber_object = $this->container->get( $subscriber );
253
254 if ( $subscriber_object instanceof SubscriberInterface ) {
255 $this->container->get( 'event_manager' )->add_subscriber( $subscriber_object );
256 }
257 }
258 }
259 }
260