'application/json' ); if ( empty( $type ) ) { return self::wdkit_error_msg( 'Something went wrong.' ); } if ( isset( $args['headers'] ) && ! empty( $args['headers'] ) ) { $headers = wp_parse_args( $args['headers'], $headers ); unset( $args['headers'] ); } $response = wp_remote_post( self::$url . $type, array( 'timeout' => 15, 'headers' => $headers, 'body' => wp_json_encode( $data ), ) ); return self::wdkit_check_errors( $response, $args ); } /** * Check Error and Get Response Body Data * * @param string $response array. * @param string $args array. * */ private static function wdkit_check_errors( &$response, $args = array() ) { if ( $response instanceof \WP_Error ) { return $response; } $res_code = wp_remote_retrieve_response_code( $response ); $res_message = wp_remote_retrieve_response_message( $response ); $is_rest = isset( $args['is_rest'] ) ? $args['is_rest'] : false; /** * Retrieve Body Data. */ $response = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $is_rest && ! $response instanceof \WP_Error && isset( $response['errors'] ) && ! empty( $response['errors'] ) ) { if ( is_array( $response['errors'] ) ) { $wp_error = new \WP_Error(); array_walk( $response['errors'], function ( $error ) use( &$wp_error ) { if ( isset( $error['message'] ) ) { $wp_error->add( 'wdesignkit_graphql_error', $error['message'] ); } } ); return $wp_error; } } if ( $is_rest && isset( $args['only_data'] ) && true == $args['only_data'] ) { if ( isset( $response['data'], $response['data'][ $args['query'] ] ) ) { return $response['data'][ $args['query'] ]; } } return $response; } } WDesignKit_Data_Query::get_instance(); }