/var/www/html_uk/wp-content/plugins/automatewoo/includes/Integration.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php

namespace AutomateWoo;

/**
 * Abstract for API integration classes.
 *
 * @class Integration
 * @since 2.3
 */
abstract class Integration {

    
/** @var string */
    
public $integration_id;

    
/** @var bool */
    
public $log_errors true;

    
/**
     * Add a log entry.
     *
     * @param string $message The message to log.
     *
     * @return void
     */
    
public function log$message ): void {
        if ( ! 
$this->log_errors ) {
            return;
        }

        
Logger::info'integration-' $this->integration_id$message );
    }


    
/**
     * Maybe log the results of a request.
     *
     * @param Remote_Request $request The request object to maybe log.
     *
     * @return void
     */
    
public function maybe_log_request_errors$request ): void {
        if ( ! 
$this->log_errors ) {
            return;
        }

        if ( 
$request->is_http_error() ) {
            
$this->log$request->get_http_error_message() );
        } elseif ( 
$request->is_api_error() ) {
            
$this->log(
                
$request->get_response_code() . ' ' $request->get_response_message()
                . 
'. Method: ' $request->method
                
'. Endpoint: ' $request->url
                
'. Response body: ' print_r$request->get_body(), true )  // phpcs:ignore WordPress.PHP.DevelopmentFunctions
            
);
        }
    }

    
/**
     * Test if the current API config is valid.
     *
     * @return bool True if the integration can communicate with external API or false otherwise
     */
    
abstract public function test_integration(): bool;

    
/**
     * Check if the integration is enabled.
     *
     * @return bool True if the integration is enabled.
     */
    
abstract public function is_enabled(): bool;
}