complianz-gdpr
Last commit date
DNSMPD
2 years ago
assets
2 years ago
config
2 years ago
cookie
2 years ago
cookiebanner
2 years ago
cron
3 years ago
grid
3 years ago
gutenberg
2 years ago
integrations
2 years ago
languages
2 years ago
proof-of-consent
3 years ago
rest-api
3 years ago
settings
2 years ago
shepherd
3 years ago
templates
2 years ago
upgrade
2 years ago
LICENSE.txt
4 years ago
README.md
4 years ago
callback-notices.php
3 years ago
class-admin.php
2 years ago
class-company.php
4 years ago
class-cookie-blocker.php
2 years ago
class-document.php
2 years ago
class-export.php
3 years ago
class-field.php
2 years ago
class-installer.php
2 years ago
class-review.php
3 years ago
class-wizard.php
3 years ago
complianz-gpdr.php
2 years ago
composer.json
3 years ago
functions.php
2 years ago
gulpfile.js
3 years ago
index.php
7 years ago
loco.xml
4 years ago
readme.txt
2 years ago
security.md
2 years ago
system-status.php
3 years ago
uninstall.php
3 years ago
upgrade.php
2 years ago
class-installer.php
171 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 | /** |
| 4 | * Install suggested plugins |
| 5 | */ |
| 6 | |
| 7 | if ( !class_exists('cmplz_installer') ){ |
| 8 | class cmplz_installer { |
| 9 | private $slug = ''; |
| 10 | public $action = ''; |
| 11 | |
| 12 | public function __construct($slug) { |
| 13 | if ( !current_user_can('install_plugins')) return; |
| 14 | |
| 15 | $this->slug = $slug; |
| 16 | if ( !$this->plugin_is_downloaded() && !$this->plugin_is_activated() ) { |
| 17 | $this->action = 'cmplz_download_plugin'; |
| 18 | } |
| 19 | |
| 20 | if ($this->plugin_is_downloaded() && !$this->plugin_is_activated() ) { |
| 21 | $this->action = 'cmplz_activate_plugin'; |
| 22 | } |
| 23 | |
| 24 | // |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Check if plugin is downloaded |
| 29 | * @return bool |
| 30 | */ |
| 31 | |
| 32 | public function plugin_is_downloaded(){ |
| 33 | return file_exists(trailingslashit(WP_PLUGIN_DIR).$this->get_activation_slug() ); |
| 34 | } |
| 35 | /** |
| 36 | * Check if plugin is activated |
| 37 | * @return bool |
| 38 | */ |
| 39 | public function plugin_is_activated(){ |
| 40 | return is_plugin_active($this->get_activation_slug() ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Install plugin |
| 45 | * @param string $step |
| 46 | * |
| 47 | * @return void |
| 48 | */ |
| 49 | public function install($step){ |
| 50 | |
| 51 | if ( !current_user_can('install_plugins')) return; |
| 52 | |
| 53 | if ( $step === 'download' ) { |
| 54 | $this->download_plugin(); |
| 55 | } |
| 56 | if ( $step === 'activate' ) { |
| 57 | $this->activate_plugin(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get slug to activate plugin with |
| 63 | * @return string |
| 64 | */ |
| 65 | public function get_activation_slug(){ |
| 66 | $slugs = [ |
| 67 | 'burst-statistics' => 'burst-statistics/burst.php', |
| 68 | ]; |
| 69 | return $slugs[$this->slug]; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Cancel shepherd tour |
| 74 | * @return void |
| 75 | */ |
| 76 | public function cancel_tour(){ |
| 77 | $prefixes = [ |
| 78 | 'burst-statistics' => 'burst', |
| 79 | ]; |
| 80 | $prefix = $prefixes[$this->slug]; |
| 81 | update_site_option( $prefix.'_tour_started', false ); |
| 82 | update_site_option( $prefix.'_tour_shown_once', true ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Download the plugin |
| 87 | * @return bool |
| 88 | */ |
| 89 | public function download_plugin() { |
| 90 | if ( !current_user_can('install_plugins')) return; |
| 91 | |
| 92 | if ( !get_transient("cmplz_plugin_download_active") ) { |
| 93 | set_transient("cmplz_plugin_download_active", 5 * MINUTE_IN_SECONDS ); |
| 94 | $info = $this->get_plugin_info(); |
| 95 | $download_link = esc_url_raw( $info->versions['trunk'] ); |
| 96 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 97 | include_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 98 | |
| 99 | $skin = new WP_Ajax_Upgrader_Skin(); |
| 100 | $upgrader = new Plugin_Upgrader($skin); |
| 101 | |
| 102 | $result = $upgrader->install($download_link); |
| 103 | |
| 104 | if (is_wp_error($result)) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | delete_transient("cmplz_plugin_download_active"); |
| 109 | } |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Activate the plugin |
| 116 | * |
| 117 | * @return bool |
| 118 | */ |
| 119 | public function activate_plugin(): bool |
| 120 | { |
| 121 | if (!current_user_can('install_plugins')) { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | $slug = $this->get_activation_slug(); |
| 126 | $plugin_file_path = trailingslashit(WP_PLUGIN_DIR) . $slug; |
| 127 | |
| 128 | // Make sure the plugin file exists before trying to activate it |
| 129 | if (!file_exists($plugin_file_path)) { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | // Use plugin_basename to generate the correct slug, considering the WP_PLUGIN_DIR |
| 134 | $plugin_slug = plugin_basename($plugin_file_path); |
| 135 | |
| 136 | $networkwide = is_multisite(); |
| 137 | |
| 138 | if (!defined('DOING_CRON')) { |
| 139 | define('DOING_CRON', true); |
| 140 | } |
| 141 | |
| 142 | $result = activate_plugin($plugin_slug, '', $networkwide); |
| 143 | if (is_wp_error($result)) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | $this->cancel_tour(); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get plugin info |
| 153 | * @return array|WP_Error |
| 154 | */ |
| 155 | public function get_plugin_info() |
| 156 | { |
| 157 | require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 158 | $plugin_info = get_transient('cmplz_'.$this->slug . '_plugin_info'); |
| 159 | if ( empty($plugin_info) ) { |
| 160 | $plugin_info = plugins_api('plugin_information', array('slug' => $this->slug)); |
| 161 | if (!is_wp_error($plugin_info)) { |
| 162 | set_transient('cmplz_'.$this->slug . '_plugin_info', $plugin_info, WEEK_IN_SECONDS); |
| 163 | } |
| 164 | } |
| 165 | return $plugin_info; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | } |
| 170 | |
| 171 |