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