245 lines
7.9 KiB
PHP
245 lines
7.9 KiB
PHP
<?php
|
|
/**
|
|
* رسانه انجمن: لایتباکس تصویر، پلیر ویدیو، دانلود فایل
|
|
*
|
|
* @package Hesabix2
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
if ( ! function_exists( 'hesabix2_bbpress_is_active' ) || ! hesabix2_bbpress_is_active() ) {
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
function hesabix2_bbpress_enqueue_media_assets() {
|
|
if ( ! hesabix2_is_bbpress_page() ) {
|
|
return;
|
|
}
|
|
|
|
$js_path = get_template_directory() . '/assets/js/bbpress-media.js';
|
|
if ( ! is_readable( $js_path ) ) {
|
|
return;
|
|
}
|
|
|
|
wp_enqueue_script(
|
|
'hesabix2-bbpress-media',
|
|
get_template_directory_uri() . '/assets/js/bbpress-media.js',
|
|
array(),
|
|
(string) filemtime( $js_path ),
|
|
true
|
|
);
|
|
}
|
|
add_action( 'wp_enqueue_scripts', 'hesabix2_bbpress_enqueue_media_assets', 115 );
|
|
|
|
/**
|
|
* آیا پست از پیامرسان (بله/تلگرام) آمده است؟
|
|
*
|
|
* @param int $post_id Post ID.
|
|
* @return string|false Origin slug or false.
|
|
*/
|
|
function hesabix2_bbpress_messenger_origin( $post_id ) {
|
|
$origin = get_post_meta( (int) $post_id, '_hesabix_bale_sync_origin', true );
|
|
if ( in_array( $origin, array( 'bale', 'telegram' ), true ) ) {
|
|
return $origin;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* کلاس اضافی برای پستهای واردشده از بله / تلگرام
|
|
*
|
|
* @param array $post_classes Post classes.
|
|
* @param int $reply_id Reply ID.
|
|
* @param array $classes Extra classes.
|
|
* @return array
|
|
*/
|
|
function hesabix2_bbpress_reply_bale_class( $post_classes, $reply_id, $classes ) {
|
|
unset( $classes );
|
|
|
|
$origin = hesabix2_bbpress_messenger_origin( $reply_id );
|
|
if ( $origin ) {
|
|
$post_classes[] = 'hesabix2-bbp-post--from-bale';
|
|
$post_classes[] = 'hesabix2-bbp-post--from-messenger';
|
|
$post_classes[] = 'hesabix2-bbp-post--from-' . sanitize_html_class( $origin );
|
|
}
|
|
|
|
return $post_classes;
|
|
}
|
|
add_filter( 'bbp_get_reply_class', 'hesabix2_bbpress_reply_bale_class', 20, 3 );
|
|
|
|
/**
|
|
* @param array $post_classes Post classes.
|
|
* @param int $topic_id Topic ID.
|
|
* @param array $classes Extra classes.
|
|
* @return array
|
|
*/
|
|
function hesabix2_bbpress_topic_bale_class( $post_classes, $topic_id, $classes ) {
|
|
unset( $classes );
|
|
|
|
$origin = hesabix2_bbpress_messenger_origin( $topic_id );
|
|
if ( $origin ) {
|
|
$post_classes[] = 'hesabix2-bbp-post--from-bale';
|
|
$post_classes[] = 'hesabix2-bbp-post--from-messenger';
|
|
$post_classes[] = 'hesabix2-bbp-post--from-' . sanitize_html_class( $origin );
|
|
}
|
|
|
|
return $post_classes;
|
|
}
|
|
add_filter( 'bbp_get_topic_class', 'hesabix2_bbpress_topic_bale_class', 20, 3 );
|
|
|
|
/**
|
|
* آیا خطای ثبتشدهٔ GD bbPress فقط بهخاطر فیلد فایل خالی است؟
|
|
*
|
|
* @param mixed $error Meta value.
|
|
* @return bool
|
|
*/
|
|
function hesabix2_bbpress_is_empty_file_upload_error( $error ) {
|
|
if ( ! is_array( $error ) ) {
|
|
return false;
|
|
}
|
|
|
|
$file = isset( $error['file'] ) ? (string) $error['file'] : '';
|
|
$msg = isset( $error['message'] ) ? (string) $error['message'] : '';
|
|
|
|
$unknown = ( '' === $file || 'Unknown file' === $file );
|
|
$not_uploaded = ( false !== stripos( $msg, 'File not uploaded' ) );
|
|
|
|
return $unknown && $not_uploaded;
|
|
}
|
|
|
|
/**
|
|
* فیلد خالی d4p_attachment[] را از $_FILES حذف میکند تا افزونه
|
|
* «Unknown file: File not uploaded» ثبت نکند.
|
|
*
|
|
* @return void
|
|
*/
|
|
function hesabix2_bbpress_drop_empty_attachment_uploads() {
|
|
if ( empty( $_FILES['d4p_attachment'] ) || ! is_array( $_FILES['d4p_attachment'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
|
|
return;
|
|
}
|
|
|
|
$files = &$_FILES['d4p_attachment']; // phpcs:ignore WordPress.Security.NonceVerification.Missing
|
|
if ( empty( $files['error'] ) || ! is_array( $files['error'] ) ) {
|
|
return;
|
|
}
|
|
|
|
foreach ( $files['error'] as $key => $error ) {
|
|
$name = isset( $files['name'][ $key ] ) ? (string) $files['name'][ $key ] : '';
|
|
if ( UPLOAD_ERR_NO_FILE !== (int) $error || '' !== $name ) {
|
|
continue;
|
|
}
|
|
foreach ( array( 'name', 'type', 'tmp_name', 'error', 'size', 'full_path' ) as $field ) {
|
|
if ( isset( $files[ $field ] ) && is_array( $files[ $field ] ) ) {
|
|
unset( $files[ $field ][ $key ] );
|
|
}
|
|
}
|
|
}
|
|
|
|
if ( empty( $files['error'] ) ) {
|
|
unset( $_FILES['d4p_attachment'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
|
|
}
|
|
}
|
|
add_action( 'bbp_new_reply', 'hesabix2_bbpress_drop_empty_attachment_uploads', 1 );
|
|
add_action( 'bbp_new_topic', 'hesabix2_bbpress_drop_empty_attachment_uploads', 1 );
|
|
add_action( 'bbp_edit_reply', 'hesabix2_bbpress_drop_empty_attachment_uploads', 1 );
|
|
add_action( 'bbp_edit_topic', 'hesabix2_bbpress_drop_empty_attachment_uploads', 1 );
|
|
|
|
/**
|
|
* خطاهای کاذب آپلود خالی را از متای پست پاک میکند.
|
|
*
|
|
* @param int $post_id Topic or reply ID.
|
|
* @return void
|
|
*/
|
|
function hesabix2_bbpress_purge_empty_attachment_errors( $post_id ) {
|
|
$post_id = (int) $post_id;
|
|
if ( $post_id <= 0 ) {
|
|
return;
|
|
}
|
|
|
|
$errors = get_post_meta( $post_id, '_bbp_attachment_upload_error' );
|
|
if ( empty( $errors ) ) {
|
|
return;
|
|
}
|
|
|
|
foreach ( $errors as $error ) {
|
|
if ( hesabix2_bbpress_is_empty_file_upload_error( $error ) ) {
|
|
delete_post_meta( $post_id, '_bbp_attachment_upload_error', $error );
|
|
}
|
|
}
|
|
}
|
|
add_action( 'bbp_new_reply', 'hesabix2_bbpress_purge_empty_attachment_errors', 20 );
|
|
add_action( 'bbp_new_topic', 'hesabix2_bbpress_purge_empty_attachment_errors', 20 );
|
|
add_action( 'bbp_edit_reply', 'hesabix2_bbpress_purge_empty_attachment_errors', 20 );
|
|
add_action( 'bbp_edit_topic', 'hesabix2_bbpress_purge_empty_attachment_errors', 20 );
|
|
|
|
/**
|
|
* فارسیسازی عنوان پیوستهای GD bbPress و حذف خطای کاذب آپلود خالی.
|
|
*
|
|
* @param string $content
|
|
* @param int $post_id
|
|
* @return string
|
|
*/
|
|
function hesabix2_bbpress_localize_attachment_heading( $content, $post_id ) {
|
|
if ( ! is_string( $content ) ) {
|
|
return $content;
|
|
}
|
|
|
|
hesabix2_bbpress_purge_empty_attachment_errors( (int) $post_id );
|
|
|
|
if ( false !== strpos( $content, 'bbp-attachments-errors' ) ) {
|
|
$errors = get_post_meta( (int) $post_id, '_bbp_attachment_upload_error' );
|
|
if ( empty( $errors ) ) {
|
|
$content = preg_replace( '/<div class="bbp-attachments-errors">.*?<\/div>/s', '', $content );
|
|
}
|
|
}
|
|
|
|
if ( ! hesabix2_is_bbpress_page() || false === strpos( $content, 'bbp-attachments' ) ) {
|
|
return $content;
|
|
}
|
|
|
|
$content = str_replace(
|
|
array( '<h6>Attachments:</h6>', '<h6>Attachments</h6>', '<h6>پیوستها:</h6>' ),
|
|
'<h6 class="hesabix2-bbp-attachments__title">' . esc_html__( 'پیوستها', 'hesabix2' ) . '</h6>',
|
|
$content
|
|
);
|
|
|
|
return str_replace(
|
|
array( '<h6>Upload Errors:</h6>', '<h6>خطاهای بارگذاری:</h6>' ),
|
|
'<h6 class="hesabix2-bbp-attachments__title">' . esc_html__( 'خطاهای بارگذاری', 'hesabix2' ) . '</h6>',
|
|
$content
|
|
);
|
|
}
|
|
add_filter( 'bbp_get_reply_content', 'hesabix2_bbpress_localize_attachment_heading', 120, 2 );
|
|
add_filter( 'bbp_get_topic_content', 'hesabix2_bbpress_localize_attachment_heading', 120, 2 );
|
|
|
|
/**
|
|
* برچسبهای انگلیسی فرم پیوست GD bbPress.
|
|
*
|
|
* @param string $translation Translated text.
|
|
* @param string $text Original text.
|
|
* @param string $domain Text domain.
|
|
* @return string
|
|
*/
|
|
function hesabix2_bbpress_attachments_i18n( $translation, $text, $domain ) {
|
|
if ( 'gd-bbpress-attachments' !== $domain ) {
|
|
return $translation;
|
|
}
|
|
|
|
$map = array(
|
|
'Upload Attachments' => 'پیوست فایل',
|
|
'Attachments' => 'پیوستها',
|
|
'Add another file' => 'افزودن فایل دیگر',
|
|
'Maximum file size allowed is %s.' => 'حداکثر حجم مجاز هر فایل %s است.',
|
|
'Upload Errors' => 'خطاهای بارگذاری',
|
|
'File not uploaded.' => 'فایل بارگذاری نشد.',
|
|
'Unknown file' => 'فایل ناشناخته',
|
|
);
|
|
|
|
return isset( $map[ $text ] ) ? $map[ $text ] : $translation;
|
|
}
|
|
add_filter( 'gettext', 'hesabix2_bbpress_attachments_i18n', 10, 3 );
|