PluginProbe ʕ •ᴥ•ʔ
پارسی دیت – Parsi Date / 1.1
پارسی دیت – Parsi Date v1.1
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 / wp-parsidate.php
wp-parsidate Last commit date
css 12 years ago js 12 years ago languages 12 years ago lib 12 years ago admin.php 12 years ago index.php 12 years ago readme.txt 12 years ago screenshot-1.png 12 years ago screenshot-2.png 12 years ago widegets.php 12 years ago wp-parsidate.php 12 years ago
wp-parsidate.php
647 lines
1 <?php
2
3 /*
4 Plugin Name: WP-Parsidate
5 Version: 1.1
6 Author: Mobin Ghasempoor
7 Author URI: http://wp-parsi.com/
8 Plugin URI: http://forum.wp-parsi.com/
9 Description: Persian package builder for WordPress, Full RTL and Shamsi(Jalali) date in: posts, comments, pages, archives, search, categories, permalinks and all admin sections such as tinymce editor, posts lists, post quick edit, comments lists, comments quick edit, pages lists, pages quick edit. This package already has Shamsi(Jalali) archie widget.
10 */
11
12 /*
13 Special thanks to :
14 Wordpress Parsi admins and moderators (Parsa Kafi, Mohsen Ghiasi, Saeed Fard, Abdolmajed Shahbakhsh, Morteza Rocky and Mostafa Soufi)
15 Wordpress Parsi forum members for great support(forum.wp-parsi.com)
16 */
17
18 define('wp_parsipath', dirname(__file__));
19 define('wp_contentpath',dirname(dirname(wp_parsipath)));
20 global $timezone,$persian_month_names;
21 $persian_month_names = array('','فروردین','اردیبهشت','خرداد','تیر','�
22 رداد','شهریور','�
23 هر','آبان','آذر','دی','به�
24 ن','اسفند');
25
26 $timezone = get_option('timezone_string');
27 if(empty($timezone))
28 $timezone='Asia/Tehran';
29 date_default_timezone_set($timezone);
30
31 include_once (join(DIRECTORY_SEPARATOR,array(wp_parsipath,'lib','parsidate.php')));
32 include_once (join(DIRECTORY_SEPARATOR,array(wp_parsipath,'lib','functions.php')));
33 include_once (join(DIRECTORY_SEPARATOR,array(wp_parsipath,'widegets.php')));
34 include_once (join(DIRECTORY_SEPARATOR,array(wp_parsipath,'admin.php')));
35 register_activation_hook( __FILE__,'parsidate_plugin_install');
36
37 function parsidate_plugin_install()
38 {
39 if (!is_dir(join(DIRECTORY_SEPARATOR,array(wp_contentpath,'languages'))))
40 mkdir(join(DIRECTORY_SEPARATOR,array(wp_contentpath,'languages')));
41
42 $source = join(DIRECTORY_SEPARATOR,array(wp_parsipath,'languages','*'));
43 $destination = join(DIRECTORY_SEPARATOR,array(wp_contentpath,'languages'));
44 $files = glob($source);
45
46 foreach($files as $sfile)
47 {
48 @copy($sfile, $destination.DIRECTORY_SEPARATOR.basename($sfile));
49 }
50 }
51
52 if(isset($_POST['wp_parsidate_save']))
53 {
54 $val['sep_fixdate'] = $_POST['sep_fixdate'];
55 $val['sep_persian'] = $_POST['sep_persian'];
56 $val['sep_titlenum'] = (isset($_POST['sep_titlenum']) ?'checked':'');
57 $val['sep_postnum'] = (isset($_POST['sep_postnum']) ?'checked':'');
58 $val['sep_commentnum'] = (isset($_POST['sep_commentnum'])?'checked':'');
59 $val['sep_commentcnt'] = (isset($_POST['sep_commentcnt'])?'checked':'');
60 $val['sep_datesnum'] = (isset($_POST['sep_datesnum']) ?'checked':'');
61 $val['sep_catnum'] = (isset($_POST['sep_catnum']) ?'checked':'');
62 $val['sep_excnum'] = (isset($_POST['sep_excnum']) ?'checked':'');
63 $val['sep_fixarabic'] = $_POST['sep_fixarabic'];
64 $val['sep_fixurl'] = $_POST['sep_fixurl'];
65 $val['sep_planet'] = $_POST['sep_planet'];
66
67 update_option('parsidate_option',$val);
68 }
69
70 global $val;
71
72 $val = get_option('parsidate_option');
73
74 if(empty($val))
75 {
76 $val['sep_fixdate'] = 'بلی';
77 $val['sep_persian'] = 'بلی';
78 $val['sep_titlenum'] = '';
79 $val['sep_postnum'] = '';
80 $val['sep_commentnum'] = '';
81 $val['sep_commentcnt'] = '';
82 $val['sep_datesnum'] = '';
83 $val['sep_catnum'] = '';
84 $val['sep_fixarabic'] = 'بلی';
85 $val['sep_fixurl'] = 'بلی';
86 $val['sep_planet'] = 0;
87 $val['sep_excnum'] = '';
88 }
89
90 if($val['sep_persian']=='بلی')
91 add_filter('locale','new_locale');
92
93 function new_locale()
94 {
95 return 'fa_IR';
96 }
97 /*
98 * admin config page
99 */
100 add_action('admin_menu', 'add_persiandate_menu');
101
102 function add_persiandate_menu()
103 {
104 add_menu_page('تنظی�
105 ات پارسی', 'تنظی�
106 ات پارسی', 'add_users', 'parsi_plugin_page','parsi_plugin_page', '');
107 }
108
109 /*
110 * fix theme editor rtl
111 */
112 add_action( 'admin_print_styles-plugin-editor.php', 'theme_editor_add_init', 11 );
113 add_action( 'admin_print_styles-theme-editor.php', 'theme_editor_add_init', 11 );
114 function theme_editor_add_init(){
115 wp_enqueue_style("functions", plugins_url(basename(wp_parsipath)."/css/admin.css"), false, "1.0", "all");
116 }
117
118
119 /*
120 * fix tiny mce rtl
121 */
122 add_filter('tiny_mce_before_init', 'wpb_mce_set_direction',1000);
123
124 function wpb_mce_set_direction($input)
125 {
126 $input['content_css']=plugins_url(basename(wp_parsipath).'/css/editor.css');
127 return $input;
128 }
129
130 /*
131 * hooks and filters for persian date
132 */
133
134 if($val['sep_fixdate']=='بلی')
135 {
136 if(!detect_rss())
137 {
138 add_filter('the_time', 'add_ptime',1001,2);
139 add_filter('the_date', 'add_pdate',1001,2);
140 add_action('date_i18n', 'add_pi18n',1001,3);//revision
141 add_filter('get_comment_time', 'add_ctime',1001,2);
142 add_filter('get_comment_date', 'add_cdate',1001,2);
143 }
144 }
145
146 function add_ptime($time,$format='')
147 {
148 global $post,$val;
149 if(empty($format))
150 $format=get_option('time_format');
151 if(empty($val['sep_datesnum']))
152 return parsidate($format,$post->post_date,'eng');
153 else
154 return parsidate($format,$post->post_date);
155 }
156
157 function add_pdate($time,$format='')
158 {
159 global $post,$val;
160 if(empty($format))
161 $format=get_option('date_format');
162 if(empty($val['sep_datesnum']))
163 return parsidate($format,$post->post_date,'eng');
164 else
165 return parsidate($format,$post->post_date);
166 }
167
168 function add_ctime($time,$format='')
169 {
170 global $comment,$val;
171 if(empty($format))
172 $format=get_option('time_format');
173 if(empty($val['sep_datesnum']))
174 return parsidate($format,$comment->comment_date,'eng');
175 else
176 return parsidate($format,$comment->comment_date);
177 }
178
179 function add_cdate($time,$format='')
180 {
181 global $comment,$val;
182 if(empty($format))
183 $format=get_option('date_format');
184 if(empty($val['sep_datesnum']))
185 return parsidate($format,$comment->comment_date,'eng');
186 else
187 return parsidate($format,$comment->comment_date);
188 }
189
190 function add_pi18n($dateformatstring,$unixtimestamp,$gmt)
191 {
192 global $val;
193 if(empty($val['sep_datesnum']))
194 return parsidate($unixtimestamp,$gmt,'eng');
195 else
196 return parsidate($unixtimestamp,$gmt);
197 }
198 /*
199 * fix persian numbers
200 */
201 if(!empty($val['sep_postnum']))
202 add_filter('the_content', 'fixnumber');
203 if(!empty($val['sep_titlenum']))
204 add_filter('the_title', 'fixnumber');
205 if(!empty($val['sep_commentnum']))
206 add_filter('comment_text', 'fixnumber');
207 if(!empty($val['sep_commentcnt']))
208 add_filter('comments_number', 'fixnumber');
209 if(!empty($val['sep_catnum']))
210 add_filter('wp_list_categories', 'fixnumber');
211 if(!empty($val['sep_excnum']))
212 add_filter('the_excerpt', 'fixnumber');
213
214 /*
215 * fix arabic characters
216 */
217 if($val['sep_fixarabic']=='بلی')
218 {
219 add_filter('the_content', 'fixarabic');
220 add_filter('the_title', 'fixarabic');
221 add_filter('comment_text', 'fixarabic');
222 add_filter('wp_list_categories', 'fixarabic');
223 add_filter('the_excerpt', 'fixarabic');
224 }
225 /*
226 *fix archive title
227 */
228 add_filter( 'wp_title', 'wp_pdtitle', 1001,3);
229
230 function wp_pdtitle($title, $sep,$seplocation)
231 {
232 global $persian_month_names,$wp_query;
233 $query=$wp_query->query;
234
235 if(!is_archive() or (is_archive() and !isset($query['monthnum']) ))
236 return $title;
237 if($seplocation=='right')
238 $query = array_reverse($query);
239 $query['name']=get_option('blogname');
240 $query['monthnum']=$persian_month_names[intval($query['monthnum'])];
241 return fixnumber(implode(" $sep ",$query));
242 }
243 /*
244 * fix persian permalink
245 */
246 if ($val['sep_fixurl']=='بلی')
247 {
248 add_filter("post_link","get_pdpermalink",10,3);
249 add_action( 'pre_get_posts', 'wppd_pre_get_posts');
250 add_filter( 'posts_where' ,'wppd_posts_where');
251 }
252
253 function get_pdpermalink($perma, $post,$leavename = false)
254 {
255 if(empty($post->ID))
256 return false;
257 if ( $post->post_type == 'page' || $post->post_status == 'static')
258 return get_page_link($post->ID);
259 elseif ( $post->post_type == 'attachment' )
260 return get_attachment_link( $post->ID);
261 elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false))))
262 return get_post_permalink($post->ID);
263 $permalink = get_option('permalink_structure');
264 preg_match_all('/%([^\/]*)%/',$permalink,$rewritecode);
265 $rewritecode = $rewritecode[0];
266 if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
267 {
268 if($leavename)
269 $rewritecode=array_diff($rewritecode,array('%postname%','%pagename%'));
270
271 $date= explode(" ",parsidate('Y m d H i s',$post->post_date,'eng'));
272 $out=array();
273 foreach($rewritecode as $rewrite)
274 {
275 switch($rewrite)
276 {
277 case'%year%':
278 $out[] = $date[0];
279 break;
280 case'%monthnum%':
281 $out[] = $date[1];
282 break;
283 case'%day%':
284 $out[] = $date[2];
285 break;
286 case'%hour%':
287 $out[] = $date[3];
288 break;
289 case'%minute%':
290 $out[] = $date[4];
291 break;
292 case'%second%':
293 $out[] = $date[5];
294 break;
295 case'%postname%':
296 $out[] = $post->post_name;
297 break;
298 case'%post_id%':
299 $out[] = $post->ID;
300 break;
301 case'%category%':
302 $category='';
303 $cats = get_the_category($post->ID);
304 if ($cats)
305 {
306 usort($cats, '_usort_terms_by_ID');
307 $category_object = get_term( $category_object, 'category' );
308 $category = $category_object->slug;
309 if ( $parent = $category_object->parent )
310 $category = get_category_parents($parent, false, '/', true) . $category;
311 }
312 if (empty($category))
313 {
314 $default_category = get_term( get_option('default_category'),'category');
315 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
316 }
317 $out[] = $category;
318 break;
319 case'%author%':
320 $authordata = get_userdata($post->post_author);
321 $out[] = $authordata->user_nicename;
322 break;
323 case'%pagename%':
324 $out[] = $post->post_name;
325 break;
326 default:unset($rewritecode[array_search($rewrite,$rewritecode)]);
327 }
328 }
329 $permalink = home_url( str_replace($rewritecode, $out, $permalink));
330 return user_trailingslashit($permalink, 'single');
331 }
332 else
333 return home_url("?p=$post->ID");
334 }
335
336 function wppd_pre_get_posts( $query )
337 {
338 global $wpdb;
339 $permalink = $query->query;
340 $year = '';
341 $monthnum = '';
342 $day = '';
343 if(isset($permalink['year']))
344 $year=$permalink['year'];
345 if(isset($permalink['monthnum']))
346 $monthnum=$permalink['monthnum'];
347 if(isset($permalink['day']))
348 $day=$permalink['day'];
349 if(!empty($year)||!empty($monthnum)||!empty($day))
350 {
351 $cnt = '';
352 $post_id = '';
353 $name = '';
354 $out = false;
355 if(isset($permalink['name']))
356 {
357 $name = $permalink['name'];
358 $var = $wpdb->get_var("select post_date from {$wpdb->prefix}posts where post_name='$name' order by id");
359 $per = parsidate('Y-m-d',$var,'eng');
360 update_option('options',$per);
361 $per = explode('-',$per);
362 $out = true;
363 if(!empty($year))
364 if($year != $per[0])
365 $out = false;
366 if($out and !empty($monthnum))
367 if($monthnum!=$per[1])
368 $out = false;
369 if($out and !empty($day))
370 if($day != $per[2])
371 $out = false;
372 }
373 elseif($permalink['post_id'])
374 {
375 $out = true;
376 $post_id = $permalink['post_id'];
377 $var = $wpdb->get_var("select post_date from {$wpdb->prefix}wp_posts where ID=$post_id");
378 }
379 elseif(!empty($year)and!empty($monthnum)and!empty($day))
380 {
381 $out = true;
382 $var = gregdate('Y-m-d',"$year-$monthnum-$day");
383 }
384
385 if($out)
386 {
387 preg_match_all('!\d+!', $var, $matches);
388 $var=$matches[0];
389 $query->set( 'year', $var[0]);
390 $query->set( 'monthnum', $var[1]);
391 $query->set( 'day', $var[2]);
392 }
393 return $query;
394 }
395 else
396 return $query;
397 }
398
399 function wppd_posts_where($where)
400 {
401 global $wp_query, $wpdb;
402 if(empty($wp_query->query_vars))
403 return false;
404 $j_days_in_month = array('',31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);
405 $m = $wp_query->query_vars['m'];
406 $hour = $wp_query->query_vars['hour'];
407 $minute = $wp_query->query_vars['minute'];
408 $second = $wp_query->query_vars['second'];
409 $year = $wp_query->query_vars['year'];
410 $month = $wp_query->query_vars['monthnum'];
411 $day = $wp_query->query_vars['day'];
412
413 if(!empty($m))
414 {
415 $len = strlen($m);
416 $year = substr($m, 0,4);
417 if($len>5)
418 $month = substr($m, 4, 2);
419 if($len>7)
420 $day = substr($m, 6, 2);
421 if($len>9)
422 $hour = substr($m, 8, 2);
423 if($len>11)
424 $minute = substr($m, 10, 2);
425 if($len>13)
426 $second = substr($m, 12, 2);
427 }
428
429 if(empty($year)|| $year>1700)
430 return $where;
431
432 $stamon = 1;
433 $staday = 1;
434 $stahou = '00';
435 $stamin = '00';
436 $stasec = '00';
437 $endmon = 1;
438 $endday = 1;
439 $endhou = '00';
440 $endmin = '00';
441 $endsec = '00';
442
443 $stayear=$year;
444 $endyear=$year+1;
445 if($month!='')
446 {
447 $stamon = $month;
448 $endmon = ($month==12?1:$month+1);
449 $endyear = ($endmon==1?$stayear+1:$stayear);
450 }
451 if($day!='')
452 {
453 $staday = $day;
454 $endday = ($day==$j_days_in_month[$month]?1:$day+1);
455 $endmon = ($endday==1?$stamon+1:$stamon);
456 }
457 if($hour!='')
458 {
459 $stahou = $hour;
460 $endhou = ($hour==24?'00':$hour+1);
461 $endday = ($endhou=='00'?$staday+1:$staday);
462 }
463 if($minute!='')
464 {
465 $stamin=$minute;
466 $endmin=($minute==59?'00':$minute+1);
467 $endhou=($endmin=='00'?$stahou+1:$stahou);
468 }
469 if($second!='')
470 {
471 $stasec=$second;
472 $endsec=($second==59?'00':$second+1);
473 $endmin=($endsec=='00'?$stamin+1:$stamin);
474 }
475 $stadate = "$stayear-$stamon-$staday";
476 $enddate = "$endyear-$endmon-$endday";
477 $stadate = gregdate('Y-m-d',$stadate);
478 $enddate = gregdate('Y-m-d',$enddate);
479 $stadate.=" $stahou:$stamin:$stasec";
480 $enddate.=" $endhou:$endmin:$endsec";
481 $paterns = array('/YEAR\((.*?)post_date\s*\)\s*=\s*[0-9\']*/',
482 '/DAYOFMONTH\((.*?)post_date\s*\)\s*=\s*[0-9\']*/',
483 '/MONTH\((.*?)post_date\s*\)\s*=\s*[0-9\']*/',
484 '/HOUR\((.*?)post_date\s*\)\s*=\s*[0-9\']*/',
485 '/MINUTE\((.*?)post_date\s*\)\s*=\s*[0-9\']*/',
486 '/SECOND\((.*?)post_date\s*\)\s*=\s*[0-9\']*/');
487 foreach($paterns as $ptn)
488 {
489 $where = preg_replace($ptn,'1=1',$where);
490 }
491 $prefixp = "{$wpdb->posts}.";
492 $prefixp = (strpos($where, $prefixp) === false) ? '' : $prefixp;
493 $where .= " AND {$prefixp}post_date >= '$stadate' AND {$prefixp}post_date < '$enddate' ";
494 return $where;
495 }
496 /*
497 * fix admin edit section
498 */
499 add_action('admin_footer','parsidate_js',1);
500
501 function parsidate_js()
502 {
503 $dirname=basename(dirname(__file__));
504 echo "<script type='text/javascript' src='".plugins_url()."/$dirname/js/parsidate.js'></script>";
505 }
506 /*
507 * fix search dropdownlist admin edit.php
508 */
509 add_action('load-edit.php', 'wppd_admin_init');
510
511 function wppd_admin_init()
512 {
513 add_action('restrict_manage_posts','wppd_restrict_manage_posts');
514 add_filter('posts_where', 'wp_admin_posts_where');
515 }
516
517 function wp_admin_posts_where($where)
518 {
519 global $wp_query;
520 if( isset($_GET['mfa']) and $_GET['mfa'] != '0' )
521 {
522 $wp_query->query_vars['m'] = $_GET['mfa'];
523 $where = wpb_posts_where($where);
524 }
525 return $where;
526 }
527
528 function wppd_restrict_manage_posts()
529 {
530 global $post_type, $wpdb,$persian_month_names;
531 $list = $wpdb->get_col("SELECT DISTINCT date( post_date ) AS date
532 FROM $wpdb->posts
533 WHERE post_type = '$post_type' AND post_status <> 'auto-draft' AND date( post_date )<>'0000-00-00'
534 ORDER BY post_date DESC");
535 if ( empty($list))
536 return;
537 $m = isset( $_GET['mfa'] ) ? (int) $_GET['mfa'] : 0;
538
539 echo '<select name="mfa">';
540 echo "<option ".selected( $m, 0 ,false)." value='0'>".__( 'Show all dates','wp-persian' )."</option>\n";
541 foreach ( $list as $date )
542 {
543 $date = parsidate('Ym',$date,'eng');
544 $year = substr($date,0,4);
545 $month = substr($date,4,2);
546 $month=$persian_month_names[intval($month)];
547 if($predate != $date)
548 echo "<option %s value='$date'".selected( $m, $date, false ).">$month ".fixnumber($year)."</option>\n";
549 $predate = $date;
550 }
551 echo '</select>';
552 }
553 //___________________________________________________persian archives _______________________________________________
554 function wp_get_parchives($args='')
555 {
556 global $wpdb, $wp_locale,$persian_month_names;
557 $defaults = array(
558 'type' => 'monthly', 'limit' => '',
559 'format' => 'html', 'before' => '',
560 'after' => '', 'show_post_count' => false,
561 'echo' => 1, 'order' => 'DESC');
562
563 $r = wp_parse_args( $args, $defaults );
564 extract( $r, EXTR_SKIP );
565 $archive_link_m =home_url("'?m='");
566
567 $results= $wpdb->get_results( "SELECT date( post_date )as date,count(ID)as count FROM $wpdb->posts WHERE post_date < NOW() AND post_type = 'post' AND post_status = 'publish' group by date ORDER BY post_date DESC");
568 if(!empty($results))
569 {
570 if($type=='yearly')
571 {
572 $old_date=parsidate('Y',$results[0]->date,'eng');
573 $count=$results[0]->count;
574 $c=count($results);
575 for($i=1;$i<$c;$i++)
576 {
577 $dt=$results[$i];
578 $date=parsidate('Y',$dt->date,'eng');
579 if($date==$old_date)
580 $count+=$dt->count;
581 else
582 {
583 echo_yarchive($old_date,$format,$before,$count,$show_post_count);
584 $old_date=$date;
585 $count=$dt->count;
586 }
587 }
588 echo_yarchive($old_date,$format,$before,$count,$show_post_count);
589 }
590 elseif($type=='monthly')
591 {
592 $old_date=parsidate('Ym',$results[0]->date,'eng');
593 $count=$results[0]->count;
594 $c=count($results);
595 for($i=1;$i<$c;$i++)
596 {
597 $dt=$results[$i];
598 $date=parsidate('Ym',$dt->date,'eng');
599 if($date==$old_date)
600 $count+=$dt->count;
601 else
602 {
603 echo_marchive($old_date,$format,$before,$count,$show_post_count);
604 $old_date=$date;
605 $count=$dt->count;
606 }
607 }
608 echo_marchive($old_date,$format,$before,$count,$show_post_count);
609 }
610 elseif($type=='daily')
611 {
612 foreach($results as $row)
613 {
614 $date = parsidate('Y,m,d',$row->date,'eng');
615 $date=explode(',',$date);
616 if($show_post_count)
617 $count='&nbsp;('.fixnumber($row->count).')';
618 else
619 $count = '';
620 $text = fixnumber($date[2]).' '.$persian_month_names[intval($date[1])].' '.fixnumber($date[0]);
621 echo get_archives_link(get_day_link($date[0],$date[1],$date[2]),$text,$format, $before, $count);
622 }
623 }
624 }
625 }
626
627 function echo_yarchive($year,$format,$before,$count,$show_post_count)
628 {
629 if($show_post_count)
630 $count='&nbsp;('.fixnumber($count).')';
631 else
632 $count='';
633 echo get_archives_link(get_year_link($year),fixnumber($year), $format, $before,$count);
634 }
635
636 function echo_marchive($old_date,$format,$before,$count,$show_post_count)
637 {
638 global $persian_month_names;
639 $year=substr($old_date,0,4);
640 $month=substr($old_date,4,2);
641 if($show_post_count)
642 $count='&nbsp;('.fixnumber($count).')';
643 else
644 $count='';
645 echo get_archives_link(get_month_link($year,$month),$persian_month_names[intval($month)].' '.fixnumber($year), $format, $before,$count);
646 }
647 ?>