PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
← All changes | integrations/zoom/includes/functions.php +321 -0 5.7.4 → 6.0.3 View file →
@@ -1,0 +1,321 @@
1 +<?php
2 +/**
3 + * Functions
4 + *
5 + * @package AutomatorWP\Integrations\Zoom\Functions
6 + * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]>
7 + * @since 1.0.0
8 + */
9 +// Exit if accessed directly
10 +if( !defined( 'ABSPATH' ) ) exit;
11 +
12 +/**
13 + * Get the request parameters
14 + *
15 + * @since 1.0.0
16 + *
17 + * @param string $platform
18 + *
19 + * @return array|false
20 + */
21 +function automatorwp_zoom_get_request_parameters( $platform ) {
22 +
23 + if( empty( $platform ) ) {
24 + return false;
25 + }
26 +
27 + $auth = get_option( 'automatorwp_zoom_' . $platform . '_auth' );
28 +
29 + if( ! is_array( $auth ) ) {
30 + return false;
31 + }
32 +
33 + return array(
34 + 'user-agent' => 'AutomatorWP; ' . home_url(),
35 + 'timeout' => 120,
36 + 'httpversion' => '1.1',
37 + 'headers' => array(
38 + 'Authorization' => 'Bearer ' . $auth['access_token'],
39 + 'Content-Type' => 'application/json',
40 + 'Accept' => 'application/json'
41 + )
42 + );
43 +}
44 +
45 +/**
46 + * Get the request parameters
47 + *
48 + * @since 1.0.0
49 + *
50 + * @param string $platform
51 + *
52 + * @return string|false|WP_Error
53 + */
54 +function automatorwp_zoom_refresh_token( $platform ) {
55 +
56 + $client_id = automatorwp_zoom_get_option( $platform . '_client_id', '' );
57 + $client_secret = automatorwp_zoom_get_option( $platform . '_client_secret', '' );
58 +
59 + if( empty( $client_id ) || empty( $client_secret ) ) {
60 + return false;
61 + }
62 +
63 + $auth = get_option( 'automatorwp_zoom_' . $platform . '_auth', false );
64 +
65 + if( ! is_array( $auth ) ) {
66 + return false;
67 + }
68 +
69 + $params = array(
70 + 'headers' => array(
71 + 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
72 + 'Authorization' => 'Basic ' . base64_encode( $client_id . ':' . $client_secret ),
73 + 'Accept' => 'application/json',
74 + ),
75 + 'body' => array(
76 + 'refresh_token' => $auth['refresh_token'],
77 + 'grant_type' => 'refresh_token',
78 + )
79 + );
80 +
81 + $response = wp_remote_post( 'https://zoom.us/oauth/token', $params );
82 +
83 + if( is_wp_error( $response ) ) {
84 + return $response;
85 + }
86 +
87 + $response_code = wp_remote_retrieve_response_code( $response );
88 +
89 + if ( $response_code !== 200 ) {
90 + return false;
91 + }
92 +
93 + $body = json_decode( wp_remote_retrieve_body( $response ) );
94 +
95 + $auth = array(
96 + 'access_token' => $body->access_token,
97 + 'refresh_token' => $body->refresh_token,
98 + 'token_type' => $body->token_type,
99 + 'expires_in' => $body->expires_in,
100 + 'scope' => $body->scope,
101 + );
102 +
103 + // Update the access and refresh tokens
104 + update_option( 'automatorwp_zoom_' . $platform . '_auth', $auth );
105 +
106 + return $body->access_token;
107 +
108 +}
109 +
110 +/**
111 + * Filters the HTTP API response immediately before the response is returned.
112 + *
113 + * @since 1.0.0
114 + *
115 + * @param array $response HTTP response.
116 + * @param array $parsed_args HTTP request arguments.
117 + * @param string $url The request URL.
118 + *
119 + * @return array
120 + */
121 +function automatorwp_zoom_maybe_refresh_token( $response, $args, $url ) {
122 +
123 + // Ensure to only run this check to on Zoom request
124 + if( strpos( $url, 'api.zoom' ) !== false ) {
125 +
126 + $code = wp_remote_retrieve_response_code( $response );
127 +
128 + if( $code === 401 ) {
129 +
130 + $access_token = automatorwp_zoom_refresh_token( 'meetings' );
131 +
132 + // Send again the request if token gets refreshed successfully
133 + if( $access_token ) {
134 +
135 + $args['headers']['Authorization'] = 'Bearer ' . $access_token;
136 +
137 + $response = wp_remote_request( $url, $args );
138 +
139 + }
140 +
141 + }
142 +
143 + }
144 +
145 + return $response;
146 +
147 +}
148 +add_filter( 'http_response', 'automatorwp_zoom_maybe_refresh_token', 10, 3 );
149 +
150 +/**
151 + * Get all meetings
152 + *
153 + * @since 1.0.0
154 + *
155 + * @return array
156 + */
157 +function automatorwp_zoom_get_meetings() {
158 +
159 + $meetings = array();
160 +
161 + $transient = get_transient( 'automatorwp_zoom_meetings' );
162 +
163 + if ( $transient !== false ) {
164 + return $transient;
165 + }
166 +
167 + $params = automatorwp_zoom_get_request_parameters( 'meetings' );
168 +
169 + // Bail if the authorization has not been setup from settings
170 + if( $params === false ) {
171 + return $meetings;
172 + }
173 +
174 + $url = 'https://api.zoom.us/v2/users/me/meetings?page_number=1&page_size=300&type=upcoming';
175 + $response = wp_remote_get( $url, $params );
176 +
177 + $response = json_decode( wp_remote_retrieve_body( $response ), true );
178 +
179 + if( isset( $response['meetings'] ) && is_array( $response['meetings'] ) ) {
180 + foreach( $response['meetings'] as $meeting ) {
181 + $meetings[] = array(
182 + 'id' => $meeting['id'],
183 + 'name' => $meeting['topic'],
184 + );
185 + }
186 + }
187 +
188 + if( count( $meetings ) ) {
189 + // Set a transient for 10 mins with the meetings
190 + set_transient( 'automatorwp_zoom_meetings', $meetings, 10 * 60 );
191 + }
192 +
193 + return $meetings;
194 +
195 +}
196 +
197 +/**
198 + * Get all meetings
199 + *
200 + * @since 1.0.0
201 + *
202 + * @param string $meeting_id
203 + *
204 + * @return array
205 + */
206 +function automatorwp_zoom_get_meeting_registrants( $meeting_id ) {
207 +
208 + $registrants = array();
209 +
210 + $params = automatorwp_zoom_get_request_parameters( 'meetings' );
211 +
212 + // Bail if the authorization has not been setup from settings
213 + if( $params === false ) {
214 + return $registrants;
215 + }
216 +
217 + $url = 'https://api.zoom.us/v2/meetings/' . $meeting_id . '/registrants?page_number=1&page_size=300&status=approved';
218 + $response = wp_remote_get( $url, $params );
219 +
220 + $response = json_decode( wp_remote_retrieve_body( $response ), true );
221 +
222 + if( isset( $response['registrants'] ) && is_array( $response['registrants'] ) ) {
223 + return $response['registrants'];
224 + }
225 +
226 + return $registrants;
227 +
228 +}
229 +
230 +/**
231 + * Get all meetings
232 + *
233 + * @since 1.0.0
234 + *
235 + * @param string $meeting_id
236 + * @param string $email
237 + *
238 + * @return string|false
239 + */
240 +function automatorwp_zoom_get_meeting_registrant_id( $meeting_id, $email ) {
241 +
242 + $registrants = automatorwp_zoom_get_meeting_registrants( $meeting_id );
243 +
244 + foreach( $registrants as $registrant ) {
245 + if( $registrant['email'] === $email ) {
246 + return $registrant['id'];
247 + }
248 + }
249 +
250 + return false;
251 +
252 +}
253 +
254 +/**
255 + * Options callback for select2 fields assigned to meetings
256 + *
257 + * @since 1.0.0
258 + *
259 + * @param stdClass $field
260 + *
261 + * @return array
262 + */
263 +function automatorwp_zoom_options_cb_meetings( $field ) {
264 +
265 + // Setup vars
266 + $value = $field->escaped_value;
267 + $none_value = 'any';
268 + $none_label = __( 'any meeting', 'automatorwp' );
269 + $options = automatorwp_options_cb_none_option( $field, $none_value, $none_label );
270 +
271 + if( ! empty( $value ) ) {
272 + if( ! is_array( $value ) ) {
273 + $value = array( $value );
274 + }
275 +
276 + foreach( $value as $meeting_id ) {
277 +
278 + // Skip option none
279 + if( $meeting_id === $none_value ) {
280 + continue;
281 + }
282 +
283 + $options[$meeting_id] = automatorwp_zoom_get_meeting_title( $meeting_id );
284 + }
285 + }
286 +
287 + return $options;
288 +
289 +}
290 +
291 +/**
292 + * Get the meeting title
293 + *
294 + * @since 1.0.0
295 + *
296 + * @param int $meeting_id
297 + *
298 + * @return string|null
299 + */
300 +function automatorwp_zoom_get_meeting_title( $meeting_id ) {
301 +
302 + // Empty title if no ID provided
303 + if( absint( $meeting_id ) === 0 ) {
304 + return '';
305 + }
306 +
307 + $meetings = automatorwp_zoom_get_meetings();
308 + $meeting_name = '';
309 +
310 + foreach( $meetings as $meeting ) {
311 +
312 + if( absint( $meeting['id'] ) === absint( $meeting_id ) ) {
313 + $meeting_name = $meeting['name'];
314 + break;
315 + }
316 +
317 + }
318 +
319 + return $meeting_name;
320 +
321 +}