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

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