| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
/*---------------------------------------------------------*/ |
| 5 |
/* Compatibility Mode Functions */ |
| 6 |
/*---------------------------------------------------------*/ |
| 7 |
|
| 8 |
function mbt_is_compatability_mode_on() { |
| 9 |
return apply_filters('mbt_is_compatability_mode_on', mbt_get_setting('compatibility_mode')); |
| 10 |
} |
| 11 |
|
| 12 |
function mbt_compat_init() { |
| 13 |
if(mbt_is_compatability_mode_on()) { |
| 14 |
//modify the post query |
| 15 |
add_action('pre_get_posts', 'mbt_compat_pre_get_posts', 30); |
| 16 |
add_action('wp', 'mbt_compat_override_query_posts', -999); |
| 17 |
|
| 18 |
//override page template |
| 19 |
remove_filter('template_include', 'mbt_load_book_templates'); |
| 20 |
add_filter('template_include', 'mbt_compat_load_book_templates'); |
| 21 |
|
| 22 |
//remove admin bar links that don't work in compatability mode |
| 23 |
add_action('wp_before_admin_bar_render', 'mbt_compat_remove_admin_bar_links'); |
| 24 |
} |
| 25 |
} |
| 26 |
add_action('mbt_init', 'mbt_compat_init', 11); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/*---------------------------------------------------------*/ |
| 31 |
/* Template Overload Functions */ |
| 32 |
/*---------------------------------------------------------*/ |
| 33 |
|
| 34 |
function mbt_compat_remove_admin_bar_links() { |
| 35 |
global $wp_admin_bar, $post; |
| 36 |
if($post && $post->ID === -1) { $wp_admin_bar->remove_menu('edit'); } |
| 37 |
} |
| 38 |
|
| 39 |
function mbt_compat_custom_page_content($content) { |
| 40 |
global $post; |
| 41 |
|
| 42 |
if(mbt_has_template_context('compatability') or mbt_has_template_context('shortcode')) { return $content; } |
| 43 |
|
| 44 |
$context = ''; |
| 45 |
|
| 46 |
if((mbt_is_booktable_page() and $post->ID == mbt_get_setting('booktable_page')) or (mbt_is_archive_query() and $post->ID == -1)) { |
| 47 |
$context = 'archive'; |
| 48 |
} else if(is_singular('mbt_book')) { |
| 49 |
$context = 'single'; |
| 50 |
} |
| 51 |
|
| 52 |
if($context) { |
| 53 |
//tweak $wp_current_filter in order to allow jetpack sharing to work |
| 54 |
global $wp_current_filter; |
| 55 |
$content_filter = array_pop($wp_current_filter); |
| 56 |
|
| 57 |
ob_start(); |
| 58 |
|
| 59 |
mbt_start_template_context('compatability'); |
| 60 |
if($context == 'archive') { |
| 61 |
mbt_load_archive_query(); |
| 62 |
mbt_start_template_context('archive'); |
| 63 |
?> <div id="mbt-container"> <?php |
| 64 |
do_action('mbt_book_archive_content'); |
| 65 |
?> </div> <?php |
| 66 |
mbt_end_template_context(); |
| 67 |
mbt_unload_archive_query(); |
| 68 |
} else if($context == 'single') { |
| 69 |
$display_mode = mbt_get_book_display_mode($post->ID); |
| 70 |
mbt_start_template_context('single'); |
| 71 |
mbt_start_template_display_mode($display_mode); |
| 72 |
?> <div id="mbt-container"> <?php |
| 73 |
do_action('mbt_single_book_'.$display_mode.'_content'); |
| 74 |
?> </div> <?php |
| 75 |
mbt_end_template_display_mode(); |
| 76 |
mbt_end_template_context(); |
| 77 |
} |
| 78 |
mbt_end_template_context(); |
| 79 |
|
| 80 |
$content = ob_get_contents(); |
| 81 |
ob_end_clean(); |
| 82 |
|
| 83 |
//undo $wp_current_filter tweak |
| 84 |
$wp_current_filter[] = $content_filter; |
| 85 |
} |
| 86 |
|
| 87 |
return $content; |
| 88 |
} |
| 89 |
|
| 90 |
function mbt_compat_load_book_templates($template) { |
| 91 |
global $post; |
| 92 |
if(is_singular('mbt_book') or mbt_is_archive_query()) { |
| 93 |
if(is_singular('mbt_book') and !mbt_book_display_mode_supports(mbt_get_book_display_mode($post->ID), 'embedding')) { return mbt_load_book_templates($template); } |
| 94 |
// STORMHILL FIX 3.6.2: Block themes (FSE, e.g. Twenty Twenty-Five) have no page.php, |
| 95 |
// and their index.php is a silent stub that renders nothing. Don't override the template |
| 96 |
// for block themes — let WordPress load its own block template and inject content via |
| 97 |
// the_content filter, which the core/post-content block applies correctly. |
| 98 |
if ( ! function_exists( 'wp_is_block_theme' ) || ! wp_is_block_theme() ) { |
| 99 |
$template = locate_template('page.php'); |
| 100 |
if(empty($template)) { $template = locate_template('index.php'); } |
| 101 |
} |
| 102 |
add_filter('the_content', 'mbt_compat_custom_page_content', 999, 2); |
| 103 |
} else if(mbt_is_booktable_page()) { |
| 104 |
add_filter('the_content', 'mbt_compat_custom_page_content', 999, 2); |
| 105 |
} |
| 106 |
return $template; |
| 107 |
} |
| 108 |
|
| 109 |
function mbt_compat_pre_get_posts($query) { |
| 110 |
if(!is_admin() and $query->is_main_query() and ($query->is_post_type_archive('mbt_book') or $query->is_tax('mbt_author') or $query->is_tax('mbt_series') or $query->is_tax('mbt_genre') or $query->is_tax('mbt_tag'))) { |
| 111 |
global $mbt_archive_query; |
| 112 |
if($query->get('mbt_author')) { |
| 113 |
$mbt_archive_query = new WP_Query(array('post_type' => 'mbt_book', 'mbt_author' => $query->get('mbt_author'), 'paged' => $query->get('paged'), 'orderby' => 'menu_order', 'posts_per_page' => mbt_get_posts_per_page())); |
| 114 |
} else if($query->get('mbt_series')) { |
| 115 |
$mbt_archive_query = new WP_Query(array('post_type' => 'mbt_book', 'mbt_series' => $query->get('mbt_series'), 'paged' => $query->get('paged'), 'orderby' => 'meta_value_num', 'meta_key' => 'mbt_series_order', 'order' => 'ASC', 'posts_per_page' => mbt_get_posts_per_page())); |
| 116 |
} else if($query->get('mbt_genre')) { |
| 117 |
$mbt_archive_query = new WP_Query(array('post_type' => 'mbt_book', 'mbt_genre' => $query->get('mbt_genre'), 'paged' => $query->get('paged'), 'orderby' => 'menu_order', 'posts_per_page' => mbt_get_posts_per_page())); |
| 118 |
} else if($query->get('mbt_tag')) { |
| 119 |
$mbt_archive_query = new WP_Query(array('post_type' => 'mbt_book', 'mbt_tag' => $query->get('mbt_tag'), 'paged' => $query->get('paged'), 'orderby' => 'menu_order', 'posts_per_page' => mbt_get_posts_per_page())); |
| 120 |
} else { |
| 121 |
$mbt_archive_query = new WP_Query(array('post_type' => 'mbt_book', 'paged' => $query->get('paged'), 'orderby' => 'menu_order', 'posts_per_page' => mbt_get_posts_per_page())); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
function mbt_compat_override_query_posts() { |
| 127 |
if(mbt_is_archive_query()) { |
| 128 |
//ID must be -1, not 0, or get_post_meta will (inexplicably) return false instead of "", which some themes (such as Divi theme) can't handle. There is no hook in get_post_meta to prevent this. |
| 129 |
$post = new WP_Post((object)array( |
| 130 |
"ID" => -1, |
| 131 |
"post_author" => "1", |
| 132 |
"post_date" => "", |
| 133 |
"post_date_gmt" => "", |
| 134 |
"post_modified" => "", |
| 135 |
"post_modified_gmt" => "", |
| 136 |
"post_content" => "", |
| 137 |
"post_content_filtered" => "", |
| 138 |
"post_excerpt" => "", |
| 139 |
"post_title" => mbt_get_book_archive_title(), |
| 140 |
"post_status" => "publish", |
| 141 |
"comment_status" => "closed", |
| 142 |
"ping_status" => "closed", |
| 143 |
"post_password" => "", |
| 144 |
"post_name" => "", |
| 145 |
"to_ping" => "", |
| 146 |
"pinged" => "", |
| 147 |
"post_parent" => 0, |
| 148 |
"guid" => "", |
| 149 |
"menu_order" => 0, |
| 150 |
"post_type" => "page", |
| 151 |
"post_mime_type" => "", |
| 152 |
"comment_count" => "0", |
| 153 |
"filter" => "raw", |
| 154 |
)); |
| 155 |
|
| 156 |
// This solves the get_post($post->ID) error when ID is -1! |
| 157 |
wp_cache_set(-1, $post, 'posts'); |
| 158 |
|
| 159 |
global $wp_query; |
| 160 |
$wp_query->post = $post; |
| 161 |
$wp_query->posts = array($post); |
| 162 |
$wp_query->post_count = 1; |
| 163 |
$wp_query->is_page = true; |
| 164 |
$wp_query->is_singular = true; |
| 165 |
$wp_query->is_tax = false; |
| 166 |
$wp_query->is_archive = false; |
| 167 |
$wp_query->is_post_type_archive = false; |
| 168 |
$wp_query->queried_object = $post; |
| 169 |
$wp_query->queried_object_id = -1; |
| 170 |
$GLOBALS['post'] = $wp_query->post; |
| 171 |
$GLOBALS['posts'] = &$wp_query->posts; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
function mbt_is_archive_query() { |
| 176 |
global $mbt_archive_query; |
| 177 |
return !empty($mbt_archive_query); |
| 178 |
} |
| 179 |
|
| 180 |
function mbt_load_archive_query() { |
| 181 |
global $mbt_archive_query; |
| 182 |
if(!empty($mbt_archive_query)) { mbt_push_query('mbt_archive_query', $mbt_archive_query); } |
| 183 |
} |
| 184 |
|
| 185 |
function mbt_unload_archive_query() { |
| 186 |
mbt_pop_query('mbt_archive_query'); |
| 187 |
} |
| 188 |
|