Add option to rewrite chapter permalinks

This commit is contained in:
Tetrakern 2024-06-02 10:52:17 +02:00
parent d4ede59bbc
commit 7816d814f1
4 changed files with 95 additions and 1 deletions

View File

@ -223,7 +223,7 @@ Fictioneer customizes WordPress by using as many standard action and filter hook
| `edit_user_profile` | `fictioneer_custom_profile_fields`
| `edit_user_profile_update` | `fictioneer_update_admin_user_profile`, `fictioneer_update_my_user_profile`, `fictioneer_update_admin_unlocked_posts`
| `get_header` | `fictioneer_maintenance_mode`
| `init` | `fictioneer_add_character_taxonomy`, `fictioneer_add_content_warning_taxonomy`, `fictioneer_add_epub_download_endpoint`, `fictioneer_add_fandom_taxonomy`, `fictioneer_add_genre_taxonomy`, `fictioneer_add_logout_endpoint`, `fictioneer_add_oauth2_endpoint`, `fictioneer_restrict_admin_panel`, `fictioneer_disable_heartbeat`, `fictioneer_fcn_chapter_post_type`, `fictioneer_fcn_collection_post_type`, `fictioneer_fcn_recommendation_post_type`, `fictioneer_fcn_story_post_type`, `fictioneer_modify_allowed_tags`, `fictioneer_story_rss`, `fictioneer_remove_custom_fields_supports`, `fictioneer_add_sitemap_rewrite_rule`, `fictioneer_fast_ajax`
| `init` | `fictioneer_add_character_taxonomy`, `fictioneer_add_content_warning_taxonomy`, `fictioneer_add_epub_download_endpoint`, `fictioneer_add_fandom_taxonomy`, `fictioneer_add_genre_taxonomy`, `fictioneer_add_logout_endpoint`, `fictioneer_add_oauth2_endpoint`, `fictioneer_restrict_admin_panel`, `fictioneer_disable_heartbeat`, `fictioneer_fcn_chapter_post_type`, `fictioneer_fcn_collection_post_type`, `fictioneer_fcn_recommendation_post_type`, `fictioneer_fcn_story_post_type`, `fictioneer_modify_allowed_tags`, `fictioneer_story_rss`, `fictioneer_remove_custom_fields_supports`, `fictioneer_add_sitemap_rewrite_rule`, `fictioneer_fast_ajax`, `fictioneer_add_chapter_rewrite_tags`, `fictioneer_add_chapter_rewrite_rules`, `fictioneer_rewrite_chapter_permalink`
| `login_form` | `fictioneer_after_logout_cleanup`
| `login_head` | `fictioneer_wp_login_scripts`
| `manage_comments_custom_column` | `fictioneer_add_comments_report_column_content`

View File

@ -233,10 +233,87 @@ function fictioneer_fcn_chapter_post_type() {
'map_meta_cap' => true
);
if ( get_option( 'fictioneer_rewrite_chapter_permalinks' ) ) {
$args['rewrite'] = array( 'slug' => 'story/%story_slug%', 'with_front' => false );
}
register_post_type( 'fcn_chapter', $args );
}
add_action( 'init', 'fictioneer_fcn_chapter_post_type', 0 );
/**
* Adds chapter rewrite tags
*
* @since 5.19.1
*/
function fictioneer_add_chapter_rewrite_tags() {
add_rewrite_tag( '%story_slug%', '([^/]+)', 'story_slug=' );
}
if ( get_option( 'fictioneer_rewrite_chapter_permalinks' ) ) {
add_action( 'init', 'fictioneer_add_chapter_rewrite_tags' );
}
/**
* Adds chapter rewrite rules
*
* @since 5.19.1
*/
function fictioneer_add_chapter_rewrite_rules() {
// Rule for chapters with story
add_rewrite_rule(
'^story/([^/]+)/([^/]+)/?$',
'index.php?post_type=fcn_chapter&name=$matches[2]',
'top'
);
// Rule for standalone chapters
add_rewrite_rule(
'^chapter/([^/]+)/?$',
'index.php?post_type=fcn_chapter&name=$matches[1]',
'top'
);
}
if ( get_option( 'fictioneer_rewrite_chapter_permalinks' ) ) {
add_action( 'init', 'fictioneer_add_chapter_rewrite_rules' );
}
/**
* Adds chapter rewrite rules
*
* @since 5.19.1
*
* @param string $post_link The posts permalink.
* @param WP_Post $post The post in question.
*
* @return string The modified permalink.
*/
function fictioneer_rewrite_chapter_permalink( $post_link, $post ) {
// Only for chapters...
if ( $post->post_type === 'fcn_chapter' ) {
$story_id = get_post_meta( $post->ID, 'fictioneer_chapter_story', true );
if ( $story_id ) {
// Chapter with story
$post_link = str_replace( '%story_slug%', get_post_field( 'post_name', $story_id ), $post_link );
} else {
// Standalone chapter
$post_link = str_replace( 'story/%story_slug%', 'chapter', $post_link );
}
}
// Continue filter
return $post_link;
}
if ( get_option( 'fictioneer_rewrite_chapter_permalinks' ) ) {
add_filter( 'post_type_link', 'fictioneer_rewrite_chapter_permalink', 10, 2 );
}
// =============================================================================
// CUSTOM POST TYPE: FCN_COLLECTION
// =============================================================================

View File

@ -623,6 +623,12 @@ define( 'FICTIONEER_OPTIONS', array(
'group' => 'fictioneer-settings-general-group',
'sanitize_callback' => 'fictioneer_sanitize_checkbox',
'default' => 0
),
'fictioneer_rewrite_chapter_permalinks' => array(
'name' => 'fictioneer_rewrite_chapter_permalinks',
'group' => 'fictioneer-settings-general-group',
'sanitize_callback' => 'fictioneer_sanitize_checkbox',
'default' => 0
)
),
'integers' => array(

View File

@ -1149,6 +1149,17 @@
<h3 class="fictioneer-card__header"><?php _e( 'Compatibility', 'fictioneer' ); ?></h3>
<div class="fictioneer-card__content">
<div class="fictioneer-card__row">
<?php
fictioneer_settings_label_checkbox(
'fictioneer_rewrite_chapter_permalinks',
__( 'Rewrite chapter permalinks to include story', 'fictioneer' ),
__( 'Becomes <code>/story/story-slug/chapter-slug[-n]</code>. You must flush your permalinks and purge the theme caches.', 'fictioneer' ),
__( 'Changes the permalinks of chapters to include the story. This allows for more readable and descriptive URLs. Chapter slugs are still kept globally unique — meaning multiple "Chapter N" titles will end up as "chapter-n-x" slugs — and the post editor will continue to display the default permalink structure for technical reasons.', 'fictioneer' )
);
?>
</div>
<div class="fictioneer-card__row">
<?php
fictioneer_settings_label_checkbox(