Add constant to toggle extended meta queries

This commit is contained in:
Tetrakern 2024-01-28 18:59:23 +01:00
parent 7d8de5260c
commit a35ebe7900
11 changed files with 190 additions and 43 deletions

View File

@ -1176,7 +1176,7 @@ Fictioneer loads the free version of [Font Awesome 6.4.2](https://fontawesome.co
### Constants
Some options are not available in the settings because tempering with them can break the theme or result in unexpected behavior. Those options are defined via constants in the **function.php**. If you want to change them, you need a [child theme](https://developer.wordpress.org/themes/advanced-topics/child-themes/). Just override them in the child themes own **function.php**, but only if you know what you are doing!
Some options are not available in the settings because tempering with them can break the theme or result in unexpected behavior. Those options are defined via constants in the **function.php**. If you want to change them, you need a [child theme](https://developer.wordpress.org/themes/advanced-topics/child-themes/) or access to your **wp-config.php**. Just override them in the child themes own **function.php**, but only if you know what you are doing!
```php
define( 'CONSTANT_NAME', value );
@ -1244,4 +1244,13 @@ define( 'CONSTANT_NAME', value );
| FICTIONEER_ENABLE_MENU_TRANSIENTS | boolean | Whether to cache nav menus as Transients. Default `true`.
| FICTIONEER_ORDER_STORIES_BY_LATEST_CHAPTER | boolean | Whether to order updated stories based on the latest chapter added, excluding stories without chapters. Default `false`.
| FICTIONEER_ENABLE_STORY_CHANGELOG | boolean | Whether changes to the story chapter list should be logged. Default `true`.
| FICTIONEER_ENABLE_BROWSER_NOTES | boolean | Whether to inform visitors of missing browser features. Default `true`.
| FICTIONEER_EXTEND_STORY_META_QUERY | boolean | Whether to use the extended meta query for stories. Default `true`.
| FICTIONEER_EXTEND_CHAPTER_META_QUERY | boolean | Whether to use the extended meta query for chapters. Default `true`.
| FICTIONEER_EXAMPLE_CHAPTER_ICONS | array | Collection of example Font Awesome icon class strings.
#### FICTIONEER_EXTEND_STORY_META_QUERY & FICTIONEER_EXTEND_CHAPTER_META_QUERY
These two constants technically make querying **lists** of stories and chapters slower, in exchange for keeping your database smaller. This is a long-lasting grievance with WordPress queries, which exclude posts that lack requested meta fields even if they evaluate to false. Accounting for that is expensive, but saving those otherwise superfluous meta fields can add thousands of rows to your database, slowing down your site as well. This is a tradeoff and if you have proper caching, the impact is negligible.
If you would rather have faster queries but store more rows in your database, you can set these constants to `false`. Once you have done this, head to **Fictioneer > Tools > Database Tools** and add the missing **[Story Hidden]** and **[Chapter Hidden]** fields, otherwise your lists will be empty. Also purge the theme caches. Anything else is taken care of by the theme, like allowing the fields to be stored in the future.

View File

@ -33,8 +33,12 @@ $query_args = array(
'orderby' => $orderby,
'paged' => $page,
'posts_per_page' => get_option( 'posts_per_page', 8 ),
'update_post_term_cache' => ! get_option( 'fictioneer_hide_taxonomies_on_chapter_cards' ),
'meta_query' => array(
'update_post_term_cache' => ! get_option( 'fictioneer_hide_taxonomies_on_chapter_cards' )
);
// Use extended meta query?
if ( FICTIONEER_EXTEND_CHAPTER_META_QUERY ) {
$query_args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'fictioneer_chapter_hidden',
@ -44,8 +48,15 @@ $query_args = array(
'key' => 'fictioneer_chapter_hidden',
'compare' => 'NOT EXISTS'
)
)
);
);
} else {
$query_args['meta_query'] = array(
array(
'key' => 'fictioneer_chapter_hidden',
'value' => '0'
)
);
}
// Append date query (if any)
$query_args = fictioneer_append_date_query( $query_args, $ago, $orderby );

View File

@ -376,11 +376,21 @@ if ( ! defined( 'FICTIONEER_ENABLE_STORY_CHANGELOG' ) ) {
define( 'FICTIONEER_ENABLE_STORY_CHANGELOG', true );
}
// Boolean: Enable tracking of chapter changes in stories
// Boolean: Enable showing of outdated browsers' missing features
if ( ! defined( 'FICTIONEER_ENABLE_BROWSER_NOTES' ) ) {
define( 'FICTIONEER_ENABLE_BROWSER_NOTES', true );
}
// Boolean: Enable extended story meta query
if ( ! defined( 'FICTIONEER_EXTEND_STORY_META_QUERY' ) ) {
define( 'FICTIONEER_EXTEND_STORY_META_QUERY', true );
}
// Boolean: Enable extended chapter meta query
if ( ! defined( 'FICTIONEER_EXTEND_CHAPTER_META_QUERY' ) ) {
define( 'FICTIONEER_EXTEND_CHAPTER_META_QUERY', true );
}
/*
* Arrays
*/

View File

@ -313,4 +313,46 @@ if ( FICTIONEER_ENABLE_STICKY_CARDS ) {
add_filter( 'posts_clauses', 'fictioneer_clause_sticky_stories', 10, 2 );
}
// =============================================================================
// LIST META QUERIES
// =============================================================================
/**
* Adds 'fictioneer_chapter_hidden' to be saved falsy
*
* @since 5.9.4
*
* @param array $allowed Array of allowed falsy meta fields.
*
* @return array The updated array.
*/
function fictioneer_allow_falsy_chapter_hidden( $allowed ) {
$allowed[] = 'fictioneer_chapter_hidden';
return $allowed;
}
if ( ! FICTIONEER_EXTEND_CHAPTER_META_QUERY ) {
add_filter( 'fictioneer_filter_falsy_meta_allow_list', 'fictioneer_allow_falsy_chapter_hidden' );
}
/**
* Adds 'fictioneer_story_hidden' to be saved falsy
*
* @since 5.9.4
*
* @param array $allowed Array of allowed falsy meta fields.
*
* @return array The updated array.
*/
function fictioneer_allow_falsy_story_hidden( $allowed ) {
$allowed[] = 'fictioneer_story_hidden';
return $allowed;
}
if ( ! FICTIONEER_EXTEND_STORY_META_QUERY ) {
add_filter( 'fictioneer_filter_falsy_meta_allow_list', 'fictioneer_allow_falsy_story_hidden' );
}
?>

View File

@ -45,20 +45,30 @@ $query_args = array(
'orderby' => $args['orderby'],
'posts_per_page' => $args['count'] + 8, // Little buffer in case of unpublished parent story
'no_found_rows' => true,
'update_post_term_cache' => false,
'meta_query' => array(
'update_post_term_cache' => false
);
// Use extended meta query?
if ( FICTIONEER_EXTEND_CHAPTER_META_QUERY ) {
$query_args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'fictioneer_chapter_hidden',
'compare' => 'NOT EXISTS'
'value' => '0'
),
array(
'key' => 'fictioneer_chapter_hidden',
'value' => 0,
'compare' => '='
'compare' => 'NOT EXISTS'
)
)
);
);
} else {
$query_args['meta_query'] = array(
array(
'key' => 'fictioneer_chapter_hidden',
'value' => '0'
)
);
}
// Author?
if ( ! empty( $args['author'] ) ) {

View File

@ -46,20 +46,30 @@ $query_args = array(
'order' => $args['order'],
'posts_per_page' => $args['count'] + 8, // Little buffer in case of unpublished parent story
'no_found_rows' => true,
'update_post_term_cache' => false,
'meta_query' => array(
'update_post_term_cache' => false
);
// Use extended meta query?
if ( FICTIONEER_EXTEND_CHAPTER_META_QUERY ) {
$query_args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'fictioneer_chapter_hidden',
'compare' => 'NOT EXISTS'
'value' => '0'
),
array(
'key' => 'fictioneer_chapter_hidden',
'value' => 0,
'compare' => '='
'compare' => 'NOT EXISTS'
)
)
);
);
} else {
$query_args['meta_query'] = array(
array(
'key' => 'fictioneer_chapter_hidden',
'value' => '0'
)
);
}
// Author?
if ( ! empty( $args['author'] ) ) {

View File

@ -43,7 +43,12 @@ $query_args = array(
'order' => $args['order'],
'orderby' => $args['orderby'],
'posts_per_page' => $args['count'],
'meta_query' => array(
'no_found_rows' => true
);
// Use extended meta query?
if ( FICTIONEER_EXTEND_STORY_META_QUERY ) {
$query_args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'fictioneer_story_hidden',
@ -53,9 +58,15 @@ $query_args = array(
'key' => 'fictioneer_story_hidden',
'compare' => 'NOT EXISTS'
)
),
'no_found_rows' => true
);
);
} else {
$query_args['meta_query'] = array(
array(
'key' => 'fictioneer_story_hidden',
'value' => '0'
)
);
}
// Author?
if ( ! empty( $args['author'] ) ) {

View File

@ -40,7 +40,12 @@ $query_args = array(
'order' => $args['order'],
'orderby' => $args['orderby'],
'posts_per_page' => $args['count'],
'meta_query' => array(
'no_found_rows' => true
);
// Use extended meta query?
if ( FICTIONEER_EXTEND_STORY_META_QUERY ) {
$query_args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'fictioneer_story_hidden',
@ -50,9 +55,15 @@ $query_args = array(
'key' => 'fictioneer_story_hidden',
'compare' => 'NOT EXISTS'
)
),
'no_found_rows' => true
);
);
} else {
$query_args['meta_query'] = array(
array(
'key' => 'fictioneer_story_hidden',
'value' => '0'
)
);
}
// Author?
if ( ! empty( $args['author'] ) ) {

View File

@ -45,7 +45,13 @@ $query_args = array(
'orderby' => 'meta_value',
'meta_key' => 'fictioneer_chapters_added',
'posts_per_page' => $args['count'] + 4, // Account for non-eligible posts!
'meta_query' => array(
'no_found_rows' => true,
'update_post_term_cache' => false
);
// Use extended meta query?
if ( FICTIONEER_EXTEND_STORY_META_QUERY ) {
$query_args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'fictioneer_story_hidden',
@ -54,11 +60,16 @@ $query_args = array(
array(
'key' => 'fictioneer_story_hidden',
'compare' => 'NOT EXISTS'
),
),
'no_found_rows' => true,
'update_post_term_cache' => false
);
)
);
} else {
$query_args['meta_query'] = array(
array(
'key' => 'fictioneer_story_hidden',
'value' => '0'
)
);
}
// Author?
if ( ! empty( $args['author'] ) ) {

View File

@ -47,7 +47,12 @@ $query_args = array(
'orderby' => 'meta_value',
'meta_key' => 'fictioneer_chapters_added',
'posts_per_page' => $args['count'] + 4, // Account for non-eligible posts!
'meta_query' => array(
'no_found_rows' => true
);
// Use extended meta query?
if ( FICTIONEER_EXTEND_STORY_META_QUERY ) {
$query_args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'fictioneer_story_hidden',
@ -56,10 +61,16 @@ $query_args = array(
array(
'key' => 'fictioneer_story_hidden',
'compare' => 'NOT EXISTS'
),
),
'no_found_rows' => true
);
)
);
} else {
$query_args['meta_query'] = array(
array(
'key' => 'fictioneer_story_hidden',
'value' => '0'
)
);
}
// Author?
if ( ! empty( $args['author'] ) ) {

View File

@ -34,8 +34,12 @@ $query_args = array (
'orderby' => $orderby,
'paged' => $page,
'posts_per_page' => get_option( 'posts_per_page', 8 ),
'update_post_term_cache' => ! get_option( 'fictioneer_hide_taxonomies_on_story_cards' ),
'meta_query' => array(
'update_post_term_cache' => ! get_option( 'fictioneer_hide_taxonomies_on_story_cards' )
);
// Use extended meta query?
if ( FICTIONEER_EXTEND_STORY_META_QUERY ) {
$query_args['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'fictioneer_story_hidden',
@ -45,8 +49,15 @@ $query_args = array (
'key' => 'fictioneer_story_hidden',
'compare' => 'NOT EXISTS'
)
)
);
);
} else {
$query_args['meta_query'] = array(
array(
'key' => 'fictioneer_story_hidden',
'value' => '0'
)
);
}
// Order by latest chapter update timestamp?
if ( FICTIONEER_ORDER_STORIES_BY_LATEST_CHAPTER && $orderby === 'modified' ) {