PluginProbe
Post Grid Gutenberg Blocks – PostX / 2.5.8
Post Grid Gutenberg Blocks – PostX v2.5.8
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 2.5.8, at classes/Caches.php

333 lines 9.5 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 /**
20 * API Endpoint
21 *
22 * @since v.1.0.0
23 */
24 private $api_endpoint = 'https://ultp.wpxpo.com/wp-json/restapi/v2/';
25
26
27 /**
28 * Setup class.
29 *
30 * @since v.1.0.0
31 */
32 public function __construct(){
33 add_action('admin_init', array($this, 'get_source_data_callback'), 10, 1);
34 add_action('rest_api_init', array($this, 'get_template_data'));
35 }
36
37
38 /**
39 * Where is Cache is Located if not found the download and create file
40 *
41 * @since v.1.0.0
42 * @return ARRAY / Exception
43 */
44 public function get_source_data_callback(){
45 $this->get_source_data('all');
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_all_templates/',
59 array(
60 array(
61 'methods' => 'POST',
62 'callback' => array( $this, 'get_all_templates_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_design/',
73 array(
74 array(
75 'methods' => 'POST',
76 'callback' => array( $this, 'get_design_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_design/',
87 array(
88 array(
89 'methods' => 'POST',
90 'callback' => array( $this, 'fetch_all_design_callback'),
91 'permission_callback' => function () {
92 return current_user_can( 'edit_posts' );
93 },
94 'args' => array()
95 )
96 )
97 );
98 }
99
100 /**
101 * ResetData from API
102 *
103 * @since v.2.4.4
104 * @param ARRAY
105 * @return ARRAY | Data of the Design
106 */
107 public function fetch_all_design_callback($request) {
108 $test = '';
109 $upload = wp_upload_dir();
110 $upload_dir = $upload['basedir'];
111 $upload_dir = $upload_dir . '/ultp';
112 if ( file_exists($upload_dir . '/template_nd_design.json') ) {
113 $test .= $upload_dir . '/template_nd_design.json';
114 wp_delete_file($upload_dir . '/template_nd_design.json');
115 }
116 if ( file_exists($upload_dir . '/design.json') ) {
117 $test .= $upload_dir . '/design.json';
118 wp_delete_file($upload_dir . '/design.json');
119 }
120 $this->get_source_data('all');
121 return array('success' => true, 'data' => __('Data Fetched!!!', 'ultimate-post'));
122 }
123
124 /**
125 * Get Design Data from API
126 *
127 * @since v.1.0.0
128 * @param ARRAY
129 * @return ARRAY | Data of the Design
130 */
131 public function get_design_callback($request){
132 try{
133 global $wp_filesystem;
134 if ( ! $wp_filesystem ) {
135 require_once( ABSPATH . 'wp-admin/includes/file.php' );
136 }
137
138 $upload_dir_url = wp_upload_dir();
139 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
140 $file_path = $dir . "design.json";
141
142 if (file_exists( $file_path )) {
143 return array( 'success' => true, 'data' => file_get_contents($file_path) );
144 } else {
145 $this->get_source_data('design');
146 }
147
148 }catch(Exception $e){
149 return [ 'success'=> false, 'message'=> $e->getMessage() ];
150 }
151 }
152
153
154 /**
155 * Get Template Data from API
156 *
157 * @since v.1.0.0
158 * @param ARRAY
159 * @return ARRAY | Data of the Design
160 */
161 public function get_all_templates_callback($request){
162 try{
163 global $wp_filesystem;
164 if ( ! $wp_filesystem ) {
165 require_once( ABSPATH . 'wp-admin/includes/file.php' );
166 }
167
168 $upload_dir_url = wp_upload_dir();
169 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
170 $file_path = $dir . "template_nd_design.json";
171
172 if (file_exists( $file_path )) {
173 return array( 'success' => true, 'data' => file_get_contents($file_path) );
174 } else {
175 $this->get_source_data('templates');
176 }
177
178 }catch(Exception $e){
179 return [ 'success'=> false, 'message'=> $e->getMessage() ];
180 }
181 }
182
183
184 /**
185 * Create a Directory in Upload Folder
186 *
187 * @since v.1.0.0
188 * @param NULL
189 * @return STRING | Directory Path
190 */
191 public function create_directory() {
192 try{
193 global $wp_filesystem;
194 if ( ! $wp_filesystem ) {
195 require_once( ABSPATH . 'wp-admin/includes/file.php' );
196 }
197 $upload_dir_url = wp_upload_dir();
198 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
199 WP_Filesystem( false, $upload_dir_url['basedir'], true );
200 if( ! $wp_filesystem->is_dir( $dir ) ) {
201 $wp_filesystem->mkdir( $dir );
202 }
203 if (!file_exists($dir . 'template_nd_design.json')) {
204 fopen( $dir . 'template_nd_design.json', "w" );
205 }
206 if (!file_exists($dir . 'design.json')) {
207 fopen( $dir . 'design.json', "w" );
208 }
209 return $dir;
210 } catch(Exception $e) {
211 return [ 'success'=> false, 'message'=> $e->getMessage() ];
212 }
213 }
214
215
216 /**
217 * Get Source Data from the file or API
218 *
219 * @since v.1.0.0
220 * @param STRING | Type (STRING)
221 * @return ARRAY | Exception Message
222 */
223 public function get_source_data($type = 'all') {
224 $caching_date = 86400*2; // 2 days cache
225 $caches = get_transient( 'ulpt_caches_'.ULTP_VER );
226 if($caches != 'updated' && $type == 'all') {
227 $this->download_source($type);
228 set_transient( 'ulpt_caches_'.ULTP_VER, 'updated', $caching_date );
229 }else{
230 if($type == 'templates' || $type == 'design'){
231 $this->download_source($type);
232 }
233 if($type == 'all'){
234 try{
235 global $wp_filesystem;
236 if ( ! $wp_filesystem ) {
237 require_once( ABSPATH . 'wp-admin/includes/file.php' );
238 }
239 $upload_dir_url = wp_upload_dir();
240 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
241 if (!file_exists( $dir . "template_nd_design.json" )) {
242 $this->download_source($type);
243 }else{
244 if (!file_exists( $dir . "design.json" )) {
245 $this->download_source($type);
246 }
247 }
248 }catch(Exception $e){
249 return [ 'success'=> false, 'message'=> $e->getMessage() ];
250 }
251 }
252 }
253 }
254
255
256 /**
257 * Download Source from the Server API
258 *
259 * @since v.1.0.0
260 * @param STRING | Type (STRING)
261 * @return ARRAY | Message from the API
262 */
263 public function download_source($type) {
264 if($type == 'all' || $type == 'templates'){
265 $response = wp_remote_post( $this->api_endpoint.'templates', array(
266 'method' => 'POST',
267 'timeout' => 120,
268 'body' => array( 'type' => 'layouts', 'design' => 'all' )
269 )
270 );
271
272 if ( is_wp_error( $response ) ) {
273 $error_message = $response->get_error_message();
274 if($type != 'all'){
275 return array( 'success' => false, 'data' => "Something went wrong: $error_message" );
276 }
277 } else {
278 $path_url = $this->create_directory();
279 file_put_contents($path_url.'template_nd_design.json', $response['body']);
280 if($type != 'all'){
281 return array( 'success' => true, 'data' => $response['body'] );
282 }
283 }
284 }
285 if($type == 'all' || $type == 'design'){
286 $response = wp_remote_post( $this->api_endpoint.'design', array(
287 'method' => 'POST',
288 'timeout' => 120
289 )
290 );
291 if ( is_wp_error( $response ) ) {
292 $error_message = $response->get_error_message();
293 if($type != 'all'){
294 return array( 'success' => false, 'data' => "Something went wrong: $error_message" );
295 }
296 } else {
297 $path_url = $this->create_directory();
298 file_put_contents($path_url.'design.json', $response['body']);
299 if($type != 'all'){
300 return array( 'success' => true, 'data' => $response['body'] );
301 }
302 }
303 }
304 }
305
306
307 /**
308 * Download Images from the Source
309 *
310 * @since v.1.0.0
311 * @param MIXED | JSON Encode (STRING), URL (STRING)
312 * @return NULL
313 */
314 public function download_images($data, $path_url) {
315 if($data){
316 $data = json_decode($data);
317 foreach ($data as $val) {
318 $response = wp_remote_get($val->image);
319 if( !is_wp_error( $response ) ) {
320 $file_name = $path_url.'/'.wp_basename($val->image);
321 if(!file_exists($file_name)){
322 $responseBody = wp_remote_retrieve_body( $response );
323 $fp = fopen($path_url.'/'.wp_basename($val->image), "w");
324 fwrite($fp, $responseBody);
325 fclose($fp);
326 }
327 }
328 }
329 }
330 }
331
332
333 }