PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.2.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.2.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.1, at classes/Plugin.php

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