PluginProbe
Hostinger Tools / 2.2.4
Hostinger Tools v2.2.4
3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.4 All 108 releases
hostinger / includes / Surveys / Rest / Rest.php

Rest.php in Hostinger Tools 2.2.4, at includes/Surveys/Rest/Rest.php

107 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Hostinger\Surveys\Rest;
4
5 use Hostinger\Requests\Client;
6
7 defined( 'ABSPATH' ) || exit;
8
9 class Rest {
10 public const SUBMIT_SURVEY = '/v3/wordpress/survey/store';
11 public const CLIENT_SURVEY_ELIGIBILITY = '/v3/wordpress/survey/client-eligible';
12 public const CLIENT_SURVEY_IDENTIFIER = 'customer_satisfaction_score';
13 public const GET_SURVEY = '/v3/wordpress/survey/get';
14
15 private Client $client;
16
17 public function __construct( Client $client ) {
18 $this->client = $client;
19 }
20
21 public function is_client_eligible(): bool {
22 $response = $this->client->get(
23 self::CLIENT_SURVEY_ELIGIBILITY,
24 array(
25 'identifier' => self::CLIENT_SURVEY_IDENTIFIER,
26 ),
27 array(),
28 10
29 );
30
31 $decoded_response = $this->decode_response( $response );
32 $response_data = isset( $decoded_response['response_data']['data'] ) ? $decoded_response['response_data']['data'] : null;
33
34 if ( $response_data !== true ) {
35 return false;
36 }
37
38 return (bool) $this->get_result( $response );
39 }
40
41 public function get_survey_questions(): array {
42
43 $response = $this->client->get(
44 self::GET_SURVEY,
45 array(
46 'identifier' => self::CLIENT_SURVEY_IDENTIFIER,
47 ),
48 array(),
49 10
50 );
51
52 if ( ! $this->get_result( $response ) ) {
53 return array();
54 }
55
56 $response_data = $this->decode_response( $response )['response_data']['data'];
57
58 if ( isset( $response_data['questions'] ) && is_array( $response_data['questions'] ) ) {
59 return $response_data['questions'];
60 }
61
62 return array();
63 }
64
65 public function submit_survey_data( array $data ): bool {
66 $response = $this->client->post( self::SUBMIT_SURVEY, $data );
67 return $this->get_result( $response );
68 }
69
70 /**
71 * @param array|WP_Error $response
72 *
73 * @return mixed
74 */
75 public function get_result( $response ) {
76 $data = $this->decode_response( $response );
77
78 if ( is_wp_error( $data ) || $data['response_code'] !== 200 ) {
79 error_log( 'Error: ' . $data['response_body'] );
80 return false;
81 }
82
83 return $data['response_data']['data'];
84 }
85
86 /**
87 * @param array|WP_Error $response
88 *
89 * @return array
90 */
91 public function decode_response( $response ): array {
92 $response_body = wp_remote_retrieve_body( $response );
93 $response_code = wp_remote_retrieve_response_code( $response );
94 $response_data = json_decode( $response_body, true );
95
96 if ( ! is_array( $response_data ) ) {
97 $response_data = array( 'data' => null );
98 }
99
100 return array(
101 'response_code' => $response_code,
102 'response_data' => $response_data,
103 'response_body' => $response_body,
104 );
105 }
106 }
107