PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 1.3.3
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v1.3.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
← All changes | core/api/class-query.php +156 -0 1-final1.3.3 View file →
@@ -1,0 +1,156 @@
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 $instance = null;
14 + private static $dev_mode = false;
15 +
16 + public static function instance(){
17 + if( is_null( static::$instance ) ) {
18 + static::$instance = new static;
19 + }
20 + return static::$instance;
21 + }
22 +
23 + public function __construct(){
24 + self::$dev_mode = defined('TEMPLATELY_DEV') && TEMPLATELY_DEV;
25 + }
26 +
27 + private static function get_url(){
28 + if( self::$dev_mode ) {
29 + self::$url = 'https://app.templately.dev/api/plugin';
30 + }
31 + return self::$url;
32 + }
33 +
34 + public static function prepare( $query, ...$args ){
35 + return \sprintf( $query, ...$args );
36 + }
37 +
38 + public static function get( $query, $args = array() ){
39 + $headers = array(
40 + 'Content-Type' => 'application/json',
41 + );
42 +
43 + if( isset( $args['headers'] ) && ! empty( $args['headers'] ) ) {
44 + $headers = wp_parse_args( $args['headers'], $headers );
45 + unset( $args['headers'] );
46 + }
47 +
48 + $response = wp_remote_post( self::get_url(),
49 + array(
50 + 'timeout' => self::$dev_mode ? 40 : 15,
51 + 'headers' => $headers,
52 + 'body' => wp_json_encode([
53 + 'query' => $query
54 + ])
55 + )
56 + );
57 + return self::maybeErrors( $response, $args );
58 + }
59 +
60 + private static function maybeErrors( &$response, $args = [] ){
61 + if( $response instanceof \WP_Error ) {
62 + return $response; // Return WP_Error, if it is an error.
63 + }
64 +
65 + $response_code = wp_remote_retrieve_response_code($response);
66 + $response_message = wp_remote_retrieve_response_message($response);
67 +
68 + /**
69 + * Retrive Data from Response Body.
70 + */
71 + $response = json_decode( wp_remote_retrieve_body( $response ), true );
72 + /**
73 + * @param mixed $args['is_rest']
74 + */
75 + $is_rest = isset( $args['is_rest'] ) ? $args['is_rest'] : false;
76 + /**
77 + * If the graphql hit with any error.
78 + */
79 + if( $is_rest && ! $response instanceof \WP_Error && isset( $response['errors'] ) && ! empty( $response['errors'] ) ) {
80 + if( is_array( $response['errors'] ) ) {
81 + $wp_error = new \WP_Error;
82 + array_walk( $response['errors'], function( $error ) use( &$wp_error ) {
83 + if( isset( $error['message'] ) ) {
84 + $wp_error->add( 'templately_graphql_error', $error['message'] );
85 + }
86 + });
87 + return $wp_error;
88 + }
89 + }
90 +
91 + if( $is_rest && isset( $args['only_data'] ) && $args['only_data'] == true ) {
92 + if( isset( $response['data'], $response['data'][ $args['query'] ] ) ) {
93 + return $response['data'][ $args['query'] ];
94 + }
95 + }
96 +
97 + return $response;
98 + }
99 +
100 + public static function getFromLibrary( $id ){
101 + $local = new \Elementor\TemplateLibrary\Source_Local();
102 + $content = $local->get_data( [
103 + 'template_id' => $id
104 + ] );
105 + return $content;
106 + }
107 +
108 + public static function remove_cloud_item( $id, $api_key, $from = 'cloud' ){
109 + $query = sprintf(
110 + '%s( api_key: "%s", file_id: %s){ status, data, message }}',
111 + $from === 'cloud' ? 'query{ removeFromMyCloud' : 'mutation { deleteWorkspaceFile',
112 + $api_key,
113 + $id
114 + );
115 + return self::get( $query );
116 + }
117 +
118 + public static function push( $name, $file_content, $api_key, $item_type, $args = array() ){
119 + $headers = array(
120 + 'Content-Type' => 'application/json',
121 + );
122 +
123 + if( isset( $args['headers'] ) && ! empty( $args['headers'] ) ) {
124 + $headers = wp_parse_args( $args['headers'], $headers );
125 + }
126 + $workspace_id = false;
127 + if( isset( $args['workspace_id'] ) && ! empty( $args['workspace_id'] ) ) {
128 + $workspace_id = intval( $args['workspace_id'] );
129 + }
130 +
131 + $mutation = \sprintf(
132 + 'mutation{ pushToMyCloud( file_type: "%s", name: "%s", file_content: "%s", api_key: "%s"%s%s){ message, status, data } }',
133 + $item_type,
134 + $name,
135 + $file_content,
136 + $api_key,
137 + $workspace_id !== false ? ', workspace_id: ' . $workspace_id : '',
138 + isset( $args['thumbnail'] ) ? ', thumbnail: "' . $args['thumbnail'] . '"' : ''
139 + );
140 +
141 + $response = wp_remote_post( self::get_url(),
142 + array(
143 + 'headers' => $headers,
144 + 'body' => wp_json_encode([
145 + 'query' => $mutation
146 + ])
147 + )
148 + );
149 +
150 + if( is_wp_error( $response ) ) {
151 + return $response;
152 + }
153 + $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
154 + return $api_response;
155 + }
156 +}