PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.0.39
Post Grid Gutenberg Blocks – PostX v5.0.39
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 2.1.2 All 237 releases
ultimate-post / classes / Caches.php

Caches.php in Post Grid Gutenberg Blocks – PostX 5.0.39, at classes/Caches.php

224 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Cache Designs/Templates/Starter Sites.
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 * 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 }
30
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() {
38 register_rest_route(
39 'ultp/v2',
40 '/fetch_premade_data/',
41 array(
42 array(
43 'methods' => 'POST',
44 'callback' => array( $this, 'fetch_premade_data_callback' ),
45 'permission_callback' => function () {
46 return current_user_can( 'edit_others_posts' );
47 },
48 'args' => array(),
49 ),
50 )
51 );
52 }
53
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'] ) : '';
64
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
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 }
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 }
116
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/';
127
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 }
146
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();
160
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 }
192
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 }
224