-
Notifications
You must be signed in to change notification settings - Fork 28
Beta feature: Add endpoint to retrieve custom fields of a post type. #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+153
−20
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6453b6c
Initial commit, still with errors
cbravobernal dda11f7
Default should be false
cbravobernal ff4c6b0
Remove unit to try e2e
cbravobernal 5e704c1
Add query parameter to filter by post type registration ori
priethor 9f5576e
Fix PHPCS error
priethor 4c27770
Don't require the sidebar experiment to be enabled
priethor a8e3a33
Remove the `origin` parameter functionality to do it in a separate PR
priethor aebfd8e
Cleanup
priethor 56404bb
Cleanup
priethor a7a2d8a
Undoing the beta feature admin initialization since this doesn't depe…
priethor 6c7beef
Display fields only with the experiment enabled
cbravobernal 3cc978b
Abort inside function
cbravobernal b7f5637
Update tests to underscores
cbravobernal c18cc60
Rename uninstall feature remove
cbravobernal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
/** | ||
* SCF REST Types Endpoint Extension | ||
* | ||
* @package SecureCustomFields | ||
* @subpackage REST_API | ||
*/ | ||
|
||
// Exit if accessed directly. | ||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
/** | ||
* Class SCF_Rest_Types_Endpoint | ||
* | ||
* Extends the /wp/v2/types endpoint to include SCF fields. | ||
* | ||
* @since 6.5.0 | ||
*/ | ||
class SCF_Rest_Types_Endpoint { | ||
|
||
/** | ||
* Initialize the class. | ||
* | ||
* @since 6.5.0 | ||
*/ | ||
public function __construct() { | ||
add_action( 'rest_api_init', array( $this, 'register_extra_fields' ) ); | ||
} | ||
|
||
/** | ||
* Register extra SCF fields for the post types endpoint. | ||
* | ||
* @since 6.5.0 | ||
* | ||
* @return void | ||
*/ | ||
public function register_extra_fields() { | ||
if ( ! (bool) get_option( 'scf_beta_feature_editor_sidebar_enabled', false ) ) { | ||
return; | ||
} | ||
register_rest_field( | ||
'type', | ||
'scf_field_groups', | ||
array( | ||
'get_callback' => array( $this, 'get_scf_fields' ), | ||
'schema' => $this->get_field_schema(), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Get SCF fields for a post type. | ||
* | ||
* @since 6.5.0 | ||
* | ||
* @param array $post_type_object The post type object. | ||
* @return array Array of field data. | ||
*/ | ||
public function get_scf_fields( $post_type_object ) { | ||
$post_type = $post_type_object['slug']; | ||
$field_groups = acf_get_field_groups( array( 'post_type' => $post_type ) ); | ||
$field_groups_data = array(); | ||
|
||
foreach ( $field_groups as $field_group ) { | ||
$fields = acf_get_fields( $field_group ); | ||
$group_fields = array(); | ||
|
||
foreach ( $fields as $field ) { | ||
$group_fields[] = array( | ||
'label' => $field['label'], | ||
'type' => $field['type'], | ||
); | ||
} | ||
|
||
$field_groups_data[] = array( | ||
'title' => $field_group['title'], | ||
'fields' => $group_fields, | ||
); | ||
} | ||
|
||
return $field_groups_data; | ||
} | ||
|
||
/** | ||
* Get the schema for the SCF fields. | ||
* | ||
* @since 6.5.0 | ||
* | ||
* @return array The schema for the SCF fields. | ||
*/ | ||
private function get_field_schema() { | ||
return array( | ||
'description' => 'Field groups attached to this post type.', | ||
'type' => 'array', | ||
'items' => array( | ||
'type' => 'object', | ||
'properties' => array( | ||
'title' => array( | ||
'type' => 'string', | ||
'description' => 'The field group title.', | ||
), | ||
'fields' => array( | ||
'type' => 'array', | ||
'description' => 'The fields in this field group.', | ||
'items' => array( | ||
'type' => 'object', | ||
'properties' => array( | ||
'label' => array( | ||
'type' => 'string', | ||
'description' => 'The field label.', | ||
), | ||
'type' => array( | ||
'type' => 'string', | ||
'description' => 'The field type.', | ||
), | ||
), | ||
), | ||
), | ||
), | ||
), | ||
'context' => array( 'view', 'edit', 'embed' ), | ||
); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Important note here: using 6.4.0 as we are not introducing the REST API in this PR, only fixing the missing file annotation that resulted in errors.
No action required!