| 1 |
<?php |
| 2 |
|
| 3 |
namespace wpCloud\StatelessMedia; |
| 4 |
|
| 5 |
/** |
| 6 |
* Compatibility abstract class |
| 7 |
* Must be extends in every compatibility module. |
| 8 |
*/ |
| 9 |
abstract class Compatibility { |
| 10 |
protected $id = ''; |
| 11 |
protected $title = ''; |
| 12 |
|
| 13 |
/** |
| 14 |
* // Type String or Array. |
| 15 |
* |
| 16 |
* Single constant: |
| 17 |
* example "WP_STATELESS_COMPATIBILITY_EDD" |
| 18 |
* |
| 19 |
* Multiple constant: |
| 20 |
* [Constant, Another_Constant, ....] |
| 21 |
* ['WP_STATELESS_MEDIA_ON_FLY', 'WP_STATELESS_DYNAMIC_IMAGE_SUPPORT'] |
| 22 |
* |
| 23 |
* Deprecated constant: |
| 24 |
* [Old Constant => New Constant, ...] |
| 25 |
* ['WP_STATELESS_MEDIA_ON_FLY' => 'WP_STATELESS_DYNAMIC_IMAGE_SUPPORT'] |
| 26 |
*/ |
| 27 |
protected $constant = ''; |
| 28 |
protected $enabled = true; |
| 29 |
protected $description = ''; |
| 30 |
protected $plugin_file = null; |
| 31 |
protected $theme_name = null; |
| 32 |
protected $first_party = false; |
| 33 |
protected $non_library_sync = false; |
| 34 |
protected $server_constant = false; |
| 35 |
protected $sm_mode_required = ''; |
| 36 |
protected $sm_mode_not_supported = []; |
| 37 |
protected $is_internal = false; |
| 38 |
|
| 39 |
public function __construct() { |
| 40 |
// Prevent conflict between internal built-in compatibility modules and addon plugins |
| 41 |
$restrict = apply_filters('wp_stateless_restrict_compatibility', false, $this->id, $this->is_internal); |
| 42 |
|
| 43 |
if ($restrict) { |
| 44 |
$this->enabled = false; |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
if ( !$this->is_internal ) { |
| 49 |
add_filter('wp_stateless_restrict_compatibility', array($this, 'restrict_compatibility'), 10, 3); |
| 50 |
} |
| 51 |
|
| 52 |
$this->init(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Checking whether the plugin is active or not. |
| 57 |
* If the plugin_file is specified then check whether plugin is active or not. |
| 58 |
* We can't use is_plugin_active function because it's defined later in init. |
| 59 |
* By default return true. |
| 60 |
* |
| 61 |
* @todo caching. |
| 62 |
*/ |
| 63 |
public function is_plugin_active() { |
| 64 |
if (!empty($this->theme_name)) { |
| 65 |
return Helper::is_theme_name($this->theme_name); |
| 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 |
'is_internal' => $this->is_internal, |
| 207 |
)); |
| 208 |
|
| 209 |
if ($this->enabled && $this->is_plugin_active() && $this->is_mode_supported()) { |
| 210 |
add_action('sm::module::init', array($this, 'module_init')); |
| 211 |
} |
| 212 |
|
| 213 |
if (!$this->enabled && !$this->first_party && $this->is_plugin_active()) { |
| 214 |
ud_get_stateless_media()->errors->add(array( |
| 215 |
'key' => $this->id, |
| 216 |
'title' => sprintf(__("%s: Compatibility for %s isn't enabled.", ud_get_stateless_media()->domain), ud_get_stateless_media()->name, $this->title), |
| 217 |
'button' => __("Enable Compatibility", ud_get_stateless_media()->domain), |
| 218 |
'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), |
| 219 |
), 'notice'); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Check requires WP-Stateless mode |
| 224 |
*/ |
| 225 |
if (!empty($this->sm_mode_required) && $this->enabled !== 'inactive') { |
| 226 |
if ($sm_mode !== $this->sm_mode_required) { |
| 227 |
ud_get_stateless_media()->errors->add(array( |
| 228 |
'key' => $this->id, |
| 229 |
'title' => sprintf(__("%s: Current Mode is not compatible with %s.", ud_get_stateless_media()->domain), ud_get_stateless_media()->name, $this->title), |
| 230 |
'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)), |
| 231 |
), 'notice'); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* @return bool |
| 238 |
*/ |
| 239 |
public function enable_compatibility() { |
| 240 |
|
| 241 |
if (is_network_admin()) { |
| 242 |
$modules = get_site_option('stateless-modules', array()); |
| 243 |
if (empty($modules[$this->id]) || $modules[$this->id] != 'true') { |
| 244 |
$modules[$this->id] = 'true'; |
| 245 |
update_site_option('stateless-modules', $modules, true); |
| 246 |
} |
| 247 |
} else { |
| 248 |
$modules = get_option('stateless-modules', array()); |
| 249 |
if (empty($modules[$this->id]) || $modules[$this->id] != 'true') { |
| 250 |
$modules[$this->id] = 'true'; |
| 251 |
update_option('stateless-modules', $modules, true); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
return true; |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* add_webp_mime |
| 260 |
* @param $t |
| 261 |
* @param $user |
| 262 |
* @return mixed |
| 263 |
*/ |
| 264 |
public function add_webp_mime($t, $user) { |
| 265 |
$t['webp'] = 'image/webp'; |
| 266 |
return $t; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Restrict internal compatibility. |
| 271 |
* @param $restrict bool |
| 272 |
* @param $id string |
| 273 |
* @param $is_internal bool |
| 274 |
* @return bool |
| 275 |
*/ |
| 276 |
public function restrict_compatibility($restrict, $id, $is_internal) { |
| 277 |
// If we have internal compatibility with the same ID - then disable internal compatibility |
| 278 |
return $is_internal && $this->id == $id ? true : $restrict; |
| 279 |
} |
| 280 |
} |
| 281 |
|