diff --git a/includes/api/class-register-custom-fields.php b/includes/api/class-register-custom-fields.php new file mode 100644 index 0000000..17ecffb --- /dev/null +++ b/includes/api/class-register-custom-fields.php @@ -0,0 +1,81 @@ + + * @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 ); + } + + } +} diff --git a/includes/base/class-base-controller.php b/includes/base/class-base-controller.php index f1726d5..01fd495 100644 --- a/includes/base/class-base-controller.php +++ b/includes/base/class-base-controller.php @@ -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." ) ); diff --git a/includes/class-init.php b/includes/class-init.php index 4b1c24e..3056556 100644 --- a/includes/class-init.php +++ b/includes/class-init.php @@ -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, diff --git a/includes/plugins/jetpack/class-portfolio.php b/includes/plugins/jetpack/class-portfolio.php index d7e8a97..b0c7a30 100644 --- a/includes/plugins/jetpack/class-portfolio.php +++ b/includes/plugins/jetpack/class-portfolio.php @@ -13,6 +13,7 @@ namespace WritePoetry\Plugins\Jetpack; use WritePoetry\Base\Base_Controller; +use WritePoetry\Api\Register_Custom_Fields; /** * Class Portfolio @@ -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(); } ); @@ -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. diff --git a/mu-plugins/project-custom-functions.php b/mu-plugins/project-custom-functions.php index cefb5cf..cb1188d 100644 --- a/mu-plugins/project-custom-functions.php +++ b/mu-plugins/project-custom-functions.php @@ -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 +); + +