| 1 |
<?php |
| 2 |
/** |
| 3 |
* File: class-wpglobus-plugin-install.php |
| 4 |
* |
| 5 |
* @since 1.5.9 |
| 6 |
* @package WPGlobus\Admin |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'WPGlobus_Plugin_Install' ) ) : |
| 14 |
|
| 15 |
/** |
| 16 |
* Class WPGlobus_Plugin_Install |
| 17 |
*/ |
| 18 |
class WPGlobus_Plugin_Install { |
| 19 |
|
| 20 |
/** |
| 21 |
* Fake version for paid plugins to prevent the "Update Now" button from appearing. |
| 22 |
* |
| 23 |
* @var string |
| 24 |
*/ |
| 25 |
const FAKE_VERSION = '999'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Fake active installs for paid plugins. |
| 29 |
* |
| 30 |
* @var int |
| 31 |
*/ |
| 32 |
const FAKE_ACTIVE_INSTALLS = 0; |
| 33 |
|
| 34 |
/** |
| 35 |
* Fake "Compatible with your version of WordPress" for paid plugins. |
| 36 |
* |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
protected static $fake_compatible_with = ''; |
| 40 |
|
| 41 |
/** |
| 42 |
* Array of plugin cards. |
| 43 |
* |
| 44 |
* @var array |
| 45 |
*/ |
| 46 |
protected static $plugin_card = array(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Array of paid plugins data. |
| 50 |
* |
| 51 |
* @var array |
| 52 |
*/ |
| 53 |
protected static $paid_plugins = array(); |
| 54 |
|
| 55 |
/** |
| 56 |
* Array of free plugins data. |
| 57 |
* |
| 58 |
* @var array |
| 59 |
*/ |
| 60 |
protected static $free_plugins = array(); |
| 61 |
|
| 62 |
/** |
| 63 |
* Controller. |
| 64 |
*/ |
| 65 |
public static function controller() { |
| 66 |
|
| 67 |
if ( 'wpglobus' !== strtolower( WPGlobus_WP::get_http_get_parameter( 's' ) ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
self::$fake_compatible_with = $GLOBALS['wp_version']; |
| 72 |
|
| 73 |
self::$plugin_card['free'] = array(); |
| 74 |
self::$plugin_card['paid'] = array(); |
| 75 |
|
| 76 |
self::setup_paid_plugins(); |
| 77 |
|
| 78 |
// Enqueue the CSS & JS scripts. |
| 79 |
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) ); |
| 80 |
|
| 81 |
add_filter( 'plugins_api_result', array( __CLASS__, 'filter__plugins_api_result' ), 10, 3 ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* List of the premium WPGlobus extensions. |
| 86 |
* This file is created manually. |
| 87 |
*/ |
| 88 |
protected static function setup_paid_plugins() { |
| 89 |
|
| 90 |
self::$paid_plugins = array(); |
| 91 |
|
| 92 |
$data_file = WPGlobus::data_path() . '/paid_plugins.json'; |
| 93 |
|
| 94 |
$_json = WPGlobus_WP::fs_get_contents( $data_file ); |
| 95 |
|
| 96 |
if ( $_json ) { |
| 97 |
self::$paid_plugins = json_decode( $_json, true ); |
| 98 |
uasort( self::$paid_plugins, array( __CLASS__, 'sort_paid_plugins' ) ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Callback for sorting the paid_plugins array. |
| 104 |
* |
| 105 |
* @param array $a First. |
| 106 |
* @param array $b Second. |
| 107 |
* |
| 108 |
* @return int |
| 109 |
*/ |
| 110 |
public static function sort_paid_plugins( $a, $b ) { |
| 111 |
return ( $a['order'] < $b['order'] ) ? - 1 : 1; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Filter api results |
| 116 |
* |
| 117 |
* @param stdClass|WP_Error $res Response object or WP_Error. |
| 118 |
* @param string $action The type of information being requested from the Plugin Install API. |
| 119 |
* @param stdClass $args Plugin API arguments. |
| 120 |
* |
| 121 |
* @return stdClass|WP_Error |
| 122 |
*/ |
| 123 |
public static function filter__plugins_api_result( |
| 124 |
$res, |
| 125 |
// @formatter:off |
| 126 |
/* @noinspection PhpUnusedParameterInspection */ $action, |
| 127 |
/* @noinspection PhpUnusedParameterInspection */ $args |
| 128 |
// @formatter:on |
| 129 |
) { |
| 130 |
|
| 131 |
if ( is_wp_error( $res ) ) { |
| 132 |
return $res; |
| 133 |
} |
| 134 |
|
| 135 |
if ( empty( $res->plugins ) ) { |
| 136 |
return $res; |
| 137 |
} |
| 138 |
|
| 139 |
foreach ( (array) $res->plugins as $key => $plugin ) { |
| 140 |
if ( is_array( $plugin ) ) { |
| 141 |
/** |
| 142 |
* Make it object. |
| 143 |
* |
| 144 |
* @since 2.1.10 |
| 145 |
*/ |
| 146 |
$plugin = (object) $plugin; |
| 147 |
} |
| 148 |
if ( false === strpos( $plugin->slug, 'wpglobus' ) ) { |
| 149 |
unset( $res->plugins[ $key ] ); |
| 150 |
} else { |
| 151 |
|
| 152 |
if ( 'wpglobus-for-black-studio-tinymce-widget' === $plugin->slug ) { |
| 153 |
|
| 154 |
/** |
| 155 |
* Set correct slug for the |
| 156 |
* `WPGlobus for Black Studio TinyMCE Widget` plugin. |
| 157 |
* |
| 158 |
* @since 1.6.3 |
| 159 |
*/ |
| 160 |
$plugin->slug = 'wpglobus-for-black-studio-widget'; |
| 161 |
|
| 162 |
self::$plugin_card['free'][] = $plugin->slug; |
| 163 |
|
| 164 |
self::$free_plugins[ $plugin->slug ]['extra_data']['correctLink'] = 'wpglobus-for-black-studio-tinymce-widget'; |
| 165 |
|
| 166 |
} else { |
| 167 |
self::$plugin_card['free'][] = $plugin->slug; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
$url_wpglobus_site = WPGlobus_Utils::url_wpglobus_site(); |
| 173 |
|
| 174 |
$all_products = self::get_all_product_info(); |
| 175 |
|
| 176 |
foreach ( self::$paid_plugins as $plugin => $plugin_data ) { |
| 177 |
|
| 178 |
$plugin_file = implode( '/', array( WP_PLUGIN_DIR, $plugin_data['slug'], $plugin_data['loader'] ) ); |
| 179 |
|
| 180 |
if ( is_readable( $plugin_file ) ) { |
| 181 |
// Plugin is installed. |
| 182 |
self::$paid_plugins[ $plugin ]['plugin_data'] = get_plugin_data( $plugin_file, false ); |
| 183 |
} else { |
| 184 |
self::$paid_plugins[ $plugin ]['plugin_data'] = null; |
| 185 |
|
| 186 |
$product_slug = ( isset( $plugin_data['product_slug'] ) ? $plugin_data['product_slug'] : $plugin ); |
| 187 |
|
| 188 |
if ( isset( $all_products[ $product_slug ] ) ) { |
| 189 |
$plugin_info = $all_products[ $product_slug ]; |
| 190 |
|
| 191 |
/** |
| 192 |
* Titles come as multilingual strings but only in 2 languages ['en','ru'] |
| 193 |
* because the WPGlobus website has only those. |
| 194 |
* So we need to force `en` language code if the admin language |
| 195 |
* is out of the list. |
| 196 |
*/ |
| 197 |
$language = WPGlobus::Config()->language; |
| 198 |
if ( ! in_array( $language, array( 'en', 'ru' ), true ) ) { |
| 199 |
$language = 'en'; |
| 200 |
} |
| 201 |
$_plugin_title = WPGlobus_Core::text_filter( $plugin_info['title'], $language ); |
| 202 |
|
| 203 |
self::$paid_plugins[ $plugin ]['plugin_data'] = array( |
| 204 |
'Description' => '', // TODO. |
| 205 |
'Name' => $_plugin_title, |
| 206 |
'Title' => $_plugin_title, |
| 207 |
'Version' => $plugin_info['_api_new_version'], |
| 208 |
'PluginURI' => $url_wpglobus_site . 'product/' . |
| 209 |
$product_slug . '/', |
| 210 |
); |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Prepend the premium add-ons to the list of plugins. |
| 217 |
*/ |
| 218 |
foreach ( self::$paid_plugins as $slug => $paid_plugin ) { |
| 219 |
|
| 220 |
$info = self::plugin_info_template(); |
| 221 |
|
| 222 |
$info->slug = $slug; |
| 223 |
|
| 224 |
/** |
| 225 |
* Internal image. |
| 226 |
* |
| 227 |
* @since 2.6.4 |
| 228 |
*/ |
| 229 |
$internal_image = true; |
| 230 |
foreach ( array( 'http://', 'https://' ) as $_scheme ) { |
| 231 |
if ( false !== strpos( $paid_plugin['image_file'], $_scheme ) ) { |
| 232 |
$internal_image = false; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if ( $internal_image ) { |
| 237 |
$info->icons['default'] = WPGlobus::internal_images_url() . '/' . $paid_plugin['image_file']; |
| 238 |
} else { |
| 239 |
$info->icons['default'] = $paid_plugin['image_file']; |
| 240 |
} |
| 241 |
$info->icons['1x'] = $info->icons['default']; |
| 242 |
$info->icons['2x'] = $info->icons['default']; |
| 243 |
|
| 244 |
if ( ! empty( $paid_plugin['plugin_data'] ) ) { |
| 245 |
$info->name = $paid_plugin['plugin_data']['Name']; |
| 246 |
$info->short_description = $paid_plugin['plugin_data']['Description']; |
| 247 |
$info->homepage = $paid_plugin['plugin_data']['PluginURI']; |
| 248 |
} else { |
| 249 |
$info->name = $slug; |
| 250 |
} |
| 251 |
|
| 252 |
self::$plugin_card['paid'][] = $slug; |
| 253 |
|
| 254 |
self::$paid_plugins[ $slug ]['card'] = $info; |
| 255 |
|
| 256 |
self::$paid_plugins[ $slug ]['extra_data']['product_url'] = $info->homepage; |
| 257 |
self::$paid_plugins[ $slug ]['extra_data']['details_url'] = $info->homepage; |
| 258 |
|
| 259 |
/** |
| 260 |
* Don't add unavailable plugin in response. |
| 261 |
* |
| 262 |
* @since 2.4.2 |
| 263 |
*/ |
| 264 |
if ( ! empty( $paid_plugin['available'] ) && false !== $paid_plugin['available'] ) { |
| 265 |
array_unshift( $res->plugins, $info ); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
$res->info['results'] = count( $res->plugins ); |
| 270 |
|
| 271 |
return $res; |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Get information about all premium plugins. |
| 276 |
* This file is created automatically at build. Do not edit! |
| 277 |
* |
| 278 |
* @return array[] |
| 279 |
*/ |
| 280 |
protected static function get_all_product_info() { |
| 281 |
$all_product_info = array(); |
| 282 |
|
| 283 |
$data_file = WPGlobus::data_path() . '/wpglobus-product-info.json'; |
| 284 |
|
| 285 |
if ( is_readable( $data_file ) ) { |
| 286 |
$all_product_info_json = WPGlobus_WP::fs_get_contents( $data_file ); |
| 287 |
$all_product_info = json_decode( $all_product_info_json, true ); |
| 288 |
} |
| 289 |
|
| 290 |
return $all_product_info; |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Template for plugin info. |
| 295 |
* |
| 296 |
* @return stdClass |
| 297 |
*/ |
| 298 |
protected static function plugin_info_template() { |
| 299 |
$url_wpglobus_site = WPGlobus_Utils::url_wpglobus_site(); |
| 300 |
|
| 301 |
$template = new stdClass(); |
| 302 |
$template->name = ''; |
| 303 |
$template->short_description = ''; |
| 304 |
$template->author = '<a href="' . $url_wpglobus_site . '">WPGlobus</a>'; |
| 305 |
$template->author_profile = $url_wpglobus_site; |
| 306 |
$template->homepage = $url_wpglobus_site; |
| 307 |
$template->slug = ''; |
| 308 |
$template->rating = 100; |
| 309 |
$template->num_ratings = 0; |
| 310 |
$template->active_installs = self::FAKE_ACTIVE_INSTALLS; |
| 311 |
$template->version = self::FAKE_VERSION; |
| 312 |
$template->tested = self::$fake_compatible_with; |
| 313 |
$template->icons['default'] = ''; |
| 314 |
$template->icons['2x'] = ''; |
| 315 |
$template->icons['1x'] = ''; |
| 316 |
$template->last_updated = gmdate( 'c' ); |
| 317 |
|
| 318 |
return $template; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Enqueue admin JS scripts. |
| 323 |
* |
| 324 |
* @param string $hook_page The current admin page. |
| 325 |
*/ |
| 326 |
public static function enqueue_scripts( $hook_page ) { |
| 327 |
|
| 328 |
if ( 'plugin-install.php' === $hook_page ) { |
| 329 |
|
| 330 |
$i18n = array(); |
| 331 |
$i18n['current_version'] = esc_html__( 'Current Version', 'wpglobus' ); |
| 332 |
$i18n['card_header'] = esc_html__( 'Premium add-on', 'wpglobus' ); |
| 333 |
$i18n['installed'] = esc_html__( 'Installed', 'wpglobus' ); |
| 334 |
|
| 335 |
/** |
| 336 |
* Fix for ' !' in French translation. |
| 337 |
* |
| 338 |
* @since 2.5.20 |
| 339 |
*/ |
| 340 |
$i18n['get_it'] = html_entity_decode( __( 'Get it now!', 'wpglobus' ) ); |
| 341 |
|
| 342 |
/** |
| 343 |
* Link to the installation instructions. |
| 344 |
* |
| 345 |
* @since 2.4.3 |
| 346 |
*/ |
| 347 |
// translators: placeholders are for the HTML tags. |
| 348 |
$pre_addons_info = esc_html__( 'If you have already purchased a WPGlobus premium extension, please read %1$sthe installation instructions here%2$s', 'wpglobus' ); |
| 349 |
$i18n['pre_addons_info'] = sprintf( $pre_addons_info, '<a href="' . WPGlobus::URL_WPGLOBUS_SITE . 'extensions/how-to-install/" target="_blank" style="color:#fff;">', '</a>' ); |
| 350 |
|
| 351 |
wp_register_script( |
| 352 |
'wpglobus-plugin-install', |
| 353 |
WPGlobus::$PLUGIN_DIR_URL . '/includes/js/wpglobus-plugin-install' . WPGlobus::SCRIPT_SUFFIX() . '.js', |
| 354 |
array( 'jquery' ), |
| 355 |
WPGLOBUS_VERSION, |
| 356 |
true |
| 357 |
); |
| 358 |
wp_enqueue_script( 'wpglobus-plugin-install' ); |
| 359 |
wp_localize_script( |
| 360 |
'wpglobus-plugin-install', |
| 361 |
'WPGlobusPluginInstall', |
| 362 |
array( |
| 363 |
'version' => WPGLOBUS_VERSION, |
| 364 |
'hookPage' => $hook_page, |
| 365 |
'pluginCard' => self::$plugin_card, |
| 366 |
'pluginData' => array_merge( self::$paid_plugins, self::$free_plugins ), |
| 367 |
'i18n' => $i18n, |
| 368 |
) |
| 369 |
); |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
endif; |
| 375 |
|