admin
4 years ago
plugins
4 years ago
widget
4 years ago
fixes-archive.php
4 years ago
fixes-archives.php
4 years ago
fixes-calendar.php
4 years ago
fixes-dates.php
4 years ago
fixes-misc.php
4 years ago
fixes-permalinks.php
4 years ago
general.php
4 years ago
install.php
4 years ago
parsidate.php
4 years ago
settings.php
4 years ago
general.php
228 lines
| 1 | <?php |
| 2 | |
| 3 | defined( 'ABSPATH' ) or exit( 'No direct script access allowed' ); |
| 4 | |
| 5 | /** |
| 6 | * WP-Parsidate general functions |
| 7 | * |
| 8 | * @author Mobin Ghasempoor |
| 9 | * @author Morteza Geransayeh |
| 10 | * @author Ehsaan |
| 11 | * @package WP-Parsidate |
| 12 | * @subpackage Core/General |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * Change Locale WordPress Admin and Front-end user |
| 17 | * |
| 18 | * @param String $locale |
| 19 | * |
| 20 | * @return String |
| 21 | */ |
| 22 | function wp_parsi_set_locale( $locale ) { |
| 23 | global $locale; |
| 24 | |
| 25 | if ( wpp_is_active( 'admin_lang' ) ) { |
| 26 | $admin_locale = "fa_IR"; |
| 27 | } else { |
| 28 | $admin_locale = $locale; |
| 29 | } |
| 30 | |
| 31 | if ( wpp_is_active( 'user_lang' ) ) { |
| 32 | $user_locale = "fa_IR"; |
| 33 | } else { |
| 34 | $user_locale = $locale; |
| 35 | } |
| 36 | |
| 37 | $locale_s = is_admin() ? $admin_locale : $user_locale; |
| 38 | |
| 39 | if ( ! empty( $locale_s ) ) { |
| 40 | $locale = $locale_s; |
| 41 | } |
| 42 | |
| 43 | setlocale( LC_ALL, $locale ); |
| 44 | |
| 45 | return $locale; |
| 46 | } |
| 47 | |
| 48 | add_filter( 'locale', 'wp_parsi_set_locale', 0 ); |
| 49 | |
| 50 | /** |
| 51 | * Change login header url in wp-login.php |
| 52 | * |
| 53 | * @return string |
| 54 | */ |
| 55 | function wpp_login_headerurl() { |
| 56 | return 'https://wp-parsi.com'; |
| 57 | } |
| 58 | |
| 59 | add_filter( 'login_headerurl', 'wpp_login_headerurl', 10, 2 ); |
| 60 | |
| 61 | /** |
| 62 | * Notice for the activation. |
| 63 | * Added dismiss feature. |
| 64 | * |
| 65 | * @return void |
| 66 | * @author Ehsaan |
| 67 | */ |
| 68 | function wpp_activation_notice() { |
| 69 | $dismissed = get_option( 'wpp_dismissed', false ); |
| 70 | |
| 71 | if ( ! $dismissed ) { |
| 72 | if ( ! wpp_is_active( 'persian_date' ) ) { |
| 73 | echo sprintf( |
| 74 | __( '<div class="updated wpp-message"><p>ParsiDate activated, you may need to configure it to work properly. <a href="%s">Go to configuration page</a> – <a href="%s">Dismiss</a></p></div>', 'wp-parsidate' ), |
| 75 | admin_url( 'admin.php?page=wp-parsi-settings' ), |
| 76 | add_query_arg( 'wpp-action', 'dismiss-notice' ) |
| 77 | ); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | add_action( 'admin_notices', 'wpp_activation_notice' ); |
| 83 | |
| 84 | /** |
| 85 | * Dismiss the notice action |
| 86 | * |
| 87 | * @return void |
| 88 | * @author Ehsaan |
| 89 | */ |
| 90 | function wpp_dismiss_notice_action() { |
| 91 | if ( isset( $_GET['wpp-action'] ) && $_GET['wpp-action'] == 'dismiss-notice' ) { |
| 92 | update_option( 'wpp_dismissed', true ); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | add_action( 'admin_init', 'wpp_dismiss_notice_action' ); |
| 97 | |
| 98 | /** |
| 99 | * disable wp widget block that introduced in WordPress 5.8 |
| 100 | * |
| 101 | * @since 4.0.0 |
| 102 | */ |
| 103 | function wpp_disable_gutenberg_blocks_widget() { |
| 104 | if ( wpp_is_active( 'disable_widget_block' ) ) { |
| 105 | add_filter( 'gutenberg_use_widgets_block_editor', '__return_false' ); |
| 106 | add_filter( 'use_widgets_block_editor', '__return_false' ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | add_action( 'init', 'wpp_disable_gutenberg_blocks_widget' ); |
| 111 | |
| 112 | /** |
| 113 | * Detects current page is feed or not |
| 114 | * |
| 115 | * @return bool True when page is feed, false when page isn't feed |
| 116 | * @since 1.0 |
| 117 | */ |
| 118 | function wpp_is_feed() { |
| 119 | if ( is_feed() ) { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | $path = $_SERVER['REQUEST_URI']; |
| 124 | $exts = array( 'xml', 'gz', 'xsl' ); |
| 125 | $ext = pathinfo( $path, PATHINFO_EXTENSION ); |
| 126 | |
| 127 | return in_array( $ext, $exts ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Converts English digits to Persian digits |
| 132 | * |
| 133 | * @param string $number Numbers |
| 134 | * |
| 135 | * @return string Formatted numbers |
| 136 | */ |
| 137 | function per_number( $number ) { |
| 138 | return str_replace( |
| 139 | range( 0, 9 ), |
| 140 | array( '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹' ), |
| 141 | $number |
| 142 | ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Converts Persian digits to English digits |
| 147 | * |
| 148 | * @param string $number Numbers |
| 149 | * |
| 150 | * @return string Formatted numbers |
| 151 | */ |
| 152 | function eng_number( $number ) { |
| 153 | return str_replace( |
| 154 | array( '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹' ), |
| 155 | range( 0, 9 ), |
| 156 | $number |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Converts English numbers to Persian numbers in post contents |
| 162 | * |
| 163 | * @param string $content Post content |
| 164 | * |
| 165 | * @return string Formatted content |
| 166 | */ |
| 167 | function persian_number( $content ) { |
| 168 | return isset( $content[1] ) ? per_number( $content[1] ) : $content[0]; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Fix numbers and convert them to Persian digits style |
| 173 | * |
| 174 | * @param string $content |
| 175 | * |
| 176 | * @return array|string|string[]|null |
| 177 | */ |
| 178 | function fix_number( $content ) { |
| 179 | return preg_replace_callback( '/(?:&#\d{2,4};)|(?:[0]?[a-z][\x20-\x3B=\x3F-\x7F]*)|(\d+[\d]*)|<\s*[^>]+>/i', 'persian_number', $content ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Fix arabic foreign characters |
| 184 | * |
| 185 | * @param string $content |
| 186 | * |
| 187 | * @return array|string|string[] |
| 188 | */ |
| 189 | function fix_arabic( $content ) { |
| 190 | return str_replace( array( 'ي', 'ك', '٤', '٥', '٦', 'ة' ), array( 'ی', 'ک', '۴', '۵', '۶', 'ه' ), $content ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * parsidate_check_format() |
| 195 | * checks format for iso definitions |
| 196 | * |
| 197 | * @param string $format |
| 198 | * |
| 199 | * @return boolean |
| 200 | */ |
| 201 | function parsidate_check_format( $format ) { |
| 202 | return in_array( $format, array( |
| 203 | 'Z', // Timezone offset in seconds // -43200 through 50400 |
| 204 | 'T', // Timezone abbreviation // Examples: EST, MDT |
| 205 | 'O', // Difference to Greenwich time (GMT) in hours // Example: +0200 |
| 206 | 'P', // Difference to Greenwich time (GMT) with colon between hours and minutes // Example: +02:00 |
| 207 | 'U', // Seconds since the Unix Epoch (January 1, 1970 00:00:00 GMT) |
| 208 | 'u', // Microseconds // Example: 654321 |
| 209 | 'e', // Timezone identifier // Examples: UTC, GMT, Atlantic/Azores |
| 210 | 'r', // RFC 2822 formatted date // Example: Thu, 21 Dec 2000 16:01:07 +0200 |
| 211 | 'c', // ISO 8601 date // 2004-02-12T15:19:21+00:00 // 'Y-m-d\TH:i:s\Z' |
| 212 | 'G', // 24-hour format of an hour without leading zeros // 0 through 23 |
| 213 | 'I', // Whether the date is in daylight saving time // 1 if Daylight Saving Time, 0 otherwise. |
| 214 | |
| 215 | // Commented this lines, because user/system want to convert these formats. |
| 216 | /*'Y-m-d_H-i-s', |
| 217 | 'Y-m-d_G-i-s', |
| 218 | 'Y-m-d H:i:s', |
| 219 | 'Y-m-d G:i:s', |
| 220 | 'd-M-Y H:i',*/ |
| 221 | |
| 222 | DATE_W3C, // eq `c` |
| 223 | DATE_ISO8601, // eq `c` |
| 224 | DATE_RFC2822, // eq `r` |
| 225 | 'Y-m-d\TH:i:s+00:00', // eq `DATE_W3C` @SEE: http://jochenhebbrecht.be/site/node/761 |
| 226 | 'Y-m-d\TH:i:sP', |
| 227 | ) ); |
| 228 | } |