class-auxels-plugin-check-update.php
6 years ago
class-auxin-dashboard-notice.php
7 years ago
class-auxin-license-activation.php
7 years ago
class-auxin-notices.php
6 years ago
class-auxin-upgrader-ajax-handlers.php
7 years ago
class-auxin-upgrader-http-api.php
6 years ago
class-auxin-upgrader-plugin.php
7 years ago
class-auxin-upgrader-prepare.php
6 years ago
class-auxin-upgrader-theme.php
7 years ago
class-auxin-upgrader-http-api.php
126 lines
| 1 | <?php |
| 2 | |
| 3 | class Auxin_Upgrader_Http_Api { |
| 4 | |
| 5 | protected $api = 'http://api.averta.net/envato/items/'; |
| 6 | |
| 7 | function __construct(){} |
| 8 | |
| 9 | /** |
| 10 | * Get single setting value |
| 11 | * |
| 12 | * @param string $option |
| 13 | * @param string $section |
| 14 | * @param string $default |
| 15 | * @return string |
| 16 | */ |
| 17 | private function get_setting( $option, $section, $default = '' ) { |
| 18 | $options = get_site_option( $section ); |
| 19 | |
| 20 | if ( isset( $options[$option] ) ) { |
| 21 | return $options[$option]; |
| 22 | } |
| 23 | return $default; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Make a wordpress remote post request on averta API |
| 28 | * |
| 29 | * @param array $args |
| 30 | * @return void |
| 31 | */ |
| 32 | private function remote_post( $args ){ |
| 33 | global $wp_version; |
| 34 | |
| 35 | $request_string = array( |
| 36 | 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo('url'), |
| 37 | 'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30: 3), |
| 38 | 'body' => $args |
| 39 | ); |
| 40 | |
| 41 | // Start checking for an update |
| 42 | $request = wp_remote_post( $this->api, $request_string ); |
| 43 | |
| 44 | if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) !== 200 ) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | return $request; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get our non official items list |
| 53 | * |
| 54 | * @return array |
| 55 | */ |
| 56 | private function get_non_official_items(){ |
| 57 | return apply_filters( 'auxin_set_non_official_items', array( |
| 58 | '3909293' => 'phlox-pro', |
| 59 | '3909293-03' => 'auxin-shop', |
| 60 | '3909293-04' => 'auxin-the-news', |
| 61 | '3909293-05' => 'auxin-pro-tools', |
| 62 | '3909293-06' => 'masterslider', |
| 63 | '3909293-07' => 'js_composer', |
| 64 | '3909293-08' => 'bdthemes-element-pack', |
| 65 | '3909293-09' => 'Ultimate_VC_Addons', |
| 66 | '3909293-10' => 'waspthemes-yellow-pencil', |
| 67 | '3909293-11' => 'LayerSlider', |
| 68 | '3909293-12' => 'revslider', |
| 69 | '3909293-13' => 'go_pricing', |
| 70 | '3909293-14' => 'convertplug' |
| 71 | ) ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get download link from averta API |
| 76 | * |
| 77 | * @return void |
| 78 | */ |
| 79 | public function get_download_link( $key ){ |
| 80 | |
| 81 | $token = $this->get_setting( 'token' , AUXELS_PURCHASE_KEY ); |
| 82 | $token = empty( $token ) ? $this->get_setting( 'token' , THEME_ID . '_license' ) : $token; |
| 83 | |
| 84 | if( empty( $token ) ) { |
| 85 | return new WP_Error( 'no_credentials', |
| 86 | __( 'Envato username, API key and your item purchase code are required for downloading updates from Envato marketplace.', 'auxin-elements' ) |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | $items = $this->get_non_official_items(); |
| 91 | |
| 92 | if( ! in_array( $key, $items ) ) { |
| 93 | return new WP_Error( 'no_credentials', |
| 94 | '"' . $key . '" ' . __( 'Is not exist in our non official list. ', 'auxin-elements' ) |
| 95 | ); |
| 96 | } |
| 97 | $item_ID = array_search( $key, $items ); |
| 98 | |
| 99 | $request = $this->remote_post( array( |
| 100 | 'cat' => 'download-purchase', |
| 101 | 'action' => 'token', |
| 102 | 'item-id' => $item_ID, |
| 103 | 'token' => $token, |
| 104 | 'url' => get_site_url() |
| 105 | ) ); |
| 106 | |
| 107 | if( $request === false ){ |
| 108 | return new WP_Error( 'process_faild', |
| 109 | __( 'Error in getting download link.', 'auxin-elements' ) |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | $response = wp_remote_retrieve_body( $request ); |
| 114 | $response = json_decode( $response, true ); |
| 115 | |
| 116 | if( ! isset( $response['download_url'] ) || empty( $response['download_url'] ) ) { |
| 117 | // Envato API error .. |
| 118 | return new WP_Error( 'no_credentials', |
| 119 | __( 'Error on connecting to download API...', 'auxin-elements' ) |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | return $response['download_url']; |
| 124 | } |
| 125 | |
| 126 | } |