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