| 1 |
<?php |
| 2 |
namespace ULTP; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
class Caches{ |
| 7 |
|
| 8 |
private $api_endpoint = 'https://ultp.wpxpo.com/wp-json/restapi/v2/'; |
| 9 |
|
| 10 |
public function __construct(){ |
| 11 |
add_action('admin_init', array($this, 'get_source_data_callback'), 10, 1); |
| 12 |
add_action('rest_api_init', array($this, 'get_template_data')); |
| 13 |
} |
| 14 |
|
| 15 |
public function get_source_data_callback(){ |
| 16 |
$this->get_source_data('all'); |
| 17 |
} |
| 18 |
|
| 19 |
// API Routes for save CSS |
| 20 |
public function get_template_data(){ |
| 21 |
register_rest_route( |
| 22 |
'ultp/v2', |
| 23 |
'/get_all_templates/', |
| 24 |
array( |
| 25 |
array( |
| 26 |
'methods' => 'POST', |
| 27 |
'callback' => array( $this, 'get_all_templates_callback'), |
| 28 |
'permission_callback' => function () { |
| 29 |
return current_user_can( 'edit_posts' ); |
| 30 |
}, |
| 31 |
'args' => array() |
| 32 |
) |
| 33 |
) |
| 34 |
); |
| 35 |
register_rest_route( |
| 36 |
'ultp/v2', |
| 37 |
'/get_design/', |
| 38 |
array( |
| 39 |
array( |
| 40 |
'methods' => 'POST', |
| 41 |
'callback' => array( $this, 'get_design_callback'), |
| 42 |
'permission_callback' => function () { |
| 43 |
return current_user_can( 'edit_posts' ); |
| 44 |
}, |
| 45 |
'args' => array() |
| 46 |
) |
| 47 |
) |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
// Get Design data callback |
| 52 |
public function get_design_callback($request){ |
| 53 |
try{ |
| 54 |
global $wp_filesystem; |
| 55 |
if ( ! $wp_filesystem ) { |
| 56 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 57 |
} |
| 58 |
|
| 59 |
$upload_dir_url = wp_upload_dir(); |
| 60 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 61 |
$file_path = $dir . "design.json"; |
| 62 |
|
| 63 |
if (file_exists( $file_path )) { |
| 64 |
return array( 'success' => true, 'data' => file_get_contents($file_path) ); |
| 65 |
} else { |
| 66 |
$this->get_source_data('design'); |
| 67 |
} |
| 68 |
|
| 69 |
}catch(Exception $e){ |
| 70 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
// Get All Template callback |
| 76 |
public function get_all_templates_callback($request){ |
| 77 |
try{ |
| 78 |
global $wp_filesystem; |
| 79 |
if ( ! $wp_filesystem ) { |
| 80 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 81 |
} |
| 82 |
|
| 83 |
$upload_dir_url = wp_upload_dir(); |
| 84 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 85 |
$file_path = $dir . "template_nd_design.json"; |
| 86 |
|
| 87 |
if (file_exists( $file_path )) { |
| 88 |
return array( 'success' => true, 'data' => file_get_contents($file_path) ); |
| 89 |
} else { |
| 90 |
$this->get_source_data('templates'); |
| 91 |
} |
| 92 |
|
| 93 |
}catch(Exception $e){ |
| 94 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public function create_directory() { |
| 101 |
$upload = wp_upload_dir(); |
| 102 |
$upload_dir = $upload['basedir']; |
| 103 |
$upload_dir = $upload_dir . '/ultp'; |
| 104 |
if ( !is_dir($upload_dir) ) { |
| 105 |
mkdir( $upload_dir, 0700 ); |
| 106 |
fopen( $upload_dir . '/template_nd_design.json', "w" ); |
| 107 |
fopen( $upload_dir . '/design.json', "w" ); |
| 108 |
} |
| 109 |
return $upload_dir; |
| 110 |
} |
| 111 |
|
| 112 |
public function get_source_data($type = 'all') { |
| 113 |
$caching_date = 86400*2; // 2 days cache |
| 114 |
$caches = get_transient( 'ulpt_caches_'.ULTP_VER ); |
| 115 |
if($caches != 'updated' && $type == 'all') { |
| 116 |
$this->download_source($type); |
| 117 |
set_transient( 'ulpt_caches_'.ULTP_VER, 'updated', $caching_date ); |
| 118 |
}else{ |
| 119 |
if($type == 'templates' || $type == 'design'){ |
| 120 |
$this->download_source($type); |
| 121 |
} |
| 122 |
if($type == 'all'){ |
| 123 |
try{ |
| 124 |
global $wp_filesystem; |
| 125 |
if ( ! $wp_filesystem ) { |
| 126 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 127 |
} |
| 128 |
$upload_dir_url = wp_upload_dir(); |
| 129 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 130 |
if (!file_exists( $dir . "template_nd_design.json" )) { |
| 131 |
$this->download_source($type); |
| 132 |
}else{ |
| 133 |
if (!file_exists( $dir . "design.json" )) { |
| 134 |
$this->download_source($type); |
| 135 |
} |
| 136 |
} |
| 137 |
}catch(Exception $e){ |
| 138 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
public function download_source($type) { |
| 145 |
if($type == 'all' || $type == 'templates'){ |
| 146 |
$response = wp_remote_post( $this->api_endpoint.'templates', array( |
| 147 |
'method' => 'POST', |
| 148 |
'timeout' => 120, |
| 149 |
'body' => array( 'type' => 'layouts', 'design' => 'all' ) |
| 150 |
) |
| 151 |
); |
| 152 |
|
| 153 |
if ( is_wp_error( $response ) ) { |
| 154 |
$error_message = $response->get_error_message(); |
| 155 |
if($type != 'all'){ |
| 156 |
return array( 'success' => false, 'data' => "Something went wrong: $error_message" ); |
| 157 |
} |
| 158 |
} else { |
| 159 |
$path_url = $this->create_directory(); |
| 160 |
file_put_contents($path_url.'/template_nd_design.json', $response['body']); |
| 161 |
if($type != 'all'){ |
| 162 |
return array( 'success' => true, 'data' => $response['body'] ); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
if($type == 'all' || $type == 'design'){ |
| 167 |
$response = wp_remote_post( $this->api_endpoint.'design', array( |
| 168 |
'method' => 'POST', |
| 169 |
'timeout' => 120 |
| 170 |
) |
| 171 |
); |
| 172 |
if ( is_wp_error( $response ) ) { |
| 173 |
$error_message = $response->get_error_message(); |
| 174 |
if($type != 'all'){ |
| 175 |
return array( 'success' => false, 'data' => "Something went wrong: $error_message" ); |
| 176 |
} |
| 177 |
} else { |
| 178 |
$path_url = $this->create_directory(); |
| 179 |
file_put_contents($path_url.'/design.json', $response['body']); |
| 180 |
if($type != 'all'){ |
| 181 |
return array( 'success' => true, 'data' => $response['body'] ); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
public function download_images($data, $path_url) { |
| 189 |
if($data){ |
| 190 |
$data = json_decode($data); |
| 191 |
foreach ($data as $val) { |
| 192 |
$response = wp_remote_get($val->image); |
| 193 |
if( !is_wp_error( $response ) ) { |
| 194 |
$file_name = $path_url.'/'.wp_basename($val->image); |
| 195 |
if(!file_exists($file_name)){ |
| 196 |
$responseBody = wp_remote_retrieve_body( $response ); |
| 197 |
$fp = fopen($path_url.'/'.wp_basename($val->image), "w"); |
| 198 |
fwrite($fp, $responseBody); |
| 199 |
fclose($fp); |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
} |