diff --git a/404.php b/404.php index 1ef979b8..7fd56492 100644 --- a/404.php +++ b/404.php @@ -11,7 +11,7 @@ diff --git a/includes/functions/_content_helpers.php b/includes/functions/_content_helpers.php index 873657ef..06c23a14 100644 --- a/includes/functions/_content_helpers.php +++ b/includes/functions/_content_helpers.php @@ -1075,19 +1075,19 @@ if ( ! function_exists( 'fictioneer_user_menu_items' ) ) { function fictioneer_user_menu_items() { // Setup - $bookmarks_link = get_permalink( get_option( 'fictioneer_bookmarks_page' ) ); - $discord_link = get_option( 'fictioneer_discord_invite_link' ); - $bookshelf_link = get_permalink( get_option( 'fictioneer_bookshelf_page' ) ); + $bookmarks_link = fictioneer_get_assigned_page_link( 'fictioneer_bookmarks_page' ); + $bookshelf_link = fictioneer_get_assigned_page_link( 'fictioneer_bookshelf_page' ); $bookshelf_title = trim( get_the_title( get_option( 'fictioneer_bookshelf_page' ) ) ); + $discord_link = get_option( 'fictioneer_discord_invite_link' ); $can_checkmarks = get_option( 'fictioneer_enable_checkmarks' ); $can_follows = get_option( 'fictioneer_enable_follows' ); $can_reminders = get_option( 'fictioneer_enable_reminders' ); - $output = []; $profile_link = get_edit_profile_url( 0 ); // Make sure this is always the default link - $profile_page_id = intval( get_option( 'fictioneer_user_profile_page', -1 ) ); + $profile_page_id = intval( get_option( 'fictioneer_user_profile_page', -1 ) ?: -1 ); + $output = []; - if ( $profile_page_id && $profile_page_id > 0 ) { - $profile_link = get_permalink( $profile_page_id ); + if ( ! empty( $profile_page_id ) && $profile_page_id > 0 ) { + $profile_link = fictioneer_get_assigned_page_link( 'fictioneer_user_profile_page' ); } // Build @@ -1568,4 +1568,87 @@ if ( ! function_exists( 'fictioneer_get_list_chapter_meta_row' ) ) { } } +// ============================================================================= +// GET STORY BLOG POSTS +// ============================================================================= + +if ( ! function_exists( 'fictioneer_get_story_blog_posts' ) ) { + /** + * Returns WP_Query with blog posts associated with the story + * + * @since 5.4.8 + * + * @param int $story_id The story ID. + * + * @return WP_Query Queried blog posts. + */ + + function fictioneer_get_story_blog_posts( $story_id ) { + // Setup + $category = implode( ', ', wp_get_post_categories( $story_id ) ); + $blog_posts = new WP_Query(); + + // Query by category + $blog_category_query_args = array ( + 'ignore_sticky_posts' => 1, + 'author__in' => fictioneer_get_post_author_ids( $story_id ), + 'nopaging' => false, + 'posts_per_page' => 10, + 'cat' => empty( $category ) ? '99999999' : $category, + 'no_found_rows' => true, + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false + ); + + $blog_category_posts = new WP_Query( $blog_category_query_args ); + + // Query by ACF relationship + $blog_relationship_query_args = array ( + 'ignore_sticky_posts' => 1, + 'author__in' => fictioneer_get_post_author_ids( $story_id ), + 'nopaging' => false, + 'posts_per_page' => 10, + 'no_found_rows' => true, + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'meta_query' => array( + array( + 'key' => 'fictioneer_post_story_blogs', + 'value' => '"' . $story_id . '"', + 'compare' => 'LIKE', + ) + ) + ); + + $blog_associated_posts = new WP_Query( $blog_relationship_query_args ); + + // Merge results + $merged_blog_posts = array_merge( $blog_category_posts->posts, $blog_associated_posts->posts ); + + // Make sure posts are unique + $unique_blog_posts = []; + + foreach ( $merged_blog_posts as $blog_post ) { + if ( ! in_array( $blog_post, $unique_blog_posts ) ) { + $unique_blog_posts[] = $blog_post; + } + } + + // Sort by date + usort( $unique_blog_posts, function( $a, $b ) { + return strcmp( $b->post_date, $a->post_date ); + }); + + // Limit to 10 posts + $unique_blog_posts = array_slice( $unique_blog_posts, 0, 10 ); + + // Set up query object + $blog_posts->posts = $unique_blog_posts; + $blog_posts->post_count = count( $unique_blog_posts ); + + // Return merged query + return $blog_posts; + } +} + ?> diff --git a/includes/functions/_utility.php b/includes/functions/_utility.php index 736861a9..e065f2db 100644 --- a/includes/functions/_utility.php +++ b/includes/functions/_utility.php @@ -1623,89 +1623,6 @@ if ( ! function_exists( 'fictioneer_get_post_author_ids' ) ) { } } -// ============================================================================= -// GET STORY BLOG POSTS -// ============================================================================= - -if ( ! function_exists( 'fictioneer_get_story_blog_posts' ) ) { - /** - * Returns WP_Query with blog posts associated with the story - * - * @since 5.4.8 - * - * @param int $story_id The story ID. - * - * @return WP_Query Queried blog posts. - */ - - function fictioneer_get_story_blog_posts( $story_id ) { - // Setup - $category = implode( ', ', wp_get_post_categories( $story_id ) ); - $blog_posts = new WP_Query(); - - // Query by category - $blog_category_query_args = array ( - 'ignore_sticky_posts' => 1, - 'author__in' => fictioneer_get_post_author_ids( $story_id ), - 'nopaging' => false, - 'posts_per_page' => 10, - 'cat' => empty( $category ) ? '99999999' : $category, - 'no_found_rows' => true, - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false - ); - - $blog_category_posts = new WP_Query( $blog_category_query_args ); - - // Query by ACF relationship - $blog_relationship_query_args = array ( - 'ignore_sticky_posts' => 1, - 'author__in' => fictioneer_get_post_author_ids( $story_id ), - 'nopaging' => false, - 'posts_per_page' => 10, - 'no_found_rows' => true, - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'meta_query' => array( - array( - 'key' => 'fictioneer_post_story_blogs', - 'value' => '"' . $story_id . '"', - 'compare' => 'LIKE', - ) - ) - ); - - $blog_associated_posts = new WP_Query( $blog_relationship_query_args ); - - // Merge results - $merged_blog_posts = array_merge( $blog_category_posts->posts, $blog_associated_posts->posts ); - - // Make sure posts are unique - $unique_blog_posts = []; - - foreach ( $merged_blog_posts as $blog_post ) { - if ( ! in_array( $blog_post, $unique_blog_posts ) ) { - $unique_blog_posts[] = $blog_post; - } - } - - // Sort by date - usort( $unique_blog_posts, function( $a, $b ) { - return strcmp( $b->post_date, $a->post_date ); - }); - - // Limit to 10 posts - $unique_blog_posts = array_slice( $unique_blog_posts, 0, 10 ); - - // Set up query object - $blog_posts->posts = $unique_blog_posts; - $blog_posts->post_count = count( $unique_blog_posts ); - - // Return merged query - return $blog_posts; - } -} - // ============================================================================= // DELETE TRANSIENTS THAT INCLUDE A STRING // ============================================================================= @@ -1764,4 +1681,41 @@ if ( ! function_exists( 'fictioneer_delete_transients_like' ) ) { } } +// ============================================================================= +// GET OPTION PAGE LINK +// ============================================================================= + +if ( ! function_exists( 'fictioneer_get_assigned_page_link' ) ) { + /** + * Returns permalink for an assigned page or null + * + * @since 5.4.9 + * + * @param string $option The option name of the page assignment. + * + * @return string|null The permalink or null. + */ + + function fictioneer_get_assigned_page_link( $option ) { + // Setup + $page_id = get_option( $option ); + + // Null if no page has been selected (null or -1) + if ( empty( $page_id ) || $page_id < 0 ) { + return null; + } + + // Get permalink from options or post + $link = get_option( "{$option}_link" ); + + if ( empty( $link ) ) { + $link = get_permalink( $page_id ); + update_option( "{$option}_link", $link, true ); // Save for next time + } + + // Return + return $link; + } +} + ?> diff --git a/includes/functions/comments/_comments_form.php b/includes/functions/comments/_comments_form.php index 79063175..b00106a6 100644 --- a/includes/functions/comments/_comments_form.php +++ b/includes/functions/comments/_comments_form.php @@ -240,11 +240,11 @@ function fictioneer_comment_form_args( $defaults = [], $post_id = null ) { $toolbar = fictioneer_get_comment_toolbar(); $oauth_links = fictioneer_get_oauth_links( false, '', 'comments', $post_id ); $profile_link = get_edit_profile_url(); - $profile_page = intval( get_option( 'fictioneer_user_profile_page', -1 ) ); + $profile_page_id = intval( get_option( 'fictioneer_user_profile_page', -1 ) ?: -1 ); $aria_label_textarea = __( 'Leave a comments please', 'fictioneer' ); - if ( $profile_page && $profile_page > 0 ) { - $profile_link = get_permalink( $profile_page ); + if ( ! empty( $profile_page_id ) && $profile_page_id > 0 ) { + $profile_link = fictioneer_get_assigned_page_link( 'fictioneer_user_profile_page' ); } // Build arguments diff --git a/includes/functions/hooks/_mobile_menu_hooks.php b/includes/functions/hooks/_mobile_menu_hooks.php index abbc60f6..a0919a24 100644 --- a/includes/functions/hooks/_mobile_menu_hooks.php +++ b/includes/functions/hooks/_mobile_menu_hooks.php @@ -353,19 +353,19 @@ if ( ! function_exists( 'fictioneer_mobile_user_menu' ) ) { function fictioneer_mobile_user_menu() { // Setup $post_type = is_archive() ? 'archive' : get_post_type(); - $profile_link = get_edit_profile_url(); - $profile_page = intval( get_option( 'fictioneer_user_profile_page', -1 ) ); - $discord_link = get_option( 'fictioneer_discord_invite_link' ); - $bookshelf_link = get_permalink( get_option( 'fictioneer_bookshelf_page' ) ); + $bookmarks_link = fictioneer_get_assigned_page_link( 'fictioneer_bookmarks_page' ); + $bookshelf_link = fictioneer_get_assigned_page_link( 'fictioneer_bookshelf_page' ); $bookshelf_title = trim( get_the_title( get_option( 'fictioneer_bookshelf_page' ) ) ); - $bookmarks_link = get_permalink( get_option( 'fictioneer_bookmarks_page' ) ); + $discord_link = get_option( 'fictioneer_discord_invite_link' ); $can_checkmarks = get_option( 'fictioneer_enable_checkmarks' ); $can_follows = get_option( 'fictioneer_enable_follows' ); $can_reminders = get_option( 'fictioneer_enable_reminders' ); + $profile_link = get_edit_profile_url(); + $profile_page_id = intval( get_option( 'fictioneer_user_profile_page', -1 ) ?: -1 ); $output = []; - if ( $profile_page && $profile_page > 0 ) { - $profile_link = get_permalink( $profile_page ); + if ( $profile_page_id && $profile_page_id > 0 ) { + $profile_link = fictioneer_get_assigned_page_link( 'fictioneer_user_profile_page' ); } // Build diff --git a/includes/functions/settings/_register_settings.php b/includes/functions/settings/_register_settings.php index de9a927d..4aa56b69 100644 --- a/includes/functions/settings/_register_settings.php +++ b/includes/functions/settings/_register_settings.php @@ -1040,4 +1040,64 @@ function fictioneer_validate_phrase_cookie_consent_banner( $input ) { return strlen( $input ) < 32 ? $default : $output; } +// ============================================================================= +// UPDATED HOOK ACTIONS +// ============================================================================= + +add_filter( 'sanitize_option_fictioneer_user_profile_page', function( $new_value ) { + $link = get_permalink( $new_value ); + update_option( 'fictioneer_user_profile_page_link', $link, true ); + + return $new_value; +}, 99); + +add_filter( 'sanitize_option_fictioneer_bookmarks_page', function( $new_value ) { + $link = get_permalink( $new_value ); + update_option( 'fictioneer_bookmarks_page_link', $link, true ); + + return $new_value; +}, 99); + +add_filter( 'sanitize_option_fictioneer_stories_page', function( $new_value ) { + $link = get_permalink( $new_value ); + update_option( 'fictioneer_stories_page_link', $link, true ); + + return $new_value; +}, 99); + +add_filter( 'sanitize_option_fictioneer_chapters_page', function( $new_value ) { + $link = get_permalink( $new_value ); + update_option( 'fictioneer_chapters_page_link', $link, true ); + + return $new_value; +}, 99); + +add_filter( 'sanitize_option_fictioneer_recommendations_page', function( $new_value ) { + $link = get_permalink( $new_value ); + update_option( 'fictioneer_recommendations_page_link', $link, true ); + + return $new_value; +}, 99); + +add_filter( 'sanitize_option_fictioneer_collections_page', function( $new_value ) { + $link = get_permalink( $new_value ); + update_option( 'fictioneer_collections_page_link', $link, true ); + + return $new_value; +}, 99); + +add_filter( 'sanitize_option_fictioneer_bookshelf_page', function( $new_value ) { + $link = get_permalink( $new_value ); + update_option( 'fictioneer_bookshelf_page_link', $link, true ); + + return $new_value; +}, 99); + +add_filter( 'sanitize_option_fictioneer_404_page', function( $new_value ) { + $link = get_permalink( $new_value ); + update_option( 'fictioneer_404_page_link', $link, true ); + + return $new_value; +}, 99); + ?> diff --git a/partials/_icon-menu.php b/partials/_icon-menu.php index 0665ebdc..461cfaab 100644 --- a/partials/_icon-menu.php +++ b/partials/_icon-menu.php @@ -16,13 +16,13 @@ 0 ) { - $profile_link = get_permalink( $profile_page ); +if ( ! empty( $profile_page_id ) && $profile_page_id > 0 ) { + $profile_link = fictioneer_get_assigned_page_link( 'fictioneer_user_profile_page' ); } ?> diff --git a/partials/account/_data.php b/partials/account/_data.php index fb0477d1..f48f9381 100644 --- a/partials/account/_data.php +++ b/partials/account/_data.php @@ -18,8 +18,8 @@ // Setup $current_user = $args['user']; -$bookshelf_link = get_permalink( get_option( 'fictioneer_bookshelf_page' ) ); -$bookmarks_link = get_permalink( get_option( 'fictioneer_bookmarks_page' ) ); +$bookmarks_link = fictioneer_get_assigned_page_link( 'fictioneer_bookmarks_page' ); +$bookshelf_link = fictioneer_get_assigned_page_link( 'fictioneer_bookshelf_page' ); $checkmarks = fictioneer_load_checkmarks( $current_user ); $follows = fictioneer_load_follows( $current_user ); $reminders = fictioneer_load_reminders( $current_user ); diff --git a/single-fcn_chapter.php b/single-fcn_chapter.php index f9a6128d..f618a963 100644 --- a/single-fcn_chapter.php +++ b/single-fcn_chapter.php @@ -190,15 +190,15 @@ get_header( null, $header_args ); ); // Add stories list breadcrumb (if set) - $stories_page = intval( get_option( 'fictioneer_stories_page', -1 ) ); + $stories_page_id = intval( get_option( 'fictioneer_stories_page', -1 ) ?: -1 ); - if ( $stories_page > 0 ) { - $stories_page_title = trim( get_the_title( $stories_page ) ); + if ( $stories_page_id > 0 ) { + $stories_page_title = trim( get_the_title( $stories_page_id ) ); $stories_page_title = empty( $stories_page_title ) ? __( 'Stories', 'fictioneer' ) : $stories_page_title; $footer_args['breadcrumbs'][] = array( $stories_page_title, - get_permalink( $stories_page ) + fictioneer_get_assigned_page_link( 'fictioneer_stories_page' ) ); } diff --git a/single-fcn_collection.php b/single-fcn_collection.php index 434be0af..3fa192e8 100644 --- a/single-fcn_collection.php +++ b/single-fcn_collection.php @@ -115,15 +115,15 @@ ); // Add recommendation list breadcrumb (if set) - $collections_page = intval( get_option( 'fictioneer_collections_page', -1 ) ); + $collections_page_id = intval( get_option( 'fictioneer_collections_page', -1 ) ?: -1 ); - if ( $collections_page > 0 ) { - $collections_page_title = trim( get_the_title( $collections_page ) ); + if ( $collections_page_id > 0 ) { + $collections_page_title = trim( get_the_title( $collections_page_id ) ); $collections_page_title = empty( $collections_page_title ) ? __( 'Stories', 'fictioneer' ) : $collections_page_title; $footer_args['breadcrumbs'][] = array( $collections_page_title, - get_permalink( $collections_page ) + fictioneer_get_assigned_page_link( 'fictioneer_collections_page' ) ); } diff --git a/single-fcn_recommendation.php b/single-fcn_recommendation.php index 8db53a61..822605ca 100644 --- a/single-fcn_recommendation.php +++ b/single-fcn_recommendation.php @@ -70,15 +70,15 @@ ); // Add recommendations list breadcrumb (if set) - $rec_page = intval( get_option( 'fictioneer_recommendations_page', -1 ) ); + $rec_page_id = intval( get_option( 'fictioneer_recommendations_page', -1 ) ?: -1 ); - if ( $rec_page > 0 ) { - $rec_page_title = trim( get_the_title( $rec_page ) ); + if ( $rec_page_id > 0 ) { + $rec_page_title = trim( get_the_title( $rec_page_id ) ); $rec_page_title = empty( $rec_page_title ) ? __( 'Recommendations', 'fictioneer' ) : $rec_page_title; $footer_args['breadcrumbs'][] = array( $rec_page_title, - get_permalink( $rec_page ) + fictioneer_get_assigned_page_link( 'fictioneer_recommendations_page' ) ); } diff --git a/single-fcn_story.php b/single-fcn_story.php index bd392a08..73e2991f 100644 --- a/single-fcn_story.php +++ b/single-fcn_story.php @@ -99,15 +99,15 @@ get_header( null, $header_args ); ); // Add stories list breadcrumb (if set) - $stories_page = intval( get_option( 'fictioneer_stories_page', -1 ) ); + $stories_page_id = intval( get_option( 'fictioneer_stories_page', -1 ) ?: -1 ); - if ( $stories_page > 0 ) { - $stories_page_title = trim( get_the_title( $stories_page ) ); + if ( $stories_page_id > 0 ) { + $stories_page_title = trim( get_the_title( $stories_page_id ) ); $stories_page_title = empty( $stories_page_title ) ? __( 'Stories', 'fictioneer' ) : $stories_page_title; $footer_args['breadcrumbs'][] = array( $stories_page_title, - get_permalink( $stories_page ) + fictioneer_get_assigned_page_link( 'fictioneer_stories_page' ) ); } diff --git a/singular-bookmarks.php b/singular-bookmarks.php index 7c250524..ad072a42 100644 --- a/singular-bookmarks.php +++ b/singular-bookmarks.php @@ -77,7 +77,7 @@ if ( ! get_option( 'fictioneer_enable_bookmarks' ) ) {