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

284 lines 7.9 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 }
85
86
87 /**
88 * Get Design Data from API
89 *
90 * @since v.1.0.0
91 * @param ARRAY
92 * @return ARRAY | Data of the Design
93 */
94 public function get_design_callback($request){
95 try{
96 global $wp_filesystem;
97 if ( ! $wp_filesystem ) {
98 require_once( ABSPATH . 'wp-admin/includes/file.php' );
99 }
100
101 $upload_dir_url = wp_upload_dir();
102 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
103 $file_path = $dir . "design.json";
104
105 if (file_exists( $file_path )) {
106 return array( 'success' => true, 'data' => file_get_contents($file_path) );
107 } else {
108 $this->get_source_data('design');
109 }
110
111 }catch(Exception $e){
112 return [ 'success'=> false, 'message'=> $e->getMessage() ];
113 }
114 }
115
116
117 /**
118 * Get Template Data from API
119 *
120 * @since v.1.0.0
121 * @param ARRAY
122 * @return ARRAY | Data of the Design
123 */
124 public function get_all_templates_callback($request){
125 try{
126 global $wp_filesystem;
127 if ( ! $wp_filesystem ) {
128 require_once( ABSPATH . 'wp-admin/includes/file.php' );
129 }
130
131 $upload_dir_url = wp_upload_dir();
132 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
133 $file_path = $dir . "template_nd_design.json";
134
135 if (file_exists( $file_path )) {
136 return array( 'success' => true, 'data' => file_get_contents($file_path) );
137 } else {
138 $this->get_source_data('templates');
139 }
140
141 }catch(Exception $e){
142 return [ 'success'=> false, 'message'=> $e->getMessage() ];
143 }
144 }
145
146
147 /**
148 * Create a Directory in Upload Folder
149 *
150 * @since v.1.0.0
151 * @param NULL
152 * @return STRING | Directory Path
153 */
154 public function create_directory() {
155 $upload = wp_upload_dir();
156 $upload_dir = $upload['basedir'];
157 $upload_dir = $upload_dir . '/ultp';
158 if ( !is_dir($upload_dir) ) {
159 mkdir( $upload_dir, 0700 );
160 fopen( $upload_dir . '/template_nd_design.json', "w" );
161 fopen( $upload_dir . '/design.json', "w" );
162 }
163 return $upload_dir;
164 }
165
166
167 /**
168 * Get Source Data from the file or API
169 *
170 * @since v.1.0.0
171 * @param STRING | Type (STRING)
172 * @return ARRAY | Exception Message
173 */
174 public function get_source_data($type = 'all') {
175 $caching_date = 86400*2; // 2 days cache
176 $caches = get_transient( 'ulpt_caches_'.ULTP_VER );
177 if($caches != 'updated' && $type == 'all') {
178 $this->download_source($type);
179 set_transient( 'ulpt_caches_'.ULTP_VER, 'updated', $caching_date );
180 }else{
181 if($type == 'templates' || $type == 'design'){
182 $this->download_source($type);
183 }
184 if($type == 'all'){
185 try{
186 global $wp_filesystem;
187 if ( ! $wp_filesystem ) {
188 require_once( ABSPATH . 'wp-admin/includes/file.php' );
189 }
190 $upload_dir_url = wp_upload_dir();
191 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
192 if (!file_exists( $dir . "template_nd_design.json" )) {
193 $this->download_source($type);
194 }else{
195 if (!file_exists( $dir . "design.json" )) {
196 $this->download_source($type);
197 }
198 }
199 }catch(Exception $e){
200 return [ 'success'=> false, 'message'=> $e->getMessage() ];
201 }
202 }
203 }
204 }
205
206
207 /**
208 * Download Source from the Server API
209 *
210 * @since v.1.0.0
211 * @param STRING | Type (STRING)
212 * @return ARRAY | Message from the API
213 */
214 public function download_source($type) {
215 if($type == 'all' || $type == 'templates'){
216 $response = wp_remote_post( $this->api_endpoint.'templates', array(
217 'method' => 'POST',
218 'timeout' => 120,
219 'body' => array( 'type' => 'layouts', 'design' => 'all' )
220 )
221 );
222
223 if ( is_wp_error( $response ) ) {
224 $error_message = $response->get_error_message();
225 if($type != 'all'){
226 return array( 'success' => false, 'data' => "Something went wrong: $error_message" );
227 }
228 } else {
229 $path_url = $this->create_directory();
230 file_put_contents($path_url.'/template_nd_design.json', $response['body']);
231 if($type != 'all'){
232 return array( 'success' => true, 'data' => $response['body'] );
233 }
234 }
235 }
236 if($type == 'all' || $type == 'design'){
237 $response = wp_remote_post( $this->api_endpoint.'design', array(
238 'method' => 'POST',
239 'timeout' => 120
240 )
241 );
242 if ( is_wp_error( $response ) ) {
243 $error_message = $response->get_error_message();
244 if($type != 'all'){
245 return array( 'success' => false, 'data' => "Something went wrong: $error_message" );
246 }
247 } else {
248 $path_url = $this->create_directory();
249 file_put_contents($path_url.'/design.json', $response['body']);
250 if($type != 'all'){
251 return array( 'success' => true, 'data' => $response['body'] );
252 }
253 }
254 }
255 }
256
257
258 /**
259 * Download Images from the Source
260 *
261 * @since v.1.0.0
262 * @param MIXED | JSON Encode (STRING), URL (STRING)
263 * @return NULL
264 */
265 public function download_images($data, $path_url) {
266 if($data){
267 $data = json_decode($data);
268 foreach ($data as $val) {
269 $response = wp_remote_get($val->image);
270 if( !is_wp_error( $response ) ) {
271 $file_name = $path_url.'/'.wp_basename($val->image);
272 if(!file_exists($file_name)){
273 $responseBody = wp_remote_retrieve_body( $response );
274 $fp = fopen($path_url.'/'.wp_basename($val->image), "w");
275 fwrite($fp, $responseBody);
276 fclose($fp);
277 }
278 }
279 }
280 }
281 }
282
283
284 }