Advertisement
luisabarca

Xavier Gallery Plugin

Nov 28th, 2011
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.51 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Xavier plugin
  4. */
  5.  
  6. $gallery = Xavier_Gallery::instance();
  7.  
  8. /*
  9.  *
  10.  */
  11. function get_galleries()
  12. {
  13.     return get_terms(Xavier_Gallery::TAXONOMY_NAME);
  14. }
  15.  
  16. /*
  17.  *
  18.  */
  19. function get_gallery_featured_image_src($term, $size)
  20. {
  21.     $args = array(
  22.         'posts_per_page' => 1,
  23.         'order' => 'DESC',
  24.         'orderby' => 'date',
  25.         'tax_query' => array(
  26.             array(
  27.                 'taxonomy' => Xavier_Gallery::TAXONOMY_NAME,
  28.                 'fields'   => 'id',
  29.                 'terms'    => $term->term_id
  30.             )
  31.         )
  32.     );
  33.    
  34.     $mypost = new WP_Query($args);
  35.    
  36.     if ( $mypost->have_posts() ) {
  37.         $mypost->the_post();
  38.        
  39.         return get_the_post_thumbnail( get_the_ID(), $size );  
  40.     }
  41.    
  42.     return false;
  43. }
  44.  
  45. /*
  46.  *
  47.  */
  48. function the_gallery_featured_image( $term, $size = 'thumbnail' )
  49. {
  50.     $img = get_gallery_featured_image_src( $term, $size );
  51.    
  52.     if ( $img ) {
  53.         echo $img;
  54.     }
  55. }
  56.  
  57. /*
  58.  *
  59.  */
  60. function get_the_gallery_link( $term )
  61. {
  62.     return get_term_link($term->slug, $term->taxonomy);
  63. }
  64.  
  65. /*
  66.  *
  67.  */
  68. function the_gallery_link( $term )
  69. {
  70.     echo get_the_gallery_link($term);
  71. }
  72.  
  73. /*
  74.  *
  75.  */
  76. class Xavier_Gallery {
  77.     const CUSTOM_TYPE_NAME = 'gallery';
  78.     const TAXONOMY_NAME = 'album';
  79.     private static $instance;
  80.    
  81.     /***************************************************************************
  82.      * Static functions
  83.      **************************************************************************/
  84.     public static function instance () {
  85.  
  86.         if ( ! isset( self::$instance ) ) {
  87.             $class_name = __CLASS__;
  88.             self::$instance = new $class_name;
  89.         }
  90.  
  91.         return self::$instance;
  92.        
  93.     }
  94.    
  95.     private function __construct()
  96.     {                
  97.         add_action('init', array($this, 'register_custom_types') );
  98.         add_filter('post_type_link', array($this, 'custom_permalinks'), 1, 3);
  99.        
  100.         register_activation_hook(__FILE__, array($this, 'activate') );
  101.         register_deactivation_hook(__FILE__, array($this, 'deactivate') );
  102.     }
  103.    
  104.     /*
  105.      *
  106.      */
  107.     public function custom_permalinks($url, $post = null, $leavename = false)
  108.     {
  109.         // products only
  110.         if ($post->post_type != self::CUSTOM_TYPE_NAME) {
  111.             return $url;
  112.         }
  113.        
  114.         $post_id = $post->ID;
  115.    
  116.         $taxonomy = self::TAXONOMY_NAME;
  117.         $taxonomy_tag = '%' . $taxonomy . '%';
  118.    
  119.         // Check if exists the product type tag
  120.         if (strpos($taxonomy_tag, $url) < 0) {
  121.             // replace taxonomy tag
  122.             $url = str_replace($taxonomy_tag, '', $url);
  123.         } else {
  124.             // Get the terms
  125.             $terms = wp_get_post_terms($post_id, $taxonomy);
  126.    
  127.             if (is_array($terms) && sizeof($terms) > 0) {
  128.                 $category = $terms[0];
  129.                 // replace taxonomy tag with the term slug
  130.                 $url = str_replace($taxonomy_tag, $category->slug, $url);
  131.             }
  132.         }    
  133.    
  134.         return $url;
  135.     }
  136.    
  137.    
  138.     /*
  139.      * Register the CPT
  140.      */
  141.     public function register_custom_types()
  142.     {
  143.         global $wp_rewrite;
  144.         global $wp_query;
  145.    
  146.         register_post_type(self::CUSTOM_TYPE_NAME, array(
  147.             'label'           => 'Gallery',
  148.             'description'     => '',
  149.             'public'          => true,
  150.             'show_ui'         => true,
  151.             'show_in_menu'    => true,
  152.             'capability_type' => 'post',
  153.             'hierarchical'    => true,
  154.             'rewrite'         => array('slug' => self::CUSTOM_TYPE_NAME),
  155.             'query_var'       => true,
  156.             'has_archive'     => true,
  157.             'menu_position'   => 6,
  158.             'supports'        => array( 'title', 'editor', 'excerpt', 'revisions', 'thumbnail', 'author' ),
  159.             'labels'          => array(
  160.                 'name'               => 'Galleries',
  161.                 'singular_name'      => 'Gallery',
  162.                 'menu_name'          => 'Galleries',
  163.                 'add_new'            => 'Add Gallery',
  164.                 'add_new_item'       => 'Add New Gallery',
  165.                 'edit'               => 'Edit',
  166.                 'edit_item'          => 'Edit Gallery',
  167.                 'new_item'           => 'New Gallery',
  168.                 'view'               => 'View Gallery',
  169.                 'view_item'          => 'View Gallery',
  170.                 'search_items'       => 'Search Gallery',
  171.                 'not_found'          => 'No Galleries Found',
  172.                 'not_found_in_trash' => 'No items found in Trash',
  173.                 'parent'             => 'Parent Gallery')
  174.             )
  175.         );
  176.    
  177.         register_taxonomy(self::TAXONOMY_NAME, self::CUSTOM_TYPE_NAME, array(
  178.             'hierarchical'   => true,
  179.             'label'          => 'Albums',
  180.             'show_ui'        => true,
  181.             'query_var'      => true,
  182.             'rewrite'        => array('slug' => self::CUSTOM_TYPE_NAME . '/' . self::TAXONOMY_NAME),
  183.             'singular_label' => 'Album')
  184.         );
  185.        
  186.         $wp_rewrite->extra_permastructs[self::CUSTOM_TYPE_NAME][0] = '/' . self::CUSTOM_TYPE_NAME . '/' . self::TAXONOMY_NAME . '/%' . self::TAXONOMY_NAME . '%/%' . self::CUSTOM_TYPE_NAME . '%';
  187.     }
  188.    
  189.    
  190.     /**
  191.      *
  192.      */
  193.     public function activate()
  194.     {
  195.         self::register_custom_types();
  196.         flush_rewrite_rules();
  197.     }
  198.    
  199.     // }}}
  200.     // {{{
  201.    
  202.     /**
  203.      *
  204.      */
  205.     public function deactivate()
  206.     {
  207.         flush_rewrite_rules();
  208.     }
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement