Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Add code to register custom fields for all post types
Browse files Browse the repository at this point in the history
  • Loading branch information
Giacomo Secchi committed Sep 6, 2024
1 parent 5fd8e19 commit 12be133
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 42 deletions.
81 changes: 81 additions & 0 deletions includes/api/class-register-custom-fields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Register custom post types
*
* @package WritePoetry
* @subpackage WritePoetry/Api
* @author Giacomo Secchi <[email protected]>
* @copyright 2023 Giacomo Secchi
* @license GPL-2.0-or-later
* @since 0.3.5
*/

namespace WritePoetry\Api;

use WritePoetry\Base\Base_Controller;


/**
* Registers meta keys for posts class.
*/
class Register_Custom_Fields extends Base_Controller {


/**
* Initialize the class
*/
public function register() {
add_action( 'wp_loaded', array( $this, 'register_post_meta' ) );
}


/**
* Register All Post Type
*
* @since 0.3.5
* @access public
* @return void
*/
public function register_post_meta() {
// Get all post types.
$post_types = get_post_types( array(), 'names' );

// Loop through each post type and register custom fields.
foreach ( $post_types as $post_type ) {
$this->register_custom_field(
apply_filters( "{$this->prefix}_add_custom_fields_to_{$post_type}", array() ),
$post_type
);
}
}

/**
* Registers custom fields for a specific post type.
*
* @param array $custom_fields Array of custom fields.
* @param string $post_type Post type to register custom fields for.
*/
public function register_custom_field( $custom_fields, $post_type ) {

// Default Post Type Arguments.
$default_args = array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'default' => 'ciaone',
);

foreach ( $custom_fields as $custom_field => $args ) {
if ( ! post_type_exists( $post_type ) ) {
continue; // Use continue instead of return to avoid breaking the entire function
}

// Merge default arguments with specific arguments.
$args = array_merge( $default_args, $args );

// Register the meta field for the post type.
register_post_meta( $post_type, $custom_field, $args );
}

}
}
2 changes: 1 addition & 1 deletion includes/base/class-base-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct() {
*/
public function __get( $property ) {

if ( property_exists( $this->config, $property ) ) {
if ( $this->config && property_exists( $this->config, $property ) ) {
return $this->config->$property;
} else {
throw new \Exception( esc_html( "Property '$property' does not exist." ) );
Expand Down
1 change: 1 addition & 0 deletions includes/class-init.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static function get_services() {
$services = array(
Api\Register_Post_Types::class,
Api\Register_Post_Taxonomies::class,
Api\Register_Custom_Fields::class,
Base\Development\Maintenance_Mode::class,
Base\Development\Utils::class,
Base\Utils::class,
Expand Down
61 changes: 20 additions & 41 deletions includes/plugins/jetpack/class-portfolio.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace WritePoetry\Plugins\Jetpack;

use WritePoetry\Base\Base_Controller;
use WritePoetry\Api\Register_Custom_Fields;

/**
* Class Portfolio
Expand All @@ -23,22 +24,28 @@ class Portfolio extends Base_Controller {

const CUSTOM_POST_TYPE = 'jetpack-portfolio';

private $register_custom_fields;


/**
* Invoke hooks.
*
* @return void
*/
public function register() {

$this->register_custom_fields = new Register_Custom_Fields();

add_filter(
'wp_loaded',
function () {
if ( ! post_type_exists( self::CUSTOM_POST_TYPE ) ) {
return;
}

add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_block_assets' ) );
// Add meta box to ensure backward compatibility with the Classic Editor.
add_action( 'add_meta_boxes', array( $this, 'add_portfolio_meta_box' ) );

$this->register_portfolio_meta();
}
);
Expand Down Expand Up @@ -70,61 +77,33 @@ public function enqueue_block_assets() {
}

/**
* Register portfolio meta.
* Register portfolio custom meta fields.
*
* @return void
*/
public function register_portfolio_meta() {
$default_args = array();
$default_args = array(
'type' => 'string',
'default' => 'project_',
);

$meta_fields = array(
'project_url' => $default_args,
'project_year' => array(
"{$this->prefix}_project_url" => $default_args,
"{$this->prefix}_project_year" => array(
'type' => 'number',
'default' => date( 'Y' ),
'description' => 'Year of the project',
),
'project_client' => $default_args,
'project_expertise' => $default_args,
'project_industry' => $default_args
"{$this->prefix}_project_client" => $default_args,
"{$this->prefix}_project_expertise" => $default_args,
"{$this->prefix}_project_industry" => $default_args
);

foreach( $meta_fields as $meta_field => $args ) {
$this->register_post_meta( "{$this->prefix}_$meta_field", $args );
}
// Register custom fields for the portfolio post type.
$this->register_custom_fields->register_custom_field( $meta_fields, self::CUSTOM_POST_TYPE );
}

/**
* Register post meta.
*
* @param string $meta_key Meta key.
* @param array $args Meta args.
*
* @return void
*/
private function register_post_meta( $meta_key, $args ) {

$meta_args = array(
'show_in_rest' => true,
'single' => true,
'sanitize_callback' => 'sanitize_text_field',
);

$possible_keys = [ 'type', 'default', 'description' ];

foreach ( $possible_keys as $key ) {

if ( isset( $args[$key] ) ) {
$meta_args[$key] = $args[$key];
}
}

register_post_meta(
self::CUSTOM_POST_TYPE,
$meta_key,
$meta_args
);
}

/**
* Add portfolio meta box.
Expand Down
50 changes: 50 additions & 0 deletions mu-plugins/project-custom-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,53 @@ function () {
10,
3
);


add_filter(
'writepoetry_add_custom_fields_to_post',
function () {
$meta_keys = array(
'test_meta_key' => array(
'show_in_rest' => true,
'single' => true,
'type' => 'boolean',
'default' => false,
),
'test_meta_key2' => array(
'show_in_rest' => true,
'single' => true,
'type' => 'string'
),
);

return $meta_keys;
},
10,
3
);

add_filter(
'writepoetry_add_custom_fields_to_page',
function () {
$meta_keys = array(
'test_page' => array(
'show_in_rest' => true,
'single' => true,
'type' => 'test',
'default' => false,
),
'test_page_key2' => array(
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'default' => 'test2',
),
);

return $meta_keys;
},
10,
3
);


0 comments on commit 12be133

Please sign in to comment.