| 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 |
add_action('rest_api_init', array($this, 'get_template_data')); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Get Template or Desing from the API Action |
| 38 |
* |
| 39 |
* @since v.1.0.0 |
| 40 |
* @return NULL |
| 41 |
*/ |
| 42 |
public function get_template_data(){ |
| 43 |
register_rest_route( |
| 44 |
'ultp/v2', |
| 45 |
'/get_pattern_n_template/', |
| 46 |
array( |
| 47 |
array( |
| 48 |
'methods' => 'POST', |
| 49 |
'callback' => array( $this, 'get_pattern_n_template_callback'), |
| 50 |
'permission_callback' => function () { |
| 51 |
return current_user_can( 'edit_posts' ); |
| 52 |
}, |
| 53 |
'args' => array() |
| 54 |
) |
| 55 |
) |
| 56 |
); |
| 57 |
register_rest_route( |
| 58 |
'ultp/v2', |
| 59 |
'/get_single/', |
| 60 |
array( |
| 61 |
array( |
| 62 |
'methods' => 'POST', |
| 63 |
'callback' => array( $this, 'get_single_callback'), |
| 64 |
'permission_callback' => function () { |
| 65 |
return current_user_can( 'edit_posts' ); |
| 66 |
}, |
| 67 |
'args' => array() |
| 68 |
) |
| 69 |
) |
| 70 |
); |
| 71 |
register_rest_route( |
| 72 |
'ultp/v2', |
| 73 |
'/fetch_all_data/', |
| 74 |
array( |
| 75 |
array( |
| 76 |
'methods' => 'POST', |
| 77 |
'callback' => array( $this, 'fetch_all_data_callback'), |
| 78 |
'permission_callback' => function () { |
| 79 |
return current_user_can( 'edit_posts' ); |
| 80 |
}, |
| 81 |
'args' => array() |
| 82 |
) |
| 83 |
) |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* ResetData from API |
| 89 |
* |
| 90 |
* @since v.2.4.4 |
| 91 |
* @param ARRAY |
| 92 |
* @return ARRAY | Data of the Design |
| 93 |
*/ |
| 94 |
public function fetch_all_data_callback($request) { |
| 95 |
$upload = wp_upload_dir(); |
| 96 |
$upload_dir = $upload['basedir']; |
| 97 |
$upload_dir = $upload_dir . '/ultp'; |
| 98 |
if ( file_exists($upload_dir . '/template_nd_design.json') ) { |
| 99 |
wp_delete_file($upload_dir . '/template_nd_design.json'); |
| 100 |
} |
| 101 |
if ( file_exists($upload_dir . '/design.json') ) { |
| 102 |
wp_delete_file($upload_dir . '/design.json'); |
| 103 |
} |
| 104 |
$this->get_source_data('all'); |
| 105 |
return array('success' => true, 'data' => __('Data Fetched!!!', 'ultimate-post')); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get Design Data from API |
| 110 |
* |
| 111 |
* @since v.1.0.0 |
| 112 |
* @param ARRAY |
| 113 |
* @return ARRAY | Data of the Design |
| 114 |
*/ |
| 115 |
public function get_single_callback($request){ |
| 116 |
try{ |
| 117 |
global $wp_filesystem; |
| 118 |
if ( ! $wp_filesystem ) { |
| 119 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 120 |
} |
| 121 |
|
| 122 |
$upload_dir_url = wp_upload_dir(); |
| 123 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 124 |
$file_path = $dir . "design.json"; |
| 125 |
|
| 126 |
if (file_exists( $file_path )) { |
| 127 |
return array( 'success' => true, 'data' => file_get_contents($file_path) ); |
| 128 |
} else { |
| 129 |
return array( 'success' => true, 'data' => $this->get_source_data('design') ); |
| 130 |
} |
| 131 |
|
| 132 |
}catch(Exception $e){ |
| 133 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Get Template Data from API |
| 140 |
* |
| 141 |
* @since v.1.0.0 |
| 142 |
* @param ARRAY |
| 143 |
* @return ARRAY | Data of the Design |
| 144 |
*/ |
| 145 |
public function get_pattern_n_template_callback($request){ |
| 146 |
try{ |
| 147 |
global $wp_filesystem; |
| 148 |
if ( ! $wp_filesystem ) { |
| 149 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 150 |
} |
| 151 |
|
| 152 |
$upload_dir_url = wp_upload_dir(); |
| 153 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 154 |
$file_path = $dir . "template_nd_design.json"; |
| 155 |
|
| 156 |
if (file_exists( $file_path )) { |
| 157 |
return array( 'success' => true, 'data' => file_get_contents($file_path) ); |
| 158 |
} else { |
| 159 |
return array( 'success' => true, 'data' => $this->get_source_data('templates') ); |
| 160 |
} |
| 161 |
}catch(Exception $e){ |
| 162 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* Create a Directory in Upload Folder |
| 169 |
* |
| 170 |
* @since v.1.0.0 |
| 171 |
* @param NULL |
| 172 |
* @return STRING | Directory Path |
| 173 |
*/ |
| 174 |
public function create_directory($type = 'all') { |
| 175 |
try{ |
| 176 |
global $wp_filesystem; |
| 177 |
if ( ! $wp_filesystem ) { |
| 178 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 179 |
} |
| 180 |
$upload_dir_url = wp_upload_dir(); |
| 181 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 182 |
WP_Filesystem( false, $upload_dir_url['basedir'], true ); |
| 183 |
if( ! $wp_filesystem->is_dir( $dir ) ) { |
| 184 |
$wp_filesystem->mkdir( $dir ); |
| 185 |
} |
| 186 |
if($type == 'templates' || $type == 'all'){ |
| 187 |
if (!file_exists($dir . 'template_nd_design.json')) { |
| 188 |
fopen( $dir . 'template_nd_design.json', "w" ); |
| 189 |
} |
| 190 |
} |
| 191 |
if($type == 'all' || $type == 'design'){ |
| 192 |
if (!file_exists($dir . 'design.json')) { |
| 193 |
fopen( $dir . 'design.json', "w" ); |
| 194 |
} |
| 195 |
} |
| 196 |
return $dir; |
| 197 |
} catch(Exception $e) { |
| 198 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
/** |
| 204 |
* Get Source Data from the file or API |
| 205 |
* |
| 206 |
* @since v.1.0.0 |
| 207 |
* @param STRING | Type (STRING) |
| 208 |
* @return ARRAY | Exception Message |
| 209 |
*/ |
| 210 |
public function get_source_data($type = 'all') { |
| 211 |
if($type == 'templates' || $type == 'design'){ |
| 212 |
return $this->download_source($type); |
| 213 |
} else if($type == 'all'){ |
| 214 |
try{ |
| 215 |
global $wp_filesystem; |
| 216 |
if ( ! $wp_filesystem ) { |
| 217 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 218 |
} |
| 219 |
$upload_dir_url = wp_upload_dir(); |
| 220 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 221 |
if (!file_exists( $dir . "template_nd_design.json" )) { |
| 222 |
return $this->download_source($type); |
| 223 |
} |
| 224 |
if (!file_exists( $dir . "design.json" )) { |
| 225 |
return $this->download_source($type); |
| 226 |
} |
| 227 |
}catch(Exception $e){ |
| 228 |
return false; |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* Download Source from the Server API |
| 236 |
* |
| 237 |
* @since v.1.0.0 |
| 238 |
* @param STRING | Type (STRING) |
| 239 |
* @return ARRAY | Message from the API |
| 240 |
*/ |
| 241 |
public function download_source($type) { |
| 242 |
$data = ''; |
| 243 |
if($type == 'all' || $type == 'templates'){ |
| 244 |
$response = wp_remote_post( |
| 245 |
$this->api_endpoint.'templates', |
| 246 |
array( |
| 247 |
'method' => 'POST', |
| 248 |
'timeout' => 120, |
| 249 |
'body' => array( 'type' => 'layouts', 'design' => 'all' ) |
| 250 |
) |
| 251 |
); |
| 252 |
|
| 253 |
if ( !is_wp_error( $response ) ) { |
| 254 |
$path_url = $this->create_directory($type); |
| 255 |
$data = $response['body']; |
| 256 |
file_put_contents($path_url.'template_nd_design.json', $data); |
| 257 |
} |
| 258 |
} |
| 259 |
if($type == 'all' || $type == 'design'){ |
| 260 |
$response = wp_remote_post( |
| 261 |
$this->api_endpoint.'design', |
| 262 |
array( |
| 263 |
'method' => 'POST', |
| 264 |
'timeout' => 120 |
| 265 |
) |
| 266 |
); |
| 267 |
if ( !is_wp_error( $response ) ) { |
| 268 |
$path_url = $this->create_directory($type); |
| 269 |
$data = $response['body']; |
| 270 |
file_put_contents($path_url.'design.json', $data); |
| 271 |
} |
| 272 |
} |
| 273 |
return $data; |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
/** |
| 278 |
* Download Images from the Source |
| 279 |
* |
| 280 |
* @since v.1.0.0 |
| 281 |
* @param MIXED | JSON Encode (STRING), URL (STRING) |
| 282 |
* @return NULL |
| 283 |
*/ |
| 284 |
public function download_images($data, $path_url) { |
| 285 |
if($data){ |
| 286 |
$data = json_decode($data); |
| 287 |
foreach ($data as $val) { |
| 288 |
$response = wp_remote_get($val->image); |
| 289 |
if( !is_wp_error( $response ) ) { |
| 290 |
$file_name = $path_url.'/'.wp_basename($val->image); |
| 291 |
if(!file_exists($file_name)){ |
| 292 |
$responseBody = wp_remote_retrieve_body( $response ); |
| 293 |
$fp = fopen($path_url.'/'.wp_basename($val->image), "w"); |
| 294 |
fwrite($fp, $responseBody); |
| 295 |
fclose($fp); |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
} |