| 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 |
register_rest_route( |
| 86 |
'ultp/v2', |
| 87 |
'/get_premade/', |
| 88 |
array( |
| 89 |
array( |
| 90 |
'methods' => 'POST', |
| 91 |
'callback' => array( $this, 'get_premade_callback'), |
| 92 |
'permission_callback' => function () { |
| 93 |
return current_user_can( 'edit_posts' ); |
| 94 |
}, |
| 95 |
'args' => array() |
| 96 |
) |
| 97 |
) |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
/** |
| 103 |
* Premade Template Data |
| 104 |
* |
| 105 |
* @since v.2.7.0 |
| 106 |
* @param ARRAY |
| 107 |
* @return ARRAY | Data of the Premade |
| 108 |
*/ |
| 109 |
public function get_premade_callback($request) { |
| 110 |
try{ |
| 111 |
global $wp_filesystem; |
| 112 |
if (! $wp_filesystem ) { |
| 113 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 114 |
} |
| 115 |
|
| 116 |
$upload_dir_url = wp_upload_dir(); |
| 117 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 118 |
$file_path = $dir . "premade.json"; |
| 119 |
|
| 120 |
if (file_exists( $file_path )) { |
| 121 |
return array( 'success' => true, 'data' => file_get_contents($file_path) ); |
| 122 |
} else { |
| 123 |
$this->get_source_data('premade'); |
| 124 |
} |
| 125 |
|
| 126 |
}catch(Exception $e) { |
| 127 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/** |
| 133 |
* ResetData from API |
| 134 |
* |
| 135 |
* @since v.2.4.4 |
| 136 |
* @param ARRAY |
| 137 |
* @return ARRAY | Data of the Design |
| 138 |
*/ |
| 139 |
public function fetch_all_data_callback($request) { |
| 140 |
$upload = wp_upload_dir(); |
| 141 |
$upload_dir = $upload['basedir']; |
| 142 |
$upload_dir = $upload_dir . '/ultp'; |
| 143 |
if ( file_exists($upload_dir . '/template_nd_design.json') ) { |
| 144 |
wp_delete_file($upload_dir . '/template_nd_design.json'); |
| 145 |
} |
| 146 |
if ( file_exists($upload_dir . '/design.json') ) { |
| 147 |
wp_delete_file($upload_dir . '/design.json'); |
| 148 |
} |
| 149 |
if (file_exists($upload_dir . '/premade.json') ) { |
| 150 |
wp_delete_file($upload_dir . '/premade.json'); |
| 151 |
} |
| 152 |
$this->get_source_data('all'); |
| 153 |
return array('success' => true, 'data' => __('Data Fetched!!!', 'ultimate-post')); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Get Design Data from API |
| 158 |
* |
| 159 |
* @since v.1.0.0 |
| 160 |
* @param ARRAY |
| 161 |
* @return ARRAY | Data of the Design |
| 162 |
*/ |
| 163 |
public function get_single_callback($request){ |
| 164 |
try{ |
| 165 |
global $wp_filesystem; |
| 166 |
if (! $wp_filesystem ) { |
| 167 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 168 |
} |
| 169 |
|
| 170 |
$upload_dir_url = wp_upload_dir(); |
| 171 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 172 |
$file_path = $dir . "design.json"; |
| 173 |
|
| 174 |
if (file_exists( $file_path )) { |
| 175 |
return array( 'success' => true, 'data' => file_get_contents($file_path) ); |
| 176 |
} else { |
| 177 |
return array( 'success' => true, 'data' => $this->get_source_data('design') ); |
| 178 |
} |
| 179 |
|
| 180 |
}catch(Exception $e) { |
| 181 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
/** |
| 187 |
* Get Template Data from API |
| 188 |
* |
| 189 |
* @since v.1.0.0 |
| 190 |
* @param ARRAY |
| 191 |
* @return ARRAY | Data of the Design |
| 192 |
*/ |
| 193 |
public function get_pattern_n_template_callback($request){ |
| 194 |
try{ |
| 195 |
global $wp_filesystem; |
| 196 |
if (! $wp_filesystem ) { |
| 197 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 198 |
} |
| 199 |
|
| 200 |
$upload_dir_url = wp_upload_dir(); |
| 201 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 202 |
$file_path = $dir . "template_nd_design.json"; |
| 203 |
|
| 204 |
if (file_exists( $file_path )) { |
| 205 |
return array( 'success' => true, 'data' => file_get_contents($file_path) ); |
| 206 |
} else { |
| 207 |
return array( 'success' => true, 'data' => $this->get_source_data('templates') ); |
| 208 |
} |
| 209 |
}catch(Exception $e){ |
| 210 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
/** |
| 216 |
* Create a Directory in Upload Folder |
| 217 |
* |
| 218 |
* @since v.1.0.0 |
| 219 |
* @param NULL |
| 220 |
* @return STRING | Directory Path |
| 221 |
*/ |
| 222 |
public function create_directory($type = 'all') { |
| 223 |
try{ |
| 224 |
global $wp_filesystem; |
| 225 |
if (! $wp_filesystem ) { |
| 226 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 227 |
} |
| 228 |
$upload_dir_url = wp_upload_dir(); |
| 229 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 230 |
WP_Filesystem( false, $upload_dir_url['basedir'], true ); |
| 231 |
if (! $wp_filesystem->is_dir( $dir ) ) { |
| 232 |
$wp_filesystem->mkdir( $dir ); |
| 233 |
} |
| 234 |
if($type == 'templates' || $type == 'all'){ |
| 235 |
if (!file_exists($dir . 'template_nd_design.json')) { |
| 236 |
fopen( $dir . 'template_nd_design.json', "w" ); |
| 237 |
} |
| 238 |
} |
| 239 |
if($type == 'all' || $type == 'design'){ |
| 240 |
if (!file_exists($dir . 'design.json')) { |
| 241 |
fopen( $dir . 'design.json', "w" ); |
| 242 |
} |
| 243 |
} |
| 244 |
if($type == 'all' || $type == 'premade'){ |
| 245 |
if (!file_exists($dir . 'premade.json')) { |
| 246 |
fopen( $dir . 'premade.json', "w" ); |
| 247 |
} |
| 248 |
} |
| 249 |
return $dir; |
| 250 |
} catch(Exception $e) { |
| 251 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
/** |
| 257 |
* Get Source Data from the file or API |
| 258 |
* |
| 259 |
* @since v.1.0.0 |
| 260 |
* @param STRING | Type (STRING) |
| 261 |
* @return ARRAY | Exception Message |
| 262 |
*/ |
| 263 |
public function get_source_data($type = 'all') { |
| 264 |
if($type == 'templates' || $type == 'design' || $type == 'premade'){ |
| 265 |
return $this->download_source($type); |
| 266 |
} else if($type == 'all'){ |
| 267 |
try{ |
| 268 |
global $wp_filesystem; |
| 269 |
if ( ! $wp_filesystem ) { |
| 270 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 271 |
} |
| 272 |
$upload_dir_url = wp_upload_dir(); |
| 273 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 274 |
if (!file_exists( $dir . "template_nd_design.json" )) { |
| 275 |
$this->download_source($type); |
| 276 |
} |
| 277 |
if (!file_exists( $dir . "design.json" )) { |
| 278 |
$this->download_source($type); |
| 279 |
} |
| 280 |
if (!file_exists( $dir . "premade.json" )) { |
| 281 |
$this->download_source($type); |
| 282 |
} |
| 283 |
}catch(Exception $e){ |
| 284 |
return false; |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
/** |
| 291 |
* Download Source from the Server API |
| 292 |
* |
| 293 |
* @since v.1.0.0 |
| 294 |
* @param STRING | Type (STRING) |
| 295 |
* @return ARRAY | Message from the API |
| 296 |
*/ |
| 297 |
public function download_source($type) { |
| 298 |
$data = ''; |
| 299 |
if($type == 'all' || $type == 'templates'){ |
| 300 |
$response = wp_remote_post( |
| 301 |
$this->api_endpoint.'templates', |
| 302 |
array( |
| 303 |
'method' => 'POST', |
| 304 |
'timeout' => 120, |
| 305 |
'body' => array( 'type' => 'layouts', 'design' => 'all' ) |
| 306 |
) |
| 307 |
); |
| 308 |
|
| 309 |
if ( !is_wp_error( $response ) ) { |
| 310 |
$path_url = $this->create_directory($type); |
| 311 |
$data = $response['body']; |
| 312 |
file_put_contents($path_url.'template_nd_design.json', $data); |
| 313 |
} |
| 314 |
} |
| 315 |
if($type == 'all' || $type == 'design'){ |
| 316 |
$response = wp_remote_post( |
| 317 |
$this->api_endpoint.'design', |
| 318 |
array( |
| 319 |
'method' => 'POST', |
| 320 |
'timeout' => 120 |
| 321 |
) |
| 322 |
); |
| 323 |
if ( !is_wp_error( $response ) ) { |
| 324 |
$path_url = $this->create_directory($type); |
| 325 |
$data = $response['body']; |
| 326 |
file_put_contents($path_url.'design.json', $data); |
| 327 |
} |
| 328 |
} |
| 329 |
if ($type == 'all' || $type == 'premade') { |
| 330 |
$response = wp_remote_post( |
| 331 |
$this->api_endpoint.'premade', |
| 332 |
array( |
| 333 |
'method' => 'POST', |
| 334 |
'timeout' => 120 |
| 335 |
) |
| 336 |
); |
| 337 |
if ( !is_wp_error( $response ) ) { |
| 338 |
$path_url = $this->create_directory($type); |
| 339 |
$data = $response['body']; |
| 340 |
file_put_contents($path_url.'premade.json', $data); |
| 341 |
} |
| 342 |
} |
| 343 |
return $data; |
| 344 |
} |
| 345 |
|
| 346 |
} |