PluginProbe
WP-Stateless – Google Cloud Storage / 3.0.2
WP-Stateless – Google Cloud Storage v3.0.2
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / compatibility / ICompatibility.php

ICompatibility.php in WP-Stateless – Google Cloud Storage 3.0.2, at lib/classes/compatibility/ICompatibility.php

269 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Compatibility abstract class
4 *
5 * Must be extends in every compatibility module.
6 * @todo check if the plugin Active or not.
7 *
8 * @class ICompatibility
9 */
10
11 namespace wpCloud\StatelessMedia {
12
13 abstract class ICompatibility {
14 protected $id = '';
15 protected $title = '';
16 /**
17 * // Type String or Array.
18 *
19 * Single constant:
20 * example "WP_STATELESS_COMPATIBILITY_EDD"
21 *
22 * Multiple constant:
23 * [Constant, Another_Constant, ....]
24 * ['WP_STATELESS_MEDIA_ON_FLY', 'WP_STATELESS_DYNAMIC_IMAGE_SUPPORT']
25 *
26 * Deprecated constant:
27 * [Old Constant => New Constant, ...]
28 * ['WP_STATELESS_MEDIA_ON_FLY' => 'WP_STATELESS_DYNAMIC_IMAGE_SUPPORT']
29 */
30 protected $constant = '';
31 protected $enabled = false;
32 protected $description = '';
33 protected $plugin_file = null;
34 protected $theme_name = null;
35 protected $first_party = false;
36 protected $non_library_sync = false;
37 protected $server_constant = false;
38 protected $sm_mode_required = '';
39 protected $sm_mode_not_supported = [];
40
41 public function __construct() {
42 $this->init();
43 }
44
45 /**
46 * Checking whether the plugin is active or not.
47 * If the plugin_file is specified then check whether plugin is active or not.
48 * We can't use is_plugin_active function because it's defined later in init.
49 * By default return true.
50 *
51 * @todo caching.
52 */
53 public function is_plugin_active() {
54 if (!empty($this->theme_name)) {
55 $theme = wp_get_theme();
56 if ($theme->Name == $this->theme_name) {
57 return true;
58 }
59
60 $parent_theme = $theme->parent();
61 if (is_a($parent_theme, 'WP_Theme') && $parent_theme->Name == $this->theme_name) {
62 return true;
63 }
64
65 return false;
66 }
67
68 if (!empty($this->plugin_file)) {
69 // Converting string to array for foreach
70 if (is_string($this->plugin_file)) {
71 $this->plugin_file = array($this->plugin_file);
72 }
73
74 // If multisite then check if plugin is network active
75 if (is_multisite()) {
76 $active_plugins = (array)get_site_option('active_sitewide_plugins');
77 foreach ($this->plugin_file as $plugin_file) {
78 if (isset($active_plugins[$plugin_file])) {
79 return true;
80 }
81 }
82
83 // If we are in network admin then return, unless it will get data from main site.
84 if (is_network_admin()) {
85 return false;
86 }
87 }
88
89 $active_plugins = (array)get_option('active_plugins', array());
90 foreach ($this->plugin_file as $plugin_file) {
91 if (in_array($plugin_file, $active_plugins)) {
92 return true;
93 }
94 }
95
96 return false;
97 }
98
99 /**
100 * If server constant is set - check if exist it on global $_SERVER
101 */
102 if (!empty( $this->server_constant )) {
103 if ( isset($_SERVER[ $this->server_constant ]) ) {
104 return true;
105 }
106 return false;
107 }
108
109 return true;
110 }
111
112 /**
113 * Checking whether current mode is supported.
114 * By default return true.
115 *
116 * @todo caching.
117 */
118 public function is_mode_supported () {
119 $sm_mode = isset($_POST['sm']['mode']) ? $_POST['sm']['mode'] : ud_get_stateless_media()->get( 'sm.mode' );
120 if ( in_array( $sm_mode, $this->sm_mode_not_supported ) ) {
121 return false;
122 }
123 return true;
124 }
125
126 /**
127 * Initialize the module
128 * Check whether plugin is active or not.
129 * Register module.
130 *
131 * Add action for sm::module::init hook for module_init, which is fired(do_action) on Bootstrap::init()
132 */
133 public function init() {
134 $is_constant = false;
135 $is_network_override = false;
136 $sm_mode = isset($_POST['sm']['mode']) ? $_POST['sm']['mode'] : ud_get_stateless_media()->get( 'sm.mode' );
137 if (is_network_admin()) {
138 $this->enabled = null;
139 }
140
141 if (is_array($this->constant)) {
142 foreach ($this->constant as $old_const => $new_const) {
143 if (defined($new_const)) {
144 $is_constant = true;
145 $this->enabled = constant($new_const);
146 break;
147 }
148 if (is_string($old_const) && defined($old_const)) {
149 $is_constant = true;
150 $this->enabled = constant($old_const);
151 ud_get_stateless_media()->errors->add(array(
152 'key' => $this->id,
153 'title' => sprintf(__("%s: Deprecated Notice (%s)", ud_get_stateless_media()->domain), ud_get_stateless_media()->name, $this->title),
154 'message' => sprintf(__("<i>%s</i> constant is deprecated, please use <i>%s</i> instead.", ud_get_stateless_media()->domain), $old_const, $new_const),
155 ), 'notice');
156 break;
157 }
158 }
159 } elseif (defined($this->constant)) {
160 $this->enabled = constant($this->constant);
161 $is_constant = true;
162 }
163
164 if (!$is_constant) {
165 $modules = get_option('stateless-modules', array());
166 if (empty($this->enabled)) {
167 $this->enabled = !empty($modules[$this->id]) && $modules[$this->id] == 'true' ? true : false;
168 }
169 if (is_multisite()) {
170 $modules = get_site_option('stateless-modules', array());
171 if (is_network_admin()) {
172 $this->enabled = !empty($modules[$this->id]) ? ($modules[$this->id] == 'true' ? true : false) : '';
173 } elseif (!empty($modules[$this->id])) {
174 $this->enabled = !empty($modules[$this->id]) ? ($modules[$this->id] == 'true' ? true : false) : '';
175 $is_network_override = true;
176 }
177 }
178 }
179
180 if (!is_network_admin() && ( !$this->is_plugin_active() || !$this->is_mode_supported() )) {
181 $this->enabled = 'inactive';
182 }
183
184 /**
185 * Checking whether to show manual sync option.
186 */
187 if ($this->is_plugin_active() && $this->non_library_sync == true) {
188 global $show_non_library_sync;
189 $show_non_library_sync = true;
190 }
191
192 Module::register_module(array(
193 'id' => $this->id,
194 'self' => $this,
195 'title' => $this->title,
196 'enabled' => $this->enabled,
197 'description' => $this->description,
198 'is_constant' => $is_constant,
199 'is_network_override' => $is_network_override,
200 'is_plugin_active' => $this->is_plugin_active(),
201 'is_network_admin' => is_network_admin(),
202 'is_plugin' => !empty($this->plugin_file),
203 'is_theme' => !empty($this->theme_name),
204 'is_mode_supported' => $this->is_mode_supported(),
205 'mode' => ucfirst( $sm_mode )
206 ));
207
208 if ($this->enabled && $this->is_plugin_active() && $this->is_mode_supported()) {
209 add_action('sm::module::init', array($this, 'module_init'));
210 }
211
212 if (!$this->enabled && !$this->first_party && $this->is_plugin_active()) {
213 ud_get_stateless_media()->errors->add(array(
214 'key' => $this->id,
215 'title' => sprintf(__("%s: Compatibility for %s isn't enabled.", ud_get_stateless_media()->domain), ud_get_stateless_media()->name, $this->title),
216 'button' => __("Enable Compatibility", ud_get_stateless_media()->domain),
217 'message' => __("Please enable the compatibility to ensure the functionality will work properly between <b>{$this->title}</b> and <b>WP-Stateless</b>.", ud_get_stateless_media()->domain),
218 ), 'notice');
219 }
220
221 /**
222 * Check requires WP-Stateless mode
223 */
224 if ( !empty( $this->sm_mode_required ) && $this->enabled !== 'inactive' ) {
225 if ( $sm_mode !== $this->sm_mode_required ) {
226 ud_get_stateless_media()->errors->add( array(
227 'key' => $this->id,
228 'title' => sprintf( __( "%s: Current Mode is not compatible with %s.", ud_get_stateless_media()->domain ), ud_get_stateless_media()->name, $this->title ),
229 'message' => sprintf( __( "%s compatibility requires %s in %s mode.", ud_get_stateless_media()->domain ), $this->title, ud_get_stateless_media()->name, ucfirst( $this->sm_mode_required ) ),
230 ), 'notice' );
231 }
232 }
233 }
234
235 /**
236 * @return bool
237 */
238 public function enable_compatibility() {
239
240 if (is_network_admin()) {
241 $modules = get_site_option('stateless-modules', array());
242 if (empty($modules[$this->id]) || $modules[$this->id] != 'true') {
243 $modules[$this->id] = 'true';
244 update_site_option('stateless-modules', $modules, true);
245 }
246 } else {
247 $modules = get_option('stateless-modules', array());
248 if (empty($modules[$this->id]) || $modules[$this->id] != 'true') {
249 $modules[$this->id] = 'true';
250 update_option('stateless-modules', $modules, true);
251 }
252 }
253
254 return true;
255 }
256
257 /**
258 * add_webp_mime
259 * @param $t
260 * @param $user
261 * @return mixed
262 */
263 public function add_webp_mime($t, $user) {
264 $t['webp'] = 'image/webp';
265 return $t;
266 }
267 }
268
269 }