Add fictioneer_get_post_type_label()

This commit is contained in:
Tetrakern 2024-10-04 16:28:08 +02:00
parent c86b95d198
commit a19303e5a1
3 changed files with 34 additions and 63 deletions

View File

@ -1475,20 +1475,9 @@ function fictioneer_ajax_get_relationship_story_pages( $post_id, $meta_key ) {
*/
function fictioneer_callback_relationship_collection( $selected, $meta_key, $args = [] ) {
// Setup
$post_type_labels = array(
'post' => _x( 'Post', 'Post type label.', 'fictioneer' ),
'page' => _x( 'Page', 'Post type label.', 'fictioneer' ),
'fcn_story' => _x( 'Story', 'Post type label.', 'fictioneer' ),
'fcn_chapter' => _x( 'Chapter', 'Post type label.', 'fictioneer' ),
'fcn_collection' => _x( 'Collection', 'Post type label.', 'fictioneer' ),
'fcn_recommendation' => _x( 'Rec', 'Post type label.', 'fictioneer' )
);
// Build HTML
foreach ( $selected as $item ) {
$title = fictioneer_get_safe_title( $item, 'admin-callback-relationship-collection' );
$label = esc_html( $post_type_labels[ $item->post_type ] ?? _x( '?', 'Relationship item label.', 'fictioneer' ) );
$label = esc_html( fictioneer_get_post_type_label( $item->post_type ) );
$classes = ['fictioneer-meta-field__relationships-item', 'fictioneer-meta-field__relationships-values-item'];
// Start HTML ---> ?>
@ -1523,15 +1512,6 @@ function fictioneer_ajax_get_relationship_collection( $post_id, $meta_key ) {
$post_type = sanitize_text_field( $_REQUEST['post_type'] ?? '' );
$post_type = in_array( $post_type, $allowed_post_types ) ? $post_type : null;
$post_type_labels = array(
'post' => _x( 'Post', 'Post type label.', 'fictioneer' ),
'page' => _x( 'Page', 'Post type label.', 'fictioneer' ),
'fcn_story' => _x( 'Story', 'Post type label.', 'fictioneer' ),
'fcn_chapter' => _x( 'Chapter', 'Post type label.', 'fictioneer' ),
'fcn_collection' => _x( 'Collection', 'Post type label.', 'fictioneer' ),
'fcn_recommendation' => _x( 'Rec', 'Post type label.', 'fictioneer' )
);
$forbidden = array_unique(
array(
get_option( 'fictioneer_user_profile_page', 0 ),
@ -1587,7 +1567,7 @@ function fictioneer_ajax_get_relationship_collection( $post_id, $meta_key ) {
foreach ( $query->posts as $item ) {
// Chapter setup
$title = fictioneer_get_safe_title( $item, 'admin-ajax-get-relationship-collection' );
$label = esc_html( $post_type_labels[ $item->post_type ] ?? _x( '?', 'Relationship item label.', 'fictioneer' ) );
$label = esc_html( fictioneer_get_post_type_label( $item->post_type ) );
$classes = ['fictioneer-meta-field__relationships-item', 'fictioneer-meta-field__relationships-source-item'];
// Build and append item
@ -1639,19 +1619,9 @@ function fictioneer_ajax_get_relationship_collection( $post_id, $meta_key ) {
*/
function fictioneer_callback_relationship_featured( $selected, $meta_key, $args = [] ) {
// Setup
$post_type_labels = array(
'post' => _x( 'Post', 'Post type label.', 'fictioneer' ),
'fcn_story' => _x( 'Story', 'Post type label.', 'fictioneer' ),
'fcn_chapter' => _x( 'Chapter', 'Post type label.', 'fictioneer' ),
'fcn_collection' => _x( 'Collection', 'Post type label.', 'fictioneer' ),
'fcn_recommendation' => _x( 'Rec', 'Post type label.', 'fictioneer' )
);
// Build HTML
foreach ( $selected as $item ) {
$title = fictioneer_get_safe_title( $item, 'admin-callback-relationship-featured' );
$label = esc_html( $post_type_labels[ $item->post_type ] ?? _x( '?', 'Relationship item label.', 'fictioneer' ) );
$label = esc_html( fictioneer_get_post_type_label( $item->post_type ) );
$classes = ['fictioneer-meta-field__relationships-item', 'fictioneer-meta-field__relationships-values-item'];
// Start HTML ---> ?>
@ -1686,14 +1656,6 @@ function fictioneer_ajax_get_relationship_featured( $post_id, $meta_key ) {
$post_type = sanitize_text_field( $_REQUEST['post_type'] ?? '' );
$post_type = in_array( $post_type, $allowed_post_types ) ? $post_type : null;
$post_type_labels = array(
'post' => _x( 'Post', 'Post type label.', 'fictioneer' ),
'fcn_story' => _x( 'Story', 'Post type label.', 'fictioneer' ),
'fcn_chapter' => _x( 'Chapter', 'Post type label.', 'fictioneer' ),
'fcn_collection' => _x( 'Collection', 'Post type label.', 'fictioneer' ),
'fcn_recommendation' => _x( 'Rec', 'Post type label.', 'fictioneer' )
);
// Validations
if ( $post->post_type !== 'post' ) {
wp_send_json_error( array( 'error' => __( 'Error: Wrong post type.', 'fictioneer' ) ) );
@ -1733,7 +1695,7 @@ function fictioneer_ajax_get_relationship_featured( $post_id, $meta_key ) {
foreach ( $query->posts as $item ) {
// Chapter setup
$title = fictioneer_get_safe_title( $item, 'admin-ajax-get-relationship-featured' );
$label = esc_html( $post_type_labels[ $item->post_type ] ?? _x( '?', 'Relationship item label.', 'fictioneer' ) );
$label = esc_html( fictioneer_get_post_type_label( $item->post_type ) );
$classes = ['fictioneer-meta-field__relationships-item', 'fictioneer-meta-field__relationships-source-item'];
// Build and append item

View File

@ -3441,7 +3441,7 @@ function fictioneer_get_publishing_authors( $args = [] ) {
}
// =============================================================================
// GET POST STATUS LABEL
// GET POST LABELS
// =============================================================================
/**
@ -3470,3 +3470,30 @@ function fictioneer_get_post_status_label( $status ) {
return $labels[ $status ] ?? $status;
}
/**
* Returns the translated label of the post type
*
* @since 5.25.0
*
* @param string $type Post type.
*
* @return string Translated label of the post type or the post type if custom.
*/
function fictioneer_get_post_type_label( $type ) {
static $labels = null;
if ( ! $labels ) {
$labels = array(
'post' => _x( 'Post', 'Post type label.', 'fictioneer' ),
'page' => _x( 'Page', 'Post type label.', 'fictioneer' ),
'fcn_story' => _x( 'Story', 'Post type label.', 'fictioneer' ),
'fcn_chapter' => _x( 'Chapter', 'Post type label.', 'fictioneer' ),
'fcn_collection' => _x( 'Collection', 'Post type label.', 'fictioneer' ),
'fcn_recommendation' => _x( 'Rec', 'Post type label.', 'fictioneer' )
);
}
return $labels[ $type ] ?? $type;
}

View File

@ -1453,14 +1453,6 @@ function fictioneer_admin_profile_post_unlocks( $profile_user ) {
// Post data
$posts = [];
$post_type_labels = array(
'post' => _x( 'Post', 'Post type label.', 'fictioneer' ),
'page' => _x( 'Page', 'Post type label.', 'fictioneer' ),
'fcn_story' => _x( 'Story', 'Post type label.', 'fictioneer' ),
'fcn_chapter' => _x( 'Chapter', 'Post type label.', 'fictioneer' ),
'fcn_collection' => _x( 'Collection', 'Post type label.', 'fictioneer' ),
'fcn_recommendation' => _x( 'Rec', 'Post type label.', 'fictioneer' )
);
if ( $unlocks ) {
$query = new WP_Query(
@ -1532,7 +1524,7 @@ function fictioneer_admin_profile_post_unlocks( $profile_user ) {
<?php foreach ( $posts as $post ) : ?>
<?php
$type = $post_type_labels[ $post->post_type ] ?? '';
$type = fictioneer_get_post_type_label( $post->post_type );
$title = fictioneer_get_safe_title( $post->ID );
$item_title = sprintf(
_x( 'Author: %s | Title: %s', 'Unlock post item.', 'fictioneer' ),
@ -1610,18 +1602,8 @@ function fictioneer_ajax_search_posts_to_unlock() {
if ( ! $posts->posts ) {
wp_send_json_success( array( 'html' => '' ) );
} else {
// Labels
$post_type_labels = array(
'post' => _x( 'Post', 'Post type label.', 'fictioneer' ),
'page' => _x( 'Page', 'Post type label.', 'fictioneer' ),
'fcn_story' => _x( 'Story', 'Post type label.', 'fictioneer' ),
'fcn_chapter' => _x( 'Chapter', 'Post type label.', 'fictioneer' ),
'fcn_collection' => _x( 'Collection', 'Post type label.', 'fictioneer' ),
'fcn_recommendation' => _x( 'Rec', 'Post type label.', 'fictioneer' )
);
foreach ( $posts->posts as $post ) {
$type = $post_type_labels[ $post->post_type ] ?? '';
$type = fictioneer_get_post_type_label( $post->post_type );
$title = fictioneer_get_safe_title( $post->ID );
$item_title = sprintf(
_x( 'Author: %s | Title: %s', 'Unlock post item.', 'fictioneer' ),