<?php

/**************************************************************
*          All theme Paths 
***************************************************************/
//theme Directory and URI
define('Lioit_DIR', get_template_directory());
define('Lioit_URI', get_template_directory_uri());
//Lioitizat Framework
define('Lioit_FW', Lioit_DIR . '/Lioit');
define('Lioit_TM', Lioit_DIR . '/templates');
define('Lioit_CO', Lioit_DIR . '/catoptions');
//post types

define('Lioit_TYPE', Lioit_FW . '/posttype');

//assest
define('Lioit_JS', Lioit_URI . '/js');
define('Lioit_CSS', Lioit_URI . '/css');
define('Lioit_IMG', Lioit_URI . '/images');
define('Lioit_SCRIPTS', Lioit_URI. '/Lioit/scripts');
define('Lioit_FUN', Lioit_FW . '/functions');
define('Lioit_ADMIN', Lioit_FW . '/admin');
define('Lioit_META', Lioit_FW . '/metaboxes');
define('Lioit_SC', Lioit_FW . '/shortcodes');
define('Lioit_WIDGET', Lioit_FW . '/widgets');

//Admin Option
require Lioit_ADMIN . '/main-options.php';




// Custom widgets
require Lioit_WIDGET . '/ads125.php';
require Lioit_WIDGET . '/fb_likebox.php';
require Lioit_WIDGET . '/big_ad.php';
require Lioit_WIDGET . '/random-posts.php';
require Lioit_WIDGET . '/adsense.php';
require Lioit_WIDGET . '/tabs.php';

//Lioit functions
require Lioit_FUN . '/functions.php';

//ShortCodes
require Lioit_SC . '/shortcodes.php';

//Tinymce Buttons
require Lioit_FW . '/tinymce/videosc.php';
require Lioit_FW . '/tinymce/columns.php';
require Lioit_FW . '/tinymce/lists.php';
require Lioit_FW . '/tinymce/clear.php';
require Lioit_FW . '/tinymce/information.php';
require Lioit_FW . '/tinymce/warning.php';
require Lioit_FW . '/tinymce/note.php';

function add_more_buttons($buttons) {
 $buttons[] = 'hr';
 $buttons[] = 'del';
 $buttons[] = 'sub';
 $buttons[] = 'sup';
 $buttons[] = 'fontselect';
 $buttons[] = 'fontsizeselect';
 $buttons[] = 'cleanup';
 $buttons[] = 'styleselect';
 return $buttons;
}
add_filter("mce_buttons_3", "add_more_buttons");

add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'video', 'image', 'quote', 'audio' ) );
/**************************************************************
 *          Menus
 ***************************************************************/
if ( function_exists( 'register_nav_menu' ) ) {
  register_nav_menus(
   array(
    'mainnav'   => __('قائمة التصنيفات'),
   )
  );
  register_nav_menus(
   array(
    'mainmenu'   => __('القائمة الرئيسية'),
   )
  );

}
/**************************************************************
*          Tumbnails
***************************************************************/

add_theme_support('post-thumbnails');


/**************************************************************
*          Content & excerpt limit 
***************************************************************/


function the_excerpt_max_charlength($charlength) {
   $excerpt = get_the_excerpt();
   $charlength++;
   if(strlen($excerpt)>$charlength) {
       $subex = substr($excerpt,0,$charlength-5);
       $exwords = explode(" ",$subex);
       $excut = -(strlen($exwords[count($exwords)-1]));
       if($excut<0) {
            echo substr($subex,0,$excut);
       } else {
       	    echo $subex;
       }
   } else {
	   echo $excerpt;
   }
}

function short_title($after = '', $length) {
	$mytitle = explode(' ', get_the_title(), $length);
	if (count($mytitle)>=$length) {
		array_pop($mytitle);
		$mytitle = implode(" ",$mytitle). $after;
	} else {
		$mytitle = implode(" ",$mytitle);
	}
	return $mytitle;
}





// The excerpt based on words
function my_string_limit_words($string, $word_limit)
{
  $words = explode(' ', $string, ($word_limit + 1));
  if(count($words) > $word_limit)
  array_pop($words);
  return implode(' ', $words).'... ';
}


// The excerpt based on character
function my_string_limit_char($excerpt, $substr=0)
{

	$string = strip_tags(str_replace('...', '...', $excerpt));
	if ($substr>0) {
		$string = substr($string, 0, $substr);
	}
	return $string;
		}
/**********************************************************************
* 			Comments Template
********************************************************************/
function Lioit_comment ($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
     <div id="comment-<?php comment_ID(); ?>">
      <div class="comment-author vcard">
         <?php echo get_avatar($comment,$size='48',$default='<path_to_url>' ); ?>

         <?php printf(__('<cite class="fn">%s</cite> <span class="says">يقول : </span>'), get_comment_author_link()) ?>
         <div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','') ?></div>
      </div>
      <?php if ($comment->comment_approved == '0') : ?>
         <em><?php _e('تعليقك يحتاج إلى مراجعة المدير .') ?></em>
         <br />
      <?php endif; ?>

      <?php comment_text() ?>

      <div class="reply">
         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
      </div>
     </div>
     
<?php
        }







/**********************************************************************
* 			Sidebars
********************************************************************/
if ( function_exists('register_sidebar') ) {

register_sidebar(array(
'name' => 'قوائم',
'description' => 'القوائم والمربعات الجانبية  ',
'before_widget' => '<div class="widget">
                    	<div class="widgetTop">',
'after_widget' => '</div><!--End WidgetContent-->
                    </div><!--End Widget-->',
'before_title' => '<h2>',
'after_title' => '</h2></div><!--End WidgetTop-->
                        <div class="widgetContent">'
));


}

// Empty Pragraph Fix
/*
Plugin Name: Shortcode empty Paragraph fix
Plugin URI: http://www.johannheyne.de/wordpress/shortcode-empty-paragraph-fix/
Description: Fix issues when shortcodes are embedded in a block of content that is filtered by wpautop.
Author URI: http://www.johannheyne.de
Version: 0.1
Put this in /wp-content/plugins/ of your Wordpress installation
*/


add_filter('the_content', 'shortcode_empty_paragraph_fix');
function shortcode_empty_paragraph_fix($content)
{   
$array = array (
'<p>[' => '[', 
']</p>' => ']', 
']<br />' => ']'
);

$content = strtr($content, $array);

return $content;
}

//shortcodes in widgets
add_filter('widget_text', 'do_shortcode');

/*Styling the WP Login Screen */

function add_my_stylesheet() {
echo '<link rel="stylesheet" type="text/css" href="' . get_bloginfo('stylesheet_directory') . '/css/login.css" />';
}
add_action('login_head', 'add_my_stylesheet');

/* parent Category */
function post_is_in_descendant_category( $cats, $_post )
{
foreach ( (array) $cats as $cat ) {
// get_term_children() accepts integer ID only
$descendants = get_term_children( (int) $cat, 'category');
array_push($descendants,$cat);
if ( $descendants && in_category( $descendants, $_post ) )
return true;
}
return false;
}

/* Category And Child */
//force child template
function load_cat_parent_template()
{
global $wp_query;

if (!$wp_query->is_category)
return true; // saves a bit of nesting

// get current category object
$cat = $wp_query->get_queried_object();

// trace back the parent hierarchy and locate a template
while ($cat && !is_wp_error($cat)) {
$template = TEMPLATEPATH . "/category-{$cat->cat_ID}.php";

if (file_exists($template)) {
load_template($template);
exit;
}

$cat = $cat->parent ? get_category($cat->parent) : false;
}
}
add_action('template_redirect', 'load_cat_parent_template');

// If is category or subcategory of $cat_id
if (!function_exists('is_category_or_sub')) {
function is_category_or_sub($cat_id = 0) {
foreach (get_the_category() as $cat) {
if ($cat_id == $cat->cat_ID || cat_is_ancestor_of($cat_id, $cat)) return true;
}
return false;
}
}

?>
<?php
add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
 
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("معلومات حساباتك بالشبكات الإجتماعية", "blank"); ?></h3>
 
<table class="form-table">
<tr>
<th><label for="address"><?php _e("رابط حسابك بتويتر"); ?></label></th>
<td>
<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e(""); ?></span>
</td>
</tr>
<tr>
<th><label for="facebook"><?php _e("رابط حسابك بالفيسبوك"); ?></label></th>
<td>
<input type="text" name="facebook" id="facebook" value="<?php echo esc_attr( get_the_author_meta( 'facebook', $user->ID ) ); ?>" class="regular-text" /><br />
<span class="description"><?php _e(""); ?></span>
</td>
</tr>
</table>
<?php }
 
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );
 
function save_extra_user_profile_fields( $user_id ) {
 
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
 
update_usermeta( $user_id, 'twitter', $_POST['twitter'] );
update_usermeta( $user_id, 'facebook', $_POST['facebook'] );
}