Skip to content

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
merged 14 commits into from
May 5, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SCF_Admin_Beta_Feature_Editor_Sidebar extends SCF_Admin_Beta_Feature {
* @return void
*/
protected function initialize() {
$this->name = 'editor-sidebar';
$this->name = 'editor_sidebar';
$this->title = __( 'Move Elements to Editor Sidebar', 'secure-custom-fields' );
$this->description = __( 'Moves field group elements to the editor sidebar for a cleaner interface.', 'secure-custom-fields' );

Expand Down
9 changes: 8 additions & 1 deletion includes/rest-api.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<?php
/**
* REST API
*
* @package Secure Custom Fields
* @since 6.4.0
Copy link
Contributor

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!

*/

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
Expand All @@ -9,6 +15,7 @@
acf_include( 'includes/rest-api/class-acf-rest-api.php' );
acf_include( 'includes/rest-api/class-acf-rest-embed-links.php' );
acf_include( 'includes/rest-api/class-acf-rest-request.php' );
acf_include( 'includes/rest-api/class-acf-rest-types-endpoint.php' );

// Initialize.
acf_new_instance( 'ACF_Rest_Api' );
acf_new_instance( 'SCF_Rest_Types_Endpoint' );
126 changes: 126 additions & 0 deletions includes/rest-api/class-acf-rest-types-endpoint.php
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' ),
);
}
}
2 changes: 1 addition & 1 deletion secure-custom-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,7 @@ function scf_plugin_deactivated_notice() {
function scf_plugin_uninstall() {
// List of known beta features.
$beta_features = array(
'editor-sidebar',
'editor_sidebar',
);

foreach ( $beta_features as $beta_feature ) {
Expand Down
34 changes: 17 additions & 17 deletions tests/php/test-admin-beta-features.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function set_up() {
* Clean up after each test.
*/
public function tear_down() {
delete_option( 'scf_beta_feature_editor-sidebar_enabled' );
delete_option( 'scf_beta_feature_editor_sidebar_enabled' );
parent::tear_down();
}

Expand All @@ -47,8 +47,8 @@ public function tear_down() {
public function test_register_beta_feature() {
$this->beta_features->register_beta_feature( 'SCF_Admin_Beta_Feature_Editor_Sidebar' );
$beta_features = $this->beta_features->get_beta_features();
$this->assertArrayHasKey( 'editor-sidebar', $beta_features );
$this->assertInstanceOf( 'SCF_Admin_Beta_Feature_Editor_Sidebar', $beta_features['editor-sidebar'] );
$this->assertArrayHasKey( 'editor_sidebar', $beta_features );
$this->assertInstanceOf( 'SCF_Admin_Beta_Feature_Editor_Sidebar', $beta_features['editor_sidebar'] );
}

/**
Expand All @@ -57,9 +57,9 @@ public function test_register_beta_feature() {
public function test_beta_feature_initialization() {
$this->beta_features->register_beta_feature( 'SCF_Admin_Beta_Feature_Editor_Sidebar' );

$beta_feature = $this->beta_features->get_beta_feature( 'editor-sidebar' );
$beta_feature = $this->beta_features->get_beta_feature( 'editor_sidebar' );

$this->assertEquals( 'editor-sidebar', $beta_feature->name );
$this->assertEquals( 'editor_sidebar', $beta_feature->name );
$this->assertEquals( 'Move Elements to Editor Sidebar', $beta_feature->title );
$this->assertNotEmpty( $beta_feature->description );
}
Expand All @@ -70,17 +70,17 @@ public function test_beta_feature_initialization() {
public function test_beta_feature_enable_disable() {
$this->beta_features->register_beta_feature( 'SCF_Admin_Beta_Feature_Editor_Sidebar' );

$beta_feature = $this->beta_features->get_beta_feature( 'editor-sidebar' );
$beta_feature = $this->beta_features->get_beta_feature( 'editor_sidebar' );

$this->assertFalse( $beta_feature->is_enabled() );

$beta_feature->set_enabled( true );
$this->assertTrue( $beta_feature->is_enabled() );
$this->assertTrue( get_option( 'scf_beta_feature_editor-sidebar_enabled' ) );
$this->assertTrue( get_option( 'scf_beta_feature_editor_sidebar_enabled' ) );

$beta_feature->set_enabled( false );
$this->assertFalse( $beta_feature->is_enabled() );
$this->assertFalse( get_option( 'scf_beta_feature_editor-sidebar_enabled' ) );
$this->assertFalse( get_option( 'scf_beta_feature_editor_sidebar_enabled' ) );
}

/**
Expand All @@ -105,17 +105,17 @@ public function test_beta_feature_form_submission() {

$this->beta_features->register_beta_feature( 'SCF_Admin_Beta_Feature_Editor_Sidebar' );

$beta_feature = $this->beta_features->get_beta_feature( 'editor-sidebar' );
$beta_feature = $this->beta_features->get_beta_feature( 'editor_sidebar' );

$this->assertFalse( $beta_feature->is_enabled() );

$_POST['scf_beta_features_nonce'] = wp_create_nonce( 'scf_beta_features_update' );
$_POST['scf_beta_features'] = array( 'editor-sidebar' => '1' );
$_POST['scf_beta_features'] = array( 'editor_sidebar' => '1' );

$this->beta_features->check_submit();

$this->assertTrue( $beta_feature->is_enabled() );
$this->assertTrue( get_option( 'scf_beta_feature_editor-sidebar_enabled' ) );
$this->assertTrue( get_option( 'scf_beta_feature_editor_sidebar_enabled' ) );
}

/**
Expand All @@ -124,14 +124,14 @@ public function test_beta_feature_form_submission() {
public function test_beta_feature_cleanup() {
$this->beta_features->register_beta_feature( 'SCF_Admin_Beta_Feature_Editor_Sidebar' );

$beta_feature = $this->beta_features->get_beta_feature( 'editor-sidebar' );
$beta_feature = $this->beta_features->get_beta_feature( 'editor_sidebar' );

$beta_feature->set_enabled( true );
$this->assertTrue( get_option( 'scf_beta_feature_editor-sidebar_enabled' ) );
$this->assertTrue( get_option( 'scf_beta_feature_editor_sidebar_enabled' ) );

$beta_feature->cleanup();

$this->assertFalse( get_option( 'scf_beta_feature_editor-sidebar_enabled' ) );
$this->assertFalse( get_option( 'scf_beta_feature_editor_sidebar_enabled' ) );
}

/**
Expand All @@ -140,16 +140,16 @@ public function test_beta_feature_cleanup() {
public function test_beta_feature_nonce_verification() {
$this->beta_features->register_beta_feature( 'SCF_Admin_Beta_Feature_Editor_Sidebar' );

$beta_feature = $this->beta_features->get_beta_feature( 'editor-sidebar' );
$beta_feature = $this->beta_features->get_beta_feature( 'editor_sidebar' );

$this->assertFalse( $beta_feature->is_enabled() );

$_POST['scf_beta_features_nonce'] = 'invalid_nonce';
$_POST['scf_beta_features'] = array( 'editor-sidebar' => '1' );
$_POST['scf_beta_features'] = array( 'editor_sidebar' => '1' );

$this->beta_features->check_submit();

$this->assertFalse( $beta_feature->is_enabled() );
$this->assertFalse( get_option( 'scf_beta_feature_editor-sidebar_enabled' ) );
$this->assertFalse( get_option( 'scf_beta_feature_editor_sidebar_enabled' ) );
}
}
Loading