hesabixWpTheme/inc/seo.php

1582 lines
51 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* سئو، اسکیما، بازدید پست و بهینه‌سازی head
*
* @package Hesabix2
*/
if (!defined('ABSPATH')) {
exit;
}
/* -------------------------------------------------------------------------
* کمکی
* ------------------------------------------------------------------------- */
/**
* آیا Yoast SEO فعال است؟
*/
function hesabix2_seo_yoast_active() {
return defined('WPSEO_VERSION') || class_exists('WPSEO_Options', false);
}
/**
* غیرفعال کردن breadcrumb HTML یواست (نان‌راه قالب جایگزین است).
*
* @param array $links
* @return array
*/
function hesabix2_seo_disable_yoast_breadcrumb($links) {
return array();
}
add_filter('wpseo_breadcrumb_links', 'hesabix2_seo_disable_yoast_breadcrumb');
/**
* غیرفعال کردن BreadcrumbList یواست — خالی کردن wpseo_breadcrumb_links باعث itemListElement:[] می‌شود.
*/
function hesabix2_seo_disable_yoast_breadcrumb_schema() {
return false;
}
add_filter('wpseo_schema_needs_breadcrumb', 'hesabix2_seo_disable_yoast_breadcrumb_schema');
/**
* حذف ارجاع breadcrumb از WebPage یواست وقتی BreadcrumbList قالب جایگزین است.
*
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
function hesabix2_seo_yoast_webpage_remove_breadcrumb_ref($data) {
if (isset($data['breadcrumb'])) {
unset($data['breadcrumb']);
}
return $data;
}
add_filter('wpseo_schema_webpage', 'hesabix2_seo_yoast_webpage_remove_breadcrumb_ref');
/**
* حذف BreadcrumbList خالی از گراف یواست (پشتیبان در صورت تداخل افزونه).
*
* @param array<int, array<string, mixed>> $graph
* @return array<int, array<string, mixed>>
*/
function hesabix2_seo_yoast_strip_empty_breadcrumb_schema($graph) {
return array_values(array_filter($graph, function ($piece) {
if (!is_array($piece)) {
return true;
}
$types = isset($piece['@type']) ? (array) $piece['@type'] : array();
if (!in_array('BreadcrumbList', $types, true)) {
return true;
}
return !empty($piece['itemListElement']) && is_array($piece['itemListElement']);
}));
}
add_filter('wpseo_schema_graph', 'hesabix2_seo_yoast_strip_empty_breadcrumb_schema', 99);
/**
* چاپ JSON-LD
*
* @param array<string, mixed> $data
*/
function hesabix2_seo_print_json_ld(array $data) {
if (empty($data)) {
return;
}
echo '<script type="application/ld+json">' . wp_json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . '</script>' . "\n";
}
/**
* URL تصویر پیش‌فرض برای OG / schema
*/
function hesabix2_seo_default_image_url() {
if (function_exists('hesabix2_fp_hero_preview_url')) {
$hero = hesabix2_fp_hero_preview_url();
if ($hero) {
return $hero;
}
}
$site_icon = get_site_icon_url(512);
if ($site_icon) {
return $site_icon;
}
$custom_logo_id = get_theme_mod('custom_logo');
if ($custom_logo_id) {
$src = wp_get_attachment_image_src($custom_logo_id, 'full');
if (!empty($src[0])) {
return $src[0];
}
}
return '';
}
/**
* لوگوی سازمان برای schema
*
* @return array<string, string>|null
*/
function hesabix2_seo_organization_logo() {
$url = hesabix2_seo_default_image_url();
if (!$url) {
return null;
}
return array(
'@type' => 'ImageObject',
'url' => $url,
);
}
/**
* داده Organization مشترک
*
* @return array<string, mixed>
*/
function hesabix2_seo_organization_entity() {
$same_as = array_filter(array(
'https://t.me/hesabixacc',
'https://ble.ir/join/AZcuMtW5u8',
'https://source.hesabix.ir',
'https://wiki.hesabix.ir',
));
$org = array(
'@type' => 'Organization',
'@id' => home_url('/#organization'),
'name' => get_bloginfo('name'),
'url' => home_url('/'),
'sameAs' => array_values($same_as),
'parentOrganization' => array(
'@type' => 'Organization',
'name' => function_exists('hesabix2_brand_company_name')
? hesabix2_brand_company_name()
: __('شرکت توسعه فناوری سرکش', 'hesabix2'),
),
'brand' => array(
'@type' => 'Brand',
'name' => 'Hesabix',
'alternateName' => 'حسابیکس',
),
);
$logo = hesabix2_seo_organization_logo();
if ($logo) {
$org['logo'] = $logo;
}
$org['contactPoint'] = array(
'@type' => 'ContactPoint',
'contactType' => 'customer support',
'email' => 'info@hesabix.ir',
'telephone' => '+98-21-91550290',
'areaServed' => 'IR',
'availableLanguage' => array('Persian', 'fa'),
);
return apply_filters('hesabix2_seo_organization', $org);
}
/* -------------------------------------------------------------------------
* فاز ۱: بازدید پست
* ------------------------------------------------------------------------- */
/**
* آیا این بازدید قبلاً در ۲۴ ساعت گذشته شمرده شده؟
*/
function hesabix2_post_view_cookie_name($post_id) {
return 'hesabix2_pv_' . (int) $post_id;
}
function hesabix2_post_view_already_counted($post_id) {
$name = hesabix2_post_view_cookie_name($post_id);
return isset($_COOKIE[ $name ]) && $_COOKIE[ $name ] !== '';
}
function hesabix2_mark_post_view_counted($post_id) {
$name = hesabix2_post_view_cookie_name($post_id);
if (headers_sent()) {
return;
}
setcookie(
$name,
'1',
time() + DAY_IN_SECONDS,
COOKIEPATH ? COOKIEPATH : '/',
COOKIE_DOMAIN,
is_ssl(),
true
);
$_COOKIE[ $name ] = '1';
}
function hesabix2_set_post_views($post_id) {
$post_id = (int) $post_id;
if ($post_id < 1 || get_post_status($post_id) !== 'publish') {
return false;
}
$key = 'post_views_count';
$values = get_post_meta($post_id, $key, false);
if (empty($values)) {
update_post_meta($post_id, $key, 1);
return true;
}
if (count($values) > 1) {
$total = 0;
foreach ($values as $v) {
$total += (int) $v;
}
delete_post_meta($post_id, $key);
update_post_meta($post_id, $key, $total + 1);
return true;
}
update_post_meta($post_id, $key, ((int) $values[0]) + 1);
return true;
}
function hesabix2_get_post_views($post_id) {
$post_id = (int) $post_id;
$key = 'post_views_count';
$values = get_post_meta($post_id, $key, false);
if (empty($values)) {
return '0';
}
if (count($values) > 1) {
$total = 0;
foreach ($values as $v) {
$total += (int) $v;
}
delete_post_meta($post_id, $key);
update_post_meta($post_id, $key, $total);
return (string) $total;
}
return (string) max(0, (int) $values[0]);
}
/**
* ثبت یک بازدید (با کوکی ۲۴ساعته برای جلوگیری از دوباره‌شماری رفرش)
*/
function hesabix2_record_post_view($post_id) {
$post_id = (int) $post_id;
if ($post_id < 1) {
return false;
}
if (hesabix2_post_view_already_counted($post_id)) {
return false;
}
if (!hesabix2_set_post_views($post_id)) {
return false;
}
hesabix2_mark_post_view_counted($post_id);
return true;
}
function hesabix2_track_post_views() {
if (!is_singular('post')) {
return;
}
$post_id = (int) get_queried_object_id();
if ($post_id > 0) {
hesabix2_record_post_view($post_id);
}
}
add_action('template_redirect', 'hesabix2_track_post_views', 20);
/**
* ثبت بازدید از طریق AJAX (برای صفحات کش‌شده)
*/
function hesabix2_ajax_track_post_view() {
if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'hesabix2_track_view')) {
wp_send_json_error(array('message' => 'forbidden'), 403);
}
$post_id = isset($_POST['post_id']) ? absint($_POST['post_id']) : 0;
if ($post_id < 1 || get_post_type($post_id) !== 'post' || get_post_status($post_id) !== 'publish') {
wp_send_json_error(array('message' => 'invalid'), 400);
}
hesabix2_record_post_view($post_id);
wp_send_json_success(array(
'views' => hesabix2_get_post_views($post_id),
));
}
add_action('wp_ajax_hesabix2_track_post_view', 'hesabix2_ajax_track_post_view');
add_action('wp_ajax_nopriv_hesabix2_track_post_view', 'hesabix2_ajax_track_post_view');
/* -------------------------------------------------------------------------
* فاز ۱: فونت — preload + @font-face سبک (بدون base64)
* ------------------------------------------------------------------------- */
function hesabix2_font_base_uri() {
$theme_dir = get_template_directory();
$theme_uri = get_template_directory_uri();
$probe = '/assets/fonts/woff2/YekanBakhFaNum-Regular.woff2';
if (file_exists($theme_dir . $probe)) {
return $theme_uri . '/assets/fonts';
}
$legacy_dir = WP_CONTENT_DIR . '/themes/hesabix';
if (file_exists($legacy_dir . $probe)) {
return content_url('/themes/hesabix/assets/fonts');
}
return $theme_uri . '/assets/fonts';
}
function hesabix2_seo_font_preload() {
$font_base = hesabix2_font_base_uri();
$regular = $font_base . '/woff2/YekanBakhFaNum-Regular.woff2';
$bold = $font_base . '/woff2/YekanBakhFaNum-Bold.woff2';
echo '<link rel="preload" href="' . esc_url($regular) . '" as="font" type="font/woff2" crossorigin>' . "\n";
echo '<link rel="preload" href="' . esc_url($bold) . '" as="font" type="font/woff2" crossorigin>' . "\n";
}
add_action('wp_head', 'hesabix2_seo_font_preload', 2);
function hesabix2_seo_font_faces() {
$font_base = hesabix2_font_base_uri();
?>
<style id="hesabix2-fonts">
@font-face {
font-family: 'Yekan Bakh FaNum';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url('<?php echo esc_url($font_base); ?>/woff2/YekanBakhFaNum-Regular.woff2') format('woff2'),
url('<?php echo esc_url($font_base); ?>/woff/YekanBakhFaNum-Regular.woff') format('woff');
}
@font-face {
font-family: 'Yekan Bakh FaNum';
font-style: normal;
font-weight: 600;
font-display: swap;
src: url('<?php echo esc_url($font_base); ?>/woff2/YekanBakhFaNum-Bold.woff2') format('woff2'),
url('<?php echo esc_url($font_base); ?>/woff/YekanBakhFaNum-Bold.woff') format('woff');
}
@font-face {
font-family: 'Yekan Bakh FaNum';
font-style: normal;
font-weight: 700;
font-display: swap;
src: url('<?php echo esc_url($font_base); ?>/woff2/YekanBakhFaNum-Bold.woff2') format('woff2'),
url('<?php echo esc_url($font_base); ?>/woff/YekanBakhFaNum-Bold.woff') format('woff');
}
@font-face {
font-family: 'Yekan Bakh FaNum';
font-style: normal;
font-weight: 100;
font-display: swap;
src: url('<?php echo esc_url($font_base); ?>/woff2/YekanBakhFaNum-Thin.woff2') format('woff2');
}
@font-face {
font-family: 'Yekan Bakh FaNum';
font-style: normal;
font-weight: 300;
font-display: swap;
src: url('<?php echo esc_url($font_base); ?>/woff2/YekanBakhFaNum-Light.woff2') format('woff2');
}
@font-face {
font-family: 'Yekan Bakh FaNum';
font-style: normal;
font-weight: 800;
font-display: swap;
src: url('<?php echo esc_url($font_base); ?>/woff2/YekanBakhFaNum-ExtraBold.woff2') format('woff2');
}
@font-face {
font-family: 'Yekan Bakh FaNum';
font-style: normal;
font-weight: 900;
font-display: swap;
src: url('<?php echo esc_url($font_base); ?>/woff2/YekanBakhFaNum-Black.woff2') format('woff2');
}
</style>
<?php
}
add_action('wp_head', 'hesabix2_seo_font_faces', 3);
/* -------------------------------------------------------------------------
* فاز ۱: breadcrumb
* ------------------------------------------------------------------------- */
/**
* آیتم‌های breadcrumb برای صفحه جاری
*
* @return array<int, array{label: string, url?: string}>
*/
function hesabix2_seo_breadcrumb_items() {
$items = array(
array(
'label' => __('خانه', 'hesabix2'),
'url' => home_url('/'),
),
);
/* bbPress: قبل از is_archive تا برچسب «بایگانی‌ها: Forums» نیاید */
if (function_exists('hesabix2_is_bbpress_page') && hesabix2_is_bbpress_page()
&& function_exists('hesabix2_bbpress_breadcrumb_items')) {
return apply_filters('hesabix2_seo_breadcrumb_items', hesabix2_bbpress_breadcrumb_items());
}
if (is_front_page()) {
$items[] = array('label' => get_bloginfo('name'));
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
if (is_home()) {
$items[] = array('label' => __('وبلاگ', 'hesabix2'));
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
if (is_singular('post')) {
$blog_id = (int) get_option('page_for_posts');
if ($blog_id) {
$items[] = array(
'label' => get_the_title($blog_id),
'url' => get_permalink($blog_id),
);
} else {
$items[] = array(
'label' => __('وبلاگ', 'hesabix2'),
'url' => home_url('/blog/'),
);
}
$cats = get_the_category();
if (!empty($cats)) {
$cat = $cats[0];
$items[] = array(
'label' => $cat->name,
'url' => get_category_link($cat->term_id),
);
}
$items[] = array('label' => get_the_title());
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
if (function_exists('hesabix2_is_woocommerce_active') && hesabix2_is_woocommerce_active()) {
if (is_singular('product')) {
$items[] = function_exists('hesabix2_wc_seo_shop_link_item')
? hesabix2_wc_seo_shop_link_item()
: array('label' => __('فروشگاه', 'hesabix2'), 'url' => home_url('/shop/'));
$terms = get_the_terms(get_the_ID(), 'product_cat');
if (!empty($terms) && !is_wp_error($terms)) {
$term = $terms[0];
$ancestors = array_reverse(get_ancestors($term->term_id, 'product_cat', 'taxonomy'));
foreach ($ancestors as $ancestor_id) {
$ancestor = get_term($ancestor_id, 'product_cat');
if ($ancestor && !is_wp_error($ancestor)) {
$items[] = array(
'label' => $ancestor->name,
'url' => get_term_link($ancestor),
);
}
}
$items[] = array(
'label' => $term->name,
'url' => get_term_link($term),
);
}
$items[] = array('label' => get_the_title());
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
if (function_exists('is_shop') && is_shop()) {
$items[] = array('label' => __('فروشگاه', 'hesabix2'));
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
if (function_exists('is_product_taxonomy') && is_product_taxonomy()) {
$items[] = function_exists('hesabix2_wc_seo_shop_link_item')
? hesabix2_wc_seo_shop_link_item()
: array('label' => __('فروشگاه', 'hesabix2'), 'url' => home_url('/shop/'));
$term = get_queried_object();
if ($term instanceof WP_Term) {
$ancestors = array_reverse(get_ancestors($term->term_id, $term->taxonomy, 'taxonomy'));
foreach ($ancestors as $ancestor_id) {
$ancestor = get_term($ancestor_id, $term->taxonomy);
if ($ancestor && !is_wp_error($ancestor)) {
$items[] = array(
'label' => $ancestor->name,
'url' => get_term_link($ancestor),
);
}
}
$items[] = array('label' => $term->name);
}
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
}
if (is_singular('page')) {
if (function_exists('hesabix2_wc_is_order_tracking_page') && hesabix2_wc_is_order_tracking_page()) {
$items[] = array('label' => get_the_title());
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
$post = get_queried_object();
if ($post instanceof WP_Post && $post->post_parent) {
$ancestors = array_reverse(get_post_ancestors($post));
foreach ($ancestors as $ancestor_id) {
$items[] = array(
'label' => get_the_title($ancestor_id),
'url' => get_permalink($ancestor_id),
);
}
}
$items[] = array('label' => get_the_title());
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
if (is_search()) {
if (function_exists('hesabix2_wc_is_product_search') && hesabix2_wc_is_product_search()) {
$items[] = function_exists('hesabix2_wc_seo_shop_link_item')
? hesabix2_wc_seo_shop_link_item()
: array('label' => __('فروشگاه', 'hesabix2'), 'url' => home_url('/shop/'));
$items[] = array(
'label' => sprintf(
/* translators: %s: search query */
__('جستجوی محصول: «%s»', 'hesabix2'),
get_search_query()
),
);
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
$items[] = array(
'label' => sprintf(
/* translators: %s: search query */
__('نتایج جستجو برای «%s»', 'hesabix2'),
get_search_query()
),
);
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
if (is_category()) {
$items[] = array(
'label' => __('وبلاگ', 'hesabix2'),
'url' => get_option('page_for_posts') ? get_permalink((int) get_option('page_for_posts')) : home_url('/blog/'),
);
$items[] = array('label' => single_cat_title('', false));
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
if (is_tag()) {
$items[] = array(
'label' => __('وبلاگ', 'hesabix2'),
'url' => get_option('page_for_posts') ? get_permalink((int) get_option('page_for_posts')) : home_url('/blog/'),
);
$items[] = array('label' => single_tag_title('', false));
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
if (is_author()) {
$items[] = array(
'label' => __('وبلاگ', 'hesabix2'),
'url' => get_option('page_for_posts') ? get_permalink((int) get_option('page_for_posts')) : home_url('/blog/'),
);
$items[] = array('label' => get_the_author());
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
if (is_404()) {
$items[] = array('label' => __('صفحه پیدا نشد', 'hesabix2'));
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
if (is_archive()) {
$items[] = array(
'label' => wp_strip_all_tags(get_the_archive_title()),
);
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
return apply_filters('hesabix2_seo_breadcrumb_items', $items);
}
/**
* نمایش breadcrumb در قالب
*
* @param string $class
*/
function hesabix2_seo_render_breadcrumb($class = 'hesabix2-breadcrumb') {
$items = hesabix2_seo_breadcrumb_items();
if (count($items) < 2) {
return;
}
$list_class = strpos($class, 'single-breadcrumb') !== false ? 'single-breadcrumb__list' : 'hesabix2-breadcrumb__list';
$item_class = strpos($class, 'single-breadcrumb') !== false ? 'single-breadcrumb__item' : 'hesabix2-breadcrumb__item';
$sep_class = strpos($class, 'single-breadcrumb') !== false ? 'single-breadcrumb__sep' : 'hesabix2-breadcrumb__sep';
echo '<nav class="' . esc_attr($class) . '" aria-label="' . esc_attr__('مسیر صفحه', 'hesabix2') . '">';
echo '<ol class="' . esc_attr($list_class) . '">';
$last = count($items) - 1;
foreach ($items as $i => $item) {
$label = isset($item['label']) ? $item['label'] : '';
if ($label === '') {
continue;
}
echo '<li class="' . esc_attr($item_class) . '">';
if ($i < $last && !empty($item['url'])) {
echo '<a href="' . esc_url($item['url']) . '">' . esc_html($label) . '</a>';
} else {
echo '<span aria-current="page">' . esc_html($label) . '</span>';
}
echo '</li>';
if ($i < $last) {
echo '<li class="' . esc_attr($sep_class) . '" aria-hidden="true">/</li>';
}
}
echo '</ol></nav>';
}
function hesabix2_seo_current_url() {
if (is_singular()) {
return get_permalink();
}
global $wp;
return home_url(add_query_arg(array(), $wp->request ?? ''));
}
/* -------------------------------------------------------------------------
* فاز ۲: JSON-LD سراسری
* ------------------------------------------------------------------------- */
function hesabix2_seo_schema_graph() {
$graph = array();
$graph[] = hesabix2_seo_organization_entity();
$graph[] = array(
'@type' => 'WebSite',
'@id' => home_url('/#website'),
'url' => home_url('/'),
'name' => get_bloginfo('name'),
'description' => get_bloginfo('description'),
'publisher' => array('@id' => home_url('/#organization')),
'inLanguage' => 'fa-IR',
'potentialAction' => array(
'@type' => 'SearchAction',
'target' => array(
'@type' => 'EntryPoint',
'urlTemplate' => home_url('/?s={search_term_string}'),
),
'query-input' => 'required name=search_term_string',
),
);
if (is_front_page()) {
$graph = array_merge($graph, hesabix2_seo_front_page_schema_nodes());
}
if (function_exists('hesabix2_is_module_page') && hesabix2_is_module_page('faq')) {
$faq_schema = hesabix2_seo_faq_page_schema();
if ($faq_schema) {
$graph[] = $faq_schema;
}
}
if (is_singular('post')) {
$post_schema = hesabix2_seo_blog_posting_schema();
if ($post_schema) {
$graph[] = $post_schema;
}
}
if (is_singular('page') && !is_front_page()) {
$page_schema = hesabix2_seo_web_page_schema();
if ($page_schema) {
$graph[] = $page_schema;
}
}
if (is_page_template('page-about.php')) {
$about_schema = hesabix2_seo_about_page_schema();
if ($about_schema) {
$graph[] = $about_schema;
}
}
if (is_page_template('page-brand.php')) {
$brand_schema = hesabix2_seo_brand_page_schema();
if ($brand_schema) {
$graph[] = $brand_schema;
}
}
if (is_page_template('page-android.php')) {
$android_schema = hesabix2_seo_android_page_schema();
if ($android_schema) {
$graph[] = $android_schema;
}
}
if (is_page_template('page-windows.php')) {
$windows_schema = hesabix2_seo_windows_page_schema();
if ($windows_schema) {
$graph[] = $windows_schema;
}
}
if (is_page_template('page-helo-hesabix.php')) {
$helo_schema = hesabix2_seo_helo_page_schema();
if ($helo_schema) {
$graph[] = $helo_schema;
}
}
if (is_singular('docs')) {
$doc_schema = hesabix2_seo_tech_article_schema();
if ($doc_schema) {
$graph[] = $doc_schema;
}
}
if (function_exists('is_anspress') && is_anspress() && is_singular('question')) {
$qa_schema = hesabix2_seo_qa_page_schema();
if ($qa_schema) {
$graph[] = $qa_schema;
}
}
if (is_singular('product') && function_exists('hesabix2_seo_product_schema')) {
$product_schema = hesabix2_seo_product_schema();
if ($product_schema) {
$graph[] = $product_schema;
}
}
$breadcrumb_schema = hesabix2_seo_breadcrumb_list_schema();
if ($breadcrumb_schema) {
$graph[] = $breadcrumb_schema;
}
$graph = apply_filters('hesabix2_seo_schema_graph', $graph);
if (empty($graph)) {
return;
}
hesabix2_seo_print_json_ld(array(
'@context' => 'https://schema.org',
'@graph' => array_values($graph),
));
}
add_action('wp_footer', 'hesabix2_seo_schema_graph', 4);
/**
* @return array<int, array<string, mixed>>
*/
function hesabix2_seo_front_page_schema_nodes() {
$features = array(
__('حسابداری و گزارش مالی', 'hesabix2'),
__('فاکتور و فروش', 'hesabix2'),
__('CRM و باشگاه مشتریان', 'hesabix2'),
__('انبار و عملیات', 'hesabix2'),
__('ورک‌فلو و اتوماسیون', 'hesabix2'),
__('هوش مصنوعی', 'hesabix2'),
);
$app = array(
'@type' => 'SoftwareApplication',
'@id' => home_url('/#software'),
'name' => get_bloginfo('name'),
'applicationCategory' => 'BusinessApplication',
'operatingSystem' => 'Web',
'url' => home_url('/'),
'description' => hesabix2_fp_hero_tagline(),
'featureList' => $features,
'offers' => array(
'@type' => 'Offer',
'price' => '0',
'priceCurrency' => 'IRR',
),
'publisher' => array('@id' => home_url('/#organization')),
);
$image = hesabix2_seo_default_image_url();
if ($image) {
$app['screenshot'] = $image;
}
$nodes = array($app);
if (function_exists('hesabix2_fp_faq_items')) {
$faq_schema = hesabix2_seo_faq_schema_from_items(hesabix2_fp_faq_items(), home_url('/#faq'));
if ($faq_schema) {
$nodes[] = $faq_schema;
}
}
return $nodes;
}
/**
* ساخت FAQPage schema از آرایهٔ سوال/جواب
*
* @param array<int, array{q?: string, a?: string}> $items
* @param string $url
* @return array<string, mixed>|null
*/
function hesabix2_seo_faq_schema_from_items(array $items, $url) {
$entities = array();
foreach ($items as $item) {
if (empty($item['q']) || empty($item['a'])) {
continue;
}
$entities[] = array(
'@type' => 'Question',
'name' => $item['q'],
'acceptedAnswer' => array(
'@type' => 'Answer',
'text' => $item['a'],
),
);
}
if (empty($entities)) {
return null;
}
return array(
'@type' => 'FAQPage',
'@id' => $url . '#faq',
'url' => $url,
'mainEntity' => $entities,
);
}
/**
* FAQPage schema برای برگهٔ پرسش‌های متداول
*
* @return array<string, mixed>|null
*/
function hesabix2_seo_faq_page_schema() {
if (!function_exists('hesabix2_module_faq_items')) {
return null;
}
$items = hesabix2_module_faq_items('faq');
if (empty($items)) {
return null;
}
$url = function_exists('hesabix2_module_page_url')
? hesabix2_module_page_url('faq')
: home_url('/faq/');
$entities = array();
foreach ($items as $item) {
if (empty($item['q']) || empty($item['a'])) {
continue;
}
$entities[] = array(
'@type' => 'Question',
'name' => $item['q'],
'acceptedAnswer' => array(
'@type' => 'Answer',
'text' => $item['a'],
),
);
}
if (empty($entities)) {
return null;
}
return array(
'@type' => 'FAQPage',
'@id' => untrailingslashit($url) . '/#faq',
'url' => $url,
'mainEntity' => $entities,
);
}
/**
* AboutPage schema برای /about/
*
* @return array<string, mixed>|null
*/
function hesabix2_seo_about_page_schema() {
$url = function_exists('hesabix2_about_page_url')
? hesabix2_about_page_url()
: home_url('/about/');
return array(
'@type' => 'AboutPage',
'@id' => untrailingslashit($url) . '/#about',
'url' => $url,
'name' => __('داستان حسابیکس', 'hesabix2'),
'description' => __('آشنایی با ماموریت، مسیر محصول آرک و شادمان، و جامعهٔ متن‌باز حسابیکس.', 'hesabix2'),
'inLanguage' => 'fa-IR',
'isPartOf' => array('@id' => home_url('/#website')),
'publisher' => array('@id' => home_url('/#organization')),
);
}
/**
* WebPage schema برای /brand/
*
* @return array<string, mixed>|null
*/
function hesabix2_seo_brand_page_schema() {
$url = function_exists('hesabix2_brand_page_url')
? hesabix2_brand_page_url()
: home_url('/brand/');
$meta = function_exists('hesabix2_brand_seo_meta')
? hesabix2_brand_seo_meta()
: array(
'title' => __('دستورالعمل برند حسابیکس', 'hesabix2'),
'description' => __('راهنمای استفاده از نام و نشان حسابیکس.', 'hesabix2'),
);
return array(
'@type' => 'WebPage',
'@id' => untrailingslashit($url) . '/#brand',
'url' => $url,
'name' => $meta['title'],
'description' => $meta['description'],
'inLanguage' => 'fa-IR',
'isPartOf' => array('@id' => home_url('/#website')),
'publisher' => array('@id' => home_url('/#organization')),
'about' => array(
'@type' => 'Brand',
'name' => 'Hesabix',
'alternateName' => array('حسابیکس', 'حسابیکس آرک'),
),
);
}
/**
* Schema برای صفحهٔ اپ اندروید
*
* @return array<string, mixed>|null
*/
function hesabix2_seo_android_page_schema() {
$url = function_exists('hesabix2_android_page_url')
? hesabix2_android_page_url()
: home_url('/android/');
$apk = function_exists('hesabix2_android_apk_url')
? hesabix2_android_apk_url()
: '';
$ver = function_exists('hesabix2_android_app_version')
? hesabix2_android_app_version()
: '1.0.0';
$os = function_exists('hesabix2_android_min_os')
? hesabix2_android_min_os()
: 'Android 8.0+';
$app = array(
'@type' => 'MobileApplication',
'@id' => untrailingslashit($url) . '/#app',
'name' => __('حسابیکس اندروید', 'hesabix2'),
'applicationCategory' => 'BusinessApplication',
'operatingSystem' => $os,
'url' => $url,
'softwareVersion' => $ver,
'description' => __('اپلیکیشن اندروید حسابیکس — دانلود مستقیم خارج از مارکت‌ها؛ مدیریت فاکتور، انبار و CRM همگام با آرک.', 'hesabix2'),
'inLanguage' => 'fa-IR',
'offers' => array(
'@type' => 'Offer',
'price' => '0',
'priceCurrency' => 'IRR',
),
'publisher' => array('@id' => home_url('/#organization')),
'isPartOf' => array('@id' => home_url('/#website')),
);
if ($apk) {
$app['downloadUrl'] = $apk;
$app['installUrl'] = $apk;
}
return $app;
}
/**
* Schema برای صفحهٔ اپ ویندوز
*
* @return array<string, mixed>|null
*/
function hesabix2_seo_windows_page_schema() {
$url = function_exists('hesabix2_windows_page_url')
? hesabix2_windows_page_url()
: home_url('/windows/');
$msi = function_exists('hesabix2_windows_msi_url')
? hesabix2_windows_msi_url()
: '';
$ver = function_exists('hesabix2_windows_app_version')
? hesabix2_windows_app_version()
: '70.11.900';
$os = function_exists('hesabix2_windows_min_os')
? hesabix2_windows_min_os()
: 'Windows 10 / 11 (64-bit)';
$app = array(
'@type' => 'SoftwareApplication',
'@id' => untrailingslashit($url) . '/#app',
'name' => __('حسابیکس ویندوز', 'hesabix2'),
'applicationCategory' => 'BusinessApplication',
'operatingSystem' => $os,
'url' => $url,
'softwareVersion' => $ver,
'description' => __('اپلیکیشن ویندوز حسابیکس — دانلود مستقیم MSI؛ مدیریت فاکتور، انبار و CRM همگام با آرک.', 'hesabix2'),
'inLanguage' => 'fa-IR',
'offers' => array(
'@type' => 'Offer',
'price' => '0',
'priceCurrency' => 'IRR',
),
'publisher' => array('@id' => home_url('/#organization')),
'isPartOf' => array('@id' => home_url('/#website')),
);
if ($msi) {
$app['downloadUrl'] = $msi;
$app['installUrl'] = $msi;
}
return $app;
}
/**
* Schema برای صفحهٔ مهاجرت از هلو / ابزار انتقال
*
* @return array<string, mixed>|null
*/
function hesabix2_seo_helo_page_schema() {
$url = function_exists('hesabix2_helo_page_url')
? hesabix2_helo_page_url()
: home_url('/helo/');
$dl = function_exists('hesabix2_helo_migrator_url')
? hesabix2_helo_migrator_url()
: '';
$ver = function_exists('hesabix2_helo_migrator_version')
? hesabix2_helo_migrator_version()
: '1.0.0';
$os = function_exists('hesabix2_helo_migrator_min_os')
? hesabix2_helo_migrator_min_os()
: 'Windows 10 / 11 (64-bit)';
$app = array(
'@type' => 'SoftwareApplication',
'@id' => untrailingslashit($url) . '/#migrator',
'name' => __('انتقال‌دهنده هلو به حسابیکس آرک', 'hesabix2'),
'applicationCategory' => 'BusinessApplication',
'operatingSystem' => $os,
'url' => $url,
'softwareVersion' => $ver,
'description' => __('برنامهٔ رسمی انتقال داده از نرم‌افزار حسابداری هلو به حسابیکس آرک.', 'hesabix2'),
'inLanguage' => 'fa-IR',
'offers' => array(
'@type' => 'Offer',
'price' => '0',
'priceCurrency' => 'IRR',
),
'publisher' => array('@id' => home_url('/#organization')),
'isPartOf' => array('@id' => home_url('/#website')),
);
if ($dl) {
$app['downloadUrl'] = $dl;
$app['installUrl'] = $dl;
}
return $app;
}
/**
* تگ‌لاین صفحه نخست برای schema و Yoast
*/
function hesabix2_fp_hero_tagline() {
if (function_exists('hesabix2_fp_hero_subtitle')) {
return apply_filters('hesabix2_fp_hero_tagline', hesabix2_fp_hero_subtitle());
}
return apply_filters(
'hesabix2_fp_hero_tagline',
__('نرم‌افزار حسابداری و مدیریت کسب‌وکار آنلاین — CRM، انبار، ورک‌فلو و هوش مصنوعی', 'hesabix2')
);
}
/**
* @return array<string, mixed>|null
*/
function hesabix2_seo_blog_posting_schema() {
$post_id = get_the_ID();
if (!$post_id) {
return null;
}
$author_id = (int) get_post_field('post_author', $post_id);
$schema = array(
'@type' => 'BlogPosting',
'@id' => get_permalink($post_id) . '#article',
'headline' => get_the_title($post_id),
'url' => get_permalink($post_id),
'datePublished' => get_the_date('c', $post_id),
'dateModified' => get_the_modified_date('c', $post_id),
'author' => array(
'@type' => 'Person',
'name' => get_the_author_meta('display_name', $author_id),
'url' => get_author_posts_url($author_id),
),
'publisher' => array('@id' => home_url('/#organization')),
'mainEntityOfPage' => get_permalink($post_id),
'inLanguage' => 'fa-IR',
);
$summary = get_post_meta($post_id, '_hesabix_post_summary', true);
if ($summary) {
$schema['description'] = $summary;
} elseif (has_excerpt($post_id)) {
$schema['description'] = get_the_excerpt($post_id);
}
if (has_post_thumbnail($post_id)) {
$img = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'large');
if (!empty($img[0])) {
$schema['image'] = array(
'@type' => 'ImageObject',
'url' => $img[0],
'width' => $img[1] ?? null,
'height' => $img[2] ?? null,
);
}
}
$cats = get_the_category($post_id);
if (!empty($cats)) {
$schema['articleSection'] = $cats[0]->name;
}
return $schema;
}
/**
* JSON-LD محصول WooCommerce.
*
* @return array<string, mixed>|null
*/
function hesabix2_seo_product_schema() {
if (!is_singular('product') || !function_exists('wc_get_product')) {
return null;
}
$product = wc_get_product(get_the_ID());
if (!$product) {
return null;
}
$permalink = get_permalink();
$schema = array(
'@type' => 'Product',
'@id' => $permalink . '#product',
'name' => $product->get_name(),
'url' => $permalink,
'inLanguage' => 'fa-IR',
'description' => wp_trim_words(
wp_strip_all_tags($product->get_short_description() ?: $product->get_description()),
60
),
);
$sku = $product->get_sku();
if ($sku) {
$schema['sku'] = $sku;
}
$image_id = $product->get_image_id();
if ($image_id) {
$img = wp_get_attachment_image_src($image_id, 'large');
if (!empty($img[0])) {
$schema['image'] = array(
'@type' => 'ImageObject',
'url' => $img[0],
'width' => $img[1] ?? null,
'height' => $img[2] ?? null,
);
}
}
$offer = array(
'@type' => 'Offer',
'url' => $permalink,
'priceCurrency' => get_woocommerce_currency(),
'availability' => $product->is_in_stock()
? 'https://schema.org/InStock'
: 'https://schema.org/OutOfStock',
'itemCondition' => 'https://schema.org/NewCondition',
);
if ($product->get_price() !== '') {
$offer['price'] = wc_format_decimal($product->get_price(), wc_get_price_decimals());
}
$schema['offers'] = $offer;
$rating_count = $product->get_rating_count();
$average = $product->get_average_rating();
if ($rating_count > 0 && $average > 0) {
$schema['aggregateRating'] = array(
'@type' => 'AggregateRating',
'ratingValue' => (string) $average,
'reviewCount' => (string) $rating_count,
);
}
return $schema;
}
/**
* @return array<string, mixed>|null
*/
function hesabix2_seo_web_page_schema() {
$post_id = get_the_ID();
if (!$post_id) {
return null;
}
return array(
'@type' => 'WebPage',
'@id' => get_permalink($post_id) . '#webpage',
'url' => get_permalink($post_id),
'name' => get_the_title($post_id),
'description' => has_excerpt($post_id) ? get_the_excerpt($post_id) : wp_trim_words(wp_strip_all_tags(get_post_field('post_content', $post_id)), 40),
'inLanguage' => 'fa-IR',
'isPartOf' => array('@id' => home_url('/#website')),
);
}
/**
* @return array<string, mixed>|null
*/
function hesabix2_seo_tech_article_schema() {
$post_id = get_the_ID();
if (!$post_id) {
return null;
}
$schema = array(
'@type' => 'TechArticle',
'@id' => get_permalink($post_id) . '#article',
'headline' => get_the_title($post_id),
'url' => get_permalink($post_id),
'datePublished' => get_the_date('c', $post_id),
'dateModified' => get_the_modified_date('c', $post_id),
'author' => array(
'@type' => 'Person',
'name' => get_the_author_meta('display_name', (int) get_post_field('post_author', $post_id)),
),
'publisher' => array('@id' => home_url('/#organization')),
'inLanguage' => 'fa-IR',
'mainEntityOfPage' => get_permalink($post_id),
);
if (has_excerpt($post_id)) {
$schema['description'] = get_the_excerpt($post_id);
}
return $schema;
}
/**
* @return array<string, mixed>|null
*/
function hesabix2_seo_qa_page_schema() {
$post_id = get_the_ID();
if (!$post_id || !function_exists('ap_get_best_answer')) {
return null;
}
$question = array(
'@type' => 'Question',
'name' => get_the_title($post_id),
'url' => get_permalink($post_id),
'datePublished' => get_the_date('c', $post_id),
'author' => array(
'@type' => 'Person',
'name' => get_the_author_meta('display_name', (int) get_post_field('post_author', $post_id)),
),
'text' => wp_strip_all_tags(get_post_field('post_content', $post_id)),
);
$answer_id = (int) ap_get_best_answer($post_id);
if ($answer_id > 0) {
$question['acceptedAnswer'] = array(
'@type' => 'Answer',
'url' => get_permalink($answer_id),
'datePublished' => get_the_date('c', $answer_id),
'text' => wp_strip_all_tags(get_post_field('post_content', $answer_id)),
'author' => array(
'@type' => 'Person',
'name' => get_the_author_meta('display_name', (int) get_post_field('post_author', $answer_id)),
),
);
}
return array(
'@type' => 'QAPage',
'@id' => get_permalink($post_id) . '#qapage',
'mainEntity' => $question,
);
}
/**
* @return array<string, mixed>|null
*/
function hesabix2_seo_breadcrumb_list_schema() {
if (is_front_page()) {
return null;
}
$items = hesabix2_seo_breadcrumb_items();
if (count($items) < 2) {
return null;
}
$list = array();
$pos = 1;
foreach ($items as $item) {
$entry = array(
'@type' => 'ListItem',
'position' => $pos,
'name' => $item['label'],
);
if (!empty($item['url'])) {
$entry['item'] = $item['url'];
} else {
$entry['item'] = hesabix2_seo_current_url();
}
$list[] = $entry;
$pos++;
}
return array(
'@type' => 'BreadcrumbList',
'@id' => hesabix2_seo_current_url() . '#breadcrumb',
'itemListElement' => $list,
);
}
/* -------------------------------------------------------------------------
* فاز ۱: Yoast — عنوان/توضیح پیشنهادی صفحه نخست (در صورت فعال بودن)
* ------------------------------------------------------------------------- */
function hesabix2_seo_yoast_front_title($title) {
if (!is_front_page()) {
return $title;
}
$custom = apply_filters(
'hesabix2_seo_front_page_title',
sprintf(
/* translators: 1: site name */
__('%1$s | نرم‌افزار متن‌باز مدیریت کسب‌وکار', 'hesabix2'),
get_bloginfo('name')
)
);
return $custom ?: $title;
}
add_filter('wpseo_title', 'hesabix2_seo_yoast_front_title');
add_filter('wpseo_opengraph_title', 'hesabix2_seo_yoast_front_title');
function hesabix2_seo_yoast_front_desc($desc) {
if (!is_front_page()) {
return $desc;
}
$custom = apply_filters('hesabix2_seo_front_page_description', hesabix2_fp_hero_tagline());
return $custom ?: $desc;
}
add_filter('wpseo_metadesc', 'hesabix2_seo_yoast_front_desc');
add_filter('wpseo_opengraph_desc', 'hesabix2_seo_yoast_front_desc');
/* -------------------------------------------------------------------------
* Yoast: sitemap و robots — صفحات حساس / legacy
* ------------------------------------------------------------------------- */
/**
* آیا برگهٔ فعلی پورتال احراز هویت است؟
*/
function hesabix2_seo_is_portal_auth_page() {
return is_page_template('page-portal-auth.php');
}
/**
* آیا برگه legacy انس‌پرس (questions__trashed) است؟
*
* @param WP_Post|null $post
*/
function hesabix2_seo_is_legacy_anspress_page($post = null) {
if (!$post instanceof WP_Post) {
$post = get_queried_object();
}
if (!$post instanceof WP_Post || 'page' !== $post->post_type) {
return false;
}
return false !== strpos((string) $post->post_name, 'questions__trashed');
}
/**
* حذف URLهای نامناسب از sitemap یواست.
*
* @param array|false $url
* @param string $type
* @param object $object
* @return array|false
*/
function hesabix2_seo_yoast_sitemap_entry_filter($url, $type, $object) {
if (false === $url || !is_array($url) || empty($url['loc'])) {
return $url;
}
$loc = (string) $url['loc'];
if (false !== strpos($loc, '/questions__trashed/')) {
return false;
}
if ('post' === $type && $object instanceof WP_Post && 'page' === $object->post_type) {
if (hesabix2_seo_is_portal_auth_page_for_post($object) || hesabix2_seo_is_legacy_anspress_page($object)) {
return false;
}
}
return $url;
}
add_filter('wpseo_sitemap_entry', 'hesabix2_seo_yoast_sitemap_entry_filter', 25, 3);
/**
* @param WP_Post $post
*/
function hesabix2_seo_is_portal_auth_page_for_post($post) {
return 'page-portal-auth.php' === get_page_template_slug($post->ID)
|| 'portal-auth' === $post->post_name;
}
/**
* robots: noindex برای پورتال و برگه‌های legacy انس‌پرس.
*
* @param string $robots
* @return string
*/
function hesabix2_seo_yoast_robots_sensitive_pages($robots) {
if (hesabix2_seo_is_portal_auth_page()) {
return 'noindex, nofollow';
}
if (is_page() && hesabix2_seo_is_legacy_anspress_page()) {
return 'noindex, nofollow';
}
return $robots;
}
add_filter('wpseo_robots', 'hesabix2_seo_yoast_robots_sensitive_pages');
/**
* canonical پورتال — جلوگیری از پارامترهای query در ایندکس.
*
* @param string $canonical
* @return string
*/
function hesabix2_seo_yoast_portal_canonical($canonical) {
if (!hesabix2_seo_is_portal_auth_page()) {
return $canonical;
}
$url = function_exists('hesabix2_portal_auth_page_url')
? hesabix2_portal_auth_page_url()
: get_permalink();
return $url ? $url : $canonical;
}
add_filter('wpseo_canonical', 'hesabix2_seo_yoast_portal_canonical');
/* -------------------------------------------------------------------------
* فاز ۳: meta تکمیلی API وقتی Yoast پوشش نمی‌دهد
* ------------------------------------------------------------------------- */
function hesabix2_seo_api_docs_head() {
if (!is_page_template('page-api-reference.php')) {
return;
}
if (hesabix2_seo_yoast_active()) {
return;
}
$url = function_exists('hesabix2_api_docs_page_url') ? hesabix2_api_docs_page_url() : get_permalink();
$desc = __('مستندات REST API حسابیکس — احراز هویت، کسب‌وکار، اسناد مالی، گزارش و یکپارچه‌سازی. همگام با OpenAPI.', 'hesabix2');
$title = __('مرجع API حسابیکس', 'hesabix2');
$image = hesabix2_seo_default_image_url();
echo '<meta name="description" content="' . esc_attr($desc) . '" />' . "\n";
echo '<meta property="og:title" content="' . esc_attr($title) . '" />' . "\n";
echo '<meta property="og:description" content="' . esc_attr($desc) . '" />' . "\n";
echo '<meta property="og:type" content="website" />' . "\n";
echo '<meta property="og:url" content="' . esc_url($url) . '" />' . "\n";
if ($image) {
echo '<meta property="og:image" content="' . esc_url($image) . '" />' . "\n";
}
}
add_action('wp_head', 'hesabix2_seo_api_docs_head', 5);
/**
* OG تصویر API حتی با Yoast (فقط اگر og:image خالی باشد — از طریق filter)
*/
function hesabix2_seo_api_og_image($image) {
if (!is_page_template('page-api-reference.php')) {
return $image;
}
if ($image) {
return $image;
}
return hesabix2_seo_default_image_url() ?: $image;
}
add_filter('wpseo_opengraph_image', 'hesabix2_seo_api_og_image');
/* -------------------------------------------------------------------------
* فاز ۴: دسترسی‌پذیری — skip link
* ------------------------------------------------------------------------- */
function hesabix2_seo_skip_link() {
echo '<a class="hesabix2-skip-link" href="#main-content">' . esc_html__('پرش به محتوای اصلی', 'hesabix2') . '</a>';
}
add_action('wp_body_open', 'hesabix2_seo_skip_link', 1);
/* -------------------------------------------------------------------------
* پست‌های مرتبط بر اساس دسته
* ------------------------------------------------------------------------- */
/**
* @param int $post_id
* @param int $count
* @return WP_Post[]
*/
function hesabix2_seo_related_posts($post_id, $count = 5) {
$post_id = (int) $post_id;
$count = max(1, (int) $count);
$cats = wp_get_post_categories($post_id);
$args = array(
'post_type' => 'post',
'posts_per_page' => $count,
'post__not_in' => array($post_id),
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
);
if (!empty($cats)) {
$args['category__in'] = $cats;
}
$query = new WP_Query($args);
$posts = $query->posts;
wp_reset_postdata();
if (count($posts) < $count) {
$args['category__in'] = array();
$args['posts_per_page'] = $count - count($posts);
$args['post__not_in'] = array_merge(array($post_id), wp_list_pluck($posts, 'ID'));
$fallback = new WP_Query($args);
$posts = array_merge($posts, $fallback->posts);
wp_reset_postdata();
}
return $posts;
}