PluginProbe ʕ •ᴥ•ʔ
پارسی دیت – Parsi Date / 6.2.1
پارسی دیت – Parsi Date v6.2.1
6.2.1 6.2 6.1 5.1.6 5.1.7 5.1.8 5.1.8.2 6.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.0.0-alpha 2.1 2.1.1 2.1.2 2.1.3 2.1.5 2.1.6 2.1.7 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0.1 2.3.0.2 2.3.1 2.3.2 2.3.3 2.3.4 3.0.1 3.0.2 3.0.3 4.0.0 4.0.1 4.0.2 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5
wp-parsidate / inc / App / Convert / FixNumbers.php
wp-parsidate / inc / App / Convert Last commit date
Convert.php 3 weeks ago FixArabic.php 3 weeks ago FixNumbers.php 3 weeks ago
FixNumbers.php
133 lines
1 <?php
2
3 /**
4 * Fix Numbers
5 *
6 * Fix number in WP hooks
7 */
8
9 namespace WPParsidate\App\Convert;
10
11 use WPParsidate\Helper\{Number, NumberConverter, WordPress};
12 use WPParsidate\Settings\Settings;
13
14 class FixNumbers {
15 public function __construct() {
16 if ( Settings::get( 'conv_number_format_i18n', false ) ) {
17 add_filter( 'number_format_i18n', [ $this, 'changeNumbersToPersian' ], 1000 );
18 }
19
20 if ( Settings::get( 'conv_page_title', false ) ) {
21 add_filter( 'wp_title', [ $this, 'changeNumbersToPersian' ], 1000 );
22 }
23
24 if ( Settings::get( 'conv_title', false ) ) {
25 add_filter( 'the_title', [ $this, 'changeNumbersToPersian' ], 0 );
26 }
27
28 if ( Settings::get( 'conv_contents', false ) ) {
29 add_filter( 'the_content', [ $this, 'fixPostContentNumbers' ], 1000 );
30 }
31
32 if ( Settings::get( 'conv_excerpt', false ) ) {
33 add_filter( 'the_excerpt', [ $this, 'fixNumbersToPersian' ], 1000 );
34 }
35
36 if ( Settings::get( 'conv_comments', false ) ) {
37 add_filter( 'comment_text', [ $this, 'fixNumbersToPersian' ], 1000 );
38 }
39
40 if ( Settings::get( 'conv_comment_count', false ) ) {
41 add_filter( 'comments_number', [ $this, 'changeNumbersToPersian' ], 1000 );
42 }
43
44 if ( Settings::get( 'conv_cats', false ) ) {
45 add_filter( 'wp_list_categories', [ $this, 'fixNumbersToPersian' ], 1000 );
46 }
47 }
48
49 /**
50 * Fix numbers in content (Post content with HTML) and convert them to Persian digits
51 *
52 * @param $content
53 *
54 * @return string
55 */
56 public function fixPostContentNumbers( $content ): string {
57 if ( ! self::shouldRunContentNumberConverter( $content ) ) {
58 return $content;
59 }
60
61 $converted = NumberConverter::convertContent( $content );
62
63 return apply_filters( 'wp_parsidate_fix_post_content_numbers', $converted, $content );
64 }
65
66 /**
67 * Decide whether filter should run in current request context.
68 *
69 * @param string $content
70 *
71 * @return bool
72 */
73 private static function shouldRunContentNumberConverter( string $content ): bool {
74 if ( $content === '' ) {
75 return false;
76 }
77
78 // No digits => no work.
79 if ( ! preg_match( '/[0-9]/', $content ) ) {
80 return false;
81 }
82
83 // No Persian/Arabic chars => skip.
84 // Remove if your site should convert numbers in all languages.
85 if ( ! preg_match( '/[\x{0600}-\x{06FF}\x{0750}-\x{077F}\x{08A0}-\x{08FF}]/u', $content ) ) {
86 return false;
87 }
88
89 // Avoid admin screens except AJAX if needed.
90 if ( is_admin() && ! wp_doing_ajax() ) {
91 return false;
92 }
93
94 // Usually don't touch feeds.
95 if ( WordPress::isFeed() ) {
96 return false;
97 }
98
99 // Optional: skip REST responses if you don't want transformed content there.
100 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
101 return false;
102 }
103
104 if ( is_preview() ) {
105 return false;
106 }
107
108 return true;
109 }
110
111 /**
112 * Fix numbers and convert them to Persian digits style
113 *
114 * @param mixed $content
115 *
116 * @return string
117 */
118 public function fixNumbersToPersian( $content ): string {
119 return Number::fixNumber( $content );
120 }
121
122 /**
123 * Converts English digits to Persian digits
124 *
125 * @param mixed $string
126 *
127 * @return string
128 */
129 public function changeNumbersToPersian( $string ): string {
130 return Number::toPersian( $string );
131 }
132 }
133