hesabixWpTheme/inc/wiki-bridge.php

124 lines
3.1 KiB
PHP

<?php
/**
* یکپارچگی دانشنامه MediaWiki با قالب Hesabix2
*
* @package Hesabix2
*/
defined( 'ABSPATH' ) || exit;
/**
* @param array<int, string> $classes
* @return array<int, string>
*/
function hesabix2_wiki_body_class( $classes ) {
if ( function_exists( 'hwiki_is_wiki_request' ) && hwiki_is_wiki_request() ) {
$classes[] = 'hx-wiki-page';
}
return $classes;
}
add_filter( 'body_class', 'hesabix2_wiki_body_class' );
/**
* Breadcrumb برای صفحات دانشنامه.
*
* @param array<int, array<string, string>> $items
* @return array<int, array<string, string>>
*/
function hesabix2_wiki_breadcrumb_items( $items ) {
if ( ! function_exists( 'hwiki_is_wiki_request' ) || ! hwiki_is_wiki_request() ) {
return $items;
}
$hub_label = __( 'دانشنامه', 'hesabix2' );
$hub_url = function_exists( 'hwiki_hub_url' ) ? hwiki_hub_url() : home_url( '/docs/' );
$out = array(
array(
'label' => __( 'خانه', 'hesabix2' ),
'url' => home_url( '/' ),
),
array(
'label' => $hub_label,
'url' => $hub_url,
),
);
$view = get_query_var( 'hwiki_view' );
if ( 'search' === $view ) {
$q = isset( $_GET['q'] ) ? sanitize_text_field( wp_unslash( (string) $_GET['q'] ) ) : '';
$out[] = array(
'label' => $q
? sprintf(
/* translators: %s: query */
__( 'جستجو: %s', 'hesabix2' ),
$q
)
: __( 'جستجو', 'hesabix2' ),
);
return $out;
}
if ( 'article' === $view && class_exists( 'Hwiki_Router' ) ) {
$title = Hwiki_Router::instance()->current_article_title();
if ( $title ) {
$out[] = array( 'label' => $title );
}
return $out;
}
return $out;
}
add_filter( 'hesabix2_seo_breadcrumb_items', 'hesabix2_wiki_breadcrumb_items', 5 );
/**
* @return string
*/
function hesabix2_wiki_hub_url() {
return function_exists( 'hwiki_hub_url' ) ? hwiki_hub_url() : home_url( '/docs/' );
}
/**
* JSON-LD TechArticle در صفحات مقاله.
*/
function hesabix2_wiki_schema_article() {
if ( ! function_exists( 'hwiki_is_wiki_request' ) || ! hwiki_is_wiki_request() ) {
return;
}
if ( 'article' !== get_query_var( 'hwiki_view' ) || ! class_exists( 'Hwiki_Router' ) ) {
return;
}
$ctx = Hwiki_Router::template_context();
if ( empty( $ctx['article'] ) ) {
return;
}
$article = $ctx['article'];
$wiki_url = ! empty( $article['wiki_url'] )
? $article['wiki_url']
: ( class_exists( 'Hwiki_SEO' ) ? Hwiki_SEO::canonical_url() : '' );
$schema = array(
'@context' => 'https://schema.org',
'@type' => 'TechArticle',
'headline' => $article['display_title'] ?? $ctx['title'],
'url' => $wiki_url ?: ( $article['site_url'] ?? hwiki_article_url( $ctx['title'] ) ),
'inLanguage' => 'fa',
'isPartOf' => array(
'@type' => 'WebSite',
'name' => get_bloginfo( 'name' ),
'url' => home_url( '/' ),
),
);
if ( $wiki_url ) {
$schema['mainEntityOfPage'] = array(
'@type' => 'WebPage',
'@id' => $wiki_url,
);
}
echo '<script type="application/ld+json">' . wp_json_encode( $schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ) . '</script>' . "\n";
}
add_action( 'wp_head', 'hesabix2_wiki_schema_article', 20 );