Skip to content

Commit

Permalink
Export content model icon and plural label (#119)
Browse files Browse the repository at this point in the history
Co-authored-by: Candy Tsai <[email protected]>
  • Loading branch information
zaguiini and candy02058912 authored Sep 5, 2024
1 parent 341abcc commit 232159b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
19 changes: 12 additions & 7 deletions includes/exporter/class-content-model-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* @package create-content-model
*/

/**
* Handles the export functionality for Content Models.
*/
class Content_Model_Exporter {
/**
* The instance.
Expand Down Expand Up @@ -225,12 +228,14 @@ private function enqueue_content_models_preview_assets() {
*/
private function generate_json_for_model( $model ) {
return array(
'type' => 'postType',
'postType' => $model->slug,
'slug' => $model->slug,
'label' => $model->title,
'template' => $model->template,
'fields' => $this->format_fields_for_export( $model->get_meta_fields() ),
'type' => 'postType',
'postType' => $model->slug,
'slug' => $model->slug,
'label' => $model->title,
'pluralLabel' => $model->get_plural_label(),
'icon' => $model->get_icon(),
'template' => $model->template,
'fields' => $this->format_fields_for_export( $model->get_meta_fields() ),
);
}

Expand Down Expand Up @@ -365,7 +370,7 @@ private function replace_content_model_plugin_file_version() {

/**
* Creates a ZIP file containing the content models and necessary plugin files.
* This does not include content models that are not published.
* This does not include content models that are not published.
*
* @param array $all_models_json The JSON data for the content models.
* @return int|false The attachment ID of the created ZIP file, or false on failure.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ private static function register_content_models_from_json( $post_types ) {

$post_id = wp_insert_post( $content_model_post );

update_post_meta( $post_id, 'plural_label', $post_type['pluralLabel'] );
update_post_meta( $post_id, 'icon', $post_type['icon'] );

update_post_meta( $post_id, 'fields', wp_json_encode( $post_type['fields'] ) );
}
}
Expand Down
54 changes: 42 additions & 12 deletions includes/runtime/class-content-model.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,54 @@ public function __construct( WP_Post $content_model_post ) {
add_filter( 'get_post_metadata', array( $this, 'cast_meta_field_types' ), 10, 3 );
}

/**
* Retrieves the plural label for the content model.
*
* If the plural label is not set, it will default to the singular label with an 's' appended.
*
* @return string The plural label.
*/
public function get_plural_label() {
return $this->get_model_meta( 'plural_label' ) ?? "{$this->title}s";
}

/**
* Retrieves the icon for the content model.
*
* If the icon is not set, it will default to 'admin-post'.
*
* @return string The icon slug.
*/
public function get_icon() {
$icon = $this->get_model_meta( 'icon' ) ?? 'admin-post';

return str_replace( 'dashicons-', '', $icon );
}

/**
* Retrieves a meta value for the content model.
*
* @param string $key The meta key to retrieve.
* @return mixed The value of the meta field, or null if it does not exist.
*/
private function get_model_meta( $key ) {
$meta = get_post_meta( $this->post_id, $key, true );

if ( ! empty( $meta ) ) {
return $meta;
}

return null;
}

/**
* Registers the custom post type for the content model.
*
* @return void
*/
private function register_post_type() {
$singular_name = $this->title;

$plural_name = get_post_meta( $this->post_id, 'plural_label', true );
if ( empty( $plural_name ) ) {
$plural_name = $singular_name . 's';
}
$plural_name = $this->get_plural_label();

$labels = array(
'name' => $plural_name,
Expand All @@ -140,12 +176,6 @@ private function register_post_type() {
'not_found_in_trash' => sprintf( __( 'No %s found in trash' ), $plural_name ),
);

$icon = get_post_meta( $this->post_id, 'icon', true );
if ( empty( $icon ) ) {
$icon = 'admin-post';
}
$icon = str_replace( 'dashicons-', '', $icon );

register_post_type(
$this->slug,
array(
Expand All @@ -154,7 +184,7 @@ private function register_post_type() {
'show_in_menu' => true,
'has_archive' => true,
'show_in_rest' => true,
'menu_icon' => "dashicons-$icon",
'menu_icon' => "dashicons-{$this->get_icon()}",
'supports' => array( 'title', 'editor', 'custom-fields' ),
)
);
Expand Down

0 comments on commit 232159b

Please sign in to comment.