| 1 |
<?php |
| 2 |
class PatternsWP_API_Section { |
| 3 |
|
| 4 |
public $api_url; |
| 5 |
|
| 6 |
public function __construct() { |
| 7 |
add_action('admin_init', array($this, 'register_patterns_endpoint')); |
| 8 |
|
| 9 |
// API URL |
| 10 |
$this->api_url = 'https://d1zr.thepatternswp.com/'; |
| 11 |
} |
| 12 |
|
| 13 |
public function register_patterns_endpoint() { |
| 14 |
$api_url = $this->api_url . 'wp-json/patternswps/v1/patterns'; |
| 15 |
|
| 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; |
| 23 |
} |
| 24 |
|
| 25 |
// Get the response body |
| 26 |
$response_body = wp_remote_retrieve_body($response); |
| 27 |
|
| 28 |
// Decode JSON response |
| 29 |
$patterns = json_decode($response_body, true); |
| 30 |
|
| 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 |
} |
| 36 |
|
| 37 |
// Sample usage: Output the titles of the patterns |
| 38 |
foreach ($patterns as $pattern) { |
| 39 |
|
| 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)); |
| 47 |
|
| 48 |
if (isset($pattern['categories'][0]) && !empty($pattern['categories'][0])) { |
| 49 |
unset($pattern['categories'][0]); |
| 50 |
} |
| 51 |
$keywords = isset($pattern['categories']) ? $pattern['categories'] : array(); |
| 52 |
|
| 53 |
// Register block pattern category with dynamic label |
| 54 |
register_block_pattern_category($category, array('label' => $category_label)); |
| 55 |
|
| 56 |
if (empty($keywords)) { |
| 57 |
$keywords = array($category); |
| 58 |
} |
| 59 |
|
| 60 |
$keywords = array_values($keywords); |
| 61 |
|
| 62 |
$newArray = array(); |
| 63 |
foreach ($keywords as $value) { |
| 64 |
$newArray[] = "'" . $value . "'"; |
| 65 |
} |
| 66 |
|
| 67 |
$keywords = implode(", ", $newArray); |
| 68 |
|
| 69 |
$patterns_content = wp_kses_post($patterns_content); |
| 70 |
|
| 71 |
register_block_pattern( |
| 72 |
$category_rbp . '-gutenberg-block-patterns/' . $patternsm_title, |
| 73 |
array( |
| 74 |
'title' => $patterns_title, |
| 75 |
'content' => $patterns_content, |
| 76 |
'categories' => array($category), |
| 77 |
'keywords' => array($keywords), |
| 78 |
) |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$patternswp_api_section = new PatternsWP_API_Section(); |
| 85 |
|