| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Compatibility with other plugins. |
| 5 |
* |
| 6 |
* This class serves as compatibility getway. |
| 7 |
* Initiate all compatibility modules. |
| 8 |
* |
| 9 |
* @class Compatibility |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace wpCloud\StatelessMedia { |
| 13 |
|
| 14 |
class Module { |
| 15 |
|
| 16 |
private static $modules = array(); |
| 17 |
|
| 18 |
/** |
| 19 |
* Object initiated on Bootstrap::__construct |
| 20 |
* Save module data on admin_init hook. |
| 21 |
* Initiate all the compatibility modules. |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
add_action('admin_init', array($this, 'save_modules'), 1); |
| 25 |
add_filter('wp_stateless_compatibility_tab_visible', array($this, 'compatibility_tab_visible'), 10, 1); |
| 26 |
add_action('wp_stateless_compatibility_tab_content', array($this, 'tab_content')); |
| 27 |
|
| 28 |
/** |
| 29 |
* Support for Ewww Image Optimizer |
| 30 |
*/ |
| 31 |
new EWWW(); |
| 32 |
|
| 33 |
/** |
| 34 |
* Support for Imagify |
| 35 |
*/ |
| 36 |
new Imagify(); |
| 37 |
|
| 38 |
/** |
| 39 |
* Support for LearnDash |
| 40 |
*/ |
| 41 |
new LearnDash(); |
| 42 |
|
| 43 |
/** |
| 44 |
* Support for ShortPixel |
| 45 |
*/ |
| 46 |
new ShortPixel(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Support for The Events Calendar |
| 50 |
*/ |
| 51 |
new TheEventsCalendar(); |
| 52 |
|
| 53 |
/** |
| 54 |
* Support for WPBakery Page Builder |
| 55 |
*/ |
| 56 |
new WPBakeryPageBuilder(); |
| 57 |
|
| 58 |
/** |
| 59 |
* Support for Smush |
| 60 |
*/ |
| 61 |
new WPSmush(); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Register compatibility modules so that we can ues them in settings page. |
| 67 |
* Called from ICompatibility::init() method. |
| 68 |
*/ |
| 69 |
public static function register_module($args) { |
| 70 |
if (empty($args['id'])) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
if (is_bool($args['enabled'])) { |
| 75 |
$args['enabled'] = $args['enabled'] ? 'true' : 'false'; |
| 76 |
} |
| 77 |
|
| 78 |
$defaults = array( |
| 79 |
'id' => '', |
| 80 |
'self' => '', |
| 81 |
'title' => '', |
| 82 |
'enabled' => false, |
| 83 |
'description' => '', |
| 84 |
'is_constant' => false, |
| 85 |
'is_network' => false, |
| 86 |
'is_plugin_active' => false, |
| 87 |
'is_internal' => false, |
| 88 |
); |
| 89 |
|
| 90 |
self::$modules[$args['id']] = wp_parse_args($args, $defaults); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Return all the registered modules. |
| 95 |
* Used in admin_init in bootstrap class as localize_script. |
| 96 |
*/ |
| 97 |
public static function get_modules() { |
| 98 |
return self::$modules; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Return all the registered modules. |
| 103 |
* Used in admin_init in bootstrap class as localize_script. |
| 104 |
*/ |
| 105 |
public static function get_module($id) { |
| 106 |
if (!empty(self::$modules[$id])) { |
| 107 |
return self::$modules[$id]; |
| 108 |
} |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Handles saving module data. |
| 114 |
* Enable or disable modules from Compatibility tab. |
| 115 |
*/ |
| 116 |
public function save_modules() { |
| 117 |
if (isset($_POST['action']) && $_POST['action'] == 'stateless_modules' && wp_verify_nonce($_POST['_smnonce'], 'wp-stateless-modules')) { |
| 118 |
$modules = !empty($_POST['stateless-modules']) ? $_POST['stateless-modules'] : array(); |
| 119 |
$modules = apply_filters('stateless::modules::save', $modules); |
| 120 |
|
| 121 |
if (is_network_admin()) { |
| 122 |
update_site_option('stateless-modules', $modules); |
| 123 |
} else { |
| 124 |
update_option('stateless-modules', $modules, true); |
| 125 |
} |
| 126 |
wp_redirect($_POST['_wp_http_referer']); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Check if 'Compatibility' tab should be visible. |
| 132 |
*/ |
| 133 |
public function compatibility_tab_visible($visible) { |
| 134 |
return !empty(self::$modules); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Outputs 'Compatibility' tab content on the settings page. |
| 139 |
* |
| 140 |
*/ |
| 141 |
public function tab_content() { |
| 142 |
$modules = self::get_modules(); |
| 143 |
|
| 144 |
foreach ($modules as $id => $module) { |
| 145 |
if ( !$module['is_internal'] ) { |
| 146 |
unset($modules[$id]); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
$modules = Helper::array_of_objects( $modules ); |
| 151 |
|
| 152 |
include ud_get_stateless_media()->path('static/views/compatibility-tab.php', 'dir'); |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
} |