config
1 month ago
Amazon.php
4 years ago
Helper.php
1 month ago
InstalledPlugin.php
3 years ago
InstalledTheme.php
4 years ago
Login.php
1 month ago
Product.php
4 years ago
ProductActions.php
4 years ago
ProductState.php
1 year ago
WpAjaxUpgraderSkin.php
4 years ago
InstalledPlugin.php
194 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Tenweb_Authorization { |
| 4 | |
| 5 | use Tenweb_Authorization\Product; |
| 6 | |
| 7 | class InstalledPlugin extends Product implements ProductActions |
| 8 | { |
| 9 | |
| 10 | public $state = null; |
| 11 | public $wp_slug = ""; |
| 12 | |
| 13 | public function __construct($state, $id, $slug, $title, $description, $wp_slug) |
| 14 | { |
| 15 | $this->state = $state; |
| 16 | parent::__construct($id, $slug, $title, $description); |
| 17 | $this->installed = true; |
| 18 | $this->wp_slug = $wp_slug; |
| 19 | } |
| 20 | |
| 21 | public function get_state() |
| 22 | { |
| 23 | return $this->state; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @param $network_wide |
| 28 | * |
| 29 | * @return boolean true on success, false on fail |
| 30 | **/ |
| 31 | public function activate($network_wide = false) |
| 32 | { |
| 33 | $result = activate_plugin($this->wp_slug, '', $network_wide, false); |
| 34 | $is_active = is_plugin_active($this->wp_slug); |
| 35 | |
| 36 | if ($is_active == false) { |
| 37 | if (is_wp_error($result)) { |
| 38 | $this->set_error('failed_to_activate', $result->get_error_message()); |
| 39 | |
| 40 | return false; |
| 41 | } else { |
| 42 | $this->set_error('failed_to_activate', 'Failed to activate.'); |
| 43 | |
| 44 | return false; |
| 45 | } |
| 46 | } else { |
| 47 | return true; |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | |
| 52 | public function deactivate($network_wide = false) |
| 53 | { |
| 54 | deactivate_plugins($this->wp_slug, false, $network_wide); |
| 55 | |
| 56 | if (is_plugin_active($this->wp_slug)) { |
| 57 | $this->set_error('failed_to_deactivate', 'Failed to deactivate.'); |
| 58 | |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | public function update() |
| 66 | { |
| 67 | |
| 68 | $is_active = $this->state->active; |
| 69 | $is_network_active = is_plugin_active_for_network($this->wp_slug); |
| 70 | |
| 71 | $this->include_upgrade_libs(); |
| 72 | $skin = $this->get_skin(); |
| 73 | |
| 74 | if ($skin == null) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | $upgrader = new \Plugin_Upgrader($skin); |
| 79 | |
| 80 | $fs_options = apply_filters('upgrader_package_options', array('destination' => WP_PLUGIN_DIR)); |
| 81 | if ($upgrader->fs_connect(array(WP_CONTENT_DIR, $fs_options['destination'])) !== true) { |
| 82 | $this->set_error('fs_error', "File system error. Invalid file permissions or FTP credentials."); |
| 83 | |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | if ($this->set_download_data() == false) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | $update_plugins = get_site_transient('update_plugins'); |
| 92 | |
| 93 | if (is_object($update_plugins)) { |
| 94 | $old_update_plugins = clone $update_plugins; |
| 95 | } else { |
| 96 | $old_update_plugins = $update_plugins; |
| 97 | $update_plugins = new \stdClass(); |
| 98 | } |
| 99 | |
| 100 | if (!isset($update_plugins->response)) { |
| 101 | $update_plugins->response = array(); |
| 102 | } |
| 103 | |
| 104 | $plugin_object = new \stdClass(); |
| 105 | $plugin_object->package = $this->download_data['url']; |
| 106 | |
| 107 | $update_plugins->response[$this->wp_slug] = $plugin_object; |
| 108 | |
| 109 | $GLOBALS['tenweb_update_process'] = true; |
| 110 | set_site_transient('update_plugins', $update_plugins, 60 * 60); |
| 111 | |
| 112 | add_filter('http_request_args', array($this, 'add_headers'), 9999, 2); |
| 113 | $result = $upgrader->upgrade($this->wp_slug); |
| 114 | |
| 115 | $GLOBALS['tenweb_update_process'] = false; |
| 116 | set_site_transient('update_plugins', $old_update_plugins, 60 * 60); |
| 117 | |
| 118 | if ($is_active == 1) { |
| 119 | $this->activate($is_network_active); |
| 120 | } |
| 121 | |
| 122 | if ($result === true) { |
| 123 | return true; |
| 124 | } else if (is_wp_error($result)) { |
| 125 | $this->set_error('failed_to_install', $result->get_error_message()); |
| 126 | |
| 127 | return false; |
| 128 | } else { |
| 129 | $this->set_error('failed_to_install', 'Something went wrong.'); |
| 130 | |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | } |
| 135 | |
| 136 | public function delete() |
| 137 | { |
| 138 | |
| 139 | if ($this->state->active == 1 && !$this->deactivate()) { |
| 140 | $this->set_error('failed_to_delete', 'You cannot delete a plugin while it is active.'); |
| 141 | |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | // Check filesystem credentials. `delete_plugins()` will bail otherwise. |
| 146 | $url = wp_nonce_url('plugins.php?action=delete-selected&verify-delete=1&checked[]=' . $this->wp_slug, 'bulk-plugins'); |
| 147 | ob_start(); |
| 148 | $credentials = request_filesystem_credentials($url); |
| 149 | ob_end_clean(); |
| 150 | if (false === $credentials || !WP_Filesystem($credentials)) { |
| 151 | global $wp_filesystem; |
| 152 | |
| 153 | // Pass through the error from WP_Filesystem if one was raised. |
| 154 | if ($wp_filesystem instanceof \WP_Filesystem_Base && is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { |
| 155 | $this->set_error('fs_error', esc_html($wp_filesystem->errors->get_error_message())); |
| 156 | } else { |
| 157 | $this->set_error('fs_error', 'Unable to connect to the filesystem. Please confirm your credentials.'); |
| 158 | } |
| 159 | |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | $result = delete_plugins(array($this->wp_slug)); |
| 164 | |
| 165 | if (is_wp_error($result)) { |
| 166 | $this->set_error('failed_to_delete', $result->get_error_message()); |
| 167 | |
| 168 | return false; |
| 169 | } else if (false === $result) { |
| 170 | $this->set_error('failed_to_delete', 'Plugin could not be deleted.'); |
| 171 | |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | public function has_update() |
| 179 | { |
| 180 | |
| 181 | $av_version = ($this->state->is_paid) ? $this->latest_versions['paid'] : $this->latest_versions['free']; |
| 182 | $av_version = ltrim($av_version, 'v'); |
| 183 | |
| 184 | return version_compare($this->state->version, $av_version, "<"); |
| 185 | } |
| 186 | |
| 187 | public function get_wp_slug() |
| 188 | { |
| 189 | return $this->wp_slug; |
| 190 | } |
| 191 | |
| 192 | } |
| 193 | } |
| 194 |