475 lines
13 KiB
PHP
475 lines
13 KiB
PHP
<?php
|
||
/**
|
||
* توابع کمکی bbPress برای قالب Hesabix2
|
||
*
|
||
* @package Hesabix2
|
||
*/
|
||
|
||
if ( ! defined( 'ABSPATH' ) ) {
|
||
exit;
|
||
}
|
||
|
||
/**
|
||
* آیا افزونه bbPress فعال است؟
|
||
*
|
||
* @return bool
|
||
*/
|
||
function hesabix2_bbpress_is_active() {
|
||
return class_exists( 'bbPress', false );
|
||
}
|
||
|
||
/**
|
||
* آیا صفحهٔ فعلی مربوط به انجمن bbPress است؟
|
||
*
|
||
* @return bool
|
||
*/
|
||
function hesabix2_is_bbpress_page() {
|
||
if ( ! hesabix2_bbpress_is_active() ) {
|
||
return false;
|
||
}
|
||
|
||
if ( function_exists( 'hesabix2_is_bbpress_statistics_page' ) && hesabix2_is_bbpress_statistics_page() ) {
|
||
return true;
|
||
}
|
||
|
||
if ( function_exists( 'is_bbpress' ) && is_bbpress() ) {
|
||
return true;
|
||
}
|
||
|
||
/* پشتیبان برای زمانی که is_bbpress هنوز false است (مثلاً قبل از query) */
|
||
if ( is_singular( array( 'forum', 'topic', 'reply' ) ) ) {
|
||
return true;
|
||
}
|
||
|
||
if ( is_post_type_archive( array( 'forum', 'topic' ) ) ) {
|
||
return true;
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_topic_tag' ) && function_exists( 'bbp_get_topic_tag_tax_id' ) ) {
|
||
if ( is_tax( bbp_get_topic_tag_tax_id() ) ) {
|
||
return true;
|
||
}
|
||
}
|
||
|
||
if ( is_singular( 'page' ) && function_exists( 'bbp_has_shortcode' ) && bbp_has_shortcode() ) {
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
/**
|
||
* آدرس ریشهٔ انجمن (آرشیو یا برگهٔ تنظیمشده در bbPress)
|
||
*
|
||
* @return string
|
||
*/
|
||
function hesabix2_bbpress_root_url() {
|
||
if ( ! hesabix2_bbpress_is_active() ) {
|
||
return '';
|
||
}
|
||
|
||
if ( function_exists( 'bbp_get_root_url' ) ) {
|
||
$url = bbp_get_root_url();
|
||
if ( $url ) {
|
||
return $url;
|
||
}
|
||
}
|
||
|
||
$archive = get_post_type_archive_link( 'forum' );
|
||
return $archive ? $archive : '';
|
||
}
|
||
|
||
/**
|
||
* آدرس برگهٔ آمار انجمن (در صورت وجود قالب Statistics)
|
||
*
|
||
* @return string
|
||
*/
|
||
function hesabix2_bbpress_statistics_url() {
|
||
$pages = get_posts(
|
||
array(
|
||
'post_type' => 'page',
|
||
'post_status' => 'publish',
|
||
'posts_per_page' => 1,
|
||
'meta_key' => '_wp_page_template',
|
||
'meta_value' => 'page-forum-statistics.php',
|
||
'no_found_rows' => true,
|
||
)
|
||
);
|
||
|
||
if ( ! empty( $pages[0] ) ) {
|
||
return get_permalink( $pages[0]->ID );
|
||
}
|
||
|
||
return '';
|
||
}
|
||
|
||
/**
|
||
* آیا صفحهٔ فعلی برگهٔ آمار انجمن است؟
|
||
*
|
||
* @return bool
|
||
*/
|
||
function hesabix2_is_bbpress_statistics_page() {
|
||
if ( ! is_page() ) {
|
||
return false;
|
||
}
|
||
return 'page-forum-statistics.php' === get_page_template_slug( get_queried_object_id() );
|
||
}
|
||
|
||
/**
|
||
* برگهٔ ورود قالب (page-login.php)
|
||
*
|
||
* @param string $redirect_after آدرس بازگشت پس از ورود.
|
||
* @return string
|
||
*/
|
||
function hesabix2_bbpress_login_url( $redirect_after = '' ) {
|
||
$fallback = hesabix2_bbpress_root_url();
|
||
$url = function_exists( 'hesabix2_portal_auth_url' )
|
||
? hesabix2_portal_auth_url( 'login', $redirect_after ? $redirect_after : $fallback )
|
||
: '';
|
||
if ( $url ) {
|
||
return $url;
|
||
}
|
||
return wp_login_url( $redirect_after ? $redirect_after : $fallback );
|
||
}
|
||
|
||
/**
|
||
* برگهٔ ثبتنام قالب
|
||
*
|
||
* @param string $redirect_after آدرس بازگشت.
|
||
* @return string
|
||
*/
|
||
function hesabix2_bbpress_register_url( $redirect_after = '' ) {
|
||
$url = function_exists( 'hesabix2_portal_auth_url' ) ? hesabix2_portal_auth_url( 'register', $redirect_after ) : '';
|
||
if ( $url ) {
|
||
return $url;
|
||
}
|
||
return wp_registration_url();
|
||
}
|
||
|
||
/**
|
||
* لینک انجمن برای منو — bbPress در اولویت، سپس AnsPress
|
||
*
|
||
* @return string
|
||
*/
|
||
function hesabix2_community_url() {
|
||
$bbp = hesabix2_bbpress_root_url();
|
||
if ( $bbp ) {
|
||
return $bbp;
|
||
}
|
||
if ( function_exists( 'hesabix2_anspress_base_url' ) ) {
|
||
return hesabix2_anspress_base_url();
|
||
}
|
||
return home_url( '/' );
|
||
}
|
||
|
||
/**
|
||
* عنوان نمایشی صفحهٔ انجمن (بدون پیشوند «بایگانیها»)
|
||
*
|
||
* @return string
|
||
*/
|
||
function hesabix2_bbpress_page_heading() {
|
||
if ( ! hesabix2_is_bbpress_page() ) {
|
||
return '';
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_search' ) && bbp_is_search() ) {
|
||
return function_exists( 'bbp_get_search_title' )
|
||
? wp_strip_all_tags( bbp_get_search_title() )
|
||
: __( 'جستجو در انجمن', 'hesabix2' );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_forum_archive' ) && bbp_is_forum_archive() ) {
|
||
if ( function_exists( 'bbp_get_forum_archive_title' ) ) {
|
||
$title = wp_strip_all_tags( bbp_get_forum_archive_title() );
|
||
if ( $title ) {
|
||
return $title;
|
||
}
|
||
}
|
||
return __( 'انجمن', 'hesabix2' );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_topic_archive' ) && bbp_is_topic_archive() ) {
|
||
if ( function_exists( 'bbp_get_topic_archive_title' ) ) {
|
||
$title = wp_strip_all_tags( bbp_get_topic_archive_title() );
|
||
if ( $title ) {
|
||
return $title;
|
||
}
|
||
}
|
||
return __( 'موضوعات', 'hesabix2' );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_single_view' ) && bbp_is_single_view() && function_exists( 'bbp_get_view_title' ) ) {
|
||
return wp_strip_all_tags( bbp_get_view_title() );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_single_forum' ) && bbp_is_single_forum() && function_exists( 'bbp_get_forum_title' ) ) {
|
||
return wp_strip_all_tags( bbp_get_forum_title() );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_single_topic' ) && bbp_is_single_topic() && function_exists( 'bbp_get_topic_title' ) ) {
|
||
return wp_strip_all_tags( bbp_get_topic_title() );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_single_reply' ) && bbp_is_single_reply() && function_exists( 'bbp_get_reply_title' ) ) {
|
||
return wp_strip_all_tags( bbp_get_reply_title() );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_topic_tag' ) && bbp_is_topic_tag() && function_exists( 'bbp_get_topic_tag_name' ) ) {
|
||
return sprintf(
|
||
/* translators: %s: tag name */
|
||
__( 'برچسب: %s', 'hesabix2' ),
|
||
bbp_get_topic_tag_name()
|
||
);
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_single_user' ) && bbp_is_single_user() && function_exists( 'bbp_get_displayed_user_field' ) ) {
|
||
$name = bbp_get_displayed_user_field( 'display_name' );
|
||
if ( function_exists( 'bbp_is_favorites' ) && bbp_is_favorites() ) {
|
||
return sprintf(
|
||
/* translators: %s: user display name */
|
||
__( 'علاقهمندیهای %s', 'hesabix2' ),
|
||
$name
|
||
);
|
||
}
|
||
if ( function_exists( 'bbp_is_subscriptions' ) && bbp_is_subscriptions() ) {
|
||
return sprintf(
|
||
/* translators: %s: user display name */
|
||
__( 'اشتراکهای %s', 'hesabix2' ),
|
||
$name
|
||
);
|
||
}
|
||
if ( function_exists( 'bbp_is_topics_created' ) && bbp_is_topics_created() ) {
|
||
return sprintf(
|
||
/* translators: %s: user display name */
|
||
__( 'موضوعات %s', 'hesabix2' ),
|
||
$name
|
||
);
|
||
}
|
||
if ( function_exists( 'bbp_is_replies_created' ) && bbp_is_replies_created() ) {
|
||
return sprintf(
|
||
/* translators: %s: user display name */
|
||
__( 'پاسخهای %s', 'hesabix2' ),
|
||
$name
|
||
);
|
||
}
|
||
if ( function_exists( 'bbp_is_single_user_edit' ) && bbp_is_single_user_edit() ) {
|
||
return sprintf(
|
||
/* translators: %s: user display name */
|
||
__( 'ویرایش پروفایل %s', 'hesabix2' ),
|
||
$name
|
||
);
|
||
}
|
||
return $name;
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_topic_edit' ) && bbp_is_topic_edit() ) {
|
||
return __( 'ویرایش موضوع', 'hesabix2' );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_reply_edit' ) && bbp_is_reply_edit() ) {
|
||
return __( 'ویرایش پاسخ', 'hesabix2' );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_forum_edit' ) && bbp_is_forum_edit() ) {
|
||
return __( 'ویرایش انجمن', 'hesabix2' );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_topic_merge' ) && bbp_is_topic_merge() ) {
|
||
return __( 'ادغام موضوع', 'hesabix2' );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_topic_split' ) && bbp_is_topic_split() ) {
|
||
return __( 'تقسیم موضوع', 'hesabix2' );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_reply_move' ) && bbp_is_reply_move() ) {
|
||
return __( 'انتقال پاسخ', 'hesabix2' );
|
||
}
|
||
|
||
if ( function_exists( 'bbp_is_topic_tag_edit' ) && bbp_is_topic_tag_edit() ) {
|
||
return __( 'ویرایش برچسب موضوع', 'hesabix2' );
|
||
}
|
||
|
||
if ( function_exists( 'hesabix2_is_bbpress_statistics_page' ) && hesabix2_is_bbpress_statistics_page() ) {
|
||
return __( 'آمار انجمن', 'hesabix2' );
|
||
}
|
||
|
||
return __( 'انجمن', 'hesabix2' );
|
||
}
|
||
|
||
/**
|
||
* آیتمهای breadcrumb انجمن bbPress
|
||
*
|
||
* @return array<int, array{label: string, url?: string}>
|
||
*/
|
||
function hesabix2_bbpress_breadcrumb_items() {
|
||
$items = array(
|
||
array(
|
||
'label' => __( 'خانه', 'hesabix2' ),
|
||
'url' => home_url( '/' ),
|
||
),
|
||
);
|
||
|
||
$root_label = __( 'انجمن', 'hesabix2' );
|
||
$root = hesabix2_bbpress_root_url();
|
||
|
||
if ( ! function_exists( 'bbp_is_forum_archive' ) ) {
|
||
if ( $root ) {
|
||
$items[] = array(
|
||
'label' => $root_label,
|
||
'url' => $root,
|
||
);
|
||
}
|
||
return $items;
|
||
}
|
||
|
||
/* صفحهٔ اصلی انجمن: خانه › [عنوان برگه] — بدون تکرار */
|
||
if ( bbp_is_forum_archive() ) {
|
||
$items[] = array(
|
||
'label' => hesabix2_bbpress_page_heading(),
|
||
);
|
||
return $items;
|
||
}
|
||
|
||
if ( $root ) {
|
||
$items[] = array(
|
||
'label' => $root_label,
|
||
'url' => $root,
|
||
);
|
||
}
|
||
|
||
if ( bbp_is_search() ) {
|
||
$items[] = array(
|
||
'label' => function_exists( 'bbp_get_search_title' )
|
||
? wp_strip_all_tags( bbp_get_search_title() )
|
||
: __( 'جستجو در انجمن', 'hesabix2' ),
|
||
);
|
||
return $items;
|
||
}
|
||
|
||
if ( bbp_is_topic_archive() ) {
|
||
$items[] = array( 'label' => hesabix2_bbpress_page_heading() );
|
||
return $items;
|
||
}
|
||
|
||
if ( bbp_is_single_view() && function_exists( 'bbp_get_view_title' ) ) {
|
||
$items[] = array( 'label' => wp_strip_all_tags( bbp_get_view_title() ) );
|
||
return $items;
|
||
}
|
||
|
||
if ( bbp_is_single_forum() && function_exists( 'bbp_get_forum_title' ) ) {
|
||
$items[] = array( 'label' => wp_strip_all_tags( bbp_get_forum_title() ) );
|
||
return $items;
|
||
}
|
||
|
||
if ( bbp_is_single_topic() && function_exists( 'bbp_get_topic_title' ) ) {
|
||
if ( function_exists( 'bbp_get_forum_title' ) && function_exists( 'bbp_get_topic_forum_id' ) ) {
|
||
$forum_id = bbp_get_topic_forum_id();
|
||
if ( $forum_id ) {
|
||
$items[] = array(
|
||
'label' => wp_strip_all_tags( bbp_get_forum_title( $forum_id ) ),
|
||
'url' => get_permalink( $forum_id ),
|
||
);
|
||
}
|
||
}
|
||
$items[] = array( 'label' => wp_strip_all_tags( bbp_get_topic_title() ) );
|
||
return $items;
|
||
}
|
||
|
||
if ( bbp_is_single_reply() && function_exists( 'bbp_get_reply_title' ) ) {
|
||
if ( function_exists( 'bbp_get_topic_title' ) && function_exists( 'bbp_get_reply_topic_id' ) ) {
|
||
$topic_id = bbp_get_reply_topic_id();
|
||
if ( $topic_id ) {
|
||
$items[] = array(
|
||
'label' => wp_strip_all_tags( bbp_get_topic_title( $topic_id ) ),
|
||
'url' => get_permalink( $topic_id ),
|
||
);
|
||
}
|
||
}
|
||
$items[] = array( 'label' => wp_strip_all_tags( bbp_get_reply_title() ) );
|
||
return $items;
|
||
}
|
||
|
||
if ( bbp_is_topic_tag() && function_exists( 'bbp_get_topic_tag_name' ) ) {
|
||
$items[] = array( 'label' => hesabix2_bbpress_page_heading() );
|
||
return $items;
|
||
}
|
||
|
||
if ( bbp_is_topic_tag_edit() && function_exists( 'bbp_get_topic_tag_name' ) ) {
|
||
$items[] = array(
|
||
'label' => sprintf(
|
||
/* translators: %s: tag name */
|
||
__( 'ویرایش برچسب: %s', 'hesabix2' ),
|
||
bbp_get_topic_tag_name()
|
||
),
|
||
);
|
||
return $items;
|
||
}
|
||
|
||
if ( bbp_is_single_user() && function_exists( 'bbp_get_displayed_user_field' ) ) {
|
||
$user_name = bbp_get_displayed_user_field( 'display_name' );
|
||
if ( function_exists( 'bbp_get_user_profile_url' ) && function_exists( 'bbp_get_displayed_user_id' ) ) {
|
||
$items[] = array(
|
||
'label' => $user_name,
|
||
'url' => bbp_get_user_profile_url( bbp_get_displayed_user_id() ),
|
||
);
|
||
} else {
|
||
$items[] = array( 'label' => $user_name );
|
||
}
|
||
if ( function_exists( 'bbp_is_favorites' ) && bbp_is_favorites() ) {
|
||
$items[] = array( 'label' => __( 'علاقهمندیها', 'hesabix2' ) );
|
||
} elseif ( function_exists( 'bbp_is_subscriptions' ) && bbp_is_subscriptions() ) {
|
||
$items[] = array( 'label' => __( 'اشتراکها', 'hesabix2' ) );
|
||
} elseif ( function_exists( 'bbp_is_topics_created' ) && bbp_is_topics_created() ) {
|
||
$items[] = array( 'label' => __( 'موضوعات ایجادشده', 'hesabix2' ) );
|
||
} elseif ( function_exists( 'bbp_is_replies_created' ) && bbp_is_replies_created() ) {
|
||
$items[] = array( 'label' => __( 'پاسخهای ایجادشده', 'hesabix2' ) );
|
||
} elseif ( function_exists( 'bbp_is_single_user_edit' ) && bbp_is_single_user_edit() ) {
|
||
$items[] = array( 'label' => __( 'ویرایش پروفایل', 'hesabix2' ) );
|
||
}
|
||
return $items;
|
||
}
|
||
|
||
return $items;
|
||
}
|
||
|
||
/**
|
||
* آیا موضوع بیش از یک صفحه پاسخ دارد؟
|
||
*
|
||
* @return bool
|
||
*/
|
||
function hesabix2_bbpress_topic_has_paged_replies() {
|
||
if ( ! function_exists( 'bbpress' ) || ! function_exists( 'bbp_has_replies' ) || ! bbp_has_replies() ) {
|
||
return false;
|
||
}
|
||
|
||
$bbp = bbpress();
|
||
if ( ! isset( $bbp->reply_query ) || ! is_object( $bbp->reply_query ) ) {
|
||
return false;
|
||
}
|
||
|
||
$total = (int) $bbp->reply_query->found_posts;
|
||
$ppp = (int) $bbp->reply_query->posts_per_page;
|
||
|
||
return $total > $ppp;
|
||
}
|
||
|
||
/**
|
||
* آیا کاربر میتواند در انجمن موضوع/پاسخ جدید بسازد؟
|
||
*
|
||
* @return bool
|
||
*/
|
||
function hesabix2_bbpress_user_can_post() {
|
||
if ( ! is_user_logged_in() ) {
|
||
return false;
|
||
}
|
||
if ( function_exists( 'bbp_is_single_topic' ) && bbp_is_single_topic()
|
||
&& function_exists( 'bbp_current_user_can_access_create_reply_form' ) ) {
|
||
return bbp_current_user_can_access_create_reply_form();
|
||
}
|
||
if ( function_exists( 'bbp_is_single_forum' ) && bbp_is_single_forum()
|
||
&& function_exists( 'bbp_current_user_can_access_create_topic_form' ) ) {
|
||
return bbp_current_user_can_access_create_topic_form();
|
||
}
|
||
return false;
|
||
}
|