Advertisement
Digitalraindrops

post-meta-functions.php

Apr 26th, 2012
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.99 KB | None | 0 0
  1. <?php
  2. add_action("admin_init", "add_pop_meta_box");
  3.  
  4. //Add the Meta Box only to our template page
  5. function add_pop_meta_box(){   
  6.     $post_id = isset( $_GET['post'] ) ? $_GET['post'] : 0 ;
  7.     if($post_id) {
  8.         $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
  9.         if ($template_file == 'page-pop.php') {
  10.             add_meta_box("pageofposts", "Page of Posts", "pop_meta_options", "page", "side", "core");
  11.         } else {
  12.             $meta = get_post_meta($post_id, '_cat_id', true);
  13.             if( $meta ) {
  14.                 pop_update_post_meta($post_id, '_cat_id', '');
  15.                 pop_update_post_meta($post_id, '_page_title', '');
  16.                 pop_update_post_meta($post_id, '_posts_title', '');
  17.                 pop_update_post_meta($post_id, '_order_by', '');
  18.                 pop_update_post_meta($post_id, '_asc', '');
  19.                 pop_update_post_meta($post_id, '_post_count', '');
  20.                 pop_update_post_meta($post_id, '_list_style', '');
  21.                 pop_update_post_meta($post_id, '_days', '');
  22.                 remove_meta_box("pageofposts","page","side");
  23.             }
  24.         }
  25.     }
  26.     add_action('save_post', 'update_pop_meta');
  27. }
  28.  
  29. //Build the select arrays!
  30. $order_list = array(
  31.     'none' => array( 'value' => 'none','label' => 'None' ),
  32.     'id' => array( 'value' => 'ID','label' => 'Post ID' ),
  33.     'author' => array( 'value' => 'author','label' => 'Author' ),
  34.     'title' => array( 'value' => 'title','label' => 'Post Title' ),
  35.     'date' => array( 'value' => 'date', 'label' => 'Post Date'  ),
  36.     'modified' => array( 'value' => 'modified','label' => 'Modified Date' ),
  37.     'parent' => array( 'value' => 'parent','label' => 'Parent Post' ),
  38.     'rand' => array( 'value' => 'rand','label' => 'Random' ),
  39.     'comment_count' => array( 'value' => 'comment_count','label' => 'Comment Count' ),
  40.     'menu_order' => array( 'value' => 'menu_order','label' => 'Menu Order' ),
  41. );
  42.  
  43. $post_styles = array(
  44.     'default' => array( 'value' => 'default','label' => 'Default' ),
  45.     'excerpt' => array( 'value' => 'excerpt','label' => 'Excerpts' ),
  46.     'title-only' => array( 'value' => 'title-only','label' => 'Titles Only' ),
  47.     'thumb-excerpt' => array( 'value' => 'thumb-excerpt','label' => 'Thumbnails and Excerpts' ),
  48. );
  49.  
  50. $sort = array(
  51.     'DESC' => array( 'value' => 'DESC','label' => 'Descending' ),
  52.     'ASC' => array( 'value' => 'ASC','label' => 'Ascending' ),
  53. );
  54.  
  55. function pop_meta_options(){
  56.    
  57.     $post_id =  !empty($_GET['post']) ? $_GET['post'] : 0;
  58.     if( !$post_id ) return;
  59.    
  60.     $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
  61.     if ($template_file != 'page-pop.php') return;
  62.    
  63.     global $order_list,$post_styles,$sort;
  64.     $categories = get_categories();
  65.    
  66.     //See if we have any values
  67.     $post_meta=array();
  68.     $post_meta = get_post_meta( $post_id,false );
  69.    
  70.     $cat_id = isset( $post_meta['_cat_id'] ) ? $post_meta['_cat_id'][0] : 1;
  71.     $page_title = isset( $post_meta['_page_title'] ) && $post_meta['_page_title'] ? $post_meta['_page_title'][0] : '';
  72.     $posts_title = isset( $post_meta['_posts_title'] ) && $post_meta['_posts_title'] ? $post_meta['_posts_title'][0] : '';
  73.     $order_by = isset( $post_meta['_order_by'] ) ? $post_meta['_order_by'][0] : 'date';
  74.     $asc = isset( $post_meta['_asc'] ) ? $post_meta['_asc'][0] : 'DESC';
  75.     $list_style =  isset( $post_meta['_list_style'] ) ? $post_meta['_list_style'][0] : 'default';  
  76.     $post_count = isset( $post_meta['_post_count'] ) ? $post_meta['_post_count'][0] : get_option('posts_per_page');
  77.     if(!$post_count || !is_numeric( $post_count )) $post_count = get_option('posts_per_page');
  78.     $days = isset( $post_meta['_days'] ) ? $post_meta['_days'][0] : '0';
  79.     if($days && !is_numeric( $days )) $days = '0';
  80.     ?>
  81.    
  82.     <!-- Start page of posts meta box -->
  83.     <div class="inside">
  84.     <p><label><strong><?php _e( 'Page Title', 'pageofposts' ); ?></strong></label></p> 
  85.     <input id="_page_title" name="_page_title" type="text" style="width: 98%;" value="<?php echo $page_title; ?>"/>
  86.    
  87.     <p><label><strong><?php _e( 'Posts Title', 'pageofposts' ); ?></strong></label></p>
  88.     <input id="_posts_title" name="_posts_title" type="text" style="width: 98%;" value="<?php echo $posts_title; ?>"/> 
  89.    
  90.     <p><label><strong><?php _e("Category")?></strong></label></p>
  91.     <select id="_cat_id" name="_cat_id">
  92. <?php
  93.     //Category List
  94.     foreach ($categories as $cat) :
  95.         $selected = ( $cat->cat_ID == $cat_id ) ? ' selected = "selected" ' : '';
  96.         $option = '<option '.$selected .'value="' . $cat->cat_ID;
  97.         $option = $option .'">';
  98.         $option = $option .$cat->cat_name;
  99.         $option = $option .'</option>';
  100.         echo $option;
  101.     endforeach;
  102. ?>
  103.     </select>
  104.    
  105.     <p><label><strong><?php _e("Sort by")?></strong></label></p>
  106.     <select id="_order_by" name="_order_by">
  107. <?php
  108.     foreach ($order_list as $output) :
  109.         $selected = ( $output['value'] == $order_by ) ? ' selected = "selected" ' : '';
  110.         $option = '<option '.$selected .'value="' . $output['value'];
  111.         $option = $option .'">';
  112.         $option = $option .$output['label'];
  113.         $option = $option .'</option>';
  114.         echo $option;
  115.     endforeach;
  116. ?>
  117.     </select>      
  118.    
  119.     <p><label><strong><?php _e("Order")?><strong></label></p>
  120.     <select id="_asc" name="_asc">
  121. <?php
  122.     foreach ($sort as $output) :
  123.         $selected = ( $output['value'] == $asc ) ? ' selected = "selected" ' : '';
  124.         $option = '<option '.$selected .'value="' . $output['value'];
  125.         $option = $option .'">';
  126.         $option = $option .$output['label'];
  127.         $option = $option .'</option>';
  128.         echo $option;
  129.     endforeach;
  130. ?>
  131.     </select>
  132.  
  133.     <p><label><strong><?php _e("Posts Style")?><strong></label></p>
  134.     <select id="_list_style" name="_list_style">
  135. <?php
  136.     foreach ($post_styles as $output) :
  137.         $selected = ( $output['value'] == $list_style ) ? ' selected = "selected" ' : '';
  138.         $option = '<option '.$selected .'value="' . $output['value'];
  139.         $option = $option .'">';
  140.         $option = $option .$output['label'];
  141.         $option = $option .'</option>';
  142.         echo $option;
  143.     endforeach;
  144. ?>
  145.     </select>
  146.    
  147.     <p><strong><label><?php _e( 'Posts per Page', 'pageofposts' ); ?><strong></label></p>
  148.     <input id="_post_count" name="_post_count" type="text" value="<?php echo $post_count; ?>" size="3" />
  149.    
  150.     <p><strong><label><?php _e( 'Posts in the last days', 'pageofposts' ); ?><strong></label></p>
  151.     <input id="_days" name="_days" type="text" value="<?php echo $days; ?>" size="3" />
  152.     </div>
  153.     <!-- End page of posts meta box -->
  154.     <?php
  155. }
  156. function update_pop_meta( $post_id ){
  157.    
  158.     if ( empty( $_POST ) ) {
  159.         return;
  160.     } else {
  161.         $template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
  162.         if ($template_file != 'page-pop.php') return;
  163.        
  164.         if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
  165.             return $post_id;
  166.         } else {
  167.             if ( $_POST['post_type'] == 'page' ) {
  168.                 if ( !current_user_can( 'edit_page', $post_id ) )
  169.                   return $post_id;
  170.             } else {
  171.                 if ( !current_user_can( 'edit_post', $post_id ) )
  172.                   return $post_id;
  173.             }
  174.             $meta = isset( $_POST['_cat_id'] ) ? $_POST['_cat_id'] : 1;        
  175.             pop_update_post_meta($post_id, '_cat_id', $meta);
  176.             $meta = isset( $_POST['_page_title'] ) ? $_POST['_page_title'] : '';           
  177.             pop_update_post_meta($post_id, '_page_title', $meta);
  178.             $meta = isset( $_POST['_posts_title'] ) ? $_POST['_posts_title'] : '';         
  179.             pop_update_post_meta($post_id, '_posts_title', $meta);
  180.             $meta = isset( $_POST['_order_by'] ) ? $_POST['_order_by'] : 'date';           
  181.             pop_update_post_meta($post_id, '_order_by', $meta);
  182.             $meta = isset( $_POST['_asc'] ) ? $_POST['_asc'] : 'DESC';
  183.             pop_update_post_meta($post_id, '_asc', $meta);
  184.             $meta = isset( $_POST['_list_style'] ) ? $_POST['_list_style'] : 'default';                
  185.             pop_update_post_meta($post_id, '_list_style', $meta);
  186.             $meta = isset( $_POST['_post_count'] ) ? $_POST['_post_count'] : get_option('posts_per_page');
  187.             pop_update_post_meta($post_id, '_post_count', $meta);
  188.             $meta = isset( $_POST['_days'] ) ? $_POST['_days'] : 0;
  189.             pop_update_post_meta($post_id, '_days', $meta);
  190.             return;
  191.         }
  192.     }
  193. }
  194.  
  195. function pop_update_post_meta($post_id, $key, $data) {
  196.     $post_meta = get_post_meta($post_id, $key, true);
  197.     if( $data != '' && $post_meta != $data) {
  198.         update_post_meta($post_id, $key, $data);
  199.     } elseif ( $post_meta != '' && $data == '' ) {
  200.         delete_post_meta($post_id, $key);
  201.     }
  202. }
  203. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement