PluginProbe
PatternsWP – Gutenberg Block Patterns & Page Templates Library / 1.0.7
PatternsWP – Gutenberg Block Patterns & Page Templates Library v1.0.7
trunk 1.0.0 1.0.1 1.0.10 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9
patternswp / includes / class-patternswp-api.php

class-patternswp-api.php in PatternsWP – Gutenberg Block Patterns & Page Templates Library 1.0.7, at includes/class-patternswp-api.php

358 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 class PatternsWP_API_Section {
3
4 public $api_url;
5
6 /**
7 * Constructor
8 */
9 public function __construct() {
10 add_action( 'admin_init', array( $this, 'register_patterns_endpoint') );
11 add_action('patternswp_hourly_transient_load', array( $this, 'patternswp_save_transient_if_not_ajax' ) );
12
13 // API URL
14 $this->api_url = 'https://pwp4.thepatternswp.com/';
15 }
16
17 /**
18 * Register patterns endpoint
19 */
20 public function register_patterns_endpoint() {
21
22 $license_key = $licensestatus = '';
23 $chunk_size = 50;
24 $transient_expiry = DAY_IN_SECONDS;
25 $transient_base = 'patterns_cache_';
26
27 // Check for existing transients and fetch patterns if they exist
28 $cached_patterns = array();
29 for ($i = 0; $i < 50; $i++) { // Assuming a maximum of 10 transients (adjust as needed)
30 $transient_data = get_transient( $transient_base . $i );
31 if ($transient_data !== false) {
32 $cached_patterns = array_merge($cached_patterns, $transient_data);
33 } else {
34 break;
35 }
36 }
37
38 // If cached patterns are found, register patterns and categories
39 if (!empty( $cached_patterns ) ) {
40 $this->register_patterns_and_categories($cached_patterns);
41 return;
42 }else{
43
44 // Define the API URL for fetching patterns.
45 $patterns_api_url = $this->api_url . 'wp-json/patternswps_wp/v1/patterns_wp';
46 $get_lc_data = $this->get_license_data();
47 if (!empty($get_lc_data)) {
48 // Get license data
49 $license_key = $get_lc_data['license_key'];
50 $licensestatus = $get_lc_data['licensestatus'];
51 }
52
53 // Get API token
54 $api_token = $this->get_api_token();
55 // Set the request arguments including pagination parameters.
56 $request_args = array(
57 'timeout' => 10,
58 'method' => 'GET',
59 'body' => array(
60 'license_key' => $license_key,
61 'licensestatus' => $licensestatus,
62 'api_token' => $api_token,
63 ),
64 );
65
66 // Perform the GET request to fetch patterns.
67 $response = wp_remote_get($patterns_api_url, $request_args);
68
69 // Check for request errors.
70 if (is_wp_error($response)) {
71 // error_log('Failed to fetch patterns: ' . $response->get_error_message());
72 return array();
73 }
74
75 // Retrieve and decode the response body.
76 $response_body = wp_remote_retrieve_body($response);
77 $patterns = json_decode($response_body, true);
78
79 // Check if the response body was successfully decoded.
80 if (json_last_error() !== JSON_ERROR_NONE) {
81 // error_log('Failed to decode JSON response: ' . json_last_error_msg());
82 return array();
83 }
84
85 // Return empty array if patterns are not found.
86 if (empty($patterns)) {
87 // error_log('Received empty response for patterns.');
88 return array();
89 }
90
91 // Cache patterns in transients with chunking
92 $chunks = array_chunk($patterns, $chunk_size);
93 foreach ($chunks as $index => $chunk) {
94 set_transient( $transient_base . $index, $chunk, $transient_expiry );
95 }
96
97 // Register patterns and categories
98 $this->register_patterns_and_categories($patterns);
99 }
100 }
101
102 /**
103 * Get API token
104 */
105 public function get_api_token() {
106
107 $app_token = 'patternswp_app_token';
108 $timestamp = time(); // Get current timestamp
109
110 // Concatenate the value and timestamp
111 $unique_key = $app_token . $timestamp;
112
113 // Generate a hash of the concatenated value
114 $hashed_key = md5( $timestamp );
115
116 $patterns_api_token_url = $this->api_url . 'wp-json/patternswps_token/v1/token';
117 $request_args = array(
118 'timeout' => '10',
119 'method' => 'GET',
120 'body' => array(
121 'timestamp' => $timestamp,
122 'unique_key' => $hashed_key
123 )
124 );
125
126 $response = wp_remote_get( $patterns_api_token_url, $request_args );
127
128 if (is_wp_error($response)) {
129 return false;
130 }
131
132 $token = json_decode( wp_remote_retrieve_body( $response ), true );
133 return $token;
134 }
135
136 /**
137 * Get patterns category type
138 */
139 public function get_patternswp_category_type( $manually = true ) {
140 // Define the transient key and timeout.
141 $transient_key = 'patternswp_category_type';
142 $transient_timeout = HOUR_IN_SECONDS;
143
144 // Check if the transient exists.
145 $categories = get_transient($transient_key);
146
147 // If the transient does not exist, fetch the data.
148 if ( $categories === false ) {
149
150 // Fetch pattern categories from the new API.
151 $categories_api_url = $this->api_url . 'wp-json/patternswp_pattens_category_types/v1/patterns';
152 $categories_response = wp_remote_get($categories_api_url);
153 $categories_body = wp_remote_retrieve_body($categories_response);
154
155 // Decode JSON response.
156 $categories = json_decode($categories_body, true);
157
158 // If categories were successfully fetched, add them to the localized data.
159 if ( !is_wp_error( $categories_response ) && !empty( $categories ) ) {
160 if ( $manually ) {
161 foreach ( $categories as $category ) {
162 if (isset($category['name'])) {
163 register_block_pattern_category($category['name'], array('label' => $category['name']));
164 }
165 }
166 }
167
168 // Save categories data to transient for 1 hour.
169 set_transient($transient_key, $categories, $transient_timeout);
170 // } else {
171 // error_log('Failed to fetch pattern categories or received an empty response.');
172 }
173 }
174
175 return $categories;
176 }
177
178 /**
179 * Get patterns from the API
180 */
181 public function get_patternswp_pattern( $page = 1, $p_per_page = 15, $search = '', $category = '' ) {
182 $license_key = $licensestatus = '';
183
184 // Validate parameters.
185 $page = intval($page) > 0 ? intval($page) : 1;
186 $p_per_page = intval( $p_per_page ) > 0 ? intval( $p_per_page ) : 15;
187
188 // Define the API URL for fetching patterns.
189 $patterns_api_url = $this->api_url . 'wp-json/patternswps/v1/patterns';
190 $get_lc_data = $this->get_license_data();
191 if( !empty( $get_lc_data ) ){
192 //get license data
193 $license_key = $get_lc_data['license_key'];
194 $licensestatus = $get_lc_data['licensestatus'];
195 }
196
197 //Get site url OR plugin Version OR Get API Token
198 $site_url = get_site_url();
199 $plugin_version = PWP_P_VERSION;
200 $api_token = $this->get_api_token();
201
202 // Set the request arguments including pagination parameters.
203 $request_args = array(
204 'timeout' => 10,
205 'method' => 'GET',
206 'body' => array(
207 'page' => $page,
208 'patternsPerPage' => $p_per_page,
209 'search' => $search,
210 'category' => $category,
211 'site_url' => $site_url,
212 'license_key' => $license_key,
213 'licensestatus' => $licensestatus,
214 'api_token' => $api_token,
215 'plugin_version' => $plugin_version
216 ),
217 );
218
219 // Perform the GET request to fetch patterns.
220 $response = wp_remote_get( $patterns_api_url, $request_args );
221
222 // Check for request errors.
223 if (is_wp_error($response)) {
224 // error_log('Failed to fetch patterns: ' . $response->get_error_message());
225 return array();
226 }
227
228 // Retrieve and decode the response body.
229 $response_body = wp_remote_retrieve_body( $response );
230 $patterns = json_decode($response_body, true);
231
232 // Check if the response body was successfully decoded.
233 if (json_last_error() !== JSON_ERROR_NONE) {
234 // error_log('Failed to decode JSON response: ' . json_last_error_msg());
235 return array();
236 }
237
238 // Return the patterns if not empty.
239 if (!empty($patterns)) {
240 return $patterns;
241 } else {
242 // error_log('Received empty response for patterns.');
243 return array();
244 }
245 }
246
247 /**
248 * Get license data
249 */
250 public function get_license_data(){
251 $license_key = $licensestatus = '';
252 $get_license_data = get_option( 'patternswp_plugin_license_data' );
253 if( is_array( $get_license_data ) && isset( $get_license_data['license_key'] ) && !empty( $get_license_data['license_key'] ) && $get_license_data['activated'] === true ){
254 $license_key = $get_license_data['license_key']->key;
255 $licensestatus = isset( $get_license_data['activated'] ) ? $get_license_data['activated'] : false ;
256 }
257
258 return array(
259 'license_key' => $license_key,
260 'licensestatus' => $licensestatus
261 );
262 }
263
264 /**
265 * Register patterns and categories
266 */
267 private function register_patterns_and_categories( $patterns ) {
268 $registered_categories = array();
269
270 foreach ($patterns as $pattern) {
271 $categories = $pattern['categories'];
272 $pattern_title = $pattern['title'];
273 $pattern_content = $pattern['content'];
274
275 // Convert comma-separated categories to an array
276 if (is_string($categories)) {
277 $categories = array_map('trim', explode(',', $categories));
278 }
279
280 foreach ($categories as $category) {
281 // Register block pattern category if not already registered
282 if (!in_array($category, $registered_categories)) {
283 $category_label = $this->get_category_label( $category ); // Define this method to get the category label
284 register_block_pattern_category($category, array('label' => $category_label));
285 $registered_categories[] = $category;
286 }
287 }
288
289 // Register block pattern
290 register_block_pattern(
291 'patternswp-gutenberg-block-patterns/' . sanitize_title( $pattern_title ),
292 array(
293 'title' => $pattern_title,
294 'content' => $pattern_content,
295 'categories' => $categories, // This is now an array of categories
296 )
297 );
298 }
299 }
300
301 /**
302 * Get the category label
303 */
304 private function get_category_label($category) {
305 // Define how to get the category label based on your needs
306 // This is just a placeholder implementation
307 return ucfirst($category); // Example: Capitalize the category name for the label
308 }
309
310 /**
311 * Save transient if not ajax
312 */
313 public function patternswp_save_transient_if_not_ajax() {
314 $this->patternswp_save_transient_wise_category();
315 }
316
317 /**
318 * Save transient wise category
319 */
320 public function patternswp_save_transient_wise_category(){
321 $get_patternswps_all_cats = $this->get_patternswp_category_type( false );
322 $patterns_per_page = 15;
323 $search = '';
324 if ( ! empty( $get_patternswps_all_cats ) ) {
325 array_unshift( $get_patternswps_all_cats, [ 'name' => '' ] );
326 foreach ( $get_patternswps_all_cats as $patternwp_cat ) {
327 $cat_name = sanitize_text_field( $patternwp_cat['name'] );
328 if ( $cat_name === '' ) {
329 for ( $page = 1; $page <= 7; $page++ ) {
330 $cache_key = 'patternswp_' . md5($page . '-' . $patterns_per_page . '-' . $search . '-' . $cat_name);
331 $check_is_exist = get_transient( $cache_key );
332
333 if ( ! $check_is_exist ) {
334 $cache_patterns = $this->get_patternswp_pattern( $page, $patterns_per_page, $search, $cat_name );
335
336 if ( ! empty( $cache_patterns ) ) {
337 set_transient( $cache_key, $cache_patterns, 3600 );
338 }
339 }
340 }
341 } else {
342 $page = 1;
343 $cache_key = 'patternswp_' . md5($page . '-' . $patterns_per_page . '-' . $search . '-' . $cat_name);
344 $check_is_exist = get_transient( $cache_key );
345 if ( ! $check_is_exist ) {
346 $cache_patterns = $this->get_patternswp_pattern( $page, $patterns_per_page, $search, $cat_name );
347
348 if ( ! empty( $cache_patterns ) ) {
349 set_transient( $cache_key, $cache_patterns, 3600 );
350 }
351 }
352 }
353 }
354 }
355 }
356 }
357
358 $patternswp_api_section = new PatternsWP_API_Section();