ads
1 week ago
groups
1 week ago
metaboxes
1 year ago
pages
3 months ago
placements
1 week ago
class-action-links.php
1 week ago
class-addon-box.php
1 year ago
class-addon-updater.php
3 months ago
class-admin-menu.php
1 week ago
class-admin-notices.php
1 year ago
class-ajax.php
3 months ago
class-assets.php
1 week ago
class-authors.php
1 year ago
class-edd-updater.php
4 weeks ago
class-list-filters.php
1 week ago
class-marketing.php
1 year ago
class-metabox-ad-settings.php
1 year ago
class-metabox-ad.php
1 year ago
class-misc.php
1 week ago
class-page-quick-edit.php
1 year ago
class-plugin-installer.php
1 year ago
class-post-list.php
1 year ago
class-post-types.php
1 week ago
class-screen-options.php
3 months ago
class-settings.php
1 year ago
class-shortcode-creator.php
1 year ago
class-system-info.php
1 year ago
class-tinymce.php
2 years ago
class-translation-promo.php
1 year ago
class-upgrades.php
1 year ago
class-version-control.php
3 months ago
class-welcome.php
1 year ago
class-wordpress-dashboard.php
1 year ago
index.php
2 years ago
class-plugin-installer.php
111 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 stdClass; |
| 15 | use WP_Error; |
| 16 | use Plugin_Upgrader; |
| 17 | use Automatic_Upgrader_Skin; |
| 18 | |
| 19 | defined( 'ABSPATH' ) || exit; |
| 20 | |
| 21 | /** |
| 22 | * Alternate plugin version installer |
| 23 | */ |
| 24 | class Plugin_Installer { |
| 25 | /** |
| 26 | * The version to install |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | private $version; |
| 31 | |
| 32 | /** |
| 33 | * URL to the .zip archive for the desired version |
| 34 | * |
| 35 | * @var string |
| 36 | */ |
| 37 | private $package_url; |
| 38 | |
| 39 | /** |
| 40 | * The plugin name |
| 41 | * |
| 42 | * @var string |
| 43 | */ |
| 44 | private $plugin_name; |
| 45 | |
| 46 | /** |
| 47 | * The plugin slug |
| 48 | * |
| 49 | * @var string |
| 50 | */ |
| 51 | private $plugin_slug; |
| 52 | |
| 53 | /** |
| 54 | * Constructor |
| 55 | * |
| 56 | * @param string $version The version to install. |
| 57 | * @param string $package_url The url to the .zip archive on https://wordpress.org. |
| 58 | */ |
| 59 | public function __construct( $version, $package_url ) { |
| 60 | $this->version = $version; |
| 61 | $this->package_url = $package_url; |
| 62 | $this->plugin_name = ADVADS_PLUGIN_BASENAME; |
| 63 | $this->plugin_slug = basename( ADVADS_FILE ) . '.php'; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Apply package. |
| 68 | * |
| 69 | * Change the plugin data when WordPress checks for updates. This method |
| 70 | * modifies package data to update the plugin from a specific URL containing |
| 71 | * the version package. |
| 72 | */ |
| 73 | protected function apply_package() { |
| 74 | $update_plugins = get_site_transient( 'update_plugins' ); |
| 75 | if ( ! is_object( $update_plugins ) ) { |
| 76 | $update_plugins = new stdClass(); |
| 77 | } |
| 78 | |
| 79 | $plugin_info = new stdClass(); |
| 80 | $plugin_info->new_version = $this->version; |
| 81 | $plugin_info->slug = $this->plugin_slug; |
| 82 | $plugin_info->package = $this->package_url; |
| 83 | |
| 84 | $update_plugins->response[ $this->plugin_name ] = $plugin_info; |
| 85 | |
| 86 | set_site_transient( 'update_plugins', $update_plugins ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Do the plugin update process |
| 91 | * |
| 92 | * @return array|bool|WP_Error |
| 93 | */ |
| 94 | public function install() { |
| 95 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 96 | |
| 97 | $this->apply_package(); |
| 98 | |
| 99 | $upgrader_args = [ |
| 100 | 'url' => 'update.php?action=upgrade-plugin&plugin=' . rawurlencode( $this->plugin_name ), |
| 101 | 'plugin' => $this->plugin_name, |
| 102 | 'nonce' => 'upgrade-plugin_' . $this->plugin_name, |
| 103 | 'title' => esc_html__( 'Rollback to Previous Version', 'advanced-ads' ), |
| 104 | ]; |
| 105 | |
| 106 | $upgrader = new Plugin_Upgrader( new Automatic_Upgrader_Skin( $upgrader_args ) ); |
| 107 | |
| 108 | return $upgrader->upgrade( $this->plugin_name ); |
| 109 | } |
| 110 | } |
| 111 |