PluginProbe
WP-Stateless – Google Cloud Storage / 3.1.1
WP-Stateless – Google Cloud Storage v3.1.1
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / vendor / udx / lib-ud-api-client / lib / classes / class-api.php

class-api.php in WP-Stateless – Google Cloud Storage 3.1.1, at vendor/udx/lib-ud-api-client/lib/classes/class-api.php

201 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Client API
4 *
5 * @namespace UsabilityDynamics
6 *
7 */
8 namespace UsabilityDynamics\UD_API {
9
10 if( !class_exists( 'UsabilityDynamics\UD_API\API' ) ) {
11
12 /**
13 *
14 * @author: peshkov@UD
15 */
16 class API extends Scaffold {
17
18 /**
19 *
20 */
21 protected $api_url;
22
23 /**
24 *
25 */
26 protected $errors;
27
28 /**
29 *
30 */
31 protected $token;
32
33 /**
34 *
35 */
36 public function __construct( $args ) {
37 parent::__construct( $args );
38 $this->api_url = isset( $args[ 'api_url' ] ) ? $args[ 'api_url' ] : false;
39 $this->token = isset( $args[ 'token' ] ) ? $args[ 'token' ] : false;
40 }
41
42 /**
43 * Activate Product
44 */
45 public function activate( $args, $product = false, $error_log = true ) {
46 $args[ 'request' ] = 'activation';
47 return $this->request( $args, $product, $error_log );
48 }
49
50 /**
51 * Deactivate Product
52 */
53 public function deactivate( $args, $product = false, $error_log = true ) {
54 $args[ 'request' ] = 'deactivation';
55 return $this->request( $args, $product, $error_log );
56 }
57
58 /**
59 * Checks if the software is activated or deactivated
60 * @param array $args
61 * @return array
62 */
63 public function status( $args, $product = false, $error_log = false ) {
64 $args[ 'request' ] = 'status';
65 return $this->request( $args, $product, $error_log );
66 }
67
68 /**
69 * Pings remote server to maybe get specific information
70 * @param array $args
71 * @param bool $error_log
72 * @return array
73 */
74 public function ping( $args = array(), $error_log = false ) {
75 $args[ 'request' ] = 'ping';
76 return $this->request( $args, array(), $error_log );
77 }
78
79 /**
80 * May be add information to upgrade notice
81 * e.g., about available add-ons
82 * if user purchased legacy premium features
83 * COMPATIBILITY WITH OLD PRODUCTS
84 *
85 * @param array $args
86 * @param bool $error_log
87 * @return array
88 */
89 public function upgrade_notice( $args = array(), $error_log = false ) {
90 $args[ 'request' ] = 'upgrade_notice';
91 /* DEPRECATED API KEY FOR OLD PLUGINS COMPATIBILITY */
92 $args[ 'legacy_key' ] = get_option( 'ud_api_key', '' );
93 return $this->request( $args, array(), $error_log );
94 }
95
96 /**
97 * API Key URL
98 */
99 protected function create_software_api_url( $args ) {
100 $api_url = add_query_arg( 'wc-api', 'am-software-api', $this->api_url );
101 //return $api_url . '&' . http_build_query( $args );
102 $api_url .= '&';
103 foreach ($args AS $key=>$value)
104 $api_url .= $key.'='.urlencode($value).'&';
105 $api_url = rtrim($api_url, '&');
106 return $api_url;
107 }
108
109 /**
110 *
111 * @author peshkov@UD
112 */
113 protected function request( $args, $product, $error_log ) {
114 $product = wp_parse_args( $product, array(
115 'product_name' => __( 'UsabilityDynamics Product', $this->domain ),
116 ) );
117 $args = wp_parse_args( $args, array(
118 'request' => '',
119 'product_id' => '',
120 'instance' => '',
121 //'email' => '',
122 'licence_key' => '',
123 'platform' => $this->blog,
124 //** Add nocache hack. We must be sure we do not get CACHE result. peshkov@UD */
125 'nocache' => rand( 10000, 99999 ),
126 ) );
127 $target_url = $this->create_software_api_url( $args );
128 //echo "<pre>"; print_r( $target_url ); echo "</pre>"; //die();
129 $request = wp_remote_get( $target_url, array( 'timeout' => 15, 'sslverify' => false, 'headers' => array(
130 'x-ud-api-request' => $args[ 'request' ],
131 'x-ud-api-product-id' => $args[ 'product_id' ],
132 'x-ud-api-instance' => $args[ 'instance' ],
133 'x-ud-api-licence-key' => $args[ 'licence_key' ],
134 'x-ud-api-platform' => $args[ 'platform' ],
135 ) ) );
136 //echo "<pre>"; print_r( $request ); echo "</pre>"; die();
137 if( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) {
138 if( $error_log ) $this->log_request_error( sprintf( __( 'There was an error making %s request for %s. Could not do request to UsabilityDynamics.', $this->domain ), $args[ 'request' ], $product[ 'product_name' ] ) );
139 } else {
140 $response = wp_remote_retrieve_body( $request );
141 $response = @json_decode( $response, true );
142 //echo "<pre>"; print_r( $response ); echo "</pre>"; die();
143 if( empty( $response ) || !is_array( $response ) ) {
144 if( $error_log ) $this->log_request_error( sprintf( __( 'There was an error making %s request for %s, please try again', $this->domain ), $args[ 'request' ], $product[ 'product_name' ] ) );
145 } elseif( !empty( $response[ 'error' ] ) ) {
146 $error = !empty( $response[ 'additional info' ] ) ? $response[ 'additional info' ] : $response[ 'error' ];
147 if( $error_log ) $this->log_request_error( sprintf( __( 'There was an error making %s request for %s: %s' ), $args[ 'request' ], $product[ 'product_name' ], $error ) );
148 return $response;
149 } else {
150 return $response;
151 }
152 }
153 return false;
154 }
155
156 /**
157 * Log an error from an API request.
158 *
159 * @access private
160 * @since 1.0.0
161 * @param string $error
162 */
163 public function log_request_error ( $error ) {
164 $this->errors[] = $error;
165 }
166
167 /**
168 * Store logged errors in a temporary transient, such that they survive a page load.
169 * @since 1.0.0
170 * @return void
171 */
172 public function store_error_log () {
173 set_transient( $this->token . '-request-error', $this->errors );
174 }
175
176 /**
177 * Get the current error log.
178 *
179 * @since 1.0.0
180 * @return void
181 */
182 public function get_error_log () {
183 return get_transient( $this->token . '-request-error' );
184 }
185
186 /**
187 * Clear the current error log.
188 *
189 * @since 1.0.0
190 * @return void
191 */
192 public function clear_error_log () {
193 return delete_transient( $this->token . '-request-error' );
194 }
195
196 }
197
198 }
199
200 }
201