PluginProbe
Post Grid Gutenberg Blocks – PostX / 2.2.5
Post Grid Gutenberg Blocks – PostX v2.2.5
5.0.41 5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 5.0.17 2.1.1 All 238 releases
← All changes | classes/Caches.php +187 -203 5.0.302.2.5 View file →
@@ -1,223 +1,207 @@
1 1 <?php
2 -/**
3 - * Cache Designs/Templates/Starter Sites.
4 - *
5 - * @package ULTP\Caches
6 - * @since v.1.0.0
7 - */
2 +namespace ULTP;
8 3
9 -namespace ULTP;
4 +defined('ABSPATH') || exit;
10 5
11 -defined( 'ABSPATH' ) || exit;
6 +class Caches{
12 7
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 - }
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 + }
30 14
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() {
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(){
38 21 register_rest_route(
39 - 'ultp/v2',
40 - '/fetch_premade_data/',
22 + 'ultp/v2',
23 + '/get_all_templates/',
41 24 array(
42 25 array(
43 - 'methods' => 'POST',
44 - 'callback' => array( $this, 'fetch_premade_data_callback' ),
26 + 'methods' => 'POST',
27 + 'callback' => array( $this, 'get_all_templates_callback'),
45 28 'permission_callback' => function () {
46 - return current_user_can( 'edit_others_posts' );
29 + return current_user_can( 'edit_posts' );
47 30 },
48 - 'args' => array(),
49 - ),
31 + 'args' => array()
32 + )
50 33 )
51 - );
52 - }
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 + }
53 50
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'] ) : '';
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 +
64 74
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/';
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 + }
75 82
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 - }
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 +
82 98
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 - }
116 99
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/';
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 + }
127 111
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 - }
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 + }
146 143
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();
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 + }
160 187
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 - }
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 +
192 206
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 -}
207 +}