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

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