PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 1.3.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v1.3.0
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / core / api / class-query.php

class-query.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 1.3.0, at core/api/class-query.php

142 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately;
4
5 use wp_remote_retrieve_response_code;
6 use wp_remote_retrieve_response_message;
7 use wp_remote_retrieve_body;
8 use WP_REST_Response;
9
10 class Query {
11 private static $url = 'https://app.templately.com/api/plugin';
12
13 private static function get_url(){
14 if( defined('TEMPLATELY_DEV') && TEMPLATELY_DEV ) {
15 self::$url = 'https://app.templately.dev/api/plugin';
16 }
17 return self::$url;
18 }
19
20 public static function prepare( $query, ...$args ){
21 return \sprintf( $query, ...$args );
22 }
23
24 public static function get( $query, $args = array() ){
25 $headers = array(
26 'Content-Type' => 'application/json',
27 );
28
29 if( isset( $args['headers'] ) && ! empty( $args['headers'] ) ) {
30 $headers = wp_parse_args( $args['headers'], $headers );
31 unset( $args['headers'] );
32 }
33
34 $response = wp_remote_post( self::get_url(),
35 array(
36 'headers' => $headers,
37 'body' => wp_json_encode([
38 'query' => $query
39 ])
40 )
41 );
42 return self::maybeErrors( $response, $args );
43 }
44
45 private static function maybeErrors( &$response, $args = [] ){
46 if( $response instanceof \WP_Error ) {
47 return $response; // Return WP_Error, if it is an error.
48 }
49
50 $response_code = wp_remote_retrieve_response_code($response);
51 $response_message = wp_remote_retrieve_response_message($response);
52
53 /**
54 * Retrive Data from Response Body.
55 */
56 $response = json_decode( wp_remote_retrieve_body( $response ), true );
57 /**
58 * @param mixed $args['is_rest']
59 */
60 $is_rest = isset( $args['is_rest'] ) ? $args['is_rest'] : false;
61 /**
62 * If the graphql hit with any error.
63 */
64 if( $is_rest && ! $response instanceof \WP_Error && isset( $response['errors'] ) && ! empty( $response['errors'] ) ) {
65 if( is_array( $response['errors'] ) ) {
66 $wp_error = new \WP_Error;
67 array_walk( $response['errors'], function( $error ) use( &$wp_error ) {
68 if( isset( $error['message'] ) ) {
69 $wp_error->add( 'templately_graphql_error', $error['message'] );
70 }
71 });
72 return $wp_error;
73 }
74 }
75
76 if( $is_rest && isset( $args['only_data'] ) && $args['only_data'] == true ) {
77 if( isset( $response['data'], $response['data'][ $args['query'] ] ) ) {
78 return $response['data'][ $args['query'] ];
79 }
80 }
81
82 return $response;
83 }
84
85 public static function getFromLibrary( $id ){
86 $local = new \Elementor\TemplateLibrary\Source_Local();
87 $content = $local->get_data( [
88 'template_id' => $id
89 ] );
90 return $content;
91 }
92
93 public static function remove_cloud_item( $id, $api_key, $from = 'cloud' ){
94 $query = sprintf(
95 '%s( api_key: "%s", file_id: %s){ status, data, message }}',
96 $from === 'cloud' ? 'query{ removeFromMyCloud' : 'mutation { deleteWorkspaceFile',
97 $api_key,
98 $id
99 );
100 return self::get( $query );
101 }
102
103 public static function push( $name, $file_content, $api_key, $item_type, $args = array() ){
104 $headers = array(
105 'Content-Type' => 'application/json',
106 );
107
108 if( isset( $args['headers'] ) && ! empty( $args['headers'] ) ) {
109 $headers = wp_parse_args( $args['headers'], $headers );
110 }
111 $workspace_id = false;
112 if( isset( $args['workspace_id'] ) && ! empty( $args['workspace_id'] ) ) {
113 $workspace_id = intval( $args['workspace_id'] );
114 }
115
116 $mutation = \sprintf(
117 'mutation{ pushToMyCloud( file_type: "%s", name: "%s", file_content: "%s", api_key: "%s"%s%s){ message, status, data } }',
118 $item_type,
119 $name,
120 $file_content,
121 $api_key,
122 $workspace_id !== false ? ', workspace_id: ' . $workspace_id : '',
123 isset( $args['thumbnail'] ) ? ', thumbnail: "' . $args['thumbnail'] . '"' : ''
124 );
125
126 $response = wp_remote_post( self::get_url(),
127 array(
128 'headers' => $headers,
129 'body' => wp_json_encode([
130 'query' => $mutation
131 ])
132 )
133 );
134
135 if( is_wp_error( $response ) ) {
136 return $response;
137 }
138 $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
139 return $api_response;
140 }
141 }
142