PluginProbe
Property Hive / 2.2.3
Property Hive v2.2.3
2.3.0 2.2.6 2.2.5 2.2.4 2.2.3 2.2.2 1.4.46 1.4.47 1.4.48 1.4.49 1.4.5 1.4.50 1.4.51 1.4.52 1.4.53 1.4.54 1.4.55 1.4.56 1.4.57 1.4.58 1.4.59 1.4.6 1.4.60 1.4.61 1.4.62 All 260 releases
propertyhive / includes / class-ph-ai-service.php

class-ph-ai-service.php in Property Hive 2.2.3, at includes/class-ph-ai-service.php

91 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 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 /**
8 * AI Service
9 *
10 * @class PH_AI_Service
11 * @version 1.0.0
12 * @package PropertyHive/Classes/
13 * @category Class
14 * @author PropertyHive
15 */
16 class PH_AI_Service {
17
18 public function make_request( $action, $payload = array() ) {
19
20 if ( empty( $action ) ) {
21 return new WP_Error(
22 'propertyhive_ai_no_action',
23 __( 'No AI action was specified.', 'propertyhive' )
24 );
25 }
26
27 $body = array(
28 'action' => sanitize_key( $action ),
29 'payload' => $payload,
30 );
31
32 $body = apply_filters( 'propertyhive_ai_request_body', $body, $action, $payload );
33
34 $response = wp_remote_post(
35 apply_filters( 'propertyhive_ai_service_endpoint', 'https://wp-property-hive.com/ai.php', $action ),
36 array(
37 'timeout' => 60,
38 'sslverify' => true,
39 'headers' => array(
40 'Content-Type' => 'application/json',
41 'Accept' => 'application/json',
42 'X-PH-License-Key' => get_option( 'propertyhive_pro_license_key', '' ),
43 'X-PH-License-Type' => PH()->license->get_license_type(),
44 'X-PH-Instance-Id' => get_option( 'propertyhive_pro_instance_id', '' ),
45 'X-PH-Plugin-Version' => PH_VERSION,
46 ),
47 'body' => wp_json_encode($body),
48 )
49 );
50
51 if ( is_wp_error( $response ) ) {
52 return $response;
53 }
54
55 $status_code = wp_remote_retrieve_response_code( $response );
56 $raw_body = wp_remote_retrieve_body( $response );
57
58 if ( $status_code < 200 || $status_code >= 300 ) {
59 return new WP_Error(
60 'propertyhive_ai_http_error',
61 __( 'The AI service returned an unexpected response.', 'propertyhive' ),
62 array(
63 'status_code' => $status_code,
64 'body' => $raw_body,
65 )
66 );
67 }
68
69 $decoded = json_decode( $raw_body, true );
70
71 if ( ! is_array( $decoded ) ) {
72 return new WP_Error(
73 'propertyhive_ai_invalid_response',
74 __( 'The AI service returned an invalid response.', 'propertyhive' ),
75 array(
76 'body' => $raw_body,
77 )
78 );
79 }
80
81 if ( isset( $decoded['success'] ) && !$decoded['success'] ) {
82 return new WP_Error(
83 ! empty( $decoded['code'] ) ? sanitize_key( $decoded['code'] ) : 'propertyhive_ai_request_failed',
84 ! empty( $decoded['message'] ) ? $decoded['message'] : __( 'The AI service could not complete the request.', 'propertyhive' ),
85 $decoded
86 );
87 }
88
89 return $decoded;
90 }
91 }