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