| @@ -1,164 +1,164 @@ | ||
| 1 | -<?php | |
| 2 | -/** | |
| 3 | - * The file that defines the core plugin class | |
| 4 | - * | |
| 5 | - * A class definition that includes attributes and functions used across both the | |
| 6 | - * public-facing side of the site and the admin area. | |
| 7 | - * | |
| 8 | - * @link https://posimyth.com/ | |
| 9 | - * @since 1.0.0 | |
| 10 | - * | |
| 11 | - * @package Wdesignkit | |
| 12 | - * @subpackage Wdesignkit/includes | |
| 13 | - */ | |
| 14 | - | |
| 15 | -/**Exit if accessed directly. */ | |
| 16 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 17 | - exit; | |
| 18 | -} | |
| 19 | - | |
| 20 | -if ( ! class_exists( 'WDesignKit_Data_Query' ) ) { | |
| 21 | - | |
| 22 | - /** | |
| 23 | - * This class call api | |
| 24 | - * | |
| 25 | - * @since 1.0.0 | |
| 26 | - */ | |
| 27 | - class WDesignKit_Data_Query { | |
| 28 | - | |
| 29 | - /** | |
| 30 | - * Member Variable | |
| 31 | - * | |
| 32 | - * @var instance | |
| 33 | - */ | |
| 34 | - private static $instance; | |
| 35 | - | |
| 36 | - /** | |
| 37 | - * WdKit API $url | |
| 38 | - * | |
| 39 | - * @var string | |
| 40 | - */ | |
| 41 | - private static $url = WDKIT_SERVER_SITE_URL . 'api/wp/'; | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * Initiator | |
| 45 | - */ | |
| 46 | - public static function get_instance() { | |
| 47 | - if ( ! isset( self::$instance ) ) { | |
| 48 | - self::$instance = new self(); | |
| 49 | - } | |
| 50 | - return self::$instance; | |
| 51 | - } | |
| 52 | - | |
| 53 | - /** | |
| 54 | - * Define the core functionality of the plugin. | |
| 55 | - */ | |
| 56 | - public function __construct() {} | |
| 57 | - | |
| 58 | - /** | |
| 59 | - * Error JSON message | |
| 60 | - * | |
| 61 | - * @since 1.0.0 | |
| 62 | - * | |
| 63 | - * @param string $data array data. | |
| 64 | - * @param string $status message. | |
| 65 | - * */ | |
| 66 | - public function wdkit_error_msg( $data = null, $status = null ) { | |
| 67 | - wp_send_json_error( $data, $status ); | |
| 68 | - wp_die(); | |
| 69 | - } | |
| 70 | - | |
| 71 | - /** | |
| 72 | - * Success JSON message | |
| 73 | - * | |
| 74 | - * @since 1.0.0 | |
| 75 | - * | |
| 76 | - * @param string $data array data. | |
| 77 | - * @param string $status message. | |
| 78 | - * */ | |
| 79 | - public function wdkit_success_msg( $data = null, $status = null ) { | |
| 80 | - wp_send_json_success( $data, $status ); | |
| 81 | - wp_die(); | |
| 82 | - } | |
| 83 | - | |
| 84 | - /** | |
| 85 | - * Call api | |
| 86 | - * | |
| 87 | - * @since 1.0.0 | |
| 88 | - * | |
| 89 | - * @param string $type api type. | |
| 90 | - * @param string $data array. | |
| 91 | - * @param string $args array. | |
| 92 | - */ | |
| 93 | - public static function get_data( $type = '', $data = array(), $args = array() ) { | |
| 94 | - $headers = array( 'Content-Type' => 'application/json' ); | |
| 95 | - | |
| 96 | - if ( empty( $type ) ) { | |
| 97 | - return self::wdkit_error_msg( 'Something went wrong.' ); | |
| 98 | - } | |
| 99 | - | |
| 100 | - if ( isset( $args['headers'] ) && ! empty( $args['headers'] ) ) { | |
| 101 | - $headers = wp_parse_args( $args['headers'], $headers ); | |
| 102 | - | |
| 103 | - unset( $args['headers'] ); | |
| 104 | - } | |
| 105 | - | |
| 106 | - $response = wp_remote_post( | |
| 107 | - self::$url . $type, | |
| 108 | - array( | |
| 109 | - 'timeout' => 15, | |
| 110 | - 'headers' => $headers, | |
| 111 | - 'body' => wp_json_encode( $data ), | |
| 112 | - ) | |
| 113 | - ); | |
| 114 | - | |
| 115 | - return self::wdkit_check_errors( $response, $args ); | |
| 116 | - } | |
| 117 | - | |
| 118 | - /** | |
| 119 | - * Check Error and Get Response Body Data | |
| 120 | - * | |
| 121 | - * @param string $response array. | |
| 122 | - * @param string $args array. | |
| 123 | - * */ | |
| 124 | - private static function wdkit_check_errors( &$response, $args = array() ) { | |
| 125 | - if ( $response instanceof \WP_Error ) { | |
| 126 | - return $response; | |
| 127 | - } | |
| 128 | - | |
| 129 | - $res_code = wp_remote_retrieve_response_code( $response ); | |
| 130 | - $res_message = wp_remote_retrieve_response_message( $response ); | |
| 131 | - | |
| 132 | - $is_rest = isset( $args['is_rest'] ) ? $args['is_rest'] : false; | |
| 133 | - | |
| 134 | - /** | |
| 135 | - * Retrieve Body Data. | |
| 136 | - */ | |
| 137 | - $response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 138 | - | |
| 139 | - if ( $is_rest && ! $response instanceof \WP_Error && isset( $response['errors'] ) && ! empty( $response['errors'] ) ) { | |
| 140 | - if ( is_array( $response['errors'] ) ) { | |
| 141 | - $wp_error = new \WP_Error(); | |
| 142 | - array_walk( | |
| 143 | - $response['errors'], | |
| 144 | - function ( $error ) use( &$wp_error ) { | |
| 145 | - if ( isset( $error['message'] ) ) { | |
| 146 | - $wp_error->add( 'wdesignkit_graphql_error', $error['message'] ); | |
| 147 | - } | |
| 148 | - } | |
| 149 | - ); | |
| 150 | - return $wp_error; | |
| 151 | - } | |
| 152 | - } | |
| 153 | - | |
| 154 | - if ( $is_rest && isset( $args['only_data'] ) && true == $args['only_data'] ) { | |
| 155 | - if ( isset( $response['data'], $response['data'][ $args['query'] ] ) ) { | |
| 156 | - return $response['data'][ $args['query'] ]; | |
| 157 | - } | |
| 158 | - } | |
| 159 | - return $response; | |
| 160 | - } | |
| 161 | - } | |
| 162 | - | |
| 163 | - WDesignKit_Data_Query::get_instance(); | |
| 164 | -} | |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * The file that defines the core plugin class | |
| 4 | + * | |
| 5 | + * A class definition that includes attributes and functions used across both the | |
| 6 | + * public-facing side of the site and the admin area. | |
| 7 | + * | |
| 8 | + * @link https://posimyth.com/ | |
| 9 | + * @since 1.0.0 | |
| 10 | + * | |
| 11 | + * @package Wdesignkit | |
| 12 | + * @subpackage Wdesignkit/includes | |
| 13 | + */ | |
| 14 | + | |
| 15 | +/**Exit if accessed directly. */ | |
| 16 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 17 | + exit; | |
| 18 | +} | |
| 19 | + | |
| 20 | +if ( ! class_exists( 'WDesignKit_Data_Query' ) ) { | |
| 21 | + | |
| 22 | + /** | |
| 23 | + * This class call api | |
| 24 | + * | |
| 25 | + * @since 1.0.0 | |
| 26 | + */ | |
| 27 | + class WDesignKit_Data_Query { | |
| 28 | + | |
| 29 | + /** | |
| 30 | + * Member Variable | |
| 31 | + * | |
| 32 | + * @var instance | |
| 33 | + */ | |
| 34 | + private static $instance; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * WdKit API $url | |
| 38 | + * | |
| 39 | + * @var string | |
| 40 | + */ | |
| 41 | + private static $url = WDKIT_SERVER_API_URL . 'api/wp/'; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Initiator | |
| 45 | + */ | |
| 46 | + public static function get_instance() { | |
| 47 | + if ( ! isset( self::$instance ) ) { | |
| 48 | + self::$instance = new self(); | |
| 49 | + } | |
| 50 | + return self::$instance; | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Define the core functionality of the plugin. | |
| 55 | + */ | |
| 56 | + public function __construct() {} | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Error JSON message | |
| 60 | + * | |
| 61 | + * @since 1.0.0 | |
| 62 | + * | |
| 63 | + * @param string $data array data. | |
| 64 | + * @param string $status message. | |
| 65 | + * */ | |
| 66 | + public function wdkit_error_msg( $data = null, $status = null ) { | |
| 67 | + wp_send_json_error( $data, $status ); | |
| 68 | + wp_die(); | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Success JSON message | |
| 73 | + * | |
| 74 | + * @since 1.0.0 | |
| 75 | + * | |
| 76 | + * @param string $data array data. | |
| 77 | + * @param string $status message. | |
| 78 | + * */ | |
| 79 | + public function wdkit_success_msg( $data = null, $status = null ) { | |
| 80 | + wp_send_json_success( $data, $status ); | |
| 81 | + wp_die(); | |
| 82 | + } | |
| 83 | + | |
| 84 | + /** | |
| 85 | + * Call api | |
| 86 | + * | |
| 87 | + * @since 1.0.0 | |
| 88 | + * | |
| 89 | + * @param string $type api type. | |
| 90 | + * @param string $data array. | |
| 91 | + * @param string $args array. | |
| 92 | + */ | |
| 93 | + public static function get_data( $type = '', $data = array(), $args = array(), $timeout = 60 ) { | |
| 94 | + $headers = array( 'Content-Type' => 'application/json' ); | |
| 95 | + | |
| 96 | + if ( empty( $type ) ) { | |
| 97 | + return self::wdkit_error_msg( 'Something went wrong.' ); | |
| 98 | + } | |
| 99 | + | |
| 100 | + if ( isset( $args['headers'] ) && ! empty( $args['headers'] ) ) { | |
| 101 | + $headers = wp_parse_args( $args['headers'], $headers ); | |
| 102 | + | |
| 103 | + unset( $args['headers'] ); | |
| 104 | + } | |
| 105 | + | |
| 106 | + $response = wp_remote_post( | |
| 107 | + self::$url . $type, | |
| 108 | + array( | |
| 109 | + 'timeout' => $timeout, | |
| 110 | + 'headers' => $headers, | |
| 111 | + 'body' => wp_json_encode( $data ), | |
| 112 | + ) | |
| 113 | + ); | |
| 114 | + | |
| 115 | + return self::wdkit_check_errors( $response, $args ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + /** | |
| 119 | + * Check Error and Get Response Body Data | |
| 120 | + * | |
| 121 | + * @param string $response array. | |
| 122 | + * @param string $args array. | |
| 123 | + * */ | |
| 124 | + private static function wdkit_check_errors( &$response, $args = array() ) { | |
| 125 | + if ( $response instanceof \WP_Error ) { | |
| 126 | + return $response; | |
| 127 | + } | |
| 128 | + | |
| 129 | + $res_code = wp_remote_retrieve_response_code( $response ); | |
| 130 | + $res_message = wp_remote_retrieve_response_message( $response ); | |
| 131 | + | |
| 132 | + $is_rest = isset( $args['is_rest'] ) ? $args['is_rest'] : false; | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Retrieve Body Data. | |
| 136 | + */ | |
| 137 | + $response = json_decode( wp_remote_retrieve_body( $response ), true ); | |
| 138 | + | |
| 139 | + if ( $is_rest && ! $response instanceof \WP_Error && isset( $response['errors'] ) && ! empty( $response['errors'] ) ) { | |
| 140 | + if ( is_array( $response['errors'] ) ) { | |
| 141 | + $wp_error = new \WP_Error(); | |
| 142 | + array_walk( | |
| 143 | + $response['errors'], | |
| 144 | + function ( $error ) use( &$wp_error ) { | |
| 145 | + if ( isset( $error['message'] ) ) { | |
| 146 | + $wp_error->add( 'wdesignkit_graphql_error', $error['message'] ); | |
| 147 | + } | |
| 148 | + } | |
| 149 | + ); | |
| 150 | + return $wp_error; | |
| 151 | + } | |
| 152 | + } | |
| 153 | + | |
| 154 | + if ( $is_rest && isset( $args['only_data'] ) && true == $args['only_data'] ) { | |
| 155 | + if ( isset( $response['data'], $response['data'][ $args['query'] ] ) ) { | |
| 156 | + return $response['data'][ $args['query'] ]; | |
| 157 | + } | |
| 158 | + } | |
| 159 | + return $response; | |
| 160 | + } | |
| 161 | + } | |
| 162 | + | |
| 163 | + WDesignKit_Data_Query::get_instance(); | |
| 164 | +} | |