ads
3 weeks ago
groups
3 weeks ago
metaboxes
3 weeks ago
pages
3 weeks ago
placements
3 weeks ago
class-action-links.php
3 weeks ago
class-addon-box.php
3 weeks ago
class-addon-updater.php
3 weeks ago
class-admin-menu.php
3 weeks ago
class-admin-notices.php
1 year ago
class-ajax.php
3 weeks ago
class-app.php
1 month ago
class-assets.php
3 weeks ago
class-authors.php
3 weeks ago
class-edd-updater.php
3 weeks ago
class-license-admin-post.php
3 weeks ago
class-list-filters.php
3 weeks ago
class-marketing.php
3 weeks ago
class-metabox-ad-settings.php
1 year ago
class-metabox-ad.php
3 weeks ago
class-misc.php
3 weeks ago
class-page-quick-edit.php
1 month ago
class-plugin-auto-update.php
1 month ago
class-plugin-installer.php
3 weeks ago
class-post-list.php
3 weeks ago
class-post-types.php
3 weeks ago
class-screen-options.php
3 weeks ago
class-settings.php
3 weeks ago
class-shortcode-creator.php
3 weeks ago
class-system-info.php
1 year ago
class-tinymce.php
2 years ago
class-translation-promo.php
3 weeks ago
class-upgrades.php
3 weeks ago
class-version-control.php
3 weeks ago
class-welcome.php
1 year ago
class-wordpress-dashboard.php
1 year ago
index.php
2 years ago
class-plugin-installer.php
163 lines
| 1 | <?php |
| 2 | // phpcs:ignoreFile |
| 3 | |
| 4 | /** |
| 5 | * Alternative version installer. |
| 6 | * |
| 7 | * @package AdvancedAds |
| 8 | * @author Advanced Ads <info@wpadvancedads.com> |
| 9 | * @since 1.50.0 |
| 10 | */ |
| 11 | |
| 12 | namespace AdvancedAds\Admin; |
| 13 | |
| 14 | use AdvancedAds\License\License_Shop_Client; |
| 15 | use stdClass; |
| 16 | use WP_Error; |
| 17 | use Plugin_Upgrader; |
| 18 | use Automatic_Upgrader_Skin; |
| 19 | |
| 20 | defined( 'ABSPATH' ) || exit; |
| 21 | |
| 22 | /** |
| 23 | * Alternate plugin version installer |
| 24 | */ |
| 25 | class Plugin_Installer { |
| 26 | /** |
| 27 | * The version to install |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | private $version; |
| 32 | |
| 33 | /** |
| 34 | * URL to the .zip archive for the desired version |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | private $package_url; |
| 39 | |
| 40 | /** |
| 41 | * The plugin name |
| 42 | * |
| 43 | * @var string |
| 44 | */ |
| 45 | private $plugin_name; |
| 46 | |
| 47 | /** |
| 48 | * The plugin slug |
| 49 | * |
| 50 | * @var string |
| 51 | */ |
| 52 | private $plugin_slug; |
| 53 | |
| 54 | /** |
| 55 | * Whether to replace an existing plugin directory on local zip install. |
| 56 | * |
| 57 | * @var bool |
| 58 | */ |
| 59 | private $replace_existing; |
| 60 | |
| 61 | /** |
| 62 | * Constructor |
| 63 | * |
| 64 | * @param string $version The version to install. |
| 65 | * @param string $package_url The url or local path to the .zip archive. |
| 66 | * @param bool|null $replace_existing Replace an existing plugin directory for local zip installs. |
| 67 | */ |
| 68 | public function __construct( $version, $package_url, $replace_existing = null ) { |
| 69 | $this->version = $version; |
| 70 | $this->package_url = $package_url; |
| 71 | $this->plugin_name = ADVADS_PLUGIN_BASENAME; |
| 72 | $this->plugin_slug = basename( ADVADS_FILE ) . '.php'; |
| 73 | $this->replace_existing = null !== $replace_existing ? (bool) $replace_existing : is_file( $package_url ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Apply package. |
| 78 | * |
| 79 | * Change the plugin data when WordPress checks for updates. This method |
| 80 | * modifies package data to update the plugin from a specific URL containing |
| 81 | * the version package. |
| 82 | */ |
| 83 | protected function apply_package() { |
| 84 | $update_plugins = get_site_transient( 'update_plugins' ); |
| 85 | if ( ! is_object( $update_plugins ) ) { |
| 86 | $update_plugins = new stdClass(); |
| 87 | } |
| 88 | |
| 89 | $plugin_info = new stdClass(); |
| 90 | $plugin_info->new_version = $this->version; |
| 91 | $plugin_info->slug = $this->plugin_slug; |
| 92 | $plugin_info->package = $this->package_url; |
| 93 | |
| 94 | $update_plugins->response[ $this->plugin_name ] = $plugin_info; |
| 95 | |
| 96 | set_site_transient( 'update_plugins', $update_plugins ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Do the plugin update process |
| 101 | * |
| 102 | * @return array<string, mixed>|bool|\WP_Error |
| 103 | */ |
| 104 | public function install() { |
| 105 | self::require_upgrader_dependencies(); |
| 106 | |
| 107 | $this->apply_package(); |
| 108 | |
| 109 | $upgrader_args = [ |
| 110 | 'url' => 'update.php?action=upgrade-plugin&plugin=' . rawurlencode( $this->plugin_name ), |
| 111 | 'plugin' => $this->plugin_name, |
| 112 | 'nonce' => 'upgrade-plugin_' . $this->plugin_name, |
| 113 | 'title' => esc_html__( 'Rollback to Previous Version', 'advanced-ads' ), |
| 114 | ]; |
| 115 | |
| 116 | $upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin( $upgrader_args ) ); |
| 117 | |
| 118 | return $upgrader->upgrade( $this->plugin_name ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Install a plugin from a remote or local package URL. |
| 123 | * |
| 124 | * @param string $package_url Package URL or local zip path. |
| 125 | * @param bool $replace_existing Overwrite an existing plugin directory. |
| 126 | * @return bool|array<string, mixed>|\WP_Error|null |
| 127 | */ |
| 128 | public static function install_from_url( string $package_url, bool $replace_existing = false ) { |
| 129 | self::require_upgrader_dependencies(); |
| 130 | |
| 131 | License_Shop_Client::add_local_development_http_filter(); |
| 132 | if ( ! License_Shop_Client::should_verify_ssl() ) { |
| 133 | add_filter( 'https_ssl_verify', '__return_false' ); |
| 134 | } |
| 135 | |
| 136 | $upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin() ); |
| 137 | $result = $upgrader->install( |
| 138 | $package_url, |
| 139 | $replace_existing ? [ 'overwrite_package' => true ] : [] |
| 140 | ); |
| 141 | |
| 142 | if ( ! License_Shop_Client::should_verify_ssl() ) { |
| 143 | remove_filter( 'https_ssl_verify', '__return_false' ); |
| 144 | } |
| 145 | License_Shop_Client::remove_local_development_http_filter(); |
| 146 | |
| 147 | return $result; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Admin includes required by Plugin_Upgrader (not loaded during REST requests). |
| 152 | * |
| 153 | * @return void |
| 154 | */ |
| 155 | private static function require_upgrader_dependencies(): void { |
| 156 | if ( ! function_exists( 'request_filesystem_credentials' ) ) { |
| 157 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 158 | } |
| 159 | |
| 160 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 161 | } |
| 162 | } |
| 163 |