PluginProbe
WooReer / 2.2.3
WooReer v2.2.3
trunk 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 3.0.0 3.0.1 All 34 releases
wcsdm / includes / classes / class-wcsdm-api.php

class-wcsdm-api.php in WooReer 2.2.3, at includes/classes/class-wcsdm-api.php

181 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The file that defines the api request class
4 *
5 * @link https://github.com/sofyansitorus
6 * @since 2.0.8
7 *
8 * @package Wcsdm
9 * @subpackage Wcsdm/includes
10 */
11
12 // If this file is called directly, abort.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * The API request class.
19 *
20 * @since 2.0.8
21 * @package Wcsdm
22 * @subpackage Wcsdm/includes
23 * @author Sofyan Sitorus <sofyansitorus@gmail.com>
24 */
25 abstract class Wcsdm_API {
26
27 /**
28 * URL of Google Maps Distance Matrix API
29 *
30 * @since 2.0.8
31 * @var string
32 */
33 private static $api_url = 'https://maps.googleapis.com/maps/api/distancematrix/json';
34
35 /**
36 * Making HTTP request to Google Maps Distance Matrix API
37 *
38 * @param array $args Custom arguments for $settings and $package data.
39 * @param bool $is_validation Is the request for validation purpose or not.
40 *
41 * @return (array[]|WP_Error) WP_Error on failure.
42 */
43 public static function calculate_distance( $args = array(), $is_validation = false ) {
44 $request_data = wp_parse_args(
45 $args,
46 array(
47 'origins' => $is_validation ? array( WCSDM_DEFAULT_LAT, WCSDM_DEFAULT_LNG ) : '',
48 'destinations' => $is_validation ? array( WCSDM_TEST_LAT, WCSDM_TEST_LNG ) : '',
49 'key' => '',
50 'avoid' => '',
51 'language' => get_locale(),
52 'units' => 'metric',
53 'mode' => 'driving',
54 )
55 );
56
57 foreach ( $request_data as $key => $value ) {
58 if ( is_array( $value ) ) {
59 $request_data[ $key ] = implode( ',', array_map( 'rawurlencode', $value ) );
60 } else {
61 $request_data[ $key ] = rawurlencode( $value );
62 }
63 }
64
65 $request_data_masked = array_merge(
66 $request_data,
67 array(
68 'key' => '***',
69 )
70 );
71
72 $api_response = wp_remote_get( add_query_arg( $request_data, self::$api_url ) );
73
74 // Check if HTTP request is error.
75 if ( is_wp_error( $api_response ) ) {
76 $api_response->add_data(
77 array(
78 'request_data' => $request_data_masked,
79 )
80 );
81
82 return $api_response;
83 }
84
85 if ( empty( $api_response['body'] ) ) {
86 return new WP_Error(
87 'api_response_body_empty',
88 __( 'API response is empty.', 'wcsdm' ),
89 array(
90 'request_data' => $request_data_masked,
91 'api_response' => $api_response,
92 )
93 );
94 }
95
96 // Decode API response body.
97 $response_data = json_decode( $api_response['body'], true );
98
99 if ( is_null( $response_data ) ) {
100 return new WP_Error(
101 'json_last_error',
102 json_last_error_msg(),
103 array(
104 'request_data' => $request_data_masked,
105 'api_response' => $api_response,
106 )
107 );
108 }
109
110 // Check API response is OK.
111 $status = isset( $response_data['status'] ) ? $response_data['status'] : 'UNKNOWN_ERROR';
112
113 if ( 'OK' !== $status ) {
114 if ( isset( $response_data['error_message'] ) ) {
115 return new WP_Error(
116 $status,
117 $response_data['error_message'],
118 array(
119 'request_data' => $request_data_masked,
120 'response_data' => $response_data,
121 )
122 );
123 }
124
125 return new WP_Error(
126 $status,
127 $status,
128 array(
129 'request_data' => $request_data_masked,
130 'response_data' => $response_data,
131 )
132 );
133 }
134
135 $element_errors = array(
136 'NOT_FOUND' => __( 'Origin and/or destination of this pairing could not be geocoded.', 'wcsdm' ),
137 'ZERO_RESULTS' => __( 'No route could be found between the origin and destination.', 'wcsdm' ),
138 'MAX_ROUTE_LENGTH_EXCEEDED' => __( 'Requested route is too long and cannot be processed.', 'wcsdm' ),
139 'UNKNOWN' => __( 'Unknown error.', 'wcsdm' ),
140 );
141
142 $errors = new WP_Error();
143 $results = array();
144
145 // Get the shipping distance.
146 foreach ( $response_data['rows'] as $row ) {
147 foreach ( $row['elements'] as $element ) {
148 $element_status = isset( $element['status'] ) ? $element['status'] : 'UNKNOWN';
149
150 if ( 'OK' === $element_status ) {
151 $results[] = array(
152 'distance' => $element['distance']['value'], // Distance in meters unit.
153 'duration' => $element['duration']['value'], // Duration in seconds unit.
154 );
155
156 continue;
157 }
158
159 if ( ! isset( $element_errors[ $element_status ] ) ) {
160 $element_status = 'UNKNOWN';
161 }
162
163 $errors->add(
164 $element_status,
165 $element_errors[ $element_status ],
166 array(
167 'request_data' => $request_data_masked,
168 'response_data' => $response_data,
169 )
170 );
171 }
172 }
173
174 if ( ! empty( $results ) ) {
175 return $results;
176 }
177
178 return $errors;
179 }
180 }
181