robin-image-optimizer
/
libs
/
factory
/
freemius
/
includes
/
updates
/
class-freemius-repository.php
class-freemius-repository.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WBCR\Factory_Freemius_Rio_600\Updates; |
| 4 | |
| 5 | // Exit if accessed directly |
| 6 | use Exception; |
| 7 | use Wbcr_Factory600_Plugin; |
| 8 | use WBCR\Factory_600\Updates\Repository; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * @version 1.0 |
| 16 | */ |
| 17 | class Freemius_Repository extends Repository { |
| 18 | |
| 19 | /** |
| 20 | * @var \WBCR\Factory_Freemius_Rio_600\Premium\Provider |
| 21 | */ |
| 22 | private $premium; |
| 23 | |
| 24 | /** |
| 25 | * Freemius constructor. |
| 26 | * |
| 27 | * @param Wbcr_Factory600_Plugin $plugin |
| 28 | * |
| 29 | * @throws Exception |
| 30 | * @since 4.0.0 |
| 31 | */ |
| 32 | public function __construct( Wbcr_Factory600_Plugin $plugin, array $settings = [] ) { |
| 33 | $this->plugin = $plugin; |
| 34 | $this->premium = $this->plugin->premium; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @throws Exception |
| 39 | */ |
| 40 | public function init() { |
| 41 | if ( ! $this->premium instanceof \WBCR\Factory_Freemius_Rio_600\Premium\Provider ) { |
| 42 | throw new Exception( 'This repository type requires Freemius premium provider.' ); |
| 43 | } |
| 44 | |
| 45 | if ( ! $this->premium->is_activate() ) { |
| 46 | throw new Exception( 'Only premium plugins can check or receive updates via Freemius repository.' ); |
| 47 | } |
| 48 | |
| 49 | $this->initialized = true; |
| 50 | |
| 51 | add_filter( |
| 52 | 'http_request_host_is_external', |
| 53 | [ |
| 54 | $this, |
| 55 | 'http_request_host_is_external_filter', |
| 56 | ], |
| 57 | 10, |
| 58 | 3 |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @return bool |
| 64 | */ |
| 65 | public function need_check_updates() { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return bool|mixed |
| 71 | */ |
| 72 | public function is_support_premium() { |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @return string|null |
| 78 | * @throws Exception |
| 79 | */ |
| 80 | public function get_download_url() { |
| 81 | return $this->premium->get_package_download_url(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @return string|null |
| 86 | * @throws Exception |
| 87 | */ |
| 88 | public function get_last_version() { |
| 89 | try { |
| 90 | $last_package = $this->premium->get_downloadable_package_info(); |
| 91 | |
| 92 | if ( empty( $last_package->version ) ) { |
| 93 | return null; |
| 94 | } |
| 95 | } catch ( Exception $e ) { |
| 96 | if ( defined( 'FACTORY_UPDATES_DEBUG' ) && FACTORY_UPDATES_DEBUG ) { |
| 97 | throw new Exception( $e->getMessage() ); |
| 98 | } |
| 99 | |
| 100 | return null; |
| 101 | } |
| 102 | |
| 103 | return $last_package->version; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Since WP version 3.6, a new security feature was added that denies access to repository with a local ip. |
| 108 | * During development mode we want to be able updating plugin versions via our localhost repository. This |
| 109 | * filter white-list all domains including "api.freemius". |
| 110 | * |
| 111 | * @link http://www.emanueletessore.com/wordpress-download-failed-valid-url-provided/ |
| 112 | * |
| 113 | * @since 1.0.4 |
| 114 | * |
| 115 | * @param bool $allow |
| 116 | * @param string $host |
| 117 | * @param string $url |
| 118 | * |
| 119 | * @return bool |
| 120 | */ |
| 121 | function http_request_host_is_external_filter( $allow, $host, $url ) { |
| 122 | return ( false !== strpos( $host, 'freemius' ) ) ? true : $allow; |
| 123 | } |
| 124 | } |
| 125 |