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
InstalledTheme.php
161 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Tenweb_Authorization { |
| 4 | |
| 5 | use Tenweb_Authorization\Product; |
| 6 | |
| 7 | class InstalledTheme extends Product implements ProductActions |
| 8 | { |
| 9 | |
| 10 | private $state = null; |
| 11 | |
| 12 | public function __construct($state, $id, $slug, $title, $description) |
| 13 | { |
| 14 | $this->state = $state; |
| 15 | parent::__construct($id, $slug, $title, $description, 'theme'); |
| 16 | $this->installed = true; |
| 17 | } |
| 18 | |
| 19 | public function get_state() |
| 20 | { |
| 21 | return $this->state; |
| 22 | } |
| 23 | |
| 24 | public function activate() |
| 25 | { |
| 26 | |
| 27 | switch_theme($this->slug); |
| 28 | $active_theme = wp_get_theme(); |
| 29 | |
| 30 | if ($this->title == $active_theme['Name'] || str_replace(" Theme", "", $this->title) == $active_theme["Name"]) { |
| 31 | return true; |
| 32 | } else { |
| 33 | $this->set_error('failed_to_activate', 'Failed to activate.'); |
| 34 | |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | |
| 39 | } |
| 40 | |
| 41 | public function deactivate() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | public function update() |
| 46 | { |
| 47 | |
| 48 | $is_active = $this->state->active; |
| 49 | |
| 50 | $this->include_upgrade_libs(); |
| 51 | $skin = $this->get_skin(); |
| 52 | |
| 53 | if ($skin == null) { |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | $upgrader = new \Theme_Upgrader($skin); |
| 58 | |
| 59 | $fs_options = apply_filters('upgrader_package_options', array('destination' => WP_PLUGIN_DIR)); |
| 60 | if ($upgrader->fs_connect(array(WP_CONTENT_DIR, $fs_options['destination'])) !== true) { |
| 61 | $this->set_error('fs_error', "File system error. Invalid file permissions or FTP credentials."); |
| 62 | |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | if ($this->set_download_data() == false) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | $update_themes = get_site_transient('update_themes'); |
| 71 | |
| 72 | if (is_object($update_themes)) { |
| 73 | $old_update_themes = clone $update_themes; |
| 74 | } else { |
| 75 | $old_update_themes = $update_themes; |
| 76 | $update_themes = new \stdClass(); |
| 77 | } |
| 78 | |
| 79 | $theme_object = array( |
| 80 | 'package' => $this->download_data['url'] |
| 81 | ); |
| 82 | $update_themes->response[$this->slug] = $theme_object; |
| 83 | |
| 84 | $GLOBALS['tenweb_update_process'] = true; |
| 85 | set_site_transient('update_themes', $update_themes, 60 * 60); |
| 86 | |
| 87 | add_filter('http_request_args', array($this, 'add_headers'), 9999, 2); |
| 88 | |
| 89 | $result = $upgrader->upgrade($this->slug); |
| 90 | $GLOBALS['tenweb_update_process'] = false; |
| 91 | |
| 92 | set_site_transient('update_themes', $old_update_themes, 60 * 60); |
| 93 | |
| 94 | if ($is_active == 1) { |
| 95 | $this->activate(); |
| 96 | } |
| 97 | |
| 98 | if ($result === true) { |
| 99 | return true; |
| 100 | } else if (is_wp_error($result)) { |
| 101 | $this->set_error('failed_to_install', $result->get_error_message()); |
| 102 | |
| 103 | return false; |
| 104 | } else { |
| 105 | $this->set_error('failed_to_install', 'Something went wrong.'); |
| 106 | |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |
| 112 | function delete() |
| 113 | { |
| 114 | // Check filesystem credentials. `delete_theme()` will bail otherwise. |
| 115 | $url = wp_nonce_url('themes.php?action=delete&stylesheet=' . urlencode($this->slug), 'delete-theme_' . $this->slug); |
| 116 | ob_start(); |
| 117 | $credentials = request_filesystem_credentials($url); |
| 118 | ob_end_clean(); |
| 119 | |
| 120 | if (false === $credentials || !WP_Filesystem($credentials)) { |
| 121 | global $wp_filesystem; |
| 122 | |
| 123 | // Pass through the error from WP_Filesystem if one was raised. |
| 124 | if ($wp_filesystem instanceof \WP_Filesystem_Base && is_wp_error($wp_filesystem->errors) && $wp_filesystem->errors->get_error_code()) { |
| 125 | $this->set_error('fs_error', esc_html($wp_filesystem->errors->get_error_message())); |
| 126 | } else { |
| 127 | $this->set_error('fs_error', 'Unable to connect to the filesystem. Please confirm your credentials.'); |
| 128 | } |
| 129 | |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | include_once(ABSPATH . 'wp-admin/includes/theme.php'); |
| 134 | |
| 135 | $result = delete_theme($this->slug); |
| 136 | |
| 137 | if (is_wp_error($result)) { |
| 138 | $this->set_error('failed_to_delete', $result->get_error_message()); |
| 139 | |
| 140 | return false; |
| 141 | } else if (false === $result) { |
| 142 | $this->set_error('failed_to_delete', 'Theme could not be deleted.'); |
| 143 | |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | public function has_update() |
| 151 | { |
| 152 | |
| 153 | $av_version = ($this->state->is_paid) ? $this->latest_versions['paid'] : $this->latest_versions['free']; |
| 154 | $av_version = ltrim($av_version, 'v'); |
| 155 | |
| 156 | return version_compare($this->state->version, $av_version, "<"); |
| 157 | } |
| 158 | |
| 159 | } |
| 160 | } |
| 161 |