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

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