| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Utilities { |
| 4 |
|
| 5 |
use WPDataAccess\WPDA; |
| 6 |
|
| 7 |
class WPDA_Remote_Call { |
| 8 |
|
| 9 |
public static function post( |
| 10 |
$url, |
| 11 |
$body, |
| 12 |
$die = false, |
| 13 |
$headers = array(), |
| 14 |
$sslverify = true |
| 15 |
) { |
| 16 |
$response = wp_remote_post( |
| 17 |
$url, |
| 18 |
array( |
| 19 |
'headers' => $headers, |
| 20 |
'body' => $body, |
| 21 |
'timeout' => 60, |
| 22 |
'sslverify' => $sslverify, |
| 23 |
) |
| 24 |
); |
| 25 |
|
| 26 |
if ( 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_DEBUG ) ) { |
| 27 |
// WPDA::wpda_log_wp_error( $response ); |
| 28 |
} |
| 29 |
|
| 30 |
if ( is_wp_error( $response ) ) { |
| 31 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 32 |
if ( $die ) { |
| 33 |
wp_die( 'ERROR: Remote call failed [' . json_encode( $response ) . ']' ); |
| 34 |
} |
| 35 |
|
| 36 |
return $response->get_error_message(); |
| 37 |
} |
| 38 |
|
| 39 |
if ( ! isset( $response['response'], $response['body'] ) ) { |
| 40 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 41 |
if ( $die ) { |
| 42 |
wp_die( 'ERROR: Remote call failed [missing response|body]' ); |
| 43 |
} |
| 44 |
|
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
return $response; |
| 49 |
} |
| 50 |
|
| 51 |
public static function get( $url, $args = array(), $die = false ) { |
| 52 |
$response = wp_remote_get( $url, $args ); |
| 53 |
|
| 54 |
if ( 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_DEBUG ) ) { |
| 55 |
// WPDA::wpda_log_wp_error( $response ); |
| 56 |
} |
| 57 |
|
| 58 |
if ( is_wp_error( $response ) ) { |
| 59 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 60 |
if ( $die ) { |
| 61 |
wp_die( 'ERROR: Remote call failed [' . json_encode( $response ) . ']' ); |
| 62 |
} |
| 63 |
|
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
if ( ! isset( $response['response'], $response['body'] ) ) { |
| 68 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 69 |
if ( $die ) { |
| 70 |
wp_die( 'ERROR: Remote call failed [missing response|body]' ); |
| 71 |
} |
| 72 |
|
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
return $response; |
| 77 |
} |
| 78 |
|
| 79 |
public static function delete( $url, $args = array(), $headers = array(), $die = false ) { |
| 80 |
$response = wp_remote_request( |
| 81 |
$url, |
| 82 |
array( |
| 83 |
'method' => 'DELETE', |
| 84 |
'headers' => $headers, |
| 85 |
) |
| 86 |
); |
| 87 |
|
| 88 |
if ( 'on' === WPDA::get_option( WPDA::OPTION_PLUGIN_DEBUG ) ) { |
| 89 |
// WPDA::wpda_log_wp_error( $response ); |
| 90 |
} |
| 91 |
|
| 92 |
if ( is_wp_error( $response ) ) { |
| 93 |
WPDA::wpda_log_wp_error( json_encode( $response ) ); |
| 94 |
if ( $die ) { |
| 95 |
wp_die( 'ERROR: Remote DELETE failed [' . json_encode( $response ) . ']' ); |
| 96 |
} |
| 97 |
|
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
$status_code = wp_remote_retrieve_response_code( $response ); |
| 102 |
if ( $status_code === 204 ) { |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
WPDA::wpda_log_wp_error("ERROR: Delete failed: HTTP $status_code"); |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
public static function max_size() { |
| 111 |
$max = ini_get('post_max_size'); |
| 112 |
$unit = $max[ strlen( $max ) - 1 ]; |
| 113 |
$max = substr( $max, 0, strlen( $max ) - 1 ); |
| 114 |
|
| 115 |
switch($unit) { |
| 116 |
case 'G': |
| 117 |
$max *= 1024; |
| 118 |
case 'M': |
| 119 |
$max *= 1024; |
| 120 |
case 'K': |
| 121 |
$max *= 1024; |
| 122 |
} |
| 123 |
|
| 124 |
return $max; |
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
} |