<?php

/**
 * Plugin Name: WPPersian
 * Plugin URI: http://www.30yavash.com/wppersian
 * Description: WPPersian is a fast and lightweight plugin for support farsi language in wordpress.
 * Version: 1.4
 * Author: Siavash Salemi
 * Author URI: http://www.30yavash.com
 * License: GPL2
*/

/*  Copyright 2012  Siavash Salemi  (email : 30yavash@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2, as 
    published by the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


/* 
Special Thanks to :
*	Vali Allah[Mani] Monajjemi for his great plugin (wp-jalali)
* 	Wordpress Persian Team members (wp-persian.com)
*	Farsiweb.info for J2G and G2J Converstion Functions
*	Milad Raastian (miladmovie.com) for JDF (jdf.farsiprojects.com) 
*	Nima Shyanfar (phpmystery.com) for  Fast Farsi Number Conversion Method
*	Gonahkar (gonahkar.com) for WP-Jalali widgets plugin (gonahkar.com/archives/2007/02/26/wp-jalali-widgets-plugin/ ), edit jalali timestamp in write/edit panel and [arabic] writing shortcode.
*	Kaveh Ahmadi (ashoob.net/kaveh) for his valuable Farsi Keyboard Script (ashoob.net/farsitype)
*	Ali Sattari(corelist.net) for great support
* 	Ali Farhadi (farhadi.ir) for improving Farsi Number Convertor.
*   Reza Moallemi (moallemi.ir) for Jalali date in tables.
*/

if(!class_exists('WPPersian')) :
	define('WPPERSIAN_ID', 'wppersian');
	define('WPPERSIAN_NICK', 'WP Persian');
	define('WPPERSIAN_VER', '1.2');

    class WPPersian
    {
	    public function __construct()
		{
			global $table_prefix,$wpdb;
			if (!isset($wpdb->jarchive))
				$wpdb->jarchive = $table_prefix . 'wpp_jarchive';
				
			//register_activation_hook( __FILE__, array( 'WPPersian', 'install' ));
			
			add_action( 'init', array( $this, 'plugin_textdomain' ) );

		// Register hooks that are fired when the plugin is activated, deactivated, and uninstalled, respectively.
			//register_activation_hook( __FILE__, array( 'WPPersian', 'install' ));
			register_activation_hook( __FILE__, array( $this, 'activate' ) );
			register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
			register_uninstall_hook( __FILE__, array( $this, 'uninstall' ) );
			
			
			add_filter('load_textdomain_mofile', array( $this,'wpp_replace_language_folder'), 10, 2);
			$this->wpp_load_default_language();
			


			
			
			if(is_admin())
				$this->wpp_AdminInit();
			else
				$this->wpp_FrontpageInit();
			
			add_filter("posts_where", array($this, 'wpp_jalali_query') );
		}
		
		public function wpp_replace_language_folder($mofile, $domain)
		{
		if (file_exists(self::file_path("languages/".$domain.'-'.get_locale().'.mo')))
			return self::file_path("languages/".$domain.'-'.get_locale().'.mo');

		return $mofile;
		}
		
		public function wpp_load_default_textdomain()
		{
			$mofile = dirname( __FILE__ ) . "/languages/".get_locale().".mo";
			return load_textdomain('default', $mofile);
		}
		
		public function wpp_load_default_admin_textdomain()
		{
			$mofile = dirname( __FILE__ ) . "/languages/admin-".get_locale().".mo";
			return load_textdomain('default', $mofile);
		}
		
		public function wpp_load_default_language()
		{
			if(is_admin())
				add_action('plugins_loaded',array( $this,'wpp_load_default_admin_textdomain'));
			add_action('plugins_loaded',array( $this,'wpp_load_default_textdomain'));
		}
	

		
		private function wpp_AdminInit()
		{
			global $locale,$text_direction,$wpdb;
			add_action('admin_init', array($this, 'register'));
			add_action('admin_menu', array($this, 'menu'));
			
		
			if(get_option('wpp_admin_locale')=='fa_IR')
			{
				$locale='fa_IR';
				$text_direction = "rtl";
			}else{
				$locale='en_US';
				$text_direction = "ltr";
			}
			if(get_option('wpp_convert_admin_date'))
			{
				//$this->wpp_AddDateFilters();
				add_action('edit_form_advanced', array($this,'wpp_jalali_timestamp_admin')); // edit posts
				add_action('edit_page_form', array($this,'wpp_jalali_timestamp_admin')); // edit pages
				//add_filter('media_date_column_time', array($this,'wpp_jalali_date_column_time'));
				add_filter('post_date_column_time', array($this,'wpp_jalali_date_column_time')); // post list and page list
				
				add_action('restrict_manage_posts', array($this,'wpp_restrict_manage_posts'));
				add_action('load-edit.php', array($this,'wpp_load_editphp'));
				//add_action('admin_init', array($this,'wpp_load_editphp'));
				add_action('admin_head-edit.php', array($this,'wpp_admin_hide_gregorian_select'), 9999);
				add_action('admin_head-upload.php', array($this,'wpp_admin_hide_gregorian_select'), 9999);
			}
			add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_styles' ) );
			
			//add_action('publish_post', array($this, 'wpp_publish_post'));
		
		}
		
		private function wpp_FrontpageInit()
		{
			global $locale,$text_direction;
			if(get_option('wpp_frontpage_locale')=='fa_IR')
			{
				$locale='fa_IR';
				$text_direction = "rtl";
			}else{
				$locale='en_US';
				$text_direction = "ltr";
			}
			if(get_option('wpp_convert_frontpage_date'))
			{
				$this->wpp_AddDateFilters();
				if(get_option('wpp_convert_permalink'))
					add_filter("post_link", array($this, 'wpp_post_link') );
			}
		}

		
		private function wpp_AddDateFilters()
		{
			add_filter("the_date", array($this, 'wpp_the_date'),10,4 );
			add_filter("get_the_date", array($this, 'wpp_the_date'),10,4 );
			add_filter("the_time", array($this, 'wpp_the_time'),10,4 );
			add_filter("get_the_time", array($this, 'wpp_the_time'),10,4 );
			add_filter("get_comment_date", array($this, 'wpp_get_comment_date'),10,2 );
			add_filter("get_comment_time", array($this, 'wpp_get_comment_time'),10,2 );
			add_filter("the_modified_time", array($this, 'wpp_the_modified_time') );
			add_filter("the_modified_date", array($this, 'wpp_the_modified_date') );
			
			add_filter("the_weekday","wpp_the_weekday");
			add_filter("the_weekday_date","wpp_the_weekday_date",10,3);
		}

		// activate plugin
		public function activate()
		{
			$ver=get_option('wpp_installed_version');
			if(!isset($ver) || empty($ver));
			{
				update_option( 'wpp_installed_version', WPPERSIAN_VER );
			}
			/*
			if(!file_exists(ABSPATH . '/wp-content/languages/fa_IR.mo') && file_exists(self::file_path("install/fa_IR.mo")) )
				copy(self::file_path("install/fa_IR.mo") , ABSPATH . '/wp-content/languages/fa_IR.mo');
				
			if(!file_exists(ABSPATH . '/wp-content/languages/admin-fa_IR.mo') && file_exists(self::file_path("install/admin-fa_IR.mo")) )
				copy(self::file_path("install/admin-fa_IR.mo") , ABSPATH . '/wp-content/languages/admin-fa_IR.mo');
				
			*/

		}
		
		// deactivate plugin
		public function deactivate()
		{
		}
		
		// uninstall plugin
		public function uninstall()
		{
			delete_option( 'wpp_installed_version' );
			delete_option( 'wpp_admin_locale' );
			delete_option( 'wpp_frontpage_locale' );
			delete_option( 'wpp_convert_permalink' );
			delete_option( 'wpp_convert_admin_date' );
			delete_option( 'wpp_convert_frontpage_date' );
		}


		/**
		* Loads the plugin text domain for translation
		*/
		public function plugin_textdomain()
		{
			// TODO: replace "plugin-name-locale" with a unique value for your plugin
			load_plugin_textdomain( 'wp-persian', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
		} // end plugin_textdomain


		/** function/method
		* Usage: return absolute file path
		* Arg(1): string
		* Return: string
		*/
		public static function file_path($file)
		{
			return ABSPATH.'wp-content/plugins/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__)).$file;
		}
		/** function/method
		* Usage: hooking the plugin options/settings
		* Arg(0): null
		* Return: void
		*/
		public function register()
		{
			register_setting(WPPERSIAN_ID.'_options', 'wpp_admin_locale');
			register_setting(WPPERSIAN_ID.'_options', 'wpp_frontpage_locale');
			register_setting(WPPERSIAN_ID.'_options', 'wpp_convert_permalink');
			register_setting(WPPERSIAN_ID.'_options', 'wpp_convert_admin_date');
			register_setting(WPPERSIAN_ID.'_options', 'wpp_convert_frontpage_date');
			register_setting(WPPERSIAN_ID.'_options', 'wpp_installed_version');
		}
		/** function/method
		* Usage: hooking (registering) the plugin menu
		* Arg(0): null
		* Return: void
		*/
		public function menu()
		{
			// Create menu tab
			add_options_page(WPPERSIAN_NICK.' Plugin Options', __('WP Persian','wp-persian'), 'manage_options', WPPERSIAN_ID.'_options', array('WPPersian', 'options_page'));
		}
		/** function/method
		* Usage: show options/settings form page
		* Arg(0): null
		* Return: void
		*/
		public static function options_page()
		{
			if (!current_user_can('manage_options'))
			{
				wp_die( __('You do not have sufficient permissions to access this page.') );
			}

			$plugin_id = WPPERSIAN_ID;
			// display options page
			include(self::file_path('options.php'));
		}
		
		public static function PersianNumbers($n)
		{
			return farsi_num($n);
		}

		public static function EnglishNumbers($n)
		{
			return english_num($n);
		}

		public static function MakeTimestamp($m)
		{
			return mktime(substr($m,11,2),substr($m,14,2),substr($m,17,2),substr($m,5,2),substr($m,8,2),substr($m,0,4));
		}

		function wpp_the_weekday($input) 
		{
			global $weekday, $id, $post;
			$timestamp = self::MakeTimestamp($post->post_date);
			$the_weekday = jdate('w', $timestamp);
			return $the_weekday;
		}
		function wpp_the_weekday_date($input, $before, $after) 
		{
			global $weekday, $id, $post, $day, $previousweekday;
			$the_weekday_date = '';
			$timestamp = self::MakeTimestamp($post->post_date);
			if (strlen($input) > 0) {
				$the_weekday_date .= $before;
				$the_weekday_date .= jdate('w', $timestamp);
				$the_weekday_date .= $after;
			}
			return  $the_weekday_date;
		}

		/** function/method
		* Usage: filtering the the_date and get_the_date
		* the_date( $format, $before, $after, $echo );
		*/
		public function wpp_the_date($input,$d='',$before='', $after='')
		{
			global $post, $day, $previousday, $newday;
			$result = '';
			if ($d == "") $d = get_option('date_format');
			//if ($day != $previousday) {
			if (strlen($input) > 0) { //Because $previousday is overwritten before reaching here , nice trick ;)

				$timestamp = self::MakeTimestamp($post->post_date);
				$result .= $before;
				$result .= jdate($d,$timestamp);
				$result .= $after;
				$previousday = $day;
			}
			return self::PersianNumbers($result);
		}
		
		public function wpp_the_time($input,$d='') 
		{
			global $id, $post;
			if (!empty($input)){
				if ($d == "") $d = get_option('time_format');
				$timestamp = self::MakeTimestamp($post->post_date);
				$the_time = jdate($d, $timestamp);
				return self::PersianNumbers($the_time);
			}
		}
		/** function/method
		* Usage: filtering the get_comment_date and comment_date
		*/
		public function wpp_get_comment_date($input, $d = '')
		{
			//return get_option('date_format');
			global $comment;
			$result = '';
			$timestamp = self::MakeTimestamp($comment->comment_date);
			if ( '' == $d )
				$result = jdate(get_option('date_format'), $timestamp);
			else
				$result = jdate($d, $timestamp);
			return self::PersianNumbers($result);
		}
		
		public function wpp_get_comment_time($input, $d = '') 
		{
			
			global $comment;
			$result = '';
			$timestamp = self::MakeTimestamp($comment->comment_date);
			if ( '' == $d )
				$result = jdate(get_option('time_format'), $timestamp);
			else
				$result = jdate($d, $timestamp);
			return  self::PersianNumbers($result);
		}


		public function wpp_the_modified_date($input,$d='') 
		{
			global $id, $post;
			if (!empty($input)){
				if ($d == "") $d = get_option('date_format');
				$timestamp = self::MakeTimestamp($post->post_modified);
				$the_time = jdate($d, $timestamp);
				return self::PersianNumbers($the_time);
			}
		}

		public function wpp_the_modified_time($input,$d='') 
		{
			global $id, $post;
			if (!empty($input)){
				if ($d == "") $d = get_option('time_format');
				$timestamp = self::MakeTimestamp($post->post_modified);
				$the_time = jdate($d, $timestamp);
				return self::PersianNumbers($the_time);
			}
		}
		
		/** function/method
		* Usage: permalink
		*/
		public function wpp_post_link($old_perma) 
		{
			global $post;
			/* Detecting $leavename 2.5+ */
			$leavename = ((strpos($old_perma, '%postname%') !== false) || (strpos($old_perma, '%pagename%') !== false));
			$_wp_version = get_bloginfo("version");
			$rewritecode = array(
				'%year%',
				'%monthnum%',
				'%day%',
				'%hour%',
				'%minute%',
				'%second%',
				$leavename? '' : '%postname%',
				'%post_id%',
				'%category%',
				'%author%',
				$leavename? '' : '%pagename%',
			);
			
			if ( empty($post->ID) ) return FALSE;
			
			if (( $post->post_status == 'static' ) || ( $post->post_type == 'page' ))
				return get_page_link($post->ID);
			elseif (($post->post_status == 'object') || ($post->post_type == 'attachment'))
				return get_subpost_link($post->ID);

			$permalink = get_option('permalink_structure');

			if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) 
			{
				$unixtime = strtotime($post->post_date);

				$category = '';
				if ( strpos($permalink, '%category%') !== false ) {
					$cats = get_the_category($post->ID);
					if ( $cats )
						usort($cats, '_usort_terms_by_ID'); // order by ID
					$category = (version_compare($_wp_version, '2.4', '<')) ? $cats[0]->category_nicename : $category = $cats[0]->slug;
					if ( $parent=$cats[0]->category_parent )
						$category = get_category_parents($parent, FALSE, '/', TRUE) . $category;
				}
				
				if ( empty($category) ) {
					$default_category = get_category( get_option( 'default_category' ) );
					$category = is_wp_error( $default_category)? '' : $default_category->slug; 
				}

				$author = '';
				if ( strpos($permalink, '%author%') !== false ) {
					$authordata = get_userdata($post->post_author);
					$author = $authordata->user_nicename;
				}
				
				$rewritereplace = 
				array(
					jdate('Y', $unixtime,true),
					jdate('m', $unixtime,true),
					jdate('d', $unixtime,true),
					jdate('H', $unixtime,true),
					jdate('i', $unixtime,true),
					jdate('s', $unixtime,true),
					$post->post_name,
					$post->ID,
					$category,
					$author,
					$post->post_name,
				);
				$permalink = get_option('home') . str_replace($rewritecode, $rewritereplace, $permalink);
				$permalink = user_trailingslashit($permalink, 'single');
				return $permalink;
				} else { // if they're not using the fancy permalink option
					$permalink = get_option('home') . '/?p=' . $post->ID;
					return $permalink;
				}
		}
		

		/** function/method
		* Usage: translate persian permalink
		*/
		public static function wpp_jalali_query($where)
		{
			$_wp_version = get_bloginfo("version");
			/* Wordpress 1.6+ */
			global $wp_query, $wpdb;
			global $j_days_in_month;

			$m = $wp_query->query_vars['m'];
			$hour = $wp_query->query_vars['hour'];
			$minute = $wp_query->query_vars['minute'];
			$second = $wp_query->query_vars['second'];
			$year = $wp_query->query_vars['year'];
			$monthnum = $wp_query->query_vars['monthnum'];
			$day = $wp_query->query_vars['day'];
			
			$j_monthnum = 1;
			$j_day = 1;
			$j_hour = 0;
			$j_minute = 0;
			$j_second = 0;
			$j_monthnum_next = 1;
			$j_day_next = 1;
			$j_hour_next = 0;
			$j_minute_next = 0;
			$j_second_next = 0;
			$j_doit = false;
			if ($m != '' && $m != '0') { //bugfix in wordpress 3.8
				$m = '' . preg_replace('|[^0-9]|', '', $m);
				$j_year = substr($m,0,4);
				if ($j_year < 1700) { // Wow ! It's a Jalali Date!
				$j_doit = true;
				$j_year_next = $j_year + 1;

				if (strlen($m)>5) {
					$j_monthnum = substr($m, 4, 2);
					$j_year_next = $j_year;
					$j_monthnum_next = $j_monthnum + 1;
				}

				if (strlen($m)>7) {
					$j_day = substr($m, 6, 2);
					$j_monthnum_next = $j_monthnum;
					$j_day_next = $j_day + 1;
				}
				if (strlen($m)>9) {
					$j_hour = substr($m, 8, 2);
					$j_day_next = $j_day;
					$j_hour_next = $j_hour + 1;
				}
				if (strlen($m)>11) {
					$j_minute = substr($m, 10, 2);
					$j_hour_next = $j_hour;
					$j_minute_next = $j_minute + 1;
				}
				if (strlen($m)>13) {
					$j_second = substr($m, 12, 2);
					$j_minute_next = $j_minute;
					$j_second_next = $j_second + 1;
				}
				}
			} else if (($year != '') && ($year < 1700)) {
				$j_doit = true;
				$j_year = $year;
				$j_year_next = $j_year + 1;

				if ($monthnum != '') {
					$j_monthnum = $monthnum;
					$j_year_next = $j_year;
					$j_monthnum_next = $j_monthnum + 1;
				}
				if ($day != '') {
					$j_day = $day;
					$j_monthnum_next = $j_monthnum;
					$j_day_next = $j_day + 1;
				}
				if ($hour != '') {
					$j_hour = $hour;
					$j_day_next = $j_day;
					$j_hour_next = $j_hour + 1;
				}
				if ($minute != '') {
					$j_minute = $minute;
					$j_hour_next = $j_hour;
					$j_minute_next = $j_minute + 1;
				}
				if ($second != '') {
					$j_second = $second;
					$j_minute_next = $j_minute;
					$j_second_next = $j_second + 1;
				}
			}
			
			if ($j_doit) {//echo $where;exit;
			
				/* WP 1.5+ NEEDS THIS :: CLEANING PREV. TIMINGS*/
				$tablename_prefix = $wpdb->prefix.'posts.';
				$sna = (strpos($where, $tablename_prefix) === false) ? '' : $tablename_prefix;
				// TODO: Remove above line and improve the regex
//				echo 'v ',$sna;
				//$sna='';
				$where = preg_replace("/ post_date/"," ".$sna."post_date",$where);//bugfix for wordpress 3.8+

			
				//echo $where,"<br />";
				
				$patterns =  array("/YEAR\(\s*".$sna."post_date\s*\)\s*=\s*'*[0-9]{4}'*/","/DAYOFMONTH\(\s*".$sna."post_date\s*\)\s*=\s*'*[0-9]{1,}'*/"
				,"/MONTH\(\s*".$sna."post_date\s*\)\s*=\s*'*[0-9]{1,}'*/","/HOUR\(\s*".$sna."post_date\s*\)\s*=\s*'*[0-9]{1,}'*/",
				"/MINUTE\(\s*".$sna."post_date\s*\)\s*=\s*'*[0-9]{1,}'*/","/SECOND\(\s*".$sna."post_date\s*\)\s*=\s*'*[0-9]{1,}'*/");

				foreach ($patterns as $pattern){
					$where = preg_replace($pattern,"1=1",$where); // :D good idea ! isn't it ?
				}
				
				//echo $where;exit;
				if ($j_second_next > 59) {
					$j_second_next = 0;
					$j_minute_next++;
				}
				if ($j_minute_next > 59) {
					$j_minute_next = 0;
					$j_hour_next++;
				}
				if ($j_hour_next > 23) {
					$j_hour_next = 0;
					$j_day_next++;
				}
				if ($j_day_next > $j_days_in_month[$j_monthnum-1]){
					$j_day_next = 1;
					$j_monthnum_next++;
				}
				if ($j_monthnum_next > 12) {
					$j_monthnum_next = 1;
					$j_year_next++;
				}

				$g_startdate = date("Y:m:d 00:00:00",jmaketime($j_hour,$j_minute,$j_second,$j_monthnum,$j_day,$j_year));
				$g_enddate = date("Y:m:d 00:00:00",jmaketime($j_hour_next,$j_minute_next,$j_second_next,$j_monthnum_next,$j_day_next,$j_year_next));
				
				$where .= " AND ".$sna."post_date >= '$g_startdate' AND ".$sna."post_date < '$g_enddate' ";
			}
			//echo $where;
			return  $where;
		}
		

		/**
		* Registers and enqueues admin-specific styles.
		*/
		public function register_admin_styles()
		{
			global $text_direction;
			if($text_direction=="ltr")
			{
				//wp_enqueue_style( 'plugin-name-admin-styles', plugins_url( 'plugin-name/css/admin.css' ) );
				wp_register_style( 'complex_admin_stylesheet', plugins_url('/wp-persian/css/complex.css' ));
				wp_enqueue_style( 'complex_admin_stylesheet' );
			}
		} // end register_admin_styles

		/**
		* Registers and enqueues admin-specific JavaScript.
		*/	
		public function register_admin_scripts()
		{
			// TODO: Change 'plugin-name' to the name of your plugin
			//wp_enqueue_script( 'wp-persian-editjalali-script', plugins_url( '/wp-persian/js/editjalali.js' ) );
			//wp_enqueue_script( 'plugin-name-admin-script', self::file_path( '/js/editjalali.js' ) );
			//wp_enqueue_script( 'wp-persian-editjalali-core-script', plugins_url( '/wp-persian/js/editjalali-core.php' ) );
		} // end register_admin_scripts


		public function wpp_jalali_timestamp_admin() 
		{
			global $action ;
			if ( current_user_can('edit_posts') ) 
			{
				// Register admin styles and scripts
				//add_action( 'admin_print_styles', array( $this, 'register_admin_styles' ) );
				//add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_scripts' ) );
				//add_action( 'admin_enqueue_scripts', array( $this, 'register_admin_styles' ) );
				wp_enqueue_script( 'wp-persian-editjalali-script', plugins_url( '/wp-persian/js/jalali.js' ) );
				require_once(WPPersian::file_path('/inc/editjalali-core.php'));
				jalali_touch_time(($action == 'edit'));
			}
		}

		public function wpp_jalali_date_column_time($date) 
		{
			$time='';
			if(strpos($date,' ')>1)
			{
				$arrtemp = split(' ', $date);
				$date=$arrtemp[0];
				$time=' '.$arrtemp[1]." ".$arrtemp[2];
			}
			$arrtime = split('/', $date);
			if(count($arrtime) > 1)
			{
				$gmt = mktime(0,0,0,$arrtime[1],$arrtime[2],$arrtime[0]);
				$date =  jdate('d M Y',$gmt); 
			}
			return self::PersianNumbers($date.$time);
		}

		/**
		* generate date filter in upload.php & edit.php
		*/	
		public function wpp_restrict_manage_posts()
		{
			global $post_type, $wpdb, $wp_locale;

			$months = $wpdb->get_results( $wpdb->prepare( "
				SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month, DAY( post_date ) as day
				FROM $wpdb->posts
				WHERE post_type = %s AND post_status <> 'auto-draft'
				ORDER BY post_date DESC
				", $post_type ) );
			$month_count = count( $months );
			if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
				return;
			$m = isset( $_GET['mfa'] ) ? (int) $_GET['mfa'] : 0;

			echo '<select name="mfa">';
			echo "<option ".selected( $m, 0 ,false)." value='0'>".__( 'Show all dates','wp-persian' )."</option>\n";
			foreach ( $months as $arc_row ) {
					if ( 0 == $arc_row->year )
						continue;
					$month = zeroise( $arc_row->month, 2 );
					$year = $arc_row->year;
					$gmt = mktime(0 ,0 , 0, $month, $arc_row->day, $year);
					$dateshow = self::PersianNumbers(jdate('M Y',$gmt));
					$date = english_num(jdate('Ym',$gmt));
					if($predate != $date)
						printf( "<option %s value='%s'>%s</option>\n",
							selected( $m, $date, false ),
							esc_attr( $date ),
							$dateshow
						);
					$predate = $date;
			}
			echo '</select>';
		}

		public function wpp_load_editphp() 
		{
			add_filter('posts_where', array($this,'wpp_posts_where'));
		}

		public function wpp_posts_where($where) 
		{
			global $wpdb, $wp_query;
			if( isset($_GET['mfa']) and $_GET['mfa'] != '0' )
			{
				//$m = $wp_query->query_vars['m'];
				//$mfa = $wp_query->query_vars['mfa'];
				
				$wp_query->query_vars['m'] = intval($_GET['mfa']);
				$where = self::wpp_jalali_query($where);
			}
			return $where;
		}

		/**
		* hide gregorian date in edit.php & upload.php
		*/
		public function wpp_admin_hide_gregorian_select()
		{
			echo '<script type="text/javascript" language="javascript">',
				'jQuery(document).ready(function() {',
				  'jQuery(\'select[name="m"]\').hide()',
				'});',
				'</script>';

		}
		

    }
	
endif;

$WPPersian=new WPPersian();

require_once(WPPersian::file_path('/inc/farsinum-core.php'));
require_once(WPPersian::file_path('/functions.php'));
require_once(WPPersian::file_path('/widgets.php'));

?>