PluginProbe
Bookit — Booking & Appointment Calendar / 2.6.0.5
Bookit — Booking & Appointment Calendar v2.6.0.5
2.6.0.5 2.6.0.4 2.6.0.3 2.6.0.2 2.6.0.1 2.6.0 trunk 1.2 1.2.2 1.2.3 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 All 62 releases
bookit / src / Bookit / Gateways / Contracts / Abstract_WhoDat.php

Abstract_WhoDat.php in Bookit — Booking & Appointment Calendar 2.6.0.5, at src/Bookit/Gateways/Contracts/Abstract_WhoDat.php

127 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Bookit\Gateways\Contracts;
4
5 use Bookit\Vendor\StellarWP\Arrays\Arr;
6
7 /**
8 * Abstract class to handle WhoDat connections
9 *
10 * @since 2.5.0
11 *
12 * @package Bookit\Gateways\PayPal
13 */
14 abstract class Abstract_WhoDat implements WhoDat_Interface {
15
16 /**
17 * Public WhoDat URL, used to authenticate accounts with gateway payment providers
18 *
19 * @since 2.5.0
20 *
21 * @var string
22 */
23 private $api_base_url = 'https://whodat.theeventscalendar.com/commerce/v1';
24
25 /**
26 * Returns the gateway-specific endpoint to use
27 *
28 * @since 2.5.0
29 *
30 * @return string
31 */
32 private function get_gateway_endpoint() {
33 return $this->api_endpoint;
34 }
35
36 /**
37 * Returns the WhoDat URL to use.
38 *
39 * @since 2.5.0
40 *
41 * @return string
42 */
43 private function get_api_base_url() {
44
45 if ( defined( 'BOOKIT_WHODAT_DEV_URL' ) && BOOKIT_WHODAT_DEV_URL ) {
46 return BOOKIT_WHODAT_DEV_URL;
47 }
48
49 return $this->api_base_url;
50 }
51
52 /**
53 * @inheritDoc
54 */
55 public function get_api_url( $endpoint, array $query_args = [] ) {
56 return add_query_arg( $query_args, "{$this->get_api_base_url()}/{$this->get_gateway_endpoint()}/{$endpoint}" );
57 }
58
59 /**
60 * @inheritDoc
61 */
62 public function get( $endpoint, array $query_args ) {
63 $url = $this->get_api_url( $endpoint, $query_args );
64
65 $request = wp_remote_get( $url );
66
67 if ( is_wp_error( $request ) ) {
68 $this->log_error( 'WhoDat request error:', $request->get_error_message(), $url );
69
70 return null;
71 }
72
73 $body = wp_remote_retrieve_body( $request );
74 $body = json_decode( $body, true );
75
76 return $body;
77 }
78
79 /**
80 * @inheritDoc
81 */
82 public function post( $endpoint, array $query_args = [], array $request_arguments = [] ) {
83 $url = $this->get_api_url( $endpoint, $query_args );
84
85 $default_arguments = [
86 'body' => [],
87 ];
88
89 foreach ( $default_arguments as $key => $default_argument ) {
90 $request_arguments[ $key ] = array_merge( $default_argument, Arr::get( $request_arguments, $key, [] ) );
91 }
92 $request_arguments = array_filter( $request_arguments );
93 $request = wp_remote_post( $url, $request_arguments );
94
95 if ( is_wp_error( $request ) ) {
96 $this->log_error( 'WhoDat request error:', $request->get_error_message(), $url );
97
98 return null;
99 }
100
101 $body = wp_remote_retrieve_body( $request );
102 $body = json_decode( $body, true );
103
104 if ( ! is_array( $body ) ) {
105 $this->log_error( 'WhoDat unexpected response:', $body, $url );
106 $this->log_error( 'Response:', print_r( $request, true ), '--->' );
107
108 return null;
109 }
110
111 return $body;
112 }
113
114 /**
115 * @inheritDoc
116 */
117 public function log_error( $type, $message, $url ) {
118 $log = sprintf(
119 '[%s] %s %s',
120 $url,
121 $type,
122 $message
123 );
124 error_log( $log );
125 }
126
127 }