Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

/**
* @package REST_API_Custom_Fields
* @version 1.2
* @version 1.3
*/

/*
Plugin Name: REST API Custom Fields
Description: Enhances Wordpress REST API v2 about metadata
Author: Integromat
Author URI: https://www.integromat.com/
Version: 1.2
Version: 1.3
*/

define('IMAPIE_FIELD_PREFIX', 'integromat_api_field_');
Expand Down
8 changes: 5 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
Contributors: integromat
Tags: rest, api, rest api, endpoint, endpoints, meta, data, meta_data, integromat
Requires at least: 5.0
Tested up to: 5.3
Tested up to: 5.5
Requires PHP: 5.6
Stable tag: 1.2
Stable tag: 1.3
License: GPLv2 or later

This plugin enhances Wordpress REST API v2 responses about metadata

== Description ==
This plugin enhances Wordpress REST API v2 responses about metadata.
The metadata enhancement is available only for this wordpress core object types: post, comment, user, term

== Installation ==
1. Upload "rest-api-custom-fields" folder to the "/wp-content/plugins/" directory.
Expand All @@ -20,6 +19,9 @@ The metadata enhancement is available only for this wordpress core object types:

== Changelog ==

= 1.3 =
* It is now possible to expose custom fields of custom post types

= 1.2 =
* Exposed values can now contain arrays
* Attributes representing custom fields moved directly under the top-level node in the response
Expand Down
80 changes: 66 additions & 14 deletions response/response.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,77 @@
<?php defined( 'ABSPATH' ) or die( 'No direct access allowed' );

add_action('rest_api_init', function () {
$objectTypes = ['post', 'comment', 'user', 'term'];

foreach ($objectTypes as $objectType) {
$objectTypeMetaItems = get_option('integromat_api_options_' . $objectType);
$mainObjectTypes = ['post', 'comment', 'user', 'term'];
$metaPostTypes = getMetafieldsWithPostTypes();

foreach ($mainObjectTypes as $mainObjectType) {
$mainObjectTypeMetaItems = get_option('integromat_api_options_' . $mainObjectType);

if (!empty($mainObjectTypeMetaItems)) {
foreach ($mainObjectTypeMetaItems as $metaItemFull => $val) {

if (!empty($objectTypeMetaItems)) {
foreach ($objectTypeMetaItems as $metaItemFull => $val) {
$metaItem = str_replace(IMAPIE_FIELD_PREFIX, '', $metaItemFull);

register_rest_field(
$objectType,
$metaItem,
[
'get_callback' => function ($object, $field_name, $request) use ($objectType) {
return get_metadata($objectType, $object['id'], $field_name, true);
if ($mainObjectType == 'post') {
if (isset($metaPostTypes[$metaItem])) {
foreach ($metaPostTypes[$metaItem] as $postTypeOfTheMetafield) {
register_rest_field(
$postTypeOfTheMetafield,
$metaItem,
[
'get_callback' => function ($object, $fieldName, $request) use ($mainObjectType) {
return get_metadata($mainObjectType, $object['id'], $fieldName, true);
}
]
);
}
]
);
}
} else {
register_rest_field(
$mainObjectType,
$metaItem,
[
'get_callback' => function ($object, $fieldName, $request) use ($mainObjectType) {
return get_metadata($mainObjectType, $object['id'], $fieldName, true);
}
]
);
}


}
}
}
});
});


/**
* Gets all metafields with array of post types it is related to.
* @return array
*/
function getMetafieldsWithPostTypes()
{
global $wpdb;
$query = "
SELECT
p.post_type,
m.meta_key
FROM ". $wpdb->base_prefix ."postmeta m
INNER JOIN ". $wpdb->base_prefix ."posts p ON p.ID = m.post_id
ORDER BY
post_type,
meta_key
";
$metaItems = $wpdb->get_results($query);

$out = [];
if (!empty($metaItems)) {
foreach ($metaItems as $metaItem) {
if (!isset($out[$metaItem->meta_key]) || !in_array($metaItem->post_type, $out[$metaItem->meta_key])) {
$out[$metaItem->meta_key][] = $metaItem->post_type;
}
}
}
return $out;
}