| 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 |
|
| 38 |
public function __construct(){ |
| 39 |
$this->init(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Checking whether the plugin is active or not. |
| 44 |
* If the plugin_file is specified then check whether plugin is active or not. |
| 45 |
* We can't use is_plugin_active function because it's defined later in init. |
| 46 |
* By default return true. |
| 47 |
* |
| 48 |
* @todo caching. |
| 49 |
*/ |
| 50 |
public function is_plugin_active(){ |
| 51 |
if(!empty($this->theme_name)){ |
| 52 |
$theme = wp_get_theme(); |
| 53 |
if($theme->Name == $this->theme_name){ |
| 54 |
return true; |
| 55 |
} |
| 56 |
return false; |
| 57 |
} |
| 58 |
if(!empty($this->plugin_file)){ |
| 59 |
// Converting string to array for foreach |
| 60 |
if(is_string($this->plugin_file)){ |
| 61 |
$this->plugin_file = array($this->plugin_file); |
| 62 |
} |
| 63 |
|
| 64 |
// If multisite then check if plugin is network active |
| 65 |
if(is_multisite()){ |
| 66 |
$active_plugins = (array) get_site_option( 'active_sitewide_plugins'); |
| 67 |
foreach($this->plugin_file as $plugin_file){ |
| 68 |
if(isset($active_plugins[$plugin_file])){ |
| 69 |
return true; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// If we are in network admin then return, unless it will get data from main site. |
| 74 |
if(is_network_admin()){ |
| 75 |
return false; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
$active_plugins = (array) get_option( 'active_plugins', array() ); |
| 80 |
foreach($this->plugin_file as $plugin_file){ |
| 81 |
if(in_array( $plugin_file, $active_plugins)){ |
| 82 |
return true; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Initialize the module |
| 94 |
* Check whether plugin is active or not. |
| 95 |
* Register module. |
| 96 |
* |
| 97 |
* Add action for sm::module::init hook for module_init, which is fired(do_action) on Bootstrap::init() |
| 98 |
*/ |
| 99 |
public function init(){ |
| 100 |
$is_constant = false; |
| 101 |
$is_network_override = false; |
| 102 |
if(is_network_admin()){ |
| 103 |
$this->enabled = null; |
| 104 |
} |
| 105 |
|
| 106 |
if(is_array($this->constant)){ |
| 107 |
foreach($this->constant as $old_const => $new_const){ |
| 108 |
if(defined($new_const)){ |
| 109 |
$is_constant = true; |
| 110 |
$this->enabled = constant($new_const); |
| 111 |
break; |
| 112 |
} |
| 113 |
if(is_string($old_const) && defined($old_const)){ |
| 114 |
$is_constant = true; |
| 115 |
$this->enabled = constant($old_const); |
| 116 |
ud_get_stateless_media()->errors->add( array( |
| 117 |
'key' => $this->id, |
| 118 |
'title' => sprintf( __( "%s: Deprecated Notice (%s)", ud_get_stateless_media()->domain ), ud_get_stateless_media()->name, $this->title ), |
| 119 |
'message' => sprintf(__("<i>%s</i> constant is deprecated, please use <i>%s</i> instead.", ud_get_stateless_media()->domain), $old_const, $new_const), |
| 120 |
), 'notice' ); |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
elseif (defined($this->constant)) { |
| 126 |
$this->enabled = constant($this->constant); |
| 127 |
$is_constant = true; |
| 128 |
} |
| 129 |
|
| 130 |
if(!$is_constant) { |
| 131 |
$modules = get_option('stateless-modules', array()); |
| 132 |
if (empty($this->enabled)) { |
| 133 |
$this->enabled = !empty($modules[$this->id]) && $modules[$this->id] == 'true' ? true : false; |
| 134 |
} |
| 135 |
if(is_multisite()){ |
| 136 |
$modules = get_site_option( 'stateless-modules', array() ); |
| 137 |
if(is_network_admin()){ |
| 138 |
$this->enabled = !empty($modules[$this->id]) && $modules[$this->id] == 'true' ? true : false; |
| 139 |
} |
| 140 |
elseif(!empty($modules[$this->id])){ |
| 141 |
$this->enabled = !empty($modules[$this->id]) && $modules[$this->id] == 'true' ? true : false; |
| 142 |
$is_network_override = true; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if(!is_network_admin() && !$this->is_plugin_active()){ |
| 148 |
$this->enabled = 'inactive'; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Checking whether to show manual sync option. |
| 153 |
*/ |
| 154 |
if($this->is_plugin_active() && $this->non_library_sync == true){ |
| 155 |
global $show_non_library_sync; |
| 156 |
$show_non_library_sync = true; |
| 157 |
} |
| 158 |
|
| 159 |
Module::register_module(array( |
| 160 |
'id' => $this->id, |
| 161 |
'self' => $this, |
| 162 |
'title' => $this->title, |
| 163 |
'enabled' => $this->enabled, |
| 164 |
'description' => $this->description, |
| 165 |
'is_constant' => $is_constant, |
| 166 |
'is_network_override' => $is_network_override, |
| 167 |
'is_plugin_active' => $this->is_plugin_active(), |
| 168 |
'is_network_admin' => is_network_admin(), |
| 169 |
'is_plugin' => !empty($this->plugin_file), |
| 170 |
'is_theme' => !empty($this->theme_name), |
| 171 |
)); |
| 172 |
|
| 173 |
if ($this->enabled && $this->is_plugin_active()) { |
| 174 |
add_action('sm::module::init', array($this, 'module_init')); |
| 175 |
} |
| 176 |
|
| 177 |
if (!$this->enabled && !$this->first_party && $this->is_plugin_active()) { |
| 178 |
ud_get_stateless_media()->errors->add( array( |
| 179 |
'key' => $this->id, |
| 180 |
'title' => sprintf( __( "%s: Compatibility for %s isn't enabled.", ud_get_stateless_media()->domain ), ud_get_stateless_media()->name, $this->title ), |
| 181 |
'button' => __("Enable Compatibility", ud_get_stateless_media()->domain ), |
| 182 |
'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 ), |
| 183 |
), 'notice' ); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
public function enable_compatibility(){ |
| 188 |
|
| 189 |
if(is_network_admin()){ |
| 190 |
$modules = get_site_option( 'stateless-modules', array() ); |
| 191 |
if(empty($modules[$this->id]) || $modules[$this->id] != 'true'){ |
| 192 |
$modules[$this->id] = 'true'; |
| 193 |
update_site_option('stateless-modules', $modules, true); |
| 194 |
} |
| 195 |
} |
| 196 |
else{ |
| 197 |
$modules = get_option('stateless-modules', array()); |
| 198 |
if(empty($modules[$this->id]) || $modules[$this->id] != 'true'){ |
| 199 |
$modules[$this->id] = 'true'; |
| 200 |
update_option('stateless-modules', $modules, true); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return true; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
} |