File Manager
Viewing File: frontend.php
<?php
namespace Documentor;
if(!defined('ABSPATH')){
die('HACKING ATTEMPT');
}
class FrontEnd{
static function bridge_hub_redirect(){
if(is_tax('docs_hub')){
$term_id = get_queried_object_id();
$page_id = get_term_meta($term_id, 'documentor_hub_page_id', true);
if(empty($page_id)){
$pages = get_posts([
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => 1,
'fields' => 'ids',
'meta_query' => [
[
'key' => '_documentor_linked_hub_id',
'value' => $term_id,
]
]
]);
if(!empty($pages)){
$page_id = $pages[0];
// Repair the term meta
update_term_meta($term_id, 'documentor_hub_page_id', $page_id);
}
}
if(!empty($page_id)){
wp_safe_redirect(get_permalink($page_id), 301);
exit;
}
return;
}
}
static function override_archive_title($title){
// Check if this is a page acting as a Hub
$page_id = get_queried_object_id();
if($page_id > 0){
$hub_id = get_post_meta($page_id, '_documentor_linked_hub_id', true);
if(!empty($hub_id)){
$term = get_term($hub_id, 'docs_hub');
if($term && !is_wp_error($term)){
return $term->name;
}
}
}
return $title;
}
// Change the page title for Hubs
static function display_page_title($titles = []){
$page_title = '';
// Check if the user is currently viewing a Doc Hub taxonomy page
if(is_tax('docs_hub')){
$term = get_queried_object();
if($term && ! is_wp_error($term)){
$page_title = $term->name;
}
}
if(empty($page_title)){
return $titles;
}
$titles['title'] = $page_title;
return $titles;
}
static function override_pagelayer_return_url(){
if(empty($_GET['pagelayer-live'])){
return;
}
// Making sure the current post is a doc post
$post_id = get_queried_object_id();
if(empty($post_id) || get_post_type($post_id) !== documentor()->post_type){
return;
}
$current_hub = documentor_get_post_hub($post_id);
$return_url = admin_url('admin.php?page=documentor');
// Fetching the doc hub slug, to make sure it loads the exact hub tab
if(!empty($current_hub) && $current_hub instanceof \WP_Term && !empty($current_hub->slug)){
$return_url .= '#'.$current_hub->slug;
}
// We are overriding the return URL pagelayer sets, so that in place of
// the WordPress docs post list page it goes back to the documentor page
echo '<script type="text/javascript">pagelayer_returnURL = ' . wp_json_encode($return_url) . ';</script>';
}
// Loads the Docs hubs template on base docs page
static function load_hubs_template(){
global $pagelayer;
if(empty($pagelayer) || !is_object($pagelayer)){
return;
}
$settings = get_option('documentor_settings', []);
if(empty($settings)){
return;
}
$docs_page_id = (int) !empty($settings['docs_page_id']) ? $settings['docs_page_id'] : 0;
$show_hubs = !empty($settings['doc_hub_blocks']) && $settings['doc_hub_blocks'] === 'on';
if(empty($docs_page_id) || empty($show_hubs)){
return;
}
// Docs Page is being set with a ID of 0 so we need to check if
// its doc page by comparing if it is a post type of doc and is 0
$current_page_id = get_queried_object_id();
// Docs Page has
if(!is_post_type_archive('docs') || $current_page_id != 0){
return;
}
$query = new \WP_Query(array(
'post_type' => 'pagelayer-template',
'post_status' => array('publish'),
'posts_per_page' => 1,
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'documentor_imported_content',
'value' => sanitize_text_field('hub-template'),
'compare' => '=',
),
),
));
if(empty($query) || empty($query->posts)){
$template_id = 0;
} else {
$template_id = (int) $query->posts[0];
}
if(!empty($template_id)){
$pagelayer->template_post = $template_id;
}
return;
}
// Updates all the links to follow the doc hub slug in place of /docs
static function filter_mixed_docs_permalink($post_link, $post){
if('docs' !== $post->post_type){
return $post_link;
}
$hub = documentor_get_post_hub($post->ID);
if(!$hub){
return $post_link;
}
$hub_slug = $hub->slug;
$docs_archive_id = documentor()->get_option('docs_page_id', 'documentor_settings', false);
$docs_page = $docs_archive_id ? get_post($docs_archive_id) : false;
$base_slug = $docs_page ? get_post_field('post_name', $docs_page) : 'docs';
$default_base = home_url('/' . $base_slug . '/');
$new_base = home_url('/' . $hub_slug . '/');
return str_replace($default_base, $new_base, $post_link);
}
static function bridge_hub_links($url, $term, $taxonomy){
if($taxonomy !== 'docs_hub'){
return $url;
}
$term_id = is_object($term) ? $term->term_id : $term;
$page_id = get_term_meta($term_id, 'documentor_hub_page_id', true);
// Fallback: If the term meta is missing, try to find a page linked to this Hub via post meta
if(empty($page_id)){
$pages = get_posts([
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => 1,
'fields' => 'ids',
'meta_query' => [
[
'key' => '_documentor_linked_hub_id',
'value' => $term_id,
]
]
]);
if(!empty($pages)){
$page_id = $pages[0];
// Repair the term meta for future calls
update_term_meta($term_id, 'documentor_hub_page_id', $page_id);
}
}
if(!empty($page_id)){
return get_permalink($page_id);
}
return $url;
}
static function doc_hub_radio_metabox($post, $box){
$taxonomy = !empty($box['args']['taxonomy']) ? $box['args']['taxonomy'] : '';
if(!$taxonomy){
return;
}
$terms = get_terms([
'taxonomy' => $taxonomy,
'hide_empty' => false
]);
if(empty($terms) || is_wp_error($terms)){
echo '<p>No terms found.</p>';
return;
}
// Fetch the terms already assigned to this post
$post_terms = wp_get_post_terms($post->ID, $taxonomy, ['fields' => 'ids']);
$selected_term = empty($post_terms) ? 0 : $post_terms[0];
// A hidden field ensures the term is cleared if the user somehow submits without a selection
echo '<input type="hidden" name="tax_input[' . esc_attr($taxonomy) . '][]" value="0">';
echo '<ul class="categorychecklist form-no-clear">
<li><label><input type="radio" name="tax_input[' . esc_attr($taxonomy) . '][]" value="" '.(empty($selected_term) ? 'checked' : '').'>'.esc_html__('None', 'documentor').'</label></li>';
foreach($terms as $term){
$checked = checked($selected_term, $term->term_id, false);
echo '<li>';
echo '<label>';
// The radio button enforces a single selection on the front end
echo '<input type="radio" name="tax_input[' . esc_attr($taxonomy) . '][]" value="' . esc_attr($term->term_id) . '" ' . $checked . '> ';
echo esc_html($term->name);
echo '</label>';
echo '</li>';
}
echo '</ul>';
}
// Removing out Doc Hub meta box from pagelayes, we only want to show to top level doc, a doc without any Parent
// Which in our case is a section
static function remove_pl_hub_metabox($to_remove_metabox){
global $post;
if(!empty($post) && 'docs' === $post->post_type && !empty($post->post_parent)){
$to_remove_metabox[] = 'docs_hubdiv';
}
return $to_remove_metabox;
}
}