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