Advertisement
emcniece

Latest Custom Post Type Updates - index.php

Jul 31st, 2012
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.43 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5.  * Plugin Name: Latest Custom Post Type Updates
  6.  
  7.  * Plugin URI: http://technicalmastermind.com/wordpress-plugins/latest-custom-post-type-updates/
  8.  
  9.  * Description: This simple plugin adds a widget that allows for the display of recent posts in any custom post type. It functions almost identically to the built-in WordPress "Recent Posts" widget with the added option of letting you choose which post type it pulls from. Just add the "Latest Custom Post Type" widget to a widget area, give it a title, how many posts to show and what post type you want it to pull from. It's that easy! The option is also there to allow for filtering the results based on taxonomy tags/categories. This allows you to limit to only posts that are in, not in, or in all of the selected taxonomy tags/categories (thanks to adamlaz for requesting this feature). To keep it simple this option does not display by default making the widget options easy on the eyes unless you need the power, and then it tries to stay as short and simple as possible. As always there is no added admin panel to configure options, it's all configured from the individual widget settings.
  10.  
  11.  * Version: 1.2.1
  12.  
  13.  * Author: David Wood
  14.  
  15.  * Author URI: http://technicalmastermind.com/about-david-wood/
  16.  
  17.  * License: GPL2
  18.  
  19.  */
  20. add_filter('site_transient_update_plugins', 'lcptu_remove_update_nag');
  21. function lcptu_remove_update_nag($value) {
  22.  unset($value->response[ plugin_basename(__FILE__) ]);
  23.  return $value;
  24. }
  25.  
  26.  
  27. if(!class_exists('tm_latest_cp_widget') && class_exists('WP_Widget')):
  28.  
  29. class tm_latest_cp_widget extends WP_Widget {
  30.  
  31.     function tm_latest_cp_widget() { // creates the widget
  32.  
  33.         $name = __('Latest Custom Post Type'); // The widget name as users will see it
  34.  
  35.         $description = __('Simple display of the latest updates to your custom post type.'); // The widget description as users will see it
  36.  
  37.  
  38.  
  39.         $this->WP_Widget($id_base = false,
  40.  
  41.             $name,
  42.  
  43.             $widget_options = array('classname' => strtolower(get_class($this)), 'description' => $description),
  44.  
  45.             $control_options = array());
  46.  
  47.     }
  48.  
  49.  
  50.  
  51.     function form($instance) { // outputs the options form on admin
  52.  
  53.         // include options form
  54.  
  55.         include(dirname(__FILE__).'/options.php');
  56.  
  57.     }
  58.  
  59.  
  60.  
  61.     function update($new_instance, $old_instance) {
  62.  
  63.         // processes widget options to be saved
  64.  
  65.         $instance = $old_instance;
  66.  
  67.         $instance['title'] = strip_tags($new_instance['title']);
  68.  
  69.         if(!empty($new_instance['numberposts']) && is_numeric($new_instance['numberposts'])) $instance['numberposts'] = (int) strip_tags($new_instance['numberposts']);
  70.  
  71.         else $instance['numberposts'] = 5;
  72.  
  73.         $instance['orderby'] = $new_instance['orderby'];
  74.  
  75.         $instance['order'] = $new_instance['order'];
  76.  
  77.         $instance['empty_display'] = strip_tags($new_instance['empty_display']);
  78.  
  79.         $instance['css_class'] = strip_tags($new_instance['css_class']);
  80.  
  81.         $instance['post_type'] = $new_instance['post_type'];
  82.  
  83.         $instance['randomize'] = $new_instance['randomize'];
  84.  
  85.         $instance['show_thumbnails'] = $new_instance['show_thumbnails'];
  86.  
  87.         $instance['thumbnail_format'] = $new_instance['thumbnail_format'];
  88.  
  89.         $instance['thumbnail_width'] = $new_instance['thumbnail_width'] ? $new_instance['thumbnail_width'] : 0;
  90.  
  91.         $instance['thumbnail_height'] = $new_instance['thumbnail_height'] ? $new_instance['thumbnail_height'] : 0;
  92.  
  93.         $instance['default_image'] = $new_instance['default_image'];
  94.  
  95.         $instance['show_advanced'] = $new_instance['show_advanced'];
  96.  
  97.         $instance['taxonomies'] = $new_instance['taxonomies'];
  98.  
  99.         if($new_instance['tax_in'] != 'in' && $new_instance['tax_in'] != 'not_in' && $new_instance['tax_in'] != 'and')
  100.  
  101.             $instance['tax_in'] = 'in';
  102.  
  103.         else
  104.  
  105.             $instance['tax_in'] = $new_instance['tax_in'];
  106.  
  107.         if(is_array($new_instance['tag_list']) && count($new_instance['tag_list']) > 0) {
  108.  
  109.             $instance['tag_list'] = $new_instance['tag_list'];
  110.  
  111.         } else {
  112.  
  113.             $tmp = explode(';', $new_instance['tag_list']);
  114.  
  115.             $tags = array();
  116.  
  117.             foreach($tmp as $tag) {
  118.  
  119.                 $t = addslashes(strip_tags(trim($tag)));
  120.  
  121.                 if(!empty($t)) $tags[] = $t;
  122.  
  123.             }
  124.  
  125.             $instance['tag_list'] = $tags;
  126.  
  127.         }
  128.  
  129.  
  130.  
  131.         return $instance;
  132.  
  133.     }
  134.  
  135.  
  136.  
  137.     function widget($args, $instance) {
  138.  
  139.         $before_title = $before_widget = $after_widget = $after_title = '';
  140.  
  141.         extract($args, EXTR_IF_EXISTS);
  142.  
  143.         // Begin widget display
  144.  
  145.         echo $before_widget;
  146.  
  147.         echo $before_title.$instance['title'].$after_title;
  148.  
  149.         echo '<ul class="tm-latest-updates '.$instance['css_class'].'">';
  150.  
  151.  
  152.  
  153.         // Make call to get most recent posts from the specified post type
  154.  
  155.         $params = array(
  156.  
  157.             'numberposts' => $instance['numberposts'],
  158.  
  159.             'orderby' => $instance['orderby'],
  160.  
  161.             'order' => $instance['order'],
  162.  
  163.             'post_type' => $instance['post_type'],
  164.  
  165.         );
  166.  
  167.         // Check if we are filtering based on taxonomy tags/categories
  168.  
  169.         if(($instance['show_advanced'] == 'yes')
  170.  
  171.             && ($instance['taxonomies'] != '--')
  172.  
  173.             && is_array($instance['tag_list'])
  174.  
  175.             && (count($instance['tag_list']) > 0)
  176.  
  177.         ) {
  178.  
  179.             // We are filtering, add the filter to the query
  180.  
  181.             $params['tax_query'] = array(
  182.  
  183.                 array(
  184.  
  185.                     'taxonomy' => $instance['taxonomies'],
  186.  
  187.                     'field' => 'id',
  188.  
  189.                     'terms' => $instance['tag_list'],
  190.  
  191.                     'operator' => strtoupper(str_replace('_', ' ', $instance['tax_in']))
  192.  
  193.                 )
  194.  
  195.             );
  196.  
  197.             if(is_numeric($instance['tag_list'][0])) $params['tax_query'][0]['field'] = 'id';
  198.  
  199.             else $params['tax_query'][0]['field'] = 'slug';
  200.  
  201.         }
  202.  
  203.         // Get the posts!
  204.  
  205.         $posts = get_posts($params);
  206.  
  207.         // Print 'em out!
  208.        
  209.         if($instance['randomize']) shuffle( &$posts);
  210.  
  211.         if($posts)
  212.  
  213.             foreach($posts as $post){
  214.  
  215.                 echo '<li>';
  216.                
  217.                 if($instance['show_thumbnails']){
  218.                
  219.                     if (has_post_thumbnail( $post->ID ) ){
  220.                        
  221.                         if( $instance['thumbnail_format'])
  222.                        
  223.                             $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), $instance['thumbnail_format'] );
  224.    
  225.                         else
  226.                            
  227.                            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );
  228.                            
  229.                            $image[1] = $instance['thumbnail_width'];
  230.                            
  231.                            $image[2] = $instance['thumbnail_height'];
  232.                                        
  233.                     } else{
  234.  
  235.                         $image = array( $instance['default_image'], $instance['thumbnail_width'], $instance['thumbnail_height'] );
  236.                        
  237.                     }
  238.                    
  239.                     echo '<a href="'.get_permalink($post->ID).'" title="'.$post->post_title.'">';
  240.                    
  241.                     echo '<img src="'.$image[0].'" alt="'.$post->post_title.'" width="'.$image[1].'" height="'.$image[2].'" />';
  242.                    
  243.                     echo '</a>';
  244.    
  245.                 }
  246.                
  247.                 echo '<a href="'.get_permalink($post->ID).'" title="'.$post->post_title.'">'.$post->post_title.'</a></li>';
  248.                
  249.             }
  250.         else {
  251.  
  252.             if($instance['empty_display'])
  253.  
  254.             echo '<li>'.$instance['empty_display'].'</li>';
  255.  
  256.         }
  257.  
  258.         // Always remember your closing tags!
  259.  
  260.         echo '</ul>'.$after_widget;
  261.  
  262.     }
  263.  
  264.  
  265.  
  266.     static function get_post_taxonomies($post_type = 'post', $current = '') {
  267.  
  268.         ob_start();
  269.  
  270.         $taxonomies = get_object_taxonomies($post_type, 'objects');
  271.  
  272.         echo '<option value="--">--</option>';
  273.  
  274.         if($taxonomies) {
  275.  
  276.             foreach($taxonomies as $taxonomy) {
  277.  
  278.                 if($taxonomy->show_ui != 1) continue;
  279.  
  280.                 echo '<option value="'.$taxonomy->name.'"';
  281.  
  282.                 if($current == $taxonomy->name) echo ' selected="selected"';
  283.  
  284.                 echo '>'.$taxonomy->labels->name.'</option>';
  285.  
  286.             }
  287.  
  288.         }
  289.  
  290.         return ob_get_clean();
  291.  
  292.     }
  293.  
  294.  
  295.  
  296.     static function get_taxonomy_list($taxonomy, $checkbox_name, $tag_list = array()) {
  297.  
  298.         if(!is_array($tag_list)) $tag_list = array();
  299.  
  300.         $this_tax = get_taxonomy($taxonomy);
  301.  
  302.         if($this_tax->hierarchical == 1) {
  303.  
  304.             // Treat as categories
  305.  
  306.             ob_start();
  307.  
  308.             self::taxonomy_levels($taxonomy, $tag_list, $checkbox_name);
  309.  
  310.             return ob_get_clean();
  311.  
  312.         } else {
  313.  
  314.             // Treat as tags
  315.  
  316.             ob_start();
  317.  
  318.             echo '(Semi-colon(;) seperated list)';
  319.  
  320.             echo '<textarea rows="8" cols="30" name="'.$checkbox_name.'">';
  321.  
  322.             if(count($tag_list) > 0) echo implode('; ', $tag_list);
  323.  
  324.             echo '</textarea>';
  325.  
  326.             return ob_get_clean();
  327.  
  328.         }
  329.  
  330.     }
  331.  
  332.  
  333.  
  334.     static function taxonomy_levels($taxonomy, $tag_list, $checkbox_name, $level = 0, $parent_id = 0) {
  335.  
  336.         $per_level = '&nbsp;&nbsp;';
  337.  
  338.         $checked = ' checked="checked"';
  339.  
  340.         $terms = get_terms($taxonomy, array('hide_empty' => false, 'parent' => $parent_id));
  341.  
  342.         if($terms) {
  343.  
  344.             foreach($terms as $term) {
  345.  
  346.                 echo str_repeat($per_level, $level);
  347.  
  348.                 echo '<input type="checkbox" name="'.$checkbox_name.'[]" value="'.$term->term_id.'"';
  349.  
  350.                 if(in_array($term->term_id, $tag_list)) echo $checked;
  351.  
  352.                 echo ' /> '.$term->name.'<br/>';
  353.  
  354.                 self::taxonomy_levels($taxonomy, $tag_list, $checkbox_name, $level+1, $term->term_id);
  355.  
  356.             }
  357.  
  358.         }
  359.  
  360.     }
  361.  
  362. }
  363.  
  364. // register widget
  365.  
  366. add_action('widgets_init', create_function('', 'return register_widget("tm_latest_cp_widget");'));
  367.  
  368. add_action('wp_ajax_tm_lcptu_get_taxonomies', array('tm_latest_cp_widget', 'get_post_taxonomies'));
  369.  
  370. endif;
  371.  
  372.  
  373.  
  374. if(!function_exists('tm_lcptu_enqueue')):
  375.  
  376.     add_action('admin_enqueue_scripts', 'tm_lcptu_enqueue');
  377.  
  378.     function tm_lcptu_enqueue($hook) {
  379.  
  380.         if('widgets.php' != $hook) return;
  381.  
  382.         global $wp_version;
  383.  
  384.         if(preg_match('/^3\.[3-9]/', $wp_version)) // Add script for WP 3.3 and newer (jQuery 1.7+)
  385.  
  386.             wp_enqueue_script('tm-lcptu-advanced', plugins_url('/js/tm-lcptu-options-1.7.js', __FILE__), array('jquery'));
  387.  
  388.         else // Add script for older versions of WP
  389.  
  390.             wp_enqueue_script('tm-lcptu-advanced', plugins_url('/js/tm-lcptu-options-1.6.js', __FILE__), array('jquery'));
  391.  
  392.     }
  393.  
  394. endif;
  395.  
  396.  
  397.  
  398. // Wrappers for AJAX calls
  399.  
  400. if(!function_exists('ajax_tm_lcptu_tax')):
  401.  
  402.     add_action('wp_ajax_tm_lcptu_tax', 'ajax_tm_lcptu_tax');
  403.  
  404.     function ajax_tm_lcptu_tax() {
  405.  
  406.         global $wpdb;
  407.  
  408.         if(isset($_POST['posttype'])) $post_type = $_POST['posttype'];
  409.  
  410.         else $post_type = 'post';
  411.  
  412.         echo tm_latest_cp_widget::get_post_taxonomies($post_type);
  413.  
  414.         die();
  415.  
  416.     }
  417.  
  418. endif;
  419.  
  420.  
  421.  
  422. if(!function_exists('ajax_tm_lcptu_tags')):
  423.  
  424.     add_action('wp_ajax_tm_lcptu_tags', 'ajax_tm_lcptu_tags');
  425.  
  426.     function ajax_tm_lcptu_tags() {
  427.  
  428.         global $wpdb;
  429.  
  430.         if(isset($_POST['taxonomy'])) $taxonomy = $_POST['taxonomy'];
  431.  
  432.         else $taxonomy = 'post_tag';
  433.  
  434.         echo tm_latest_cp_widget::get_taxonomy_list($taxonomy, $_POST['checkboxName']);
  435.  
  436.         die();
  437.  
  438.     }
  439.  
  440. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement