Aelia_WC_RequirementsChecks

Checks that plugin's requirements are met.

Defined (1)

The class is defined in the following location(s).

/src/lib/classes/install/aelia-wc-requirementscheck.php  
  1. class Aelia_WC_RequirementsChecks { 
  2. /** 
  3. * The KB url with the instructions to address "missing requirements" messages. 
  4. * @var string 
  5. * @since 1.8.3.170202 
  6. */ 
  7. const MISSING_REQUIREMENTS_GUIDE_URL = 'https://aelia.freshdesk.com/solution/articles/3000063640-how-to-fix-the-message-saying-that-a-plugin-could-not-be-loaded-during-to-missing-requirements-'; 
  8.  
  9. /** 
  10. * The text domain for the messages displayed by the class. This property must 
  11. * be overridden by descendant classes and it should match the text domain 
  12. * of the plugin that is going to check the requirements. 
  13. * @var string 
  14. */ 
  15. protected $text_domain = 'wc_aelia'; 
  16.  
  17. /** 
  18. * The name of the plugin for which the requirements are being checked. This 
  19. * property must be overridden by descendant classes and it must match the 
  20. * plugin name exactly. 
  21. * @var string 
  22. */ 
  23. protected $plugin_name = 'WC Template Plugin'; 
  24.  
  25. /** 
  26. * The path to the directory containing the admin-install.js file. It must 
  27. * not have a leading or trailing slash. 
  28. * @var string 
  29. * @see Aelia_WC_RequirementsChecks::js_url 
  30. * @since 1.5.1.150305 
  31. */ 
  32. protected $js_dir = 'src/js/admin'; 
  33.  
  34. /** 
  35. * The minimum version of PHP required by the plugin. 
  36. * @var string 
  37. */ 
  38. protected $required_php_version = '5.3'; 
  39.  
  40. // @var array An array of PHP extensions required by the plugin 
  41. protected $required_extensions = array( 
  42. //'curl',  
  43. ); 
  44.  
  45. // @var array A list of all the installed plugins. 
  46. protected static $_installed_plugins; 
  47.  
  48. // @var array An array of plugin details. Used for caching. 
  49. protected static $_plugins_info = array(); 
  50.  
  51. // @var array An array of WordPress plugins (name => version) required by the plugin. 
  52. protected $required_plugins = array( 
  53. 'WooCommerce' => '2.0.10',  
  54. //'Aelia Foundation Classes for WooCommerce' => array( 
  55. // 'version' => '1.8.3.170202',  
  56. // 'extra_info' => 'You can get the plugin <a href="http://bit.ly/WC_AFC_S3">from our site</a>, free of charge.',  
  57. // 'autoload' => true,  
  58. // 'url' => 'http://bit.ly/WC_AFC_S3',  
  59. //),  
  60. ); 
  61.  
  62.  
  63. // @var array An array with the details of the required plugins. 
  64. protected $required_plugins_info = array(); 
  65.  
  66. // @var array Holds a list of the errors related to missing requirements 
  67. protected $requirements_errors = array(); 
  68.  
  69. protected $plugin_actions = array(); 
  70.  
  71. public function __construct() { 
  72. // Require necessary WP Core files 
  73. if(!function_exists('get_plugins')) { 
  74. require_once(ABSPATH . 'wp-admin/includes/plugin.php'); 
  75.  
  76. // Ensure that all plugin requirements are in array format 
  77. $this->normalize_plugin_requirements(); 
  78.  
  79. // Ajax hooks 
  80. add_action('wp_ajax_' . $this->get_ajax_action('install'), array($this, 'wp_ajax_install_plugin')); 
  81. add_action('wp_ajax_' . $this->get_ajax_action('activate'), array($this, 'wp_ajax_activate_plugin')); 
  82.  
  83. /** 
  84. * Normalizes the format of the plugin requirements list. This operation is 
  85. * necessary for backward compatibility, as the plugin requirements might 
  86. * just contain the plugin version, rather than an array of plugin details. 
  87. * @since 1.5.4.150316 
  88. */ 
  89. protected function normalize_plugin_requirements() { 
  90. foreach($this->required_plugins as $plugin_name => $plugin_requirements) { 
  91. // If plugin_details is not an array, it's assumed to be a string containing 
  92. // the required plugin version 
  93. if(!is_array($plugin_requirements)) { 
  94. $this->required_plugins[$plugin_name] = array( 
  95. 'version' => $plugin_requirements,  
  96. ); 
  97.  
  98. /** 
  99. * Factory method. It MUST be copied to every descendant class, as it has to 
  100. * be compatible with PHP 5.2 and earlier, so that the class can be instantiated 
  101. * in any case and and gracefully tell the user if PHP version is insufficient. 
  102. * @return Aelia_WC_RequirementsChecks 
  103. */ 
  104. public static function factory() { 
  105. $instance = new self(); 
  106. return $instance; 
  107.  
  108. /** 
  109. * Builds and returns the action ID to install or activate a plugin. 
  110. * @param string action The action key ("install" or "activate"). 
  111. * @return string 
  112. * @since 1.5.0.150225 
  113. */ 
  114. protected function get_ajax_action($action) { 
  115. return $action . '_plugin_' . sha1(get_class($this)); 
  116.  
  117. /** 
  118. * Checks that one or more PHP extensions are loaded. 
  119. * @return array An array of error messages containing one entry for each 
  120. * extension that is not loaded. 
  121. */ 
  122. protected function check_required_extensions() { 
  123. foreach($this->required_extensions as $extension) { 
  124. if(!extension_loaded($extension)) { 
  125. $this->requirements_errors[] = sprintf(__('Plugin requires "%s" PHP extension.', $this->text_domain),  
  126. $extension); 
  127.  
  128. /** 
  129. * Returns the URL that will trigger the installation of a plugin. 
  130. * @param string plugin_name The plugin name. 
  131. * @return string 
  132. * @since 1.5.0.150225 
  133. */ 
  134. protected function get_plugin_install_url($plugin_name) { 
  135. $plugin_slug = sanitize_title($plugin_name); 
  136. $plugin_install_url = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=' . $plugin_slug), 'install-plugin_' . $plugin_slug); 
  137. return $plugin_install_url; 
  138.  
  139. /** 
  140. * Returns the Ajax URL that will trigger the installation of a plugin. 
  141. * @param string plugin_name The plugin name. 
  142. * @return string 
  143. * @since 1.5.0.150225 
  144. */ 
  145. protected function get_ajax_url($plugin_name, $action) { 
  146. $plugin_slug = sanitize_title($plugin_name); 
  147. $ajax_args = http_build_query(array( 
  148. 'action' => $this->get_ajax_action($action),  
  149. 'plugin' => $plugin_slug,  
  150. '_ajax_nonce' => wp_create_nonce('aelia-plugin-' . $action . '-' . $plugin_slug),  
  151. )); 
  152. $ajax_url = admin_url('admin-ajax.php', 'absolute') . '?' . $ajax_args; 
  153. return $ajax_url; 
  154.  
  155. /** 
  156. * Checks that the necessary plugins are installed, and that their version is 
  157. * the expected one. 
  158. * @param bool autoload_plugins Indicates if the required plugins should be 
  159. * loaded automatically, if requirements checks pass. 
  160. */ 
  161. protected function check_required_plugins($autoload_plugins = true) { 
  162. foreach($this->required_plugins as $plugin_name => $plugin_requirements) { 
  163. $plugin_info = $this->get_wp_plugin_info($plugin_name); 
  164.  
  165. $message = ''; 
  166. if(!is_array($plugin_info)) { 
  167. // Plugin is not installed. Check if it can be installed automatically 
  168. // and provide a button to do it 
  169. if(isset($plugin_requirements['url']) && filter_var($plugin_requirements['url'], FILTER_VALIDATE_URL)) { 
  170. // Debug 
  171. //var_dump($plugin_requirements['url']); 
  172. $this->plugin_actions[$plugin_name] = 'install'; 
  173. else { 
  174. if(!$plugin_info['active']) { 
  175. // If plugin can be activated, provide a button to do it 
  176. $this->plugin_actions[$plugin_name] = 'activate'; 
  177.  
  178. if(is_array($plugin_info) && ($plugin_info['active'] == true)) { 
  179. if(version_compare($plugin_info['version'], $plugin_requirements['version'], '<')) { 
  180. $message = sprintf(__('Plugin "%s" must be version "%s" or later.', $this->text_domain),  
  181. $plugin_name,  
  182. $plugin_requirements['version']); 
  183. else { 
  184. // If plugin must be loaded automatically, without waiting for WordPress to load it,  
  185. // add it to the autoload queue 
  186. if(isset($plugin_requirements['autoload']) && ($plugin_requirements['autoload'] == true)) { 
  187. $this->required_plugins_info[$plugin_name] = $plugin_info; 
  188. else { 
  189. $message = sprintf(__('Plugin "<strong>%s</strong>" must be installed and activated.', $this->text_domain),  
  190. $plugin_name); 
  191.  
  192. if(!empty($message)) { 
  193. if(isset($plugin_requirements['extra_info'])) { 
  194. $message .= ' ' . $plugin_requirements['extra_info']; 
  195. $this->requirements_errors[$plugin_name] = $message; 
  196.  
  197. /** 
  198. * Checks that plugin requirements are satisfied. 
  199. * @return bool 
  200. */ 
  201. public function check_requirements() { 
  202. $this->requirements_errors = array(); 
  203. if(PHP_VERSION < $this->required_php_version) { 
  204. $this->requirements_errors[] = sprintf(__('Plugin requires PHP %s or newer.', $this->text_domain),  
  205. $this->required_php_version); 
  206.  
  207. $this->check_required_extensions(); 
  208. $this->check_required_plugins(); 
  209.  
  210. $result = empty($this->requirements_errors); 
  211.  
  212. if($result) { 
  213. $this->load_required_plugins(); 
  214. else { 
  215. // If requirements are missing, display the appropriate notices 
  216. add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); 
  217. add_action('admin_notices', array($this, 'plugin_requirements_notices')); 
  218. return $result; 
  219.  
  220. /** 
  221. * Checks if WC plugin is active, either for the single site or, in 
  222. * case of WPMU, for the whole network. 
  223. * @return bool 
  224. */ 
  225. public static function is_wc_active() { 
  226. if(defined('WC_ACTIVE')) { 
  227. return WC_ACTIVE; 
  228.  
  229. // Test if WC is installed and active 
  230. if(self::factory()->is_plugin_active('WooCommerce')) { 
  231. define('WC_ACTIVE', true); 
  232. return true; 
  233. return false; 
  234.  
  235. /** 
  236. * Clears the chached list of installed plugins. This is necessary after a 
  237. * new plugin is installed, to allow WordPress to find it. 
  238. */ 
  239. protected function clear_plugin_cache() { 
  240. wp_cache_set('plugins', false, 'plugins'); 
  241. self::$_installed_plugins = null; 
  242.  
  243. /** 
  244. * Returns a list of the installed plugins. 
  245. * @return array 
  246. */ 
  247. protected function installed_plugins() { 
  248. if(empty(self::$_installed_plugins)) { 
  249. self::$_installed_plugins = get_plugins(); 
  250. return self::$_installed_plugins; 
  251.  
  252. /** 
  253. * Returns the details of a plugin from $this->required_plugins property. 
  254. * @param string $plugin_slug_with_hash A plugin slug. The slug must match 
  255. * one of the plugin names from the required_plugins property. 
  256. * @return array|false 
  257. * @since 1.5.0.150225 
  258. */ 
  259. protected function get_plugin_info($plugin_slug) { 
  260. foreach($this->required_plugins as $plugin_name => $plugin_info) { 
  261. if($plugin_slug === sanitize_title($plugin_name)) { 
  262. $plugin_info['name'] = $plugin_name; 
  263. return $plugin_info; 
  264. return false; 
  265.  
  266. /** 
  267. * Queries WordPress and returns the details of an installed plugin. 
  268. * @param string plugin_name The plugin name. 
  269. * @return array|bool An array of plugin details, or false if the plugin is 
  270. * not installed. 
  271. * @since 1.5.0.150225 
  272. */ 
  273. protected function get_wp_plugin_info($plugin_name) { 
  274. if(empty(self::$_plugins_info[$plugin_name])) { 
  275. self::$_plugins_info[$plugin_name] = false; 
  276. foreach($this->installed_plugins() as $path => $plugin_info) { 
  277. if(strcasecmp($plugin_info['Name'], $plugin_name) === 0) { 
  278. $plugin_info['path'] = $path; 
  279. $plugin_info['active'] = is_plugin_active($path); 
  280. $plugin_info = array_change_key_case($plugin_info, CASE_LOWER); 
  281.  
  282. self::$_plugins_info[$plugin_name] = $plugin_info; 
  283. break; 
  284. return self::$_plugins_info[$plugin_name]; 
  285.  
  286. /** 
  287. * Returns the URL to the folder containing the JavaScript that will be used 
  288. * for the frontend. 
  289. * @return string|null An URL, or null if the URL cannot be determined. 
  290. * @since 1.5.0.150225 
  291. */ 
  292. protected function js_url() { 
  293. $plugin_info = $this->get_wp_plugin_info($this->plugin_name); 
  294. if($plugin_info == false) { 
  295. trigger_error(sprintf(__('%s - Plugin not found: "%s". ' . 
  296. 'This could be due by an incorrect name specified in ' . 
  297. 'class "%s". Please report the issue to support.',  
  298. $this->text_domain),  
  299. get_class($this),  
  300. $this->plugin_name,  
  301. get_class($this)),  
  302. E_USER_WARNING); 
  303. return null; 
  304. return plugin_dir_url($plugin_info['path']) . $this->js_dir; 
  305.  
  306. /** 
  307. * Returns the details of a plugin, showing if it's installed and active. 
  308. * @param string plugin_name The name of the plugin to check. 
  309. * @return bool 
  310. * @deprecated since 1.5.0.150225 
  311. */ 
  312. public function is_plugin_active($plugin_name) { 
  313. return $this->get_wp_plugin_info($plugin_name); 
  314.  
  315. /** 
  316. * Returns the snippet of HTML code that allows to automatically install or 
  317. * activate a plugin. The method returns an empty string if none of such 
  318. * operations is possible. 
  319. * @param string plugin_name The name of the plugin to be installed or 
  320. * activated automatically. 
  321. * @return string The HTML snippet with the appropriate action (install/activate),  
  322. * or an empty string if neither action is possible. 
  323. * @since 1.5.4.150316 
  324. */ 
  325. protected function get_plugin_action_html($plugin_name) { 
  326. if(empty($this->plugin_actions[$plugin_name])) { 
  327. return ''; 
  328.  
  329. $plugin_action = $this->plugin_actions[$plugin_name]; 
  330. $plugin_slug = sanitize_title($plugin_name); 
  331. $action_html = ''; 
  332. switch($plugin_action) { 
  333. case 'install': 
  334. $plugin_install_url = $this->get_ajax_url($plugin_name, 'install'); 
  335. $plugin_action = '<a href="' . $plugin_install_url . '" ' . 
  336. 'class="plugin_action button" ' . 
  337. 'plugin_slug="' . $plugin_slug . '" ' . 
  338. 'prompt="install" ' . 
  339. 'ajax_url="' . $plugin_install_url . '">'; 
  340. $plugin_action .= __('Install plugin', $this->text_domain); 
  341. $plugin_action .= '</a>'; 
  342. break; 
  343. case 'activate': 
  344. // Plugin is installed, but not active. Add button to activate it 
  345. $plugin_action = '<a href="#" class="plugin_action button" ' . 
  346. 'plugin_slug="' . $plugin_slug . '" ' . 
  347. 'prompt="activate" ' . 
  348. 'ajax_url="' . $this->get_ajax_url($plugin_name, 'activate') . '">'; 
  349. $plugin_action .= __('Activate plugin', $this->text_domain); 
  350. $plugin_action .= '</a>'; 
  351. break; 
  352. default: 
  353. // Nothing to do 
  354.  
  355. if(!empty($plugin_action)) { 
  356. // Build the plugin wrapper that will contain the link to install or 
  357. // activate the plugin 
  358. $plugin_action_wrapper = '<span class="plugin_action_wrapper">'; 
  359. $plugin_action_wrapper .= '<span class="spinner"></span>%s'; 
  360. $plugin_action_wrapper .= '<div class="plugin_action_result"><pre class="messages"></pre></div>'; 
  361. $plugin_action_wrapper .= '</span>'; 
  362.  
  363. $action_html = sprintf($plugin_action_wrapper, $plugin_action); 
  364. return $action_html; 
  365.  
  366. /** 
  367. * Display requirements errors that prevented the plugin from being loaded. 
  368. */ 
  369. public function plugin_requirements_notices() { 
  370. if(empty($this->requirements_errors)) { 
  371. return; 
  372.  
  373. // For each missing plugin, check if it's possible to install or activate 
  374. // it automatically. If it's possible, render a button to allow the administrator 
  375. // to do so 
  376. foreach($this->requirements_errors as $plugin_name => $message) { 
  377. if(is_string($plugin_name)) { 
  378. $this->requirements_errors[$plugin_name] .= $this->get_plugin_action_html($plugin_name); 
  379. ?> 
  380. <style type="text/css"> 
  381. .wc_aelia.message .spinner,  
  382. .wc_aelia.message .button { 
  383. vertical-align: middle; 
  384. margin-left: 0.5em; 
  385. float: none; 
  386.  
  387. .wc_aelia.message .spinner { 
  388. display: none; 
  389.  
  390. .wc_aelia.message .spinner.visible { 
  391. display: inline-block; 
  392. visibility: visible; 
  393.  
  394. .wc_aelia.what_is_this { 
  395. font-weight: normal; 
  396. text-decoration: none; 
  397.  
  398. .wc_aelia.what_is_this:hover { 
  399. text-decoration: underline; 
  400. </style> 
  401. <div class="wc_aelia message error fade"> 
  402. <h4 class="wc_aeliamessage_header" style="margin: 1em 0 0 0"><?php 
  403. echo sprintf(__('Plugin "%s" could not be loaded due to missing requirements.',  
  404. $this->text_domain),  
  405. $this->plugin_name); 
  406. ?> 
  407. <span class=""><?php 
  408. ?> 
  409. [<a class="wc_aelia what_is_this" target="_blank" href="<?php echo self::MISSING_REQUIREMENTS_GUIDE_URL; ?>"><?php 
  410. echo __('How do I solve this?', $this->text_domain); 
  411. ?></a>] 
  412. </span> 
  413. </h4> 
  414. <ul style="list-style: disc inside"> 
  415. <li><?php 
  416. echo implode('</li><li>', $this->requirements_errors); 
  417. ?></li> 
  418. </ul> 
  419. <p class="info"><?php 
  420. echo __('Please review the missing requirements listed above, and ensure ' . 
  421. 'that all necessary plugins and PHP extensions are installed and ' . 
  422. 'loaded correctly. This plugin will work automatically as soon as all the ' . 
  423. 'requirements are met. If you need assistance on this matter, please ' . 
  424. '<a href="https://aelia.freshdesk.com/helpdesk/tickets/new">contact our ' . 
  425. 'Support team</a>.',  
  426. $this->text_domain); 
  427. ?></p> 
  428. </div> 
  429. <?php 
  430.  
  431. /** 
  432. * Enqueues the scripts required by the requirement checker class. 
  433. * @since 1.5.0.150225 
  434. */ 
  435. public function admin_enqueue_scripts() { 
  436. $js_url = $this->js_url(); 
  437. if(empty($js_url) || wp_script_is('aelia-install', 'enqueued')) { 
  438. return false; 
  439. wp_enqueue_script('aelia-install',  
  440. $js_url . '/aelia-install.js',  
  441. array('jquery'),  
  442. null,  
  443. true); 
  444.  
  445. $aelia_wc_requirementchecks_params = array( 
  446. 'user_interface' => array( 
  447. 'plugin_install_prompt' => __('This will install and activate the plugin ' . 
  448. 'automatically. Would you like to proceed?', $this->text_domain),  
  449. 'plugin_activate_prompt' => __('This will activate the plugin. Would you like to proceed?',  
  450. $this->text_domain),  
  451. ),  
  452. ); 
  453. wp_localize_script('aelia-install',  
  454. 'aelia_wc_requirementchecks_params',  
  455. $aelia_wc_requirementchecks_params); 
  456.  
  457. /** 
  458. * Loads the required plugins. 
  459. */ 
  460. public function load_required_plugins() { 
  461. foreach($this->required_plugins_info as $plugin_name => $plugin_info) { 
  462. // Debug 
  463. //var_dump(WP_PLUGIN_DIR . '/' . $plugin_info['path']); 
  464. require_once(WP_PLUGIN_DIR . '/' . $plugin_info['path']); 
  465.  
  466. /** 
  467. * Installs a plugin from the specified URL. 
  468. * @param string plugin_url The URL to a plugin ZIP file. 
  469. * @param string plugin_name The plugin name. 
  470. * @return array An array with the result of the installation. 
  471. * @since 1.5.0.150225 
  472. */ 
  473. protected function install_plugin($plugin_url, $plugin_name) { 
  474. $result = new stdClass(); 
  475. $result->status = false; 
  476. $result->messages = array( 
  477. sprintf(__('Installing plugin "%s"...', $this->text_domain), $plugin_name) 
  478. ); 
  479.  
  480. $wp_plugin_info = $this->get_wp_plugin_info($plugin_name); 
  481. if(is_array($wp_plugin_info)) { 
  482. $result->messages[] = __('Plugin is already installed.', $this->text_domain); 
  483. $result->status = true; 
  484. return $result; 
  485.  
  486. // If we reach this point, the plugin has to be installed 
  487. include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'); 
  488. $skin = new Automatic_Upgrader_Skin(); 
  489. $upgrader = new Plugin_Upgrader($skin); 
  490.  
  491. $install_result = $upgrader->install($plugin_url); 
  492. $result->messages = array_merge($result->messages, $skin->get_upgrade_messages()); 
  493.  
  494. if($install_result instanceof WP_Error) { 
  495. $result->status = false; 
  496. $result->messages[] = __('Plugin installation failed. See errors below.', $this->text_domain); 
  497. $result->messages = array_merge($result->messages, $install_result->get_error_messages()); 
  498. $result->messages[] = sprintf(__('Please try installing the plugin manually. You can ' . 
  499. 'download it from %s.', $this->text_domain),  
  500. $plugin_url); 
  501. else { 
  502. $result->status = true; 
  503. return $result; 
  504.  
  505. /** 
  506. * Installs a plugin from the specified URL. 
  507. * @param string plugin_url The URL to a plugin ZIP file. 
  508. * @param string plugin_name The plugin name. 
  509. * @return array An array with the result of the installation. 
  510. * @since 1.5.0.150225 
  511. */ 
  512. protected function activate_plugin($plugin_name) { 
  513. $result = new stdClass(); 
  514. $result->messages = array( 
  515. sprintf(__('Activating plugin "%s"...', $this->text_domain), $plugin_name),  
  516. ); 
  517.  
  518. $wp_plugin_info = $this->get_wp_plugin_info($plugin_name); 
  519. if(!is_array($wp_plugin_info)) { 
  520. $result->messages[] = __('Plugin is not installed.', $this->text_domain); 
  521. $result->status = false; 
  522. return $result; 
  523.  
  524. if($wp_plugin_info['active']) { 
  525. $result->messages[] = __('Plugin already active.', $this->text_domain); 
  526. $result->status = true; 
  527. return $result; 
  528.  
  529. $activation_result = activate_plugin($wp_plugin_info['path'], null, false); 
  530. if($activation_result instanceof WP_Error) { 
  531. $result->status = false; 
  532. $result->messages[] = __('Activation failed.', $this->text_domain); 
  533. $result->messages = array_merge($result->messages, $activation_result->get_error_messages()); 
  534. return $result; 
  535.  
  536. $result->messages[] = __('Plugin activated successfully. Please refresh this page to ' . 
  537. 'hide the requirement notices and this message.', $this->text_domain); 
  538. $result->status = true; 
  539. return $result; 
  540.  
  541. /** 
  542. * Validates Ajax requests. 
  543. * @param string ajax_referer_key Indicates which URL argument contains the 
  544. * Ajax nonce. 
  545. * @return bool 
  546. * @since 1.5.0.150225 
  547. */ 
  548. protected function validate_ajax_request($ajax_nonce_key) { 
  549. $result = true; 
  550. if(!current_user_can('manage_options')) { 
  551. $message = 'HTTP/1.1 400 Bad Request'; 
  552. header($message, true, 403); 
  553. $result = false; 
  554.  
  555. // Handle invalid requests (e.g. a request missing the "plugin" argument) 
  556. if($result && empty($_REQUEST['plugin'])) { 
  557. $message = 'HTTP/1.1 400 Bad Request'; 
  558. header($message, true, 400); 
  559. $result = false; 
  560.  
  561. if($result && !check_ajax_referer($ajax_nonce_key, '_ajax_nonce', false)) { 
  562. header('HTTP/1.1 400 Bad Request', true, 400); 
  563. $message = 'Ajax referer check failed'; 
  564. $result = false; 
  565. }; 
  566.  
  567. if($result == false) { 
  568. wp_send_json(array( 
  569. 'result' => $result,  
  570. 'messages' => array($message),  
  571. )); 
  572.  
  573. /** 
  574. * Handles the Ajax request to install a plugin. 
  575. * @since 1.5.0.150225 
  576. */ 
  577. public function wp_ajax_install_plugin() { 
  578. $plugin_slug = isset($_REQUEST['plugin']) ? $_REQUEST['plugin'] : ''; 
  579. $this->validate_ajax_request('aelia-plugin-install-' . $plugin_slug); 
  580.  
  581. $result = array( 
  582. 'status' => true,  
  583. 'messages' => array(),  
  584. ); 
  585.  
  586. // Get the plugin information from the required plugins list 
  587. $plugin_info = $this->get_plugin_info($plugin_slug); 
  588. if($plugin_info === false) { 
  589. $result['status'] = false; 
  590. $result['messages'][] = __('Invalid plugin specified: "%s".', $this->text_domain); 
  591.  
  592. // Ensure that the plugin has a URL from which it can be downloaded 
  593. if(empty($plugin_info['url'])) { 
  594. $result['status'] = false; 
  595. $result['messages'][] = __('Plugin does not have a URL from which it can be ' . 
  596. 'downloaded automatically.', $this->text_domain); 
  597.  
  598. // Query WordPress to check if plugin is installed. If not, install it 
  599. if($result['status']) { 
  600. $install_result = $this->install_plugin($plugin_info['url'], $plugin_info['name']); 
  601. $result['status'] = $install_result->status; 
  602. $result['messages'] = array_merge($result['messages'], $install_result->messages); 
  603.  
  604. // If installation went well, activate the plugin 
  605. if($result['status']) { 
  606. $this->clear_plugin_cache(); 
  607. $activation_result = $this->activate_plugin($plugin_info['name']); 
  608. $result['status'] = $activation_result->status; 
  609. $result['messages'] = array_merge($result['messages'], $activation_result->messages); 
  610.  
  611. wp_send_json($result); 
  612.  
  613. /** 
  614. * Handles the Ajax request to activate a plugin. 
  615. * @since 1.5.0.150225 
  616. */ 
  617. public function wp_ajax_activate_plugin() { 
  618. $plugin_slug = isset($_REQUEST['plugin']) ? $_REQUEST['plugin'] : ''; 
  619. $this->validate_ajax_request('aelia-plugin-activate-' . $plugin_slug); 
  620.  
  621. $result = array( 
  622. 'status' => true,  
  623. 'messages' => array(),  
  624. ); 
  625.  
  626. // Get the plugin information from the required plugins list 
  627. $plugin_info = $this->get_plugin_info($plugin_slug); 
  628. if($plugin_info === false) { 
  629. $result['status'] = false; 
  630. $result['messages'][] = __('Invalid plugin specified: "%s".', $this->text_domain); 
  631.  
  632. // Check if plugin is installed. If it is, activate it 
  633. if($result['status']) { 
  634. $activation_result = $this->activate_plugin($plugin_info['name']); 
  635. $result['status'] = $activation_result->status; 
  636. $result['messages'] = array_merge($result['messages'], $activation_result->messages); 
  637. wp_send_json($result);