Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action('init', 'expose_voxel_cpts_to_rest_api', 99);
- add_action('rest_api_init', 'register_custom_fields_in_rest_api', 15);
- function expose_voxel_cpts_to_rest_api() {
- $voxel_post_types = json_decode(get_option('voxel:post_types'), true) ?: [];
- foreach ($voxel_post_types as $post_type => $post_type_data) {
- if (!in_array($post_type, ['post', 'page'])) {
- $obj = get_post_type_object($post_type);
- if ($obj) {
- $obj->show_in_rest = true;
- $obj->rest_base = $post_type;
- $obj->rest_controller_class = 'WP_REST_Posts_Controller';
- }
- }
- }
- }
- function register_custom_fields_in_rest_api() {
- $voxel_post_types = json_decode(get_option('voxel:post_types'), true) ?: [];
- foreach ($voxel_post_types as $post_type => $post_type_data) {
- if (isset($post_type_data['fields'])) {
- foreach ($post_type_data['fields'] as $field) {
- if (in_array($field['type'], ['ui-step', 'title'])) continue;
- register_rest_field($post_type, $field['key'], [
- 'get_callback' => function($post_arr) use ($field) {
- return get_custom_field_value($post_arr['id'], $field);
- },
- 'update_callback' => function($value, $post, $field_name) use ($field) {
- return update_custom_field_value($post->ID, $field, $value);
- },
- 'schema' => [
- 'description' => $field['label'],
- 'type' => get_schema_type_for_field($field['type']),
- 'context' => ['view', 'edit'],
- ],
- ]);
- if ($field['type'] === 'post-relation') {
- $relation_field_name = get_relation_field_name($field);
- register_rest_field($post_type, $relation_field_name, [
- 'get_callback' => function($post_arr) use ($field) {
- return get_post_relation_value($post_arr['id'], $field['key']);
- },
- 'update_callback' => function($value, $post, $field_name) use ($field) {
- return update_post_relation_value($post->ID, $field['key'], $value);
- },
- 'schema' => [
- 'description' => $field['label'] . ' (FK)',
- 'type' => 'array',
- 'items' => ['type' => 'integer'],
- 'context' => ['view', 'edit'],
- ],
- ]);
- }
- }
- }
- }
- }
- function get_relation_field_name($field) {
- $related_post_type = $field['post_types'][0] ?? 'post';
- $related_post_type = ($related_post_type === 'page') ? 'pages' : $related_post_type;
- $related_post_type = ($related_post_type === 'post') ? 'posts' : $related_post_type;
- return $field['key'] . '_fk_' . $related_post_type;
- }
- function get_custom_field_value($post_id, $field) {
- $value = get_post_meta($post_id, $field['key'], true);
- switch ($field['type']) {
- case 'repeater':
- $repeater_values = [];
- if (is_array($value)) {
- foreach ($value as $index => $repeater_item) {
- $repeater_values[$index] = [];
- foreach ($field['fields'] as $sub_field) {
- $sub_key = $field['key'] . '_' . $index . '_' . $sub_field['key'];
- $repeater_values[$index][$sub_field['key']] = get_post_meta($post_id, $sub_key, true);
- }
- }
- }
- return $repeater_values;
- case 'post-relation':
- return null; // We'll handle this separately
- case 'image':
- case 'file':
- return (int) $value;
- case 'location':
- return maybe_unserialize($value);
- default:
- return $value;
- }
- }
- function update_custom_field_value($post_id, $field, $value) {
- switch ($field['type']) {
- case 'repeater':
- if (is_array($value)) {
- delete_post_meta($post_id, $field['key']);
- foreach ($value as $index => $repeater_item) {
- foreach ($field['fields'] as $sub_field) {
- if (isset($repeater_item[$sub_field['key']])) {
- $sub_key = $field['key'] . '_' . $index . '_' . $sub_field['key'];
- update_post_meta($post_id, $sub_key, $repeater_item[$sub_field['key']]);
- }
- }
- }
- update_post_meta($post_id, $field['key'], array_keys($value));
- }
- break;
- case 'post-relation':
- // We'll handle this separately
- break;
- case 'location':
- update_post_meta($post_id, $field['key'], maybe_serialize($value));
- break;
- default:
- update_post_meta($post_id, $field['key'], $value);
- }
- return true;
- }
- function get_schema_type_for_field($field_type) {
- switch ($field_type) {
- case 'number':
- case 'switcher':
- return 'number';
- case 'image':
- case 'file':
- return 'integer';
- case 'post-relation':
- case 'repeater':
- return 'array';
- case 'select':
- return ['string', 'array'];
- case 'location':
- return 'object';
- default:
- return 'string';
- }
- }
- function get_post_relation_value($post_id, $relation_key) {
- global $wpdb;
- $child_ids = $wpdb->get_col($wpdb->prepare(
- "SELECT child_id FROM {$wpdb->prefix}voxel_relations WHERE parent_id = %d AND relation_key = %s",
- $post_id,
- $relation_key
- ));
- return array_map('intval', $child_ids);
- }
- function update_post_relation_value($post_id, $relation_key, $child_ids) {
- global $wpdb;
- $wpdb->delete("{$wpdb->prefix}voxel_relations", [
- 'parent_id' => $post_id,
- 'relation_key' => $relation_key,
- ]);
- if (is_array($child_ids)) {
- foreach ($child_ids as $child_id) {
- $wpdb->insert("{$wpdb->prefix}voxel_relations", [
- 'parent_id' => $post_id,
- 'child_id' => (int) $child_id,
- 'relation_key' => $relation_key,
- ]);
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement