hesabixWpTheme/anspress/functions.php
2026-05-22 13:52:15 +03:30

189 lines
6.4 KiB
PHP
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
/**
* توابع افزونهٔ AnsPress برای Hesabix2 — باید منطق اولیهٔ افزونه را بار کند؛
* نسخهٔ خالی این فایل، ثبت ویجت‌ها، اسکریپت‌ها و بارگذاری overrides را قطع می‌کرد.
*
* @package Hesabix2
* @subpackage AnsPress
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$hesabix2_ap_core_theme = WP_PLUGIN_DIR . '/anspress-question-answer/templates/functions.php';
if ( ! is_readable( $hesabix2_ap_core_theme ) ) {
return;
}
/**
* فایل اصلی قالبٔ AnsPress داخل خودش ثبتٔ ویجت و اسکریپت‌ها را با __(…, 'anspress-question-answer') می‌دهد؛
* اگر همین اینجا require شود زمان after_setup_theme اجراست و وردپرس ۶.۷+ هشدار «ترجمه خیلی زود» می‌دهد.
* init با اولویت ۰ پیش از widgets_init است تا register_sidebar‌ها همان بار اول ثبت شوند.
*/
function hesabix2_ap_require_plugin_theme_functions() {
static $did = false;
if ( $did ) {
return;
}
$core = WP_PLUGIN_DIR . '/anspress-question-answer/templates/functions.php';
if ( is_readable( $core ) ) {
require_once $core;
}
$did = true;
}
add_action( 'init', 'hesabix2_ap_require_plugin_theme_functions', 0 );
/**
* کشیدن ظاهر نزدیک‌تر به قالب: حذف فونت پیش‌فرض AnsPress چون Hesabix2 از یکان استفاده می‌کند.
*/
function hesabix2_ap_dequeue_anspress_fonts() {
if ( function_exists( 'is_anspress' ) && is_anspress() ) {
wp_dequeue_style( 'anspress-fonts' );
wp_deregister_style( 'anspress-fonts' );
}
}
add_action( 'wp_enqueue_scripts', 'hesabix2_ap_dequeue_anspress_fonts', 999 );
/**
* بارگذاری overrides وقتی صفحهٔ AnsPress است یا تکیِ پرسش (برای کش/sitemap که is_anspress خاموش مانده باشد).
*/
function hesabix2_ap_enqueue_theme_overrides() {
$css_path = get_template_directory() . '/anspress/css/overrides.css';
if ( ! is_readable( $css_path ) ) {
return;
}
$load = false;
if ( function_exists( 'is_anspress' ) && is_anspress() ) {
$load = true;
}
if ( ! $load && function_exists( 'is_question' ) && is_question() ) {
$load = true;
}
if ( ! $load && is_singular( 'question' ) ) {
$load = true;
}
if ( ! $load ) {
return;
}
$deps = array();
if ( wp_style_is( 'anspress-main', 'registered' ) ) {
$deps[] = 'anspress-main';
}
if ( is_rtl() && wp_style_is( 'anspress-rtl', 'registered' ) ) {
$deps[] = 'anspress-rtl';
}
if ( wp_style_is( 'hesabix2-style', 'registered' ) ) {
$deps[] = 'hesabix2-style';
}
wp_dequeue_style( 'ap-overrides' );
wp_enqueue_style(
'hesabix2-ap-overrides',
get_template_directory_uri() . '/anspress/css/overrides.css',
$deps,
(string) filemtime( $css_path )
);
}
add_action( 'wp_enqueue_scripts', 'hesabix2_ap_enqueue_theme_overrides', 1000 );
/**
* ادیتور پاسخ را همیشه تصویری (TinyMCE) کن؛ گزینهٔ « ویرایشگر متنی» AnsPress آن را خاموش می‌کند.
*
* @param array $form آرگومان‌های فرم.
* @param bool $editing در حالت ویرایش پاسخ.
* @return array
*/
function hesabix2_ap_answer_form_visual_editor( $form, $editing ) {
if ( empty( $form['fields']['post_content'] ) || 'editor' !== ( $form['fields']['post_content']['type'] ?? '' ) ) {
return $form;
}
if ( empty( $form['fields']['post_content']['editor_args'] ) || ! is_array( $form['fields']['post_content']['editor_args'] ) ) {
$form['fields']['post_content']['editor_args'] = array();
}
$form['fields']['post_content']['editor_args']['quicktags'] = false;
$form['fields']['post_content']['label'] = __( 'متن پاسخ', 'hesabix2' );
$form['fields']['post_content']['desc'] = __( 'از نوار ابزار بالای کادر برای بولد، لینک و غیره استفاده کنید. دکمه‌های «درج رسانه» و «درج کد» کنار ادیتور هستند.', 'hesabix2' );
return $form;
}
add_filter( 'ap_answer_form_fields', 'hesabix2_ap_answer_form_visual_editor', 30, 2 );
/**
* رشته‌های انگلیسی دکمه‌های ادیتور پاسخ — ترجمه برای نمای پوستهٔ فارسی.
*
* @param string $translation متن ترجمه‌شده.
* @param string $text متن اصلی.
* @param string $domain domain.
*/
function hesabix2_ap_frontend_strings_answer_editor( $translation, $text, $domain ) {
if ( 'anspress-question-answer' !== $domain ) {
return $translation;
}
// جلوگیری از بازگشت بی‌پایان: هنگام gettext این دامنه، is_anspress() خودش دوباره __() همین دامنه را می‌زند (ap_main_pages).
static $busy = false;
if ( $busy ) {
return $translation;
}
$busy = true;
try {
if ( isset( $_GET['elementor-preview'] ) || is_admin() ) {
return $translation;
}
$allowed = false;
if ( function_exists( 'is_anspress' ) && is_anspress() ) {
$allowed = true;
}
if ( ! $allowed && function_exists( 'is_question' ) && is_question() ) {
$allowed = true;
}
if ( ! $allowed && is_singular( 'question' ) ) {
$allowed = true;
}
if ( ! $allowed && defined( 'DOING_AJAX' ) && DOING_AJAX
&& isset( $_REQUEST['ap_ajax_action'] )
&& 'load_tinymce' === sanitize_text_field( wp_unslash( $_REQUEST['ap_ajax_action'] ) ) ) {
$allowed = true;
}
if ( ! $allowed ) {
return $translation;
}
switch ( $text ) {
case 'Insert media':
case 'Insert Media (AnsPress)':
return 'درج رسانه';
case 'Insert Code':
case 'Insert code':
case 'Insert codes (AnsPress)':
return 'درج کد';
default:
return $translation;
}
} finally {
$busy = false;
}
}
add_filter( 'gettext', 'hesabix2_ap_frontend_strings_answer_editor', 999, 3 );
/**
* پیش‌فرض زبانهٔ ادیتور وردپرس روی تصویری (برای AJAX بارگذاری فرم پاسخ، کوکی «حالت متن» را دور بزنیم).
*
* @param string $editor 'tinymce' | 'html' | '';
* @return string
*/
function hesabix2_ap_default_editor_tinymce_on_ajax_answer( $editor ) {
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
return $editor;
}
if ( isset( $_REQUEST['ap_ajax_action'] ) && 'load_tinymce' === $_REQUEST['ap_ajax_action'] ) {
return 'tinymce';
}
return $editor;
}
add_filter( 'wp_default_editor', 'hesabix2_ap_default_editor_tinymce_on_ajax_answer', 20 );