PluginProbe
WP-Stateless – Google Cloud Storage / 2.3.1
WP-Stateless – Google Cloud Storage v2.3.1
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 2.3.1, at lib/classes/compatibility/ICompatibility.php

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