|
| 1 | +<?php |
| 2 | +if ( ! defined( 'ABSPATH' ) ) { |
| 3 | + exit; |
| 4 | +} |
| 5 | + |
| 6 | +/** |
| 7 | + * Simple GitHub based plugin updater. |
| 8 | + */ |
| 9 | +class WC_Utils_Updater { |
| 10 | + |
| 11 | + /** |
| 12 | + * GitHub repository raw URL. |
| 13 | + * |
| 14 | + * @var string |
| 15 | + */ |
| 16 | + private $raw_url; |
| 17 | + |
| 18 | + /** |
| 19 | + * GitHub repository zip URL. |
| 20 | + * |
| 21 | + * @var string |
| 22 | + */ |
| 23 | + private $zip_url; |
| 24 | + |
| 25 | + /** |
| 26 | + * Plugin file basename. |
| 27 | + * |
| 28 | + * @var string |
| 29 | + */ |
| 30 | + private $plugin_basename; |
| 31 | + |
| 32 | + /** |
| 33 | + * Current version. |
| 34 | + * |
| 35 | + * @var string |
| 36 | + */ |
| 37 | + private $version; |
| 38 | + |
| 39 | + /** |
| 40 | + * Constructor. |
| 41 | + */ |
| 42 | + public function __construct( $repo_raw_url, $repo_zip_url, $plugin_basename, $version ) { |
| 43 | + $this->raw_url = trailingslashit( $repo_raw_url ); |
| 44 | + $this->zip_url = $repo_zip_url; |
| 45 | + $this->plugin_basename = $plugin_basename; |
| 46 | + $this->version = $version; |
| 47 | + |
| 48 | + add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_for_update' ) ); |
| 49 | + add_filter( 'plugins_api', array( $this, 'plugins_api' ), 10, 3 ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Check GitHub for a newer version. |
| 54 | + * |
| 55 | + * @param object $transient Update transient. |
| 56 | + * @return object |
| 57 | + */ |
| 58 | + public function check_for_update( $transient ) { |
| 59 | + if ( empty( $transient->checked ) ) { |
| 60 | + return $transient; |
| 61 | + } |
| 62 | + |
| 63 | + $remote = wp_remote_get( $this->raw_url . 'woocommerce-utils.php' ); |
| 64 | + |
| 65 | + if ( is_wp_error( $remote ) ) { |
| 66 | + return $transient; |
| 67 | + } |
| 68 | + |
| 69 | + if ( 200 !== wp_remote_retrieve_response_code( $remote ) ) { |
| 70 | + return $transient; |
| 71 | + } |
| 72 | + |
| 73 | + $body = wp_remote_retrieve_body( $remote ); |
| 74 | + |
| 75 | + if ( ! preg_match( '/^\s*\*\s*Version:\s*(.*)$/mi', $body, $matches ) ) { |
| 76 | + return $transient; |
| 77 | + } |
| 78 | + |
| 79 | + $remote_version = trim( $matches[1] ); |
| 80 | + |
| 81 | + if ( version_compare( $this->version, $remote_version, '>=' ) ) { |
| 82 | + return $transient; |
| 83 | + } |
| 84 | + |
| 85 | + $plugin = new stdClass(); |
| 86 | + $plugin->slug = dirname( $this->plugin_basename ); |
| 87 | + $plugin->new_version = $remote_version; |
| 88 | + $plugin->url = $this->raw_url; |
| 89 | + $plugin->package = $this->zip_url; |
| 90 | + |
| 91 | + $transient->response[ $this->plugin_basename ] = $plugin; |
| 92 | + |
| 93 | + return $transient; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Provide plugin information for the update screen. |
| 98 | + * |
| 99 | + * @param false|object|array $result The result object or array. Default false. |
| 100 | + * @param string $action The type of information being requested from the Plugin Installation API. |
| 101 | + * @param object $args Plugin API arguments. |
| 102 | + * @return object|false |
| 103 | + */ |
| 104 | + public function plugins_api( $result, $action, $args ) { |
| 105 | + if ( 'plugin_information' !== $action ) { |
| 106 | + return $result; |
| 107 | + } |
| 108 | + |
| 109 | + if ( empty( $args->slug ) || dirname( $this->plugin_basename ) !== $args->slug ) { |
| 110 | + return $result; |
| 111 | + } |
| 112 | + |
| 113 | + $remote = wp_remote_get( $this->raw_url . 'readme.txt' ); |
| 114 | + |
| 115 | + if ( is_wp_error( $remote ) || 200 !== wp_remote_retrieve_response_code( $remote ) ) { |
| 116 | + return $result; |
| 117 | + } |
| 118 | + |
| 119 | + $readme = wp_remote_retrieve_body( $remote ); |
| 120 | + |
| 121 | + $info = new stdClass(); |
| 122 | + $info->name = 'WooCommerce Utils'; |
| 123 | + $info->slug = dirname( $this->plugin_basename ); |
| 124 | + $info->version = $this->version; |
| 125 | + $info->sections = array( |
| 126 | + 'description' => $readme, |
| 127 | + ); |
| 128 | + $info->download_link = $this->zip_url; |
| 129 | + |
| 130 | + return $info; |
| 131 | + } |
| 132 | +} |
| 133 | + |
0 commit comments