PluginProbe
Post Grid Gutenberg Blocks – PostX / 4.0.3
Post Grid Gutenberg Blocks – PostX v4.0.3
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 4.0.3, at classes/Caches.php

202 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 private $api_endpoint = 'https://ultp.wpxpo.com/wp-json/restapi/v2/';
19
20 /*
21 * Setup class.
22 * @since v.1.0.0
23 */
24 public function __construct(){
25 $this->check_premade_sync();
26 add_action('rest_api_init', array($this, 'get_template_data'));
27 }
28
29 /*
30 * Check Sync
31 * @return NULL
32 */
33 public function check_premade_sync() {
34 $ultp_premade_packs_fetched = get_transient( 'ultp_premade_packs_fetched' );
35 if($ultp_premade_packs_fetched !== 'ultp_premade_packs_fetched') {
36 $this->fetch_all_data_callback([]);
37 }
38 }
39
40 /**
41 * Get Template or Desingn from the API Action
42 *
43 * @since v.1.0.0
44 * @return NULL
45 */
46 public function get_template_data() {
47 register_rest_route(
48 'ultp/v2',
49 '/fetch_premade_data/',
50 array(
51 array(
52 'methods' => 'POST',
53 'callback' => array( $this, 'fetch_premade_data_callback'),
54 'permission_callback' => function () { return current_user_can( 'edit_posts' ); },
55 'args' => array()
56 )
57 )
58 );
59 }
60
61 /**
62 * Starter Lists data
63 * @since 4.0.0
64 * @param ARRAY
65 * @return ARRAY | Data of the starter_lists
66 */
67 public function fetch_premade_data_callback($request) {
68 $post = $request->get_params();
69 $type = isset($post['type']) ? ultimate_post()->ultp_rest_sanitize_params($post['type']) : '';
70
71 if ( $type == 'fetch_all_data' ) {
72 $this->fetch_all_data_callback([]);
73 return [ 'success'=> true, 'message'=> __('Data Fetched!!!', 'ultimate-post') ];
74 } else {
75 try {
76 global $wp_filesystem;
77 if (! $wp_filesystem ) {
78 require_once( ABSPATH . 'wp-admin/includes/file.php' );
79 }
80 WP_Filesystem();
81
82 $upload_dir_url = wp_upload_dir();
83 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
84
85 if ( $type == 'get_starter_lists_nd_design' ) {
86 return array(
87 'success' => true,
88 'data' => array(
89 "starter_lists" => file_exists( $dir . "starter_lists.json" ) ? $wp_filesystem->get_contents($dir . "starter_lists.json") : $this->reset_json_data('starter_lists'),
90 "design" => file_exists( $dir . "design.json" ) ? $wp_filesystem->get_contents($dir . "design.json") : $this->reset_json_data('design')
91 )
92 );
93 } else {
94 $_path = $dir . $type . '.json';
95 return array(
96 'success' => true,
97 'data' => file_exists( $_path ) ? $wp_filesystem->get_contents($_path) : $this->reset_json_data($type)
98 );
99 }
100 } catch ( Exception $e ) {
101 return [ 'success'=> false, 'message'=> $e->getMessage() ];
102 }
103 }
104 }
105
106 /**
107 * ResetData from API
108 *
109 * @since v.2.4.4
110 * @param ARRAY
111 * @return ARRAY | Data of the Design
112 */
113 public function fetch_all_data_callback($request) {
114 set_transient( 'ultp_premade_packs_fetched', 'ultp_premade_packs_fetched', 2 * DAY_IN_SECONDS );
115 $upload = wp_upload_dir();
116 $upload_dir = trailingslashit($upload['basedir']) . 'ultp/';
117
118 if ( file_exists($upload_dir . '/template_nd_design.json') ) {
119 wp_delete_file($upload_dir . '/template_nd_design.json');
120 }
121 if ( file_exists($upload_dir . '/premade.json') ) {
122 wp_delete_file($upload_dir . '/premade.json');
123 }
124 if ( file_exists($upload_dir . '/design.json') ) {
125 wp_delete_file($upload_dir . '/design.json');
126 }
127 if ( file_exists($upload_dir . '/starter_lists.json') ) {
128 wp_delete_file($upload_dir . '/starter_lists.json');
129 }
130 $this->reset_json_data('all');
131 return array('success' => true, 'message' => __('Data Fetched!!!', 'ultimate-post'));
132 }
133
134 /**
135 * Get and save Source Data from the file or API
136 * @since v.1.0.0 updated from 4.0.0
137 * @param STRING | Type (STRING)
138 * @return ARRAY | Exception Message
139 */
140 public function reset_json_data( $type = 'all' ) {
141 global $wp_filesystem;
142 if (! $wp_filesystem ) {
143 require_once( ABSPATH . 'wp-admin/includes/file.php' );
144 }
145 WP_Filesystem();
146
147 $file_names = $type == 'all' ? array( 'starter_lists', 'design' ) : array( $type );
148 foreach ( $file_names as $key => $name ) {
149 if ( $name == 'starter_lists' ) {
150 $response = wp_remote_get(
151 'https://postxkit.wpxpo.com/wp-json/importer/list',
152 array(
153 'method' => 'GET',
154 'timeout' => 120
155 )
156 );
157 } else {
158 $response = wp_remote_post(
159 $this->api_endpoint.'design',
160 array(
161 'method' => 'POST',
162 'timeout' => 120
163 )
164 );
165 }
166 if ( !is_wp_error( $response ) ) {
167 $path_url = $this->create_directory( $name );
168 $wp_filesystem->put_contents($path_url. $name.'.json', $response['body']);
169 if ( $type != 'all' ) {
170 return $response['body'];
171 }
172 }
173 }
174 }
175
176 /**
177 * Create a Directory in Upload Folder
178 * @since v.1.0.0 updated from 4.0.0
179 * @param File_Name
180 * @return STRING | Directory Path
181 */
182 public function create_directory( $type = '' ) {
183 try {
184 global $wp_filesystem;
185 if ( ! $wp_filesystem ) {
186 require_once( ABSPATH . 'wp-admin/includes/file.php' );
187 }
188 $upload_dir_url = wp_upload_dir();
189 $dir = trailingslashit($upload_dir_url['basedir']) . 'ultp/';
190 WP_Filesystem( false, $upload_dir_url['basedir'], true );
191 if ( ! $wp_filesystem->is_dir( $dir ) ) {
192 $wp_filesystem->mkdir( $dir );
193 }
194 if ( !file_exists($dir . $type. '.json') ) {
195 fopen( $dir . $type. '.json', "w" ); //phpcs:ignore
196 }
197 return $dir;
198 } catch ( Exception $e ) {
199 return [ 'success'=> false, 'message'=> $e->getMessage() ];
200 }
201 }
202 }