| 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 |
WP_Filesystem(); |
| 129 |
|
| 130 |
$upload_dir_url = wp_upload_dir(); |
| 131 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 132 |
$file_path = $dir . "premade.json"; |
| 133 |
|
| 134 |
if (file_exists( $file_path )) { |
| 135 |
return array( 'success' => true, 'data' => $wp_filesystem->get_contents($file_path) ); |
| 136 |
} else { |
| 137 |
$this->get_source_data('premade'); |
| 138 |
} |
| 139 |
|
| 140 |
}catch(Exception $e) { |
| 141 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* ResetData from API |
| 148 |
* |
| 149 |
* @since v.2.4.4 |
| 150 |
* @param ARRAY |
| 151 |
* @return ARRAY | Data of the Design |
| 152 |
*/ |
| 153 |
public function fetch_all_data_callback($request) { |
| 154 |
set_transient( 'ultp_premade_data_fetched', 'ultp_premade_data_fetched', 432000); |
| 155 |
$upload = wp_upload_dir(); |
| 156 |
$upload_dir = $upload['basedir']; |
| 157 |
$upload_dir = $upload_dir . '/ultp'; |
| 158 |
if ( file_exists($upload_dir . '/template_nd_design.json') ) { |
| 159 |
wp_delete_file($upload_dir . '/template_nd_design.json'); |
| 160 |
} |
| 161 |
if ( file_exists($upload_dir . '/design.json') ) { |
| 162 |
wp_delete_file($upload_dir . '/design.json'); |
| 163 |
} |
| 164 |
if (file_exists($upload_dir . '/premade.json') ) { |
| 165 |
wp_delete_file($upload_dir . '/premade.json'); |
| 166 |
} |
| 167 |
$this->get_source_data('all'); |
| 168 |
return array('success' => true, 'message' => __('Data Fetched!!!', 'ultimate-post')); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Get Design Data from API |
| 173 |
* |
| 174 |
* @since v.1.0.0 |
| 175 |
* @param ARRAY |
| 176 |
* @return ARRAY | Data of the Design |
| 177 |
*/ |
| 178 |
public function get_single_callback($request){ |
| 179 |
try{ |
| 180 |
global $wp_filesystem; |
| 181 |
if (! $wp_filesystem ) { |
| 182 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 183 |
} |
| 184 |
WP_Filesystem(); |
| 185 |
|
| 186 |
$upload_dir_url = wp_upload_dir(); |
| 187 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 188 |
$file_path = $dir . "design.json"; |
| 189 |
|
| 190 |
if (file_exists( $file_path )) { |
| 191 |
return array( 'success' => true, 'data' =>$wp_filesystem->get_contents($file_path) ); |
| 192 |
} else { |
| 193 |
return array( 'success' => true, 'data' => $this->get_source_data('design') ); |
| 194 |
} |
| 195 |
|
| 196 |
}catch(Exception $e) { |
| 197 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
/** |
| 203 |
* Get Template Data from API |
| 204 |
* |
| 205 |
* @since v.1.0.0 |
| 206 |
* @param ARRAY |
| 207 |
* @return ARRAY | Data of the Design |
| 208 |
*/ |
| 209 |
public function get_pattern_n_template_callback($request){ |
| 210 |
try{ |
| 211 |
global $wp_filesystem; |
| 212 |
if (! $wp_filesystem ) { |
| 213 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 214 |
} |
| 215 |
WP_Filesystem(); |
| 216 |
|
| 217 |
$upload_dir_url = wp_upload_dir(); |
| 218 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 219 |
$file_path = $dir . "template_nd_design.json"; |
| 220 |
|
| 221 |
if (file_exists( $file_path )) { |
| 222 |
return array( 'success' => true, 'data' => $wp_filesystem->get_contents($file_path) ); |
| 223 |
} else { |
| 224 |
return array( 'success' => true, 'data' => $this->get_source_data('templates') ); |
| 225 |
} |
| 226 |
}catch(Exception $e){ |
| 227 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
/** |
| 233 |
* Create a Directory in Upload Folder |
| 234 |
* |
| 235 |
* @since v.1.0.0 |
| 236 |
* @param NULL |
| 237 |
* @return STRING | Directory Path |
| 238 |
*/ |
| 239 |
public function create_directory($type = 'all') { |
| 240 |
try{ |
| 241 |
global $wp_filesystem; |
| 242 |
if (! $wp_filesystem ) { |
| 243 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 244 |
} |
| 245 |
$upload_dir_url = wp_upload_dir(); |
| 246 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 247 |
WP_Filesystem( false, $upload_dir_url['basedir'], true ); |
| 248 |
if (! $wp_filesystem->is_dir( $dir ) ) { |
| 249 |
$wp_filesystem->mkdir( $dir ); |
| 250 |
} |
| 251 |
if($type == 'templates' || $type == 'all'){ |
| 252 |
if (!file_exists($dir . 'template_nd_design.json')) { |
| 253 |
$wp_filesystem->put_contents($dir . 'template_nd_design.json', '', FS_CHMOD_FILE); |
| 254 |
} |
| 255 |
} |
| 256 |
if($type == 'all' || $type == 'design'){ |
| 257 |
if (!file_exists($dir . 'design.json')) { |
| 258 |
$wp_filesystem->put_contents($dir . 'design.json', '', FS_CHMOD_FILE); |
| 259 |
} |
| 260 |
} |
| 261 |
if($type == 'all' || $type == 'premade'){ |
| 262 |
if (!file_exists($dir . 'premade.json')) { |
| 263 |
$wp_filesystem->put_contents($dir . 'premade.json', '', FS_CHMOD_FILE); |
| 264 |
} |
| 265 |
} |
| 266 |
return $dir; |
| 267 |
} catch(Exception $e) { |
| 268 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
/** |
| 274 |
* Get Source Data from the file or API |
| 275 |
* |
| 276 |
* @since v.1.0.0 |
| 277 |
* @param STRING | Type (STRING) |
| 278 |
* @return ARRAY | Exception Message |
| 279 |
*/ |
| 280 |
public function get_source_data($type = 'all') { |
| 281 |
if($type == 'templates' || $type == 'design' || $type == 'premade'){ |
| 282 |
return $this->download_source($type); |
| 283 |
} else if($type == 'all'){ |
| 284 |
try{ |
| 285 |
global $wp_filesystem; |
| 286 |
if ( ! $wp_filesystem ) { |
| 287 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 288 |
} |
| 289 |
$upload_dir_url = wp_upload_dir(); |
| 290 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 291 |
if (!file_exists( $dir . "template_nd_design.json" )) { |
| 292 |
$this->download_source($type); |
| 293 |
} |
| 294 |
if (!file_exists( $dir . "design.json" )) { |
| 295 |
$this->download_source($type); |
| 296 |
} |
| 297 |
if (!file_exists( $dir . "premade.json" )) { |
| 298 |
$this->download_source($type); |
| 299 |
} |
| 300 |
}catch(Exception $e){ |
| 301 |
return false; |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
/** |
| 308 |
* Download Source from the Server API |
| 309 |
* |
| 310 |
* @since v.1.0.0 |
| 311 |
* @param STRING | Type (STRING) |
| 312 |
* @return ARRAY | Message from the API |
| 313 |
*/ |
| 314 |
public function download_source($type) { |
| 315 |
$data = ''; |
| 316 |
if($type == 'all' || $type == 'templates'){ |
| 317 |
$response = wp_remote_post( |
| 318 |
$this->api_endpoint.'templates', |
| 319 |
array( |
| 320 |
'method' => 'POST', |
| 321 |
'timeout' => 120, |
| 322 |
'body' => array( 'type' => 'layouts', 'design' => 'all' ) |
| 323 |
) |
| 324 |
); |
| 325 |
|
| 326 |
if ( !is_wp_error( $response ) ) { |
| 327 |
$path_url = $this->create_directory($type); |
| 328 |
$data = $response['body']; |
| 329 |
global $wp_filesystem; |
| 330 |
if (! $wp_filesystem ) { |
| 331 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 332 |
} |
| 333 |
WP_Filesystem(); |
| 334 |
$wp_filesystem->put_contents($path_url.'template_nd_design.json', $data); |
| 335 |
} |
| 336 |
} |
| 337 |
if($type == 'all' || $type == 'design'){ |
| 338 |
$response = wp_remote_post( |
| 339 |
$this->api_endpoint.'design', |
| 340 |
array( |
| 341 |
'method' => 'POST', |
| 342 |
'timeout' => 120 |
| 343 |
) |
| 344 |
); |
| 345 |
if ( !is_wp_error( $response ) ) { |
| 346 |
$path_url = $this->create_directory($type); |
| 347 |
$data = $response['body']; |
| 348 |
global $wp_filesystem; |
| 349 |
if (! $wp_filesystem ) { |
| 350 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 351 |
} |
| 352 |
WP_Filesystem(); |
| 353 |
$wp_filesystem->put_contents($path_url.'design.json', $data); |
| 354 |
} |
| 355 |
} |
| 356 |
if ($type == 'all' || $type == 'premade') { |
| 357 |
$response = wp_remote_post( |
| 358 |
$this->api_endpoint.'premade', |
| 359 |
array( |
| 360 |
'method' => 'POST', |
| 361 |
'timeout' => 120 |
| 362 |
) |
| 363 |
); |
| 364 |
if ( !is_wp_error( $response ) ) { |
| 365 |
$path_url = $this->create_directory($type); |
| 366 |
$data = $response['body']; |
| 367 |
global $wp_filesystem; |
| 368 |
if (! $wp_filesystem ) { |
| 369 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 370 |
} |
| 371 |
WP_Filesystem(); |
| 372 |
$wp_filesystem->put_contents($path_url.'premade.json', $data); |
| 373 |
} |
| 374 |
} |
| 375 |
return $data; |
| 376 |
} |
| 377 |
|
| 378 |
} |