| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
|
| 8 |
class Powered_Cache_Extensions { |
| 9 |
|
| 10 |
public $core_extension_dir; |
| 11 |
|
| 12 |
|
| 13 |
/** |
| 14 |
* placeholder construct |
| 15 |
* |
| 16 |
* @since 1.0 |
| 17 |
*/ |
| 18 |
public function __construct() {} |
| 19 |
|
| 20 |
/** |
| 21 |
* Setup $core_extension_dir |
| 22 |
* |
| 23 |
* @since 1.0 |
| 24 |
*/ |
| 25 |
private function setup() { |
| 26 |
$this->core_extension_dir = apply_filters( 'powered_cache_extension_dir', POWERED_CACHE_PLUGIN_DIR . 'extensions/' ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Get extension info |
| 31 |
* |
| 32 |
* @see load_extentions() method gets extension path from here! |
| 33 |
* @since 1.0 |
| 34 |
* @return mixed|void|array |
| 35 |
*/ |
| 36 |
public function get_extentions() { |
| 37 |
global $powered_cache_fs; |
| 38 |
|
| 39 |
$extentions = array( |
| 40 |
'cloudflare' => $this->core_extension_dir . 'cloudflare/cloudflare.php', |
| 41 |
'remote-cron' => $this->core_extension_dir . 'remote-cron/remote-cron.php', |
| 42 |
'lazy-load' => $this->core_extension_dir . 'lazy-load/lazy-load.php', |
| 43 |
'preload' => $this->core_extension_dir . 'preload/preload.php', |
| 44 |
'varnish' => $this->core_extension_dir . 'varnish/varnish.php', |
| 45 |
'minifier' => $this->core_extension_dir . 'minifier/minifier.php', |
| 46 |
); |
| 47 |
|
| 48 |
|
| 49 |
do_action_ref_array( 'powered_cache_register_extensions', array( $this->core_extension_dir, &$extentions ) ); |
| 50 |
|
| 51 |
$extension_info = array(); |
| 52 |
|
| 53 |
$default_headers = array( |
| 54 |
'Name' => 'Extension Name', |
| 55 |
'ExtensionURI' => 'Extension URI', |
| 56 |
'Version' => 'Version', |
| 57 |
'Description' => 'Description', |
| 58 |
'Author' => 'Author', |
| 59 |
'AuthorURI' => 'Author URI', |
| 60 |
'ExtensionImage' => 'Extension Image', |
| 61 |
'Premium' => 'Premium', |
| 62 |
); |
| 63 |
|
| 64 |
foreach ( $extentions as $id => $extention ) { |
| 65 |
|
| 66 |
if ( $powered_cache_fs->exists( $extention ) ) { |
| 67 |
|
| 68 |
$header_data = get_file_data( $extention, $default_headers ); |
| 69 |
if ( ! empty( $header_data['ExtensionImage'] ) && $powered_cache_fs->exists( plugin_dir_path( $extention ) . $header_data['ExtensionImage'] ) ) { |
| 70 |
$header_data['ExtensionImage'] = plugin_dir_url( $extention ) . $header_data['ExtensionImage']; |
| 71 |
} |
| 72 |
|
| 73 |
$extension_info[ $id ] = $header_data; |
| 74 |
$extension_info[ $id ]['path'] = $extention; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
return apply_filters( 'powered_cache_extension_info', $extension_info ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Load activated extensions |
| 85 |
* |
| 86 |
* @since 1.0 |
| 87 |
*/ |
| 88 |
public function load_extentions() { |
| 89 |
global $powered_cache_fs; |
| 90 |
|
| 91 |
do_action( 'powered_cache_before_extension_load' ); |
| 92 |
|
| 93 |
$activated_extensions = powered_cache_get_option('active_extensions'); |
| 94 |
|
| 95 |
if ( is_array( $activated_extensions ) ) { |
| 96 |
$extensions = $this->get_extentions(); |
| 97 |
|
| 98 |
foreach ( $activated_extensions as $extension ) { |
| 99 |
if ( isset( $extensions[ $extension ] ) && $powered_cache_fs->exists( $extensions[ $extension ]['path'] ) ) { |
| 100 |
include_once $extensions[ $extension ]['path']; |
| 101 |
|
| 102 |
// fire after extension loaded |
| 103 |
do_action( 'powered_cache_extension_' . $extension . '_loaded', $extension ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
// get active extensions and load |
| 110 |
do_action( 'powered_cache_extensions_loaded' ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Check powered cache's extension active |
| 115 |
* |
| 116 |
* @since 1.0 |
| 117 |
* |
| 118 |
* @param string $extension_id |
| 119 |
* |
| 120 |
* @return bool |
| 121 |
*/ |
| 122 |
public function is_active( $extension_id ) { |
| 123 |
$options = get_option( 'powered_cache_settings' ); |
| 124 |
if ( is_array( $options ) && isset( $options['active_extensions'] ) |
| 125 |
&& is_array( $options['active_extensions'] ) |
| 126 |
&& in_array( $extension_id, $options['active_extensions'] ) |
| 127 |
) { |
| 128 |
return true; |
| 129 |
} |
| 130 |
|
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* Activates extension |
| 137 |
* |
| 138 |
* @param $extension_id |
| 139 |
* |
| 140 |
* @since 1.0 |
| 141 |
* @return bool |
| 142 |
*/ |
| 143 |
public function activate( $extension_id ) { |
| 144 |
|
| 145 |
if ( $this->is_active( $extension_id ) ) { |
| 146 |
// bail if already active |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
$old_options = $new_options = powered_cache_get_settings(); |
| 151 |
|
| 152 |
if ( isset( $old_options['active_extensions'] ) && is_array( $old_options['active_extensions'] ) ) { |
| 153 |
$new_options['active_extensions'] = array_merge( $old_options['active_extensions'], array( $extension_id ) ); |
| 154 |
} else { |
| 155 |
$new_options['active_extensions'][] = $extension_id; |
| 156 |
} |
| 157 |
|
| 158 |
do_action( 'powered_cache_extension_activate_' . $extension_id ); |
| 159 |
powered_cache_save_settings( $old_options, $new_options ); |
| 160 |
|
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Deactivate given extension |
| 166 |
* |
| 167 |
* @param $extension_id |
| 168 |
* |
| 169 |
* @since 1.0 |
| 170 |
* @return bool |
| 171 |
*/ |
| 172 |
public function deactivate( $extension_id ) { |
| 173 |
|
| 174 |
if ( ! $this->is_active( $extension_id ) ) { |
| 175 |
// bail if already deactive |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
$old_options = $new_options = powered_cache_get_settings(); |
| 180 |
|
| 181 |
$key = array_search( $extension_id, $old_options['active_extensions'] ); |
| 182 |
|
| 183 |
if ( false !== $key ) { |
| 184 |
unset( $new_options['active_extensions'][ $key ] ); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
do_action( 'powered_cache_extension_deactivate_' . $extension_id ); |
| 189 |
powered_cache_save_settings( $old_options, $new_options ); |
| 190 |
|
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
/** |
| 196 |
* Return an instance of the current class |
| 197 |
* |
| 198 |
* @since 1.0 |
| 199 |
* @return Powered_Cache_Extensions |
| 200 |
*/ |
| 201 |
public static function factory() { |
| 202 |
|
| 203 |
static $instance; |
| 204 |
|
| 205 |
if ( ! $instance ) { |
| 206 |
$instance = new self(); |
| 207 |
$instance->setup(); |
| 208 |
} |
| 209 |
|
| 210 |
return $instance; |
| 211 |
} |
| 212 |
|
| 213 |
} |