hesabixWpTheme/inc/anspress-helpers.php

233 lines
6.5 KiB
PHP

<?php
/**
* توابع کمکی AnsPress برای قالب Hesabix2
*
* @package Hesabix2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* آدرس پایهٔ انجمن پرسش و پاسخ
*
* @return string
*/
function hesabix2_anspress_base_url() {
if ( function_exists( 'ap_get_link_to' ) ) {
return ap_get_link_to( '/' );
}
return home_url( '/' );
}
/**
* آدرس صفحهٔ ورود قالب (در صورت وجود برگهٔ login)
*
* @param string $redirect_after آدرس بازگشت پس از ورود.
* @return string
*/
function hesabix2_auth_page_url( $template, $redirect_after = '' ) {
if ( function_exists( 'hesabix2_portal_auth_page_url' ) ) {
return hesabix2_portal_auth_page_url( $template, $redirect_after );
}
return '';
}
/**
* لینک ورود برای AnsPress
*
* @param string $redirect_after آدرس بازگشت.
* @return string
*/
function hesabix2_anspress_login_url( $redirect_after = '' ) {
$url = hesabix2_portal_auth_url( 'login', $redirect_after ? $redirect_after : hesabix2_anspress_base_url() );
if ( $url ) {
return $url;
}
return wp_login_url( $redirect_after ? $redirect_after : hesabix2_anspress_base_url() );
}
/**
* لینک ثبت‌نام
*
* @return string
*/
function hesabix2_anspress_register_url() {
$url = hesabix2_portal_auth_url( 'register' );
if ( $url ) {
return $url;
}
return wp_registration_url();
}
/**
* آمار انجمن برای سایدبار
*
* @return array{questions: int, answers: int, tags: int}
*/
function hesabix2_anspress_community_stats() {
$q_counts = wp_count_posts( 'question' );
$a_counts = wp_count_posts( 'answer' );
$tags = wp_count_terms(
array(
'taxonomy' => 'question_tag',
'hide_empty' => true,
)
);
return array(
'questions' => isset( $q_counts->publish ) ? (int) $q_counts->publish : 0,
'answers' => isset( $a_counts->publish ) ? (int) $a_counts->publish : 0,
'tags' => is_wp_error( $tags ) ? 0 : (int) $tags,
);
}
/**
* پیام حالت خالی لیست بر اساس زمینه
*
* @return array{title: string, description: string}
*/
function hesabix2_anspress_empty_message() {
$search = get_query_var( 'ap_s' );
if ( $search ) {
return array(
'title' => __( 'نتیجه‌ای یافت نشد', 'hesabix2' ),
'description' => sprintf(
/* translators: %s: search keyword */
__( 'برای «%s» سوالی پیدا نشد. عبارت دیگری امتحان کنید یا اولین سوال را بپرسید.', 'hesabix2' ),
$search
),
);
}
if ( is_tax( 'question_tag' ) ) {
$term = get_queried_object();
$name = ( $term && ! empty( $term->name ) ) ? $term->name : '';
return array(
'title' => __( 'سوالی با این تگ نیست', 'hesabix2' ),
'description' => $name
? sprintf( __( 'هنوز سوالی با تگ «%s» ثبت نشده است.', 'hesabix2' ), $name )
: __( 'هنوز سوالی با این تگ ثبت نشده است.', 'hesabix2' ),
);
}
if ( is_tax( 'question_category' ) ) {
return array(
'title' => __( 'سوالی در این دسته نیست', 'hesabix2' ),
'description' => __( 'هنوز سوالی در این دسته‌بندی وجود ندارد.', 'hesabix2' ),
);
}
$order = function_exists( 'ap_get_current_list_filters' ) ? ap_get_current_list_filters( 'order_by' ) : '';
if ( 'unanswered' === $order ) {
return array(
'title' => __( 'همهٔ سوالات پاسخ دارند', 'hesabix2' ),
'description' => __( 'فعلاً سوال بدون پاسخی نیست. می‌توانید سوال جدید بپرسید.', 'hesabix2' ),
);
}
return array(
'title' => __( 'سوالی یافت نشد', 'hesabix2' ),
'description' => __( 'هنوز سوالی ثبت نشده است. اولین نفر باشید که سوال می‌پرسد.', 'hesabix2' ),
);
}
/**
* فیلترهای سریع لیست (chip)
*
* @param string $base_url آدرس فرم فیلتر.
* @return array<int, array{slug: string, label: string, url: string, active: bool}>
*/
function hesabix2_anspress_order_chips( $base_url ) {
$current = function_exists( 'ap_get_current_list_filters' ) ? ap_get_current_list_filters( 'order_by' ) : 'active';
$search = get_query_var( 'ap_s' );
$chips = array(
array( 'slug' => 'active', 'label' => __( 'فعال', 'hesabix2' ) ),
array( 'slug' => 'newest', 'label' => __( 'جدیدترین', 'hesabix2' ) ),
array( 'slug' => 'unanswered', 'label' => __( 'بدون پاسخ', 'hesabix2' ) ),
array( 'slug' => 'solved', 'label' => __( 'حل‌شده', 'hesabix2' ) ),
array( 'slug' => 'voted', 'label' => __( 'محبوب', 'hesabix2' ) ),
);
$out = array();
foreach ( $chips as $chip ) {
$args = array( 'ap_order_by' => $chip['slug'] );
if ( $search ) {
$args['ap_s'] = $search;
}
$out[] = array(
'slug' => $chip['slug'],
'label' => $chip['label'],
'url' => add_query_arg( $args, $base_url ),
'active' => ( $current === $chip['slug'] ),
);
}
return $out;
}
/**
* سوالات مرتبط بر اساس تگ
*
* @param int $question_id شناسهٔ سوال.
* @param int $limit تعداد.
* @return WP_Post[]
*/
function hesabix2_anspress_related_questions( $question_id, $limit = 5 ) {
$question_id = (int) $question_id;
if ( $question_id < 1 ) {
return array();
}
$tag_ids = wp_get_post_terms( $question_id, 'question_tag', array( 'fields' => 'ids' ) );
if ( empty( $tag_ids ) || is_wp_error( $tag_ids ) ) {
return array();
}
$query = new WP_Query(
array(
'post_type' => 'question',
'post_status' => 'publish',
'posts_per_page' => $limit,
'post__not_in' => array( $question_id ),
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'tax_query' => array(
array(
'taxonomy' => 'question_tag',
'field' => 'term_id',
'terms' => $tag_ids,
),
),
)
);
return $query->posts;
}
/**
* breadcrumb انجمن — در قالب غیرفعال است.
*
* @param array<int, array{label: string, url?: string}> $items آیتم‌ها.
*/
function hesabix2_anspress_render_breadcrumb( array $items ) {
return;
}
/**
* آیا صفحهٔ فعلی AnsPress است؟
*
* @return bool
*/
function hesabix2_is_anspress_page() {
if ( function_exists( 'is_anspress' ) && is_anspress() ) {
return true;
}
if ( function_exists( 'is_question' ) && is_question() ) {
return true;
}
return is_singular( 'question' );
}