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 |