<?php

/*

Plugin Name: Special Post Properties
Plugin URI: http://dennishoppe.de/wordpress-plugins/special-post-properties
Description: Adds some special properties to posts. 
Version: 1.0
Author: Dennis Hoppe
Author URI: http://DennisHoppe.de

*/

If (!Class_Exists('wp_plugin_post_special_properties')){
Class wp_plugin_post_special_properties {
  var $text_domain;
  
  Function wp_plugin_post_special_properties(){
    // Get ready to translate
    $this->Load_TextDomain();
    
    // Add MetaBox and Set Action to save it
    Add_Action( 'admin_menu', Array($this, 'Add_Meta_Box') );
    Add_Action( 'save_post', Array($this, 'Save_Meta_Box_Inputs') );
    
    // Add post classes
    Add_Action( 'post_class', Array($this, 'post_class') );

    // Include Widgets
    Require_Once DirName(__FILE__).'/widget_featured_posts.php';
    Require_Once DirName(__FILE__).'/widget_gallery_posts.php';
    Require_Once DirName(__FILE__).'/widget_random_images.php';
  }
  
  Function Load_TextDomain(){
    $this->text_domain = get_class($this);
    load_textdomain ($this->text_domain, DirName(__FILE__).'/language/'.get_locale().'.mo');
  }
  
  Function t ($text, $context = ''){
    // Translates the string $text with context $context
    If ($context == '')
      return __($text, $this->text_domain);
    Else
      return _x($text, $context, $this->text_domain);
  }
  
  Function Add_Meta_Box(){
    Add_Meta_Box( 'post_special_properties',
                  $this->t('Special properties of this post'),
                  Array($this, 'print_meta_box'),
                  'post',
                  'advanced',
                  'high' );
  }
  
  Function Print_Meta_Box(){
    ?>
    <h4><?php Echo $this->t('Featured post') ?></h4>
    <p><input type="checkbox" name="_special_properties[is_featured]" value="yes" <?php Echo ($this->is_featured()) ? 'checked="checked"' : '' ?> /> <?php Echo $this->t('This post is a featured one.') ?></p>

    <h4><?php echo $this->t('Gallery') ?></h4>
    <p><input type="checkbox" name="_special_properties[is_gallery]" value="yes" <?php Echo ($this->is_gallery()) ? 'checked="checked"' : '' ?> /> <?php Echo $this->t('Mark this post as gallery.') ?></p>
    
    <?php
  }
  
  Function Meta_Key(){
    // This is the meta key base for all values
    return '_special_properties';
  }
  
  Function Save_Meta_Box_Inputs($post_id){
    // If this is an autosave we dont care
    If ( Defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return False;
    
    // Get the meta key base
    $meta_key = $this->meta_key();
    
    // Delete all old meta field
    $arr_custom_field = get_post_custom_keys($post_id);
    If (Is_Array($arr_custom_field)){
      ForEach ($arr_custom_field AS $key)
        If (SubStr($key, 0, StrLen($meta_key)) == $meta_key)
          delete_post_meta($post_id, $key);
    }
    
    // if there are no values of our form we dont care
    If (!IsSet ($_REQUEST[$meta_key])) return False;

    // Save as meta data in the post
    ForEach ($_REQUEST[$meta_key] AS $key => $property)
      update_post_meta ($post_id, $meta_key.'_'.$key, $property);
  }
  
  Function post_class ($arr_class){
    // Read Post id
    $post_id = get_the_id();
    
    // check the class Array
    If (!Is_Array($arr_class)) $arr_class = Array();
    
    // Check if the post has any properties
    If ($this->is_featured()) $arr_class[] = 'featured-post';
    If ($this->is_gallery())  $arr_class[] = 'gallery-post';
    
    // return the classes
    return $arr_class;
  }
  
  Function CheckForMeta ($meta_key, $post_id = Null){
    If ($post_id == Null){
      Global $post;
      $post_id = $post->ID;
    }
    Else $post_id = IntVal ($post_id);

    If (get_post_meta($post_id, self::meta_key().'_'.$meta_key, True))
      return True;
    Else
      return False;
  }
  
  Function is_featured ($post_id = Null){
    If (self::CheckForMeta('is_featured', $post_id))
      return True;
    Else
      return False;
  }
  
  Function is_gallery ($post_id = Null){
    If (self::CheckForMeta('is_gallery', $post_id))
      return True;
    Else
      return False;
  }

  Function get_featured_posts ($limit = -1){
    $arr_post =& get_posts (Array( 'meta_key' => self::meta_key().'_'.'is_featured',
                                   'numberposts' => $limit ));
    
    If (!Empty($arr_post)) return $arr_post;
    Else return False;
  }
  
  Function get_gallery_posts ($limit = -1){
    $arr_post =& get_posts (Array( 'meta_key' => self::meta_key().'_'.'is_gallery',
                                   'numberposts' => $limit ));
    
    If (!Empty($arr_post)) return $arr_post;
    Else return False;
  }

  
} /* End of Class */
New wp_plugin_post_special_properties();
} /* End of If-Class-Exists-Condition */
/* End of File */