PluginProbe
Post Grid Gutenberg Blocks – PostX / 3.1.7
Post Grid Gutenberg Blocks – PostX v3.1.7
5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 5.0.17 2.1.1 2.1.2 All 237 releases
ultimate-post / classes / Caches.php

Caches.php in Post Grid Gutenberg Blocks – PostX 3.1.7, at classes/Caches.php

360 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Cache.
4 *
5 * @package ULTP\Caches
6 * @since v.1.0.0
7 */
8
9 namespace ULTP;
10
11 defined('ABSPATH') || exit;
12
13 /**
14 * Caches class.
15 */
16 class Caches{
17
18 /**
19 * API Endpoint
20 *
21 * @since v.1.0.0
22 */
23 private $api_endpoint = 'https://ultp.wpxpo.com/wp-json/restapi/v2/';
24
25
26 /**
27 * Setup class.
28 *
29 * @since v.1.0.0
30 */
31 public function __construct(){
32 $this->check_premade_sync();
33 add_action('rest_api_init', array($this, 'get_template_data'));
34 }
35
36 /**
37 * Check Sync
38 * @return NULL
39 *
40 */
41 public function check_premade_sync() {
42 $ultp_premade_data_fetched = get_transient( 'ultp_premade_data_fetched' );
43 if($ultp_premade_data_fetched !== 'ultp_premade_data_fetched') {
44 $this->fetch_all_data_callback([]);
45 }
46 }
47
48
49 /**
50 * Get Template or Desing from the API Action
51 *
52 * @since v.1.0.0
53 * @return NULL
54 */
55 public function get_template_data() {
56 register_rest_route(
57 'ultp/v2',
58 '/get_pattern_n_template/',
59 array(
60 array(
61 'methods' => 'POST',
62 'callback' => array( $this, 'get_pattern_n_template_callback'),
63 'permission_callback' => function () {
64 return current_user_can( 'edit_posts' );
65 },
66 'args' => array()
67 )
68 )
69 );
70 register_rest_route(
71 'ultp/v2',
72 '/get_single/',
73 array(
74 array(
75 'methods' => 'POST',
76 'callback' => array( $this, 'get_single_callback'),
77 'permission_callback' => function () {
78 return current_user_can( 'edit_posts' );
79 },
80 'args' => array()
81 )
82 )
83 );
84 register_rest_route(
85 'ultp/v2',
86 '/fetch_all_data/',
87 array(
88 array(
89 'methods' => 'POST',
90 'callback' => array( $this, 'fetch_all_data_callback'),
91 'permission_callback' => function () {
92 return current_user_can( 'edit_posts' );
93 },
94 'args' => array()
95 )
96 )
97 );
98 register_rest_route(
99 'ultp/v2',
100 '/get_premade/',
101 array(
102 array(
103 'methods' => 'POST',
104 'callback' => array( $this, 'get_premade_callback'),
105 'permission_callback' => function () {
106 return current_user_can( 'edit_posts' );
107 },
108 'args' => array()
109 )
110 )
111 );
112 }
113
114
115 /**
116 * Premade Template Data
117 *
118 * @since v.2.7.0
119 * @param ARRAY
120 * @return ARRAY | Data of the Premade
121 */
122 public function get_premade_callback($request) {
123 try{
124 global $wp_filesystem;
125 if (! $wp_filesystem ) {
126 require_once( ABSPATH . 'wp-admin/includes/file.php' );
127 }
128
129 $upload_dir_url = wp_upload_dir();
130 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
131 $file_path = $dir . "premade.json";
132
133 if (file_exists( $file_path )) {
134 return array( 'success' => true, 'data' => file_get_contents($file_path) );
135 } else {
136 $this->get_source_data('premade');
137 }
138
139 }catch(Exception $e) {
140 return [ 'success'=> false, 'message'=> $e->getMessage() ];
141 }
142 }
143
144
145 /**
146 * ResetData from API
147 *
148 * @since v.2.4.4
149 * @param ARRAY
150 * @return ARRAY | Data of the Design
151 */
152 public function fetch_all_data_callback($request) {
153 set_transient( 'ultp_premade_data_fetched', 'ultp_premade_data_fetched', 432000);
154 $upload = wp_upload_dir();
155 $upload_dir = $upload['basedir'];
156 $upload_dir = $upload_dir . '/ultp';
157 if ( file_exists($upload_dir . '/template_nd_design.json') ) {
158 wp_delete_file($upload_dir . '/template_nd_design.json');
159 }
160 if ( file_exists($upload_dir . '/design.json') ) {
161 wp_delete_file($upload_dir . '/design.json');
162 }
163 if (file_exists($upload_dir . '/premade.json') ) {
164 wp_delete_file($upload_dir . '/premade.json');
165 }
166 $this->get_source_data('all');
167 return array('success' => true, 'message' => __('Data Fetched!!!', 'ultimate-post'));
168 }
169
170 /**
171 * Get Design Data from API
172 *
173 * @since v.1.0.0
174 * @param ARRAY
175 * @return ARRAY | Data of the Design
176 */
177 public function get_single_callback($request){
178 try{
179 global $wp_filesystem;
180 if (! $wp_filesystem ) {
181 require_once( ABSPATH . 'wp-admin/includes/file.php' );
182 }
183
184 $upload_dir_url = wp_upload_dir();
185 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
186 $file_path = $dir . "design.json";
187
188 if (file_exists( $file_path )) {
189 return array( 'success' => true, 'data' => file_get_contents($file_path) );
190 } else {
191 return array( 'success' => true, 'data' => $this->get_source_data('design') );
192 }
193
194 }catch(Exception $e) {
195 return [ 'success'=> false, 'message'=> $e->getMessage() ];
196 }
197 }
198
199
200 /**
201 * Get Template Data from API
202 *
203 * @since v.1.0.0
204 * @param ARRAY
205 * @return ARRAY | Data of the Design
206 */
207 public function get_pattern_n_template_callback($request){
208 try{
209 global $wp_filesystem;
210 if (! $wp_filesystem ) {
211 require_once( ABSPATH . 'wp-admin/includes/file.php' );
212 }
213
214 $upload_dir_url = wp_upload_dir();
215 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
216 $file_path = $dir . "template_nd_design.json";
217
218 if (file_exists( $file_path )) {
219 return array( 'success' => true, 'data' => file_get_contents($file_path) );
220 } else {
221 return array( 'success' => true, 'data' => $this->get_source_data('templates') );
222 }
223 }catch(Exception $e){
224 return [ 'success'=> false, 'message'=> $e->getMessage() ];
225 }
226 }
227
228
229 /**
230 * Create a Directory in Upload Folder
231 *
232 * @since v.1.0.0
233 * @param NULL
234 * @return STRING | Directory Path
235 */
236 public function create_directory($type = 'all') {
237 try{
238 global $wp_filesystem;
239 if (! $wp_filesystem ) {
240 require_once( ABSPATH . 'wp-admin/includes/file.php' );
241 }
242 $upload_dir_url = wp_upload_dir();
243 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
244 WP_Filesystem( false, $upload_dir_url['basedir'], true );
245 if (! $wp_filesystem->is_dir( $dir ) ) {
246 $wp_filesystem->mkdir( $dir );
247 }
248 if($type == 'templates' || $type == 'all'){
249 if (!file_exists($dir . 'template_nd_design.json')) {
250 fopen( $dir . 'template_nd_design.json', "w" );
251 }
252 }
253 if($type == 'all' || $type == 'design'){
254 if (!file_exists($dir . 'design.json')) {
255 fopen( $dir . 'design.json', "w" );
256 }
257 }
258 if($type == 'all' || $type == 'premade'){
259 if (!file_exists($dir . 'premade.json')) {
260 fopen( $dir . 'premade.json', "w" );
261 }
262 }
263 return $dir;
264 } catch(Exception $e) {
265 return [ 'success'=> false, 'message'=> $e->getMessage() ];
266 }
267 }
268
269
270 /**
271 * Get Source Data from the file or API
272 *
273 * @since v.1.0.0
274 * @param STRING | Type (STRING)
275 * @return ARRAY | Exception Message
276 */
277 public function get_source_data($type = 'all') {
278 if($type == 'templates' || $type == 'design' || $type == 'premade'){
279 return $this->download_source($type);
280 } else if($type == 'all'){
281 try{
282 global $wp_filesystem;
283 if ( ! $wp_filesystem ) {
284 require_once( ABSPATH . 'wp-admin/includes/file.php' );
285 }
286 $upload_dir_url = wp_upload_dir();
287 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
288 if (!file_exists( $dir . "template_nd_design.json" )) {
289 $this->download_source($type);
290 }
291 if (!file_exists( $dir . "design.json" )) {
292 $this->download_source($type);
293 }
294 if (!file_exists( $dir . "premade.json" )) {
295 $this->download_source($type);
296 }
297 }catch(Exception $e){
298 return false;
299 }
300 }
301 }
302
303
304 /**
305 * Download Source from the Server API
306 *
307 * @since v.1.0.0
308 * @param STRING | Type (STRING)
309 * @return ARRAY | Message from the API
310 */
311 public function download_source($type) {
312 $data = '';
313 if($type == 'all' || $type == 'templates'){
314 $response = wp_remote_post(
315 $this->api_endpoint.'templates',
316 array(
317 'method' => 'POST',
318 'timeout' => 120,
319 'body' => array( 'type' => 'layouts', 'design' => 'all' )
320 )
321 );
322
323 if ( !is_wp_error( $response ) ) {
324 $path_url = $this->create_directory($type);
325 $data = $response['body'];
326 file_put_contents($path_url.'template_nd_design.json', $data);
327 }
328 }
329 if($type == 'all' || $type == 'design'){
330 $response = wp_remote_post(
331 $this->api_endpoint.'design',
332 array(
333 'method' => 'POST',
334 'timeout' => 120
335 )
336 );
337 if ( !is_wp_error( $response ) ) {
338 $path_url = $this->create_directory($type);
339 $data = $response['body'];
340 file_put_contents($path_url.'design.json', $data);
341 }
342 }
343 if ($type == 'all' || $type == 'premade') {
344 $response = wp_remote_post(
345 $this->api_endpoint.'premade',
346 array(
347 'method' => 'POST',
348 'timeout' => 120
349 )
350 );
351 if ( !is_wp_error( $response ) ) {
352 $path_url = $this->create_directory($type);
353 $data = $response['body'];
354 file_put_contents($path_url.'premade.json', $data);
355 }
356 }
357 return $data;
358 }
359
360 }