| 1 |
<?php |
| 2 |
/** |
| 3 |
* Cache Designs/Templates/Starter Sites. |
| 4 |
* @package ULTP\Caches |
| 5 |
* @since v.1.0.0 |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ULTP; |
| 9 |
defined('ABSPATH') || exit; |
| 10 |
|
| 11 |
/* |
| 12 |
* Caches class. |
| 13 |
*/ |
| 14 |
class Caches { |
| 15 |
|
| 16 |
/* |
| 17 |
* Setup class. |
| 18 |
* @since v.1.0.0 |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
add_action('rest_api_init', array($this, 'caches_register_rest_route')); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* GET/UPDATE templates/starter sites based on the API action |
| 26 |
*/ |
| 27 |
public function caches_register_rest_route() { |
| 28 |
register_rest_route( |
| 29 |
'ultp/v2', |
| 30 |
'/fetch_premade_data/', |
| 31 |
array( |
| 32 |
array( |
| 33 |
'methods' => 'POST', |
| 34 |
'callback' => array( $this, 'fetch_premade_data_callback'), |
| 35 |
'permission_callback' => function () { |
| 36 |
return current_user_can( 'edit_others_posts' ); |
| 37 |
}, |
| 38 |
'args' => array() |
| 39 |
) |
| 40 |
) |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Fetch Premade Data Callback |
| 46 |
* @since 4.0.0 |
| 47 |
* @param ARRAY |
| 48 |
* @return ARRAY | Premade Data ( templates/starter sites ) |
| 49 |
*/ |
| 50 |
public function fetch_premade_data_callback($request) { |
| 51 |
$post = $request->get_params(); |
| 52 |
$type = isset($post['type']) ? ultimate_post()->ultp_rest_sanitize_params($post['type']) : ''; |
| 53 |
|
| 54 |
if ( $type == 'fetch_all_data' ) { |
| 55 |
$this->fetch_all_data_callback([]); |
| 56 |
return [ 'success'=> true, 'message'=> __('Data Fetched!!!', 'ultimate-post') ]; |
| 57 |
} else { |
| 58 |
try { |
| 59 |
$upload_dir_url = wp_upload_dir(); |
| 60 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 61 |
|
| 62 |
/* sync after 3 days */ |
| 63 |
if ( |
| 64 |
file_exists(trailingslashit(wp_upload_dir()['basedir']) . 'ultp/starter_lists.json') && |
| 65 |
time() - filemtime(trailingslashit(wp_upload_dir()['basedir']) . 'ultp/starter_lists.json') >= 2 * DAY_IN_SECONDS |
| 66 |
) { |
| 67 |
$this->fetch_all_data_callback([]); |
| 68 |
} |
| 69 |
|
| 70 |
if ( $type == 'get_starter_lists_nd_design' ) { |
| 71 |
return array( |
| 72 |
'success' => true, |
| 73 |
'success2' => is_admin(), |
| 74 |
'data' => array( |
| 75 |
"starter_lists" => file_exists( $dir . "starter_lists.json" ) ? |
| 76 |
ultimate_post()->get_path_file_contents($dir . "starter_lists.json") |
| 77 |
: |
| 78 |
$this->reset_premade_json_file('starter_lists'), |
| 79 |
"design" => file_exists( $dir . "design.json" ) ? |
| 80 |
ultimate_post()->get_path_file_contents($dir . "design.json") |
| 81 |
: |
| 82 |
$this->reset_premade_json_file('design') |
| 83 |
) |
| 84 |
); |
| 85 |
} else { |
| 86 |
$_path = $dir . $type . '.json'; |
| 87 |
return array( |
| 88 |
'success' => true, |
| 89 |
'data' => file_exists( $_path ) ? |
| 90 |
ultimate_post()->get_path_file_contents($_path) |
| 91 |
: |
| 92 |
$this->reset_premade_json_file($type) |
| 93 |
); |
| 94 |
} |
| 95 |
} catch ( \Exception $e ) { |
| 96 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* ResetData from API |
| 103 |
* |
| 104 |
* @since v.2.4.4 |
| 105 |
* @param ARRAY |
| 106 |
* @return ARRAY | Data of the Design |
| 107 |
*/ |
| 108 |
public function fetch_all_data_callback($request) { |
| 109 |
$upload = wp_upload_dir(); |
| 110 |
$upload_dir = trailingslashit($upload['basedir']) . 'ultp/'; |
| 111 |
|
| 112 |
if ( file_exists($upload_dir . '/template_nd_design.json') ) { |
| 113 |
wp_delete_file($upload_dir . '/template_nd_design.json'); |
| 114 |
} |
| 115 |
if ( file_exists($upload_dir . '/premade.json') ) { |
| 116 |
wp_delete_file($upload_dir . '/premade.json'); |
| 117 |
} |
| 118 |
if ( file_exists($upload_dir . '/design.json') ) { |
| 119 |
wp_delete_file($upload_dir . '/design.json'); |
| 120 |
} |
| 121 |
if ( file_exists($upload_dir . '/starter_lists.json') ) { |
| 122 |
wp_delete_file($upload_dir . '/starter_lists.json'); |
| 123 |
} |
| 124 |
$this->reset_premade_json_file('all'); |
| 125 |
return array('success' => true, 'message' => __('Data Fetched!!!', 'ultimate-post')); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Get and save Source Data from the file or API |
| 130 |
* @since v.1.0.0 updated from 4.0.0 |
| 131 |
* @param STRING | Type (STRING) |
| 132 |
* @return ARRAY | Exception Message |
| 133 |
*/ |
| 134 |
public function reset_premade_json_file( $type = 'all' ) { |
| 135 |
global $wp_filesystem; |
| 136 |
if (! $wp_filesystem ) { |
| 137 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 138 |
} |
| 139 |
WP_Filesystem(); |
| 140 |
|
| 141 |
$file_names = $type == 'all' ? array( 'starter_lists', 'design' ) : array( $type ); |
| 142 |
foreach ( $file_names as $key => $name ) { |
| 143 |
if ( $name == 'starter_lists' ) { |
| 144 |
$response = wp_remote_post( |
| 145 |
'https://postxkit.wpxpo.com/wp-json/importer/site_lists', |
| 146 |
array( |
| 147 |
'method' => 'POST', |
| 148 |
'timeout' => 120, |
| 149 |
'body' => array( |
| 150 |
'ultp_ver' => ULTP_VER, |
| 151 |
) |
| 152 |
) |
| 153 |
); |
| 154 |
} else { |
| 155 |
$response = wp_remote_post( |
| 156 |
'https://ultp.wpxpo.com/wp-json/restapi/v2/design', |
| 157 |
array( |
| 158 |
'method' => 'POST', |
| 159 |
'timeout' => 120 |
| 160 |
) |
| 161 |
); |
| 162 |
} |
| 163 |
if ( !is_wp_error( $response ) ) { |
| 164 |
$path_url = $this->create_directory_for_premade( $name ); |
| 165 |
$wp_filesystem->put_contents($path_url. $name.'.json', $response['body']); |
| 166 |
if ( $type != 'all' ) { |
| 167 |
return $response['body']; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Create a Directory in Upload Folder |
| 175 |
* @since v.1.0.0 updated from 4.0.0 |
| 176 |
* @param File_Name |
| 177 |
* @return STRING | Directory Path |
| 178 |
*/ |
| 179 |
public function create_directory_for_premade( $type = '' ) { |
| 180 |
try { |
| 181 |
global $wp_filesystem; |
| 182 |
if ( ! $wp_filesystem ) { |
| 183 |
require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 184 |
} |
| 185 |
$upload_dir_url = wp_upload_dir(); |
| 186 |
$dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/'; |
| 187 |
WP_Filesystem( false, $upload_dir_url['basedir'], true ); |
| 188 |
if ( ! $wp_filesystem->is_dir( $dir ) ) { |
| 189 |
$wp_filesystem->mkdir( $dir ); |
| 190 |
} |
| 191 |
if ( !file_exists($dir . $type. '.json') ) { |
| 192 |
fopen( $dir . $type. '.json', "w" ); //phpcs:ignore |
| 193 |
} |
| 194 |
return $dir; |
| 195 |
} catch ( \Exception $e ) { |
| 196 |
return [ 'success'=> false, 'message'=> $e->getMessage() ]; |
| 197 |
} |
| 198 |
} |
| 199 |
} |