admin
9 years ago
plugins
9 years ago
widget
9 years ago
fixes-archive.php
9 years ago
fixes-archives.php
9 years ago
fixes-calendar.php
9 years ago
fixes-dates.php
9 years ago
fixes-misc.php
9 years ago
fixes-permalinks.php
9 years ago
general.php
9 years ago
install.php
9 years ago
parsidate.php
9 years ago
settings.php
9 years ago
fixes-permalinks.php
352 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Fixes and converts permalinks date to Jalali |
| 4 | * |
| 5 | * @author Mobin Ghasempoor |
| 6 | * @package WP-Parsidate |
| 7 | * @subpackage Fixes/Permalinks |
| 8 | */ |
| 9 | global $wpp_settings; |
| 10 | |
| 11 | if ($wpp_settings['conv_permalinks'] == 'enable') { |
| 12 | add_filter('posts_where', 'wpp_posts_where', 10, 2); |
| 13 | add_action('pre_get_posts', 'wpp_pre_get_posts'); |
| 14 | add_filter('post_link', 'wpp_permalink', 10, 3); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Converts post date pointer to Jalali pointer |
| 19 | * |
| 20 | * @param string $where |
| 21 | * @param string $wp_query |
| 22 | * |
| 23 | * @return string |
| 24 | */ |
| 25 | function wpp_posts_where($where, $wp_query) |
| 26 | { |
| 27 | global $wpdb; |
| 28 | if (!$wp_query->is_main_query() || empty($wp_query->query_vars)) { |
| 29 | return $where; |
| 30 | } |
| 31 | |
| 32 | $pd = bn_parsidate::getInstance(); |
| 33 | |
| 34 | $m = (isset($wp_query->query_vars['m'])) ? $wp_query->query_vars['m'] : ''; |
| 35 | $hour = (isset($wp_query->query_vars['hour'])) ? $wp_query->query_vars['hour'] : ''; |
| 36 | $minute = (isset($wp_query->query_vars['minute'])) ? $wp_query->query_vars['minute'] : ''; |
| 37 | $second = (isset($wp_query->query_vars['second'])) ? $wp_query->query_vars['second'] : ''; |
| 38 | $year = (isset($wp_query->query_vars['year'])) ? $wp_query->query_vars['year'] : ''; |
| 39 | $month = (isset($wp_query->query_vars['month'])) ? $wp_query->query_vars['month'] : ''; |
| 40 | $day = (isset($wp_query->query_vars['day'])) ? $wp_query->query_vars['day'] : ''; |
| 41 | |
| 42 | if (!empty($m)) { |
| 43 | $len = strlen($m); |
| 44 | $year = substr($m, 0, 4); |
| 45 | if ($len > 5) { |
| 46 | $month = substr($m, 4, 2); |
| 47 | } |
| 48 | if ($len > 7) { |
| 49 | $day = substr($m, 6, 2); |
| 50 | } |
| 51 | if ($len > 9) { |
| 52 | $hour = substr($m, 8, 2); |
| 53 | } |
| 54 | if ($len > 11) { |
| 55 | $minute = substr($m, 10, 2); |
| 56 | } |
| 57 | if ($len > 13) { |
| 58 | $second = substr($m, 12, 2); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (empty($year) || $year > 1700) { |
| 63 | return $where; |
| 64 | } |
| 65 | |
| 66 | $stamon = 1; |
| 67 | $staday = 1; |
| 68 | $stahou = '00'; |
| 69 | $stamin = '00'; |
| 70 | $stasec = '00'; |
| 71 | $endmon = 1; |
| 72 | $endday = 1; |
| 73 | $endhou = '00'; |
| 74 | $endmin = '00'; |
| 75 | $endsec = '00'; |
| 76 | |
| 77 | $stayear = $year; |
| 78 | $endyear = $year + 1; |
| 79 | |
| 80 | if ($month != '') { |
| 81 | $stamon = $month; |
| 82 | $endmon = ($month == 12 ? 1 : $month + 1); |
| 83 | $endyear = ($endmon == 1 ? $stayear + 1 : $stayear); |
| 84 | } |
| 85 | |
| 86 | if ($day != '') { |
| 87 | $staday = $day; |
| 88 | $endday = ($day == $pd->j_days_in_month[(int)$month - 1] ? 1 : $day + 1); |
| 89 | $endmon = ($endday == 1 ? $stamon + 1 : $stamon); |
| 90 | } |
| 91 | |
| 92 | if ($hour != '') { |
| 93 | $stahou = $hour; |
| 94 | $endhou = ($hour == 24 ? '00' : $hour + 1); |
| 95 | $endday = ($endhou == '00' ? $staday + 1 : $staday); |
| 96 | } |
| 97 | |
| 98 | if ($minute != '') { |
| 99 | $stamin = $minute; |
| 100 | $endmin = ($minute == 59 ? '00' : $minute + 1); |
| 101 | $endhou = ($endmin == '00' ? $stahou + 1 : $stahou); |
| 102 | } |
| 103 | |
| 104 | if ($second != '') { |
| 105 | $stasec = $second; |
| 106 | $endsec = ($second == 59 ? '00' : $second + 1); |
| 107 | $endmin = ($endsec == '00' ? $stamin + 1 : $stamin); |
| 108 | } |
| 109 | |
| 110 | $stadate = "$stayear-$stamon-$staday"; |
| 111 | $enddate = "$endyear-$endmon-$endday"; |
| 112 | $stadate = gregdate('Y-m-d', $stadate); |
| 113 | $enddate = gregdate('Y-m-d', $enddate); |
| 114 | $stadate .= " $stahou:$stamin:$stasec"; |
| 115 | $enddate .= " $endhou:$endmin:$endsec"; |
| 116 | |
| 117 | $patterns = array( |
| 118 | '/YEAR\((.*?)post_date\s*\)\s*=\s*[0-9\']*/', |
| 119 | '/DAYOFMONTH\((.*?)post_date\s*\)\s*=\s*[0-9\']*/', |
| 120 | '/MONTH\((.*?)post_date\s*\)\s*=\s*[0-9\']*/', |
| 121 | '/HOUR\((.*?)post_date\s*\)\s*=\s*[0-9\']*/', |
| 122 | '/MINUTE\((.*?)post_date\s*\)\s*=\s*[0-9\']*/', |
| 123 | '/SECOND\((.*?)post_date\s*\)\s*=\s*[0-9\']*/' |
| 124 | ); |
| 125 | foreach ($patterns as $pattern) { |
| 126 | $where = preg_replace($pattern, '1=1', $where); |
| 127 | } |
| 128 | |
| 129 | $prefixp = "{$wpdb->posts}."; |
| 130 | $prefixp = (strpos($where, $prefixp) == false) ? '' : $prefixp; |
| 131 | |
| 132 | $where .= " AND {$prefixp}post_date >= '$stadate' AND {$prefixp}post_date < '$enddate' "; |
| 133 | |
| 134 | return $where; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Converts post dates to Georgian dates for preventing errors |
| 139 | * |
| 140 | * @param WP_Query $query |
| 141 | * |
| 142 | * @return mixed |
| 143 | */ |
| 144 | function wpp_pre_get_posts($query) |
| 145 | { |
| 146 | global $wpdb; |
| 147 | |
| 148 | $permalink = $query->query; |
| 149 | $year = ''; |
| 150 | $monthnum = ''; |
| 151 | $day = '';//start |
| 152 | |
| 153 | if (isset($permalink['year'])) { |
| 154 | $year = $permalink['year']; |
| 155 | } |
| 156 | if (isset($permalink['monthnum'])) { |
| 157 | $monthnum = $permalink['monthnum']; |
| 158 | } |
| 159 | if (isset($permalink['day'])) { |
| 160 | $day = $permalink['day']; |
| 161 | } |
| 162 | if ($year > 1700) { |
| 163 | return $query; |
| 164 | } |
| 165 | |
| 166 | $out = false; |
| 167 | $pd = bn_parsidate::getInstance(); |
| 168 | if (isset($permalink['name'])) { |
| 169 | $var = $wpdb->get_var("SELECT post_date FROM {$wpdb->prefix}posts WHERE post_name='{$permalink['name']}' ORDER BY id"); |
| 170 | $per = parsidate('Y-m-d', $var, 'eng'); |
| 171 | //update_option('options', $per ); |
| 172 | $per = explode('-', $per); |
| 173 | $out = true; |
| 174 | |
| 175 | if ($year != $per[0]) { |
| 176 | $out = false; |
| 177 | } |
| 178 | if ($out && $monthnum != $per[1]) { |
| 179 | $out = false; |
| 180 | } |
| 181 | if ($out && $day != $per[2]) { |
| 182 | $out = false; |
| 183 | } |
| 184 | |
| 185 | } elseif (isset($permalink['post_id'])) { |
| 186 | $out = true; |
| 187 | $var = $wpdb->get_var("SELECT post_date FROM {$wpdb->prefix}posts WHERE ID={$permalink['post_id']}"); |
| 188 | } elseif (!empty($year) && !empty($monthnum) && !empty($day)) { |
| 189 | $out = true; |
| 190 | $var = gregdate('Y-m-d', "$year-$monthnum-$day"); |
| 191 | } elseif (!empty($year) && !empty($monthnum)) { |
| 192 | $stadate = $pd->persian_to_gregorian($year, $monthnum, 1); |
| 193 | $enddate = $pd->persian_to_gregorian($year, $monthnum, $pd->j_days_in_month[($monthnum - 1)]); |
| 194 | $date_query = array( |
| 195 | array( |
| 196 | 'after' => array( |
| 197 | 'year' => $stadate[0], |
| 198 | 'month' => $stadate[1], |
| 199 | 'day' => $stadate[2] - 1, |
| 200 | ), |
| 201 | 'before' => array( |
| 202 | 'year' => $enddate[0], |
| 203 | 'month' => $enddate[1], |
| 204 | 'day' => $enddate[2] + 1, |
| 205 | ), |
| 206 | 'inclusive' => true, |
| 207 | ), |
| 208 | ); |
| 209 | $query->set('date_query', $date_query); |
| 210 | |
| 211 | // commented for get year & month in calander widget |
| 212 | // $query->set('year', ''); |
| 213 | // $query->set('monthnum', ''); |
| 214 | $out = false; |
| 215 | |
| 216 | } elseif (!empty($year)) { |
| 217 | $stadate = $pd->persian_to_gregorian($year, 1, 1); |
| 218 | $enddate = $pd->persian_to_gregorian(($year + 1), 1, 1); |
| 219 | $date_query = array( |
| 220 | array( |
| 221 | 'after' => array( |
| 222 | 'year' => $stadate[0], |
| 223 | 'month' => $stadate[1], |
| 224 | 'day' => $stadate[2] - 1, |
| 225 | ), |
| 226 | 'before' => array( |
| 227 | 'year' => $enddate[0], |
| 228 | 'month' => $enddate[1], |
| 229 | 'day' => $enddate[2], |
| 230 | ), |
| 231 | 'inclusive' => true, |
| 232 | ), |
| 233 | ); |
| 234 | $query->set('date_query', $date_query); |
| 235 | // $query->set('year', ''); |
| 236 | $out = false; |
| 237 | } |
| 238 | |
| 239 | if ($out) { |
| 240 | preg_match_all('!\d+!', $var, $matches); |
| 241 | $var = $matches[0]; |
| 242 | |
| 243 | $query->set('year', $var[0]); |
| 244 | $query->set('monthnum', $var[1]); |
| 245 | $query->set('day', $var[2]); |
| 246 | } |
| 247 | |
| 248 | $query->is_404 = false; |
| 249 | $query->query_vars['error'] = ''; |
| 250 | |
| 251 | return $query; |
| 252 | |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Convert permalink structure to Jalali format |
| 257 | * |
| 258 | * @param mixed $perma |
| 259 | * @param WP_Post $post |
| 260 | * @param bool $leavename |
| 261 | * |
| 262 | * @return string New permalink |
| 263 | */ |
| 264 | function wpp_permalink($perma, $post, $leavename = false) |
| 265 | { |
| 266 | if (empty($post->ID)) { |
| 267 | return false; |
| 268 | } |
| 269 | if ($post->post_type == 'page' || $post->post_status == 'static') { |
| 270 | return get_page_link($post->ID); |
| 271 | } elseif ($post->post_type == 'attachment') { |
| 272 | return get_attachment_link($post->ID); |
| 273 | } elseif (in_array($post->post_type, get_post_types(array('_builtin' => false)))) { |
| 274 | return get_post_permalink($post->ID); |
| 275 | } |
| 276 | |
| 277 | $permalink = get_option('permalink_structure'); |
| 278 | preg_match_all('/%([^\/]*)%/', $permalink, $rewritecode); |
| 279 | $rewritecode = $rewritecode[0]; |
| 280 | if ('' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) { |
| 281 | if ($leavename) { |
| 282 | $rewritecode = array_diff($rewritecode, array('%postname%', '%pagename%')); |
| 283 | } |
| 284 | |
| 285 | $date = explode(' ', parsidate('Y m d H i s', $post->post_date, 'eng')); |
| 286 | $out = array(); |
| 287 | |
| 288 | foreach ($rewritecode as $rewrite) { |
| 289 | switch ($rewrite) { |
| 290 | case '%year%': |
| 291 | $out[] = $date[0]; |
| 292 | break; |
| 293 | case '%monthnum%': |
| 294 | $out[] = $date[1]; |
| 295 | break; |
| 296 | case '%day%': |
| 297 | $out[] = $date[2]; |
| 298 | break; |
| 299 | case '%hour%': |
| 300 | $out[] = $date[3]; |
| 301 | break; |
| 302 | case '%minute%': |
| 303 | $out[] = $date[4]; |
| 304 | break; |
| 305 | case '%second%': |
| 306 | $out[] = $date[5]; |
| 307 | break; |
| 308 | case '%post_id%': |
| 309 | $out[] = $post->ID; |
| 310 | break; |
| 311 | case '%postname%': |
| 312 | $out[] = $post->post_name; |
| 313 | break; |
| 314 | case '%category%': |
| 315 | $category = ''; |
| 316 | $cats = get_the_category($post->ID); |
| 317 | if ($cats) { |
| 318 | usort($cats, '_usort_terms_by_ID'); |
| 319 | $category = $cats[0]->slug; |
| 320 | if ($parent = $cats[0]->parent) { |
| 321 | $category = get_category_parents($parent, false, '/', true); |
| 322 | } |
| 323 | |
| 324 | if ($cats[0]->parent != 0) { |
| 325 | $category .= $cats[0]->slug; |
| 326 | } |
| 327 | } |
| 328 | if (empty($category)) { |
| 329 | $default_category = get_term(get_option('default_category'), 'category'); |
| 330 | $category = is_wp_error($default_category) ? '' : $default_category->slug; |
| 331 | } |
| 332 | $out[] = $category; |
| 333 | break; |
| 334 | case '%author%': |
| 335 | $authordata = get_userdata($post->post_author); |
| 336 | $out[] = $authordata->user_nicename; |
| 337 | break; |
| 338 | case '%pagename%': |
| 339 | $out[] = $post->post_name; |
| 340 | break; |
| 341 | default: |
| 342 | unset($rewritecode[array_search($rewrite, $rewritecode)]); |
| 343 | break; |
| 344 | } |
| 345 | } |
| 346 | $permalink = home_url(str_replace($rewritecode, $out, $permalink)); |
| 347 | |
| 348 | return user_trailingslashit($permalink, 'single'); |
| 349 | } else { |
| 350 | return home_url("?p=$post->ID"); |
| 351 | } |
| 352 | } |