| 1 |
<?php |
| 2 |
|
| 3 |
namespace Tableberg\DynamicData; |
| 4 |
|
| 5 |
use WP_REST_Server; |
| 6 |
use WP_REST_Request; |
| 7 |
use WP_REST_Response; |
| 8 |
use WP_Error; |
| 9 |
|
| 10 |
class RestController |
| 11 |
{ |
| 12 |
private $namespace = 'tableberg/v1'; |
| 13 |
|
| 14 |
private static $post_field_keys = array( |
| 15 |
'post_title', |
| 16 |
'post_excerpt', |
| 17 |
'post_content', |
| 18 |
'post_date', |
| 19 |
'post_modified', |
| 20 |
'post_author', |
| 21 |
'post_status', |
| 22 |
'permalink', |
| 23 |
'featured_image', |
| 24 |
); |
| 25 |
|
| 26 |
public function register_routes() |
| 27 |
{ |
| 28 |
register_rest_route( |
| 29 |
$this->namespace, |
| 30 |
'/dynamic-data/preview', |
| 31 |
[ |
| 32 |
'methods' => WP_REST_Server::READABLE, |
| 33 |
'callback' => [$this, 'get_preview'], |
| 34 |
'permission_callback' => [$this, 'check_edit_permission'], |
| 35 |
'args' => [ |
| 36 |
'key' => [ |
| 37 |
'required' => true, |
| 38 |
'type' => 'string', |
| 39 |
'sanitize_callback' => 'sanitize_text_field', |
| 40 |
], |
| 41 |
'postId' => [ |
| 42 |
'required' => false, |
| 43 |
'type' => 'integer', |
| 44 |
'sanitize_callback' => 'absint', |
| 45 |
], |
| 46 |
], |
| 47 |
] |
| 48 |
); |
| 49 |
|
| 50 |
register_rest_route( |
| 51 |
$this->namespace, |
| 52 |
'/dynamic-data/meta-keys', |
| 53 |
[ |
| 54 |
'methods' => WP_REST_Server::READABLE, |
| 55 |
'callback' => [$this, 'get_meta_keys'], |
| 56 |
'permission_callback' => [$this, 'check_edit_permission'], |
| 57 |
'args' => [ |
| 58 |
'postId' => [ |
| 59 |
'required' => false, |
| 60 |
'type' => 'integer', |
| 61 |
'sanitize_callback' => 'absint', |
| 62 |
], |
| 63 |
], |
| 64 |
] |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
public function check_edit_permission() |
| 69 |
{ |
| 70 |
if (!current_user_can('edit_posts')) { |
| 71 |
return new WP_Error( |
| 72 |
'rest_forbidden', |
| 73 |
__('You do not have permission to access dynamic data.', 'tableberg'), |
| 74 |
['status' => 403] |
| 75 |
); |
| 76 |
} |
| 77 |
return true; |
| 78 |
} |
| 79 |
|
| 80 |
public function get_preview(WP_REST_Request $request) |
| 81 |
{ |
| 82 |
$key = $request->get_param('key'); |
| 83 |
$post_id = $request->get_param('postId') ?: $this->get_current_post_id(); |
| 84 |
|
| 85 |
$value = null; |
| 86 |
|
| 87 |
if ($post_id) { |
| 88 |
$value = in_array($key, self::$post_field_keys, true) |
| 89 |
? $this->get_post_field_value($key, $post_id) |
| 90 |
: $this->get_post_meta_value($key, $post_id); |
| 91 |
} |
| 92 |
|
| 93 |
return new WP_REST_Response([ |
| 94 |
'success' => true, |
| 95 |
'value' => $value !== null ? (string) $value : null, |
| 96 |
]); |
| 97 |
} |
| 98 |
|
| 99 |
public function get_meta_keys(WP_REST_Request $request) |
| 100 |
{ |
| 101 |
$post_id = $request->get_param('postId') ?: $this->get_current_post_id(); |
| 102 |
|
| 103 |
if (!$post_id) { |
| 104 |
return new WP_REST_Response([ |
| 105 |
'keys' => [ |
| 106 |
['key' => '_thumbnail_id', 'label' => __('Featured Image ID', 'tableberg')], |
| 107 |
], |
| 108 |
]); |
| 109 |
} |
| 110 |
|
| 111 |
$meta = get_post_meta($post_id); |
| 112 |
$keys = []; |
| 113 |
|
| 114 |
$allowed_private = ['_thumbnail_id', '_wp_page_template']; |
| 115 |
|
| 116 |
if (is_array($meta)) { |
| 117 |
foreach (array_keys($meta) as $key) { |
| 118 |
if (strpos($key, '_') === 0 && !in_array($key, $allowed_private, true)) { |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
$keys[] = ['key' => $key]; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return new WP_REST_Response([ |
| 127 |
'keys' => $keys, |
| 128 |
]); |
| 129 |
} |
| 130 |
|
| 131 |
private function get_post_field_value(string $key, int $post_id) |
| 132 |
{ |
| 133 |
$post = get_post($post_id); |
| 134 |
|
| 135 |
if (!$post) { |
| 136 |
return null; |
| 137 |
} |
| 138 |
|
| 139 |
switch ($key) { |
| 140 |
case 'post_title': |
| 141 |
return get_the_title($post); |
| 142 |
|
| 143 |
case 'post_excerpt': |
| 144 |
return has_excerpt($post) ? get_the_excerpt($post) : null; |
| 145 |
|
| 146 |
case 'post_content': |
| 147 |
return apply_filters('the_content', $post->post_content); |
| 148 |
|
| 149 |
case 'post_date': |
| 150 |
return get_the_date('', $post); |
| 151 |
|
| 152 |
case 'post_modified': |
| 153 |
return get_the_modified_date('', $post); |
| 154 |
|
| 155 |
case 'post_author': |
| 156 |
return get_the_author_meta('display_name', $post->post_author); |
| 157 |
|
| 158 |
case 'post_status': |
| 159 |
return $post->post_status; |
| 160 |
|
| 161 |
case 'permalink': |
| 162 |
return get_permalink($post); |
| 163 |
|
| 164 |
case 'featured_image': |
| 165 |
$thumbnail_id = get_post_thumbnail_id($post); |
| 166 |
if ($thumbnail_id) { |
| 167 |
$image = wp_get_attachment_image_src($thumbnail_id, 'full'); |
| 168 |
return $image ? $image[0] : null; |
| 169 |
} |
| 170 |
return null; |
| 171 |
|
| 172 |
default: |
| 173 |
return apply_filters( |
| 174 |
'tableberg/dynamic_data/post_field_value', |
| 175 |
null, |
| 176 |
$key, |
| 177 |
$post_id, |
| 178 |
$post |
| 179 |
); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
private function get_post_meta_value(string $key, int $post_id) |
| 184 |
{ |
| 185 |
$value = get_post_meta($post_id, $key, true); |
| 186 |
|
| 187 |
if ($value === '' || $value === false) { |
| 188 |
return null; |
| 189 |
} |
| 190 |
|
| 191 |
return $value; |
| 192 |
} |
| 193 |
|
| 194 |
private function get_current_post_id() |
| 195 |
{ |
| 196 |
$post = get_post(); |
| 197 |
return $post ? $post->ID : null; |
| 198 |
} |
| 199 |
} |
| 200 |
|