<?php load_theme_textdomain('blank'); ?>
<?php
if ( function_exists('register_sidebars') )
	register_sidebars(2, array(
		'name'=>'Sidebar %d',
        'before_widget' => '<div class="widget">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widgettitle">',
        'after_title' => '</h3>',
    ));
?>
<?php function custom_comment($comment, $args, $depth) {
       $GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID( ); ?>">
<?php comment_author_link() ?> <?php _e('says', 'blank'); ?>:
<?php if ($comment->comment_approved == '0') : //message if comment is held for moderation ?>
<em><?php _e('Your comment is awaiting moderation', 'blank'); ?>.</em>
<?php endif; ?>
<br />
<small class="commentmetadata">
<?php comment_date('l, j F, Y') ?> <?php _e('at', 'blank'); ?> <?php comment_date('G:i') ?><?php edit_comment_link( __('Edit', 'blank'),' &nbsp;|&nbsp; ',''); ?></small>
<br />
	<?php comment_text() ?>
 <div class="reply">
<?php echo comment_reply_link(array('reply_text' => __('Reply', 'blank'), 'depth' => $depth, 'max_depth' => $args['max_depth']));  ?>
</div>
<?php } ?>
<?php function custom_pings($comment, $args, $depth) {
       $GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="comment-<?php comment_ID( ); ?>">
     <?php _e('Trackback from', 'blank'); ?> <em><?php comment_author_link() ?></em>
     <br /><small><?php comment_date('l, j F, Y') ?></small>
     <br /><?php comment_text() ?>
     <?php edit_comment_link( __('Edit', 'blank'),'<br /> &nbsp;|&nbsp; ',''); ?>
<?php } ?>
<?php
add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
	global $id;
	$comments_by_type = &separate_comments(get_comments('post_id=' . $id));
	return count($comments_by_type['comment']);
}

function premiumpress_customfields_box() {
    if( function_exists( 'add_meta_box' )) {
        add_meta_box( 'premiumpress_customfields_0', __( 'Category Setup', 'sp' ), 'premiumpress_category', 'page', 'normal', 'high' );
    }
}
function premiumpress_category(){

    global $post;
    echo '<input type="hidden" name="sp_noncename" id="sp_noncename" value="' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    $type = get_post_meta($post->ID, 'category_id', true);
    if($type == ""){  }else{ $a2="selected='selected'"; }
    $args = array(
    'type'                     => 'post',
    'child_of'                 => 0,
    'parent'                   => '',
    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 0,
    'hierarchical'             => 1,
    'exclude'                  => '',
    'include'                  => '',
    'number'                   => '',
    'taxonomy'                 => 'category',
    'pad_counts'               => false );
    
    $categories = get_categories( $args ); 
    
    echo '<select name="field[category_id]" style="font-size:14px;padding:5px; width:95%; background:#e7fff3;">';
    echo " <option value=''>Select Category</option>"; 
        foreach ($categories as $category) {
            if($type == $category->cat_ID){ $a2="selected='selected'"; }else{ $a2=""; }
        echo " <option ".$a2." value='".$category->cat_ID."'>".$category->cat_name."</option>";
    }
    echo '</select>';
    $type1 = get_post_meta($post->ID, 'category_id2', true);    
    echo '<select name="field[category_id2]" style="font-size:14px;padding:5px; width:95%; background:#e7fff3;">';
    echo " <option value=''>Select Category</option>"; 
        foreach ($categories as $category) {
            if($type1 == $category->cat_ID){ $a2="selected='selected'"; }else{ $a2=""; }
        echo " <option ".$a2." value='".$category->cat_ID."'>".$category->cat_name."</option>";
    }
    echo '</select>';
    
    

}
add_action('admin_menu', 'premiumpress_customfields_box'); 


function premiumpress_postdata($post_id, $post) {
    
    if ( !wp_verify_nonce( $_POST['sp_noncename'], plugin_basename(__FILE__) )) {
        return $post->ID;
    }

    // Is the user allowed to edit the post or page?
    if ( 'page' == $_POST['post_type'] ) {
        if ( !current_user_can( 'edit_page', $post->ID ))
        return $post->ID;
    } else {
        if ( !current_user_can( 'edit_post', $post->ID ))
        return $post->ID;
    }

    $mydata = array();
 
    // CUSTOM FIELDS
    if(is_array($_POST['field']) ){
        foreach($_POST['field'] as $key=>$val){
            if(substr($val,0,1) == ","){
                $mydata[$key] = substr($val,1);    
            }else{
                $mydata[$key] = $val;    
            }                        
        }    
    } 

    // CUSTOM FIELDS
    if(is_array($_POST['custom']) ){
        foreach($_POST['custom'] as $in_array){
    
            $mydata[$in_array['name']] = $_POST[$in_array['name']];                
        }    
    } 
 
    //print_r($mydata);
    foreach ($mydata as $key => $value) {
        if( $post->post_type == 'revision' ) return;
        $value = implode(',', (array)$value); 
        if(get_post_meta($post->ID, $key, FALSE)) { 
            //echo $key."=". $value;
            update_post_meta($post->ID, $key, $value);
        } else { 
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key);
    }

}
add_action('save_post',  'premiumpress_postdata', 1, 2);




function get_Image_url($p_id="0",$type="m",$string_image="image"){ 
    global $wpdb; 
    global $post;
      
      if(!$p_id){$p_id =  $post->ID;}
      $thumbnail= get_post_meta($p_id, $string_image, true);
      //echo "<br>aaaaaa".$thumbnail."<br>";
      if(!$thumbnail){ 
          if($post->post_type =='page'){
              return get_bloginfo('template_directory').'/images/thumbnail.png' ;
          }else{
              return get_bloginfo('template_directory').'/images/thumbnail.png' ; 
          }
          
      }
      switch($type){
                case "s": { $try         = wp_get_attachment_image_src( $thumbnail,'thumbnail' );   
                          } break;
                case "m": { $try         = wp_get_attachment_image_src( $thumbnail,'full' );             
                          } break; 
                default: {  $try         = wp_get_attachment_image_src( $thumbnail,'full' );             } break;
            }
        return $try[0] ;
}
?>