PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 1.2.3
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v1.2.3
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.2.3, at core/api/class-query.php

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