Move page assignment links to options table

This commit is contained in:
Tetrakern 2023-08-03 23:51:04 +02:00
parent f0b6c6d42d
commit 8ce5507925
14 changed files with 222 additions and 125 deletions

View File

@ -11,7 +11,7 @@
<?php
// Setup
$custom_404 = intval( get_option( 'fictioneer_404_page', -1 ) );
$custom_404 = intval( get_option( 'fictioneer_404_page', -1 ) ?: -1 );
?>

View File

@ -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;
}
}
?>

View File

@ -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;
}
}
?>

View File

@ -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

View File

@ -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

View File

@ -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);
?>

View File

@ -16,13 +16,13 @@
<?php
// Setup
$bookmarks_link = get_permalink( get_option( 'fictioneer_bookmarks_page' ) );
$bookmarks_link = fictioneer_get_assigned_page_link( 'fictioneer_bookmarks_page' );
$discord_invite_link = get_option( 'fictioneer_discord_invite_link' );
$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 );
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' );
}
?>

View File

@ -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 );

View File

@ -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' )
);
}

View File

@ -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' )
);
}

View File

@ -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' )
);
}

View File

@ -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' )
);
}

View File

@ -77,7 +77,7 @@ if ( ! get_option( 'fictioneer_enable_bookmarks' ) ) {
<?php
// Get profile link
$profile_link = get_permalink( get_option( 'fictioneer_user_profile_page' ) );
$profile_link = fictioneer_get_assigned_page_link( 'fictioneer_user_profile_page' );
$profile_link = $profile_link ? $profile_link : get_edit_profile_url();
// Footer arguments

View File

@ -196,7 +196,7 @@ $tabs[ $current_tab ]['classes'][] = '_current';
<?php
// Get profile link
$profile_link = get_permalink( get_option( 'fictioneer_user_profile_page' ) );
$profile_link = fictioneer_get_assigned_page_link( 'fictioneer_user_profile_page' );
$profile_link = $profile_link ? $profile_link : get_edit_profile_url();
// Footer arguments