LLMS_Dot_Com_API

LLMS_Dot_Com_API


Source Source

File: includes/class.llms.dot.com.api.php

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class LLMS_Dot_Com_API extends LLMS_Abstract_API_Handler {
 
    /**
     * Send requests in JSON format
     *
     * @var  bool
     */
    protected $is_json = false;
 
    /**
     * Determines if it's a request to the .com REST api
     *
     * @var  bool
     */
    protected $is_rest = true;
 
    /**
     * Construct an API call, parameters are passed to private `call()` function
     *
     * @param    stirng $resource  url endpoint or resource to make a request to
     * @param    array  $data      array of data to pass in the body of the request
     * @param    string $method    method of request (POST, GET, DELETE, PUT, etc...)
     * @param    bool   $is_rest   if true adds wp-json rest to request url, otherwise requests to site base
     * @return   void
     * @since    3.22.0
     * @version  3.22.0
     */
    public function __construct( $resource, $data, $method = null, $is_rest = true ) {
 
        $this->is_rest = $is_rest;
        parent::__construct( $resource, $data, $method );
 
    }
 
    /**
     * Determine if the current request is a rest request
     *
     * @return   bool
     * @since    3.22.0
     * @version  3.22.0
     */
    public function is_rest_request() {
        return $this->is_rest;
    }
 
    /**
     * Parse the body of the response and set a success/error
     *
     * @param    array $response  response data
     * @return   void
     * @since    3.22.0
     * @version  3.22.0
     */
    protected function parse_response( $response ) {
 
        $body = json_decode( wp_remote_retrieve_body( $response ), true );
 
        if ( isset( $response['response'] ) && isset( $response['response']['code'] ) && ! in_array( $response['response']['code'], array( 200, 201 ) ) ) {
 
            $msg = isset( $body['message'] ) ? $body['message'] : $response['response']['message'];
            $this->set_error( $msg, isset( $body['code'] ) ? $body['code'] : $response['response']['code'], $body );
 
        } else {
 
            $this->set_result( $body );
 
        }
 
    }
 
    /**
     * Set request body
     *
     * @param    array  $data      request body
     * @param    string $method    request method
     * @param    string $resource  requested resource
     * @return   array
     * @since    3.22.0
     * @version  3.22.0
     */
    protected function set_request_body( $data, $method, $resource ) {
        return apply_filters( 'llms_dot_com_api_request_body', $data, $method, $resource, $this );
    }
 
    /**
     * Set request headers
     *
     * @param    array  $headers   default request headers
     * @param    string $resource  request resource
     * @param    string $method    request method
     * @return   array
     * @since    3.22.0
     * @version  3.22.0
     */
    protected function set_request_headers( $headers, $resource, $method ) {
        return apply_filters( 'llms_dot_com_api_request_headers', $headers, $resource, $method, $this );
    }
 
    /**
     * Set the request URL
     *
     * @param    string $resource  requested resource
     * @param    string $method    request method
     * @return   string
     * @since    3.22.0
     * @version  3.22.0
     */
    protected function set_request_url( $resource, $method ) {
 
        $url = 'https://lifterlms.com';
        if ( $this->is_rest_request() ) {
            $url .= '/wp-json/llms/v3';
        }
 
        return apply_filters( 'llms_dot_com_api_request_url', $url . $resource, $resource, $method, $this );
    }
 
    /**
     * Set the request User Agent
     * Can be overridden by extending classes when necessary
     *
     * @param    string $user_agent  default user agent (LifterLMS {$version})
     * @param    string $resource    requested resource
     * @param    string $method      request method
     * @return   string
     * @since    3.22.0
     * @version  3.22.0
     */
    protected function set_user_agent( $user_agent, $resource, $method ) {
        return sprintf( 'LifterLMS/%1$s (%2$s)', LLMS_VERSION, get_site_url() );
    }
 
}


Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
3.22.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.