admin
1 year ago
plugins
1 year ago
views
1 year ago
widget
1 year ago
fixes-archive.php
1 year ago
fixes-archives.php
1 year ago
fixes-calendar.php
1 year ago
fixes-dates.php
1 year ago
fixes-misc.php
1 year ago
fixes-permalinks.php
1 year ago
general.php
1 year ago
install.php
1 year ago
parsidate.php
1 year ago
settings.php
1 year ago
tools.php
1 year ago
fixes-dates.php
233 lines
| 1 | <?php |
| 2 | |
| 3 | defined( 'ABSPATH' ) or exit( 'No direct script access allowed' ); |
| 4 | |
| 5 | /** |
| 6 | * Fixes dates and convert them to Jalali date. |
| 7 | * |
| 8 | * @author Mobin Ghasempoor |
| 9 | * @package WP-Parsidate |
| 10 | * @subpackage Fixes/Dates |
| 11 | */ |
| 12 | |
| 13 | global $wpp_settings; |
| 14 | |
| 15 | if ( get_locale() === 'fa_IR' && wpp_is_active( 'persian_date' ) ) { |
| 16 | add_filter( 'the_time', 'wpp_fix_post_time', 10, 2 ); |
| 17 | add_filter( 'the_date', 'wpp_fix_post_date', 10, 3 ); |
| 18 | add_filter( 'get_the_time', 'wpp_fix_post_time', 10, 2 ); |
| 19 | add_filter( 'get_the_date', 'wpp_fix_post_date', 100, 3 ); |
| 20 | add_filter( 'get_comment_time', 'wpp_fix_comment_time', 10, 2 ); |
| 21 | add_filter( 'get_comment_date', 'wpp_fix_comment_date', 10, 2 ); |
| 22 | //add_filter('get_post_modified_time', 'wpp_fix_post_modified_time', 10, 3); |
| 23 | add_filter( 'date_i18n', 'wpp_fix_i18n', 10, 4 ); |
| 24 | |
| 25 | if ( ! wpp_is_sitemap() ) { |
| 26 | add_filter( 'wp_date', 'wpp_fix_i18n', 10, 4 ); |
| 27 | } |
| 28 | |
| 29 | if ( WP_Parsidate::is_plugin_activated( 'seo-by-rank-math/rank-math.php' ) ) { |
| 30 | add_filter( "rank_math/opengraph/facebook/article_published_time", function ( $content ) { |
| 31 | return gregdate( 'c', eng_number( $content ) ); |
| 32 | } ); |
| 33 | |
| 34 | add_filter( "rank_math/opengraph/facebook/article_modified_time", function ( $content ) { |
| 35 | return gregdate( 'c', eng_number( $content ) ); |
| 36 | } ); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Fixes post date and returns to Jalali format |
| 42 | * |
| 43 | * @param string $time Post time |
| 44 | * @param string $format Date format |
| 45 | * |
| 46 | * @return string Formatted date |
| 47 | */ |
| 48 | function wpp_fix_post_date( $time, $format = '', $post = null ) { |
| 49 | if ( null === $post ) { |
| 50 | global $post; |
| 51 | } else { |
| 52 | $post = get_post( $post ); |
| 53 | } |
| 54 | |
| 55 | // It seems some plugin like acf does not exist $post. |
| 56 | if ( ! $post ) { |
| 57 | return $time; |
| 58 | } |
| 59 | |
| 60 | if ( empty( $format ) ) { |
| 61 | $format = get_option( 'date_format' ); |
| 62 | } |
| 63 | |
| 64 | if ( ! disable_wpp() ) { |
| 65 | return date( $format, strtotime( $post->post_modified ) ); |
| 66 | } |
| 67 | |
| 68 | return parsidate( $format, date( 'Y-m-d H:i:s', strtotime( $post->post_date ) ), ! wpp_is_active( 'conv_dates' ) ? 'eng' : 'per' ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Fixes post date and returns to Jalali format |
| 73 | * |
| 74 | * @param string $time Post time |
| 75 | * @param string $format Date format |
| 76 | * @param bool $gmt retrieve the GMT time. Default false. |
| 77 | * |
| 78 | * @return string Formatted date |
| 79 | * @author Parsa Kafi |
| 80 | */ |
| 81 | function wpp_fix_post_modified_time( $time, $format, $gmt ) { |
| 82 | if ( ! disable_wpp() ) { |
| 83 | return $time; |
| 84 | } |
| 85 | |
| 86 | return parsidate( $format, $time, ! wpp_is_active( 'conv_dates' ) ? 'eng' : 'per' ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Fixes post time and returns to Jalali format |
| 91 | * |
| 92 | * @param string $time Post time |
| 93 | * @param string $format Date format |
| 94 | * |
| 95 | * @return string Formatted date |
| 96 | */ |
| 97 | function wpp_fix_post_time( $time, $format = '', $post = null ) { |
| 98 | $post = get_post( $post ); |
| 99 | |
| 100 | if ( ! $post ) { |
| 101 | global $post; |
| 102 | } |
| 103 | |
| 104 | if ( empty( $post ) ) { |
| 105 | return $time; |
| 106 | } |
| 107 | |
| 108 | if ( empty( $format ) ) { |
| 109 | $format = get_option( 'time_format' ); |
| 110 | } |
| 111 | |
| 112 | if ( ! disable_wpp() ) { |
| 113 | return date( $format, strtotime( $post->post_date ) ); |
| 114 | } |
| 115 | |
| 116 | return parsidate( $format, $post->post_date, wpp_is_active( 'conv_dates' ) ? 'eng' : 'per' ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Fixes comment time and returns to Jalali format |
| 121 | * |
| 122 | * @param string $time Comment time |
| 123 | * @param string $format Date format |
| 124 | * |
| 125 | * @return string Formatted date |
| 126 | */ |
| 127 | function wpp_fix_comment_time( $time, $format = '' ) { |
| 128 | global $comment; |
| 129 | |
| 130 | if ( empty( $comment ) ) { |
| 131 | return $time; |
| 132 | } |
| 133 | |
| 134 | if ( empty( $format ) ) { |
| 135 | $format = get_option( 'time_format' ); |
| 136 | } |
| 137 | if ( ! disable_wpp() ) { |
| 138 | return date( $format, strtotime( $comment->comment_date ) ); |
| 139 | } |
| 140 | |
| 141 | return parsidate( $format, $comment->comment_date, ! wpp_is_active( 'conv_dates' ) ? 'eng' : 'per' ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Fixes comment date and returns in Jalali format |
| 146 | * |
| 147 | * @param string $time Comment time |
| 148 | * @param string $format Date format |
| 149 | * |
| 150 | * @return string Formatted date |
| 151 | */ |
| 152 | function wpp_fix_comment_date( $time, $format = '' ) { |
| 153 | global $comment; |
| 154 | |
| 155 | if ( empty( $comment ) ) { |
| 156 | return $time; |
| 157 | } |
| 158 | |
| 159 | if ( empty( $format ) ) { |
| 160 | $format = get_option( 'date_format' ); |
| 161 | } |
| 162 | if ( ! disable_wpp() ) { |
| 163 | return date( $format, strtotime( $comment->comment_date ) ); |
| 164 | } |
| 165 | |
| 166 | return parsidate( $format, $comment->comment_date, ! wpp_is_active( 'conv_dates' ) ? 'eng' : 'per' ); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Fixes i18n date formatting and convert them to Jalali |
| 171 | * |
| 172 | * @param string $date Formatted date string. |
| 173 | * @param string $format Format to display the date. |
| 174 | * @param int $timestamp A sum of Unix timestamp and timezone offset in seconds. |
| 175 | * Might be without offset if input omitted timestamp but requested GMT. |
| 176 | * @param bool $gmt Whether to use GMT timezone. Only applies if timestamp was not provided. |
| 177 | * Default false. |
| 178 | * |
| 179 | * @return string Formatted time |
| 180 | */ |
| 181 | function wpp_fix_i18n( $date, $format, $timestamp, $gmt ) { |
| 182 | global $post; |
| 183 | |
| 184 | //$post_id = ( is_object( $post ) && isset( $post->ID ) ) ? $post->ID : null; |
| 185 | |
| 186 | if ( ! disable_wpp() ) { |
| 187 | return $format; |
| 188 | } |
| 189 | |
| 190 | return parsidate( $format, $timestamp, ! wpp_is_active( 'conv_dates' ) ? 'eng' : 'per' ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Convert date to Jalali |
| 195 | * |
| 196 | * @param $date |
| 197 | * @param $format |
| 198 | * @param $timestamp |
| 199 | * @param $timezone |
| 200 | * |
| 201 | * @return int|mixed|string |
| 202 | */ |
| 203 | function wpp_fix_wp_date( $date, $format, $timestamp, $timezone ) { |
| 204 | if ( ! disable_wpp() ) { |
| 205 | return $format; |
| 206 | } |
| 207 | |
| 208 | return parsidate( $format, $timestamp, ! wpp_is_active( 'conv_dates' ) ? 'eng' : 'per' ); |
| 209 | } |
| 210 | |
| 211 | function array_key_exists_r( $needle, $haystack, $value = null ) { |
| 212 | $result = array_key_exists( $needle, $haystack ); |
| 213 | |
| 214 | if ( $result ) { |
| 215 | if ( $value != null && $haystack[ $needle ] ) { |
| 216 | return 1; |
| 217 | } |
| 218 | |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | foreach ( $haystack as $v ) { |
| 223 | if ( is_array( $v ) || is_object( $v ) ) { |
| 224 | $result = array_key_exists_r( $needle, $v ); |
| 225 | } |
| 226 | |
| 227 | if ( $result ) { |
| 228 | return $result; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return $result; |
| 233 | } |