| 1 |  |  | <?php namespace EmailLog\Addon\API; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | defined( 'ABSPATH' ) || exit; // Exit if accessed directly. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Wrapper for EDD API. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @since 2.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class EDDAPI { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	 * Store URL. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	 * EDD and EDD SL plugins should be installed there. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	protected $store_url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * EDDAPI constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * If store url is not passed it is read from config. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * @param string|null $store_url Store URL. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	public function __construct( $store_url = null ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 		if ( null === $store_url ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			$email_log = email_log(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			$store_url = $email_log->get_store_url(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		$this->store_url = $store_url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * Activate License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @param string $license_key License Key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * @param string $addon_name  Add-on Name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * @return object API Response JSON Object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	public function activate_license( $license_key, $addon_name ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$params = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 			'edd_action' => 'activate_license', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 			'license'    => $license_key, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			'item_name'  => urlencode( $addon_name ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 			'url'        => home_url(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		return $this->call_edd_api( $params ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * Deactivate License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * @param string $license_key License Key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * @param string $addon_name  Add-on Name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 * @return object API Response JSON Object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	public function deactivate_license( $license_key, $addon_name ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$params = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			'edd_action' => 'deactivate_license', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 			'license'    => $license_key, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			'item_name'  => urlencode( $addon_name ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 			'url'        => home_url(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		return $this->call_edd_api( $params ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * Get version information. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @param string $license_key License Key. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * @param string $addon_name  Add-on Name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 * @return object API Response JSON Object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	public function get_version( $license_key, $addon_name ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		$params = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			'edd_action' => 'get_version', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			'license'    => $license_key, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 			'item_name'  => $addon_name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			'url'        => home_url(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		return $this->call_edd_api( $params ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * Call the EDD API. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * @param array $params Parameters for request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * @throws \Exception If there is any error while making the request. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * TODO: Make the errors more user friendly and provide links to support. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 * @return object API Response in JSON. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  | 	protected function call_edd_api( $params ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  | 		$response = wp_remote_post( $this->store_url, array( | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  | 			'timeout' => 15, | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 			'body'    => $params, | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  | 		) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  | 		if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  | 			if ( is_wp_error( $response ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  | 				throw new \Exception( $response->get_error_message() ); | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  | 			throw new \Exception( __( 'Unknown error occurred while trying to contact Email Log store. Please try again after sometime. If the problem persists contact support.', 'email-log' ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  | 		$body = wp_remote_retrieve_body( $response ); | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  | 		$data = json_decode( $body ); | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  | 		if ( empty( $data ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  | 			throw new \Exception( __( 'Unable to parse the response Email Log store response. Please try again after sometime. If the problem persists contact support.', 'email-log' ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  | 		return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 127 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |  |