1 | <?php |
||
2 | namespace NirjharLo\WP_Plugin_Framework\Lib; |
||
3 | |||
4 | /** |
||
5 | * Implimentation of WordPress inbuilt API class |
||
6 | * |
||
7 | * Usage: |
||
8 | * |
||
9 | * $api = new PLUGIN_API(); |
||
10 | * $api->endpoint = 'endpoint_url' |
||
11 | * $api->header = array( "key: $val" ) |
||
12 | * $api->data_type = 'xml' or 'json' |
||
13 | * $api->call_type = 'GET' or 'POST' |
||
14 | * $api->call(); |
||
15 | * $data = $api->parse(); |
||
16 | * |
||
17 | * @author Nirjhar Lo |
||
18 | * @package wp-plugin-framework |
||
19 | */ |
||
20 | if ( ! class_exists( 'Api' ) ) { |
||
21 | |||
22 | class Api { |
||
23 | |||
24 | |||
25 | /** |
||
26 | * @var String |
||
27 | */ |
||
28 | public $endpoint; |
||
29 | |||
30 | |||
31 | /** |
||
32 | * @var Array |
||
33 | */ |
||
34 | public $header; |
||
35 | |||
36 | |||
37 | /** |
||
38 | * @var String |
||
39 | */ |
||
40 | public $data_type; |
||
41 | |||
42 | |||
43 | /** |
||
44 | * @var String |
||
45 | */ |
||
46 | public $call_type; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Define the properties inside a instance |
||
51 | * |
||
52 | * @return Void |
||
53 | */ |
||
54 | public function __construct() { |
||
55 | |||
56 | $this->endpoint = ''; |
||
57 | $this->header = array(); |
||
58 | $this->data_type = ''; //xml or json |
||
59 | $this->call_type = ''; |
||
60 | } |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Define the necessary database tables |
||
65 | * |
||
66 | * @return Array |
||
67 | */ |
||
68 | public function build() { |
||
69 | |||
70 | $args = array( |
||
71 | CURLOPT_URL => $this->endpoint, |
||
72 | CURLOPT_RETURNTRANSFER => true, |
||
73 | CURLOPT_ENCODING => "", |
||
74 | CURLOPT_MAXREDIRS => 10, |
||
75 | CURLOPT_TIMEOUT => 30, |
||
76 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||
77 | CURLOPT_CUSTOMREQUEST => $this->call_type, |
||
78 | CURLOPT_HTTPHEADER => $this->header, |
||
79 | ); |
||
80 | |||
81 | return $args; |
||
82 | } |
||
83 | |||
84 | |||
85 | /** |
||
86 | * Define the variables for db table creation |
||
87 | * |
||
88 | * @return Array |
||
89 | */ |
||
90 | public function call() { |
||
91 | |||
92 | $curl = curl_init(); |
||
93 | |||
94 | curl_setopt_array( $curl, $this->build() ); |
||
95 | |||
96 | $result = curl_exec( $curl ); |
||
97 | $err = curl_error( $curl ); |
||
98 | |||
99 | curl_close( $curl ); |
||
100 | |||
101 | if ( $err ) { |
||
102 | $result = "cURL Error #:" . $err; |
||
103 | } |
||
104 | |||
105 | return $result; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
106 | } |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Check options and tables and output the info to check if db install is successful |
||
111 | * |
||
112 | * @return Array |
||
113 | */ |
||
114 | public function parse($data) { |
||
115 | |||
116 | call_user_func( array( $this, $this->data_type ), $data ); |
||
117 | } |
||
118 | |||
119 | |||
120 | /** |
||
121 | * Parse XML data type |
||
122 | * |
||
123 | * @return Array |
||
124 | */ |
||
125 | public function xml( $data ) { |
||
126 | |||
127 | libxml_use_internal_errors( true ); |
||
128 | $parsed = ( ! $data || $data == '' ? false : simplexml_load_string( $data ) ); |
||
129 | |||
130 | if ( ! $parsed ) { |
||
131 | return false; |
||
0 ignored issues
–
show
|
|||
132 | libxml_clear_errors(); |
||
0 ignored issues
–
show
libxml_clear_errors() is not reachable.
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed. Unreachable code is most often the result of function fx() {
try {
doSomething();
return true;
}
catch (\Exception $e) {
return false;
}
return false;
}
In the above example, the last ![]() |
|||
133 | } else { |
||
134 | return $parsed; |
||
0 ignored issues
–
show
|
|||
135 | } |
||
136 | } |
||
137 | |||
138 | |||
139 | /** |
||
140 | * Parse JSON data type |
||
141 | * |
||
142 | * @return Array |
||
143 | */ |
||
144 | public function json( $data ) { |
||
145 | |||
146 | $parsed = ( ! $data || $data == '' ? false : json_decode( $data, 1 ) ); |
||
147 | return $parsed; |
||
0 ignored issues
–
show
The expression
return $parsed could also return false which is incompatible with the documented return type array . Did you maybe forget to handle an error condition?
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled. ![]() |
|||
148 | } |
||
149 | } |
||
150 | } ?> |
||
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. ![]() |
|||
151 |