Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Plugin Name: Xavier plugin
- */
- $gallery = Xavier_Gallery::instance();
- /*
- *
- */
- function get_galleries()
- {
- return get_terms(Xavier_Gallery::TAXONOMY_NAME);
- }
- /*
- *
- */
- function get_gallery_featured_image_src($term, $size)
- {
- $args = array(
- 'posts_per_page' => 1,
- 'order' => 'DESC',
- 'orderby' => 'date',
- 'tax_query' => array(
- array(
- 'taxonomy' => Xavier_Gallery::TAXONOMY_NAME,
- 'fields' => 'id',
- 'terms' => $term->term_id
- )
- )
- );
- $mypost = new WP_Query($args);
- if ( $mypost->have_posts() ) {
- $mypost->the_post();
- return get_the_post_thumbnail( get_the_ID(), $size );
- }
- return false;
- }
- /*
- *
- */
- function the_gallery_featured_image( $term, $size = 'thumbnail' )
- {
- $img = get_gallery_featured_image_src( $term, $size );
- if ( $img ) {
- echo $img;
- }
- }
- /*
- *
- */
- function get_the_gallery_link( $term )
- {
- return get_term_link($term->slug, $term->taxonomy);
- }
- /*
- *
- */
- function the_gallery_link( $term )
- {
- echo get_the_gallery_link($term);
- }
- /*
- *
- */
- class Xavier_Gallery {
- const CUSTOM_TYPE_NAME = 'gallery';
- const TAXONOMY_NAME = 'album';
- private static $instance;
- /***************************************************************************
- * Static functions
- **************************************************************************/
- public static function instance () {
- if ( ! isset( self::$instance ) ) {
- $class_name = __CLASS__;
- self::$instance = new $class_name;
- }
- return self::$instance;
- }
- private function __construct()
- {
- add_action('init', array($this, 'register_custom_types') );
- add_filter('post_type_link', array($this, 'custom_permalinks'), 1, 3);
- register_activation_hook(__FILE__, array($this, 'activate') );
- register_deactivation_hook(__FILE__, array($this, 'deactivate') );
- }
- /*
- *
- */
- public function custom_permalinks($url, $post = null, $leavename = false)
- {
- // products only
- if ($post->post_type != self::CUSTOM_TYPE_NAME) {
- return $url;
- }
- $post_id = $post->ID;
- $taxonomy = self::TAXONOMY_NAME;
- $taxonomy_tag = '%' . $taxonomy . '%';
- // Check if exists the product type tag
- if (strpos($taxonomy_tag, $url) < 0) {
- // replace taxonomy tag
- $url = str_replace($taxonomy_tag, '', $url);
- } else {
- // Get the terms
- $terms = wp_get_post_terms($post_id, $taxonomy);
- if (is_array($terms) && sizeof($terms) > 0) {
- $category = $terms[0];
- // replace taxonomy tag with the term slug
- $url = str_replace($taxonomy_tag, $category->slug, $url);
- }
- }
- return $url;
- }
- /*
- * Register the CPT
- */
- public function register_custom_types()
- {
- global $wp_rewrite;
- global $wp_query;
- register_post_type(self::CUSTOM_TYPE_NAME, array(
- 'label' => 'Gallery',
- 'description' => '',
- 'public' => true,
- 'show_ui' => true,
- 'show_in_menu' => true,
- 'capability_type' => 'post',
- 'hierarchical' => true,
- 'rewrite' => array('slug' => self::CUSTOM_TYPE_NAME),
- 'query_var' => true,
- 'has_archive' => true,
- 'menu_position' => 6,
- 'supports' => array( 'title', 'editor', 'excerpt', 'revisions', 'thumbnail', 'author' ),
- 'labels' => array(
- 'name' => 'Galleries',
- 'singular_name' => 'Gallery',
- 'menu_name' => 'Galleries',
- 'add_new' => 'Add Gallery',
- 'add_new_item' => 'Add New Gallery',
- 'edit' => 'Edit',
- 'edit_item' => 'Edit Gallery',
- 'new_item' => 'New Gallery',
- 'view' => 'View Gallery',
- 'view_item' => 'View Gallery',
- 'search_items' => 'Search Gallery',
- 'not_found' => 'No Galleries Found',
- 'not_found_in_trash' => 'No items found in Trash',
- 'parent' => 'Parent Gallery')
- )
- );
- register_taxonomy(self::TAXONOMY_NAME, self::CUSTOM_TYPE_NAME, array(
- 'hierarchical' => true,
- 'label' => 'Albums',
- 'show_ui' => true,
- 'query_var' => true,
- 'rewrite' => array('slug' => self::CUSTOM_TYPE_NAME . '/' . self::TAXONOMY_NAME),
- 'singular_label' => 'Album')
- );
- $wp_rewrite->extra_permastructs[self::CUSTOM_TYPE_NAME][0] = '/' . self::CUSTOM_TYPE_NAME . '/' . self::TAXONOMY_NAME . '/%' . self::TAXONOMY_NAME . '%/%' . self::CUSTOM_TYPE_NAME . '%';
- }
- /**
- *
- */
- public function activate()
- {
- self::register_custom_types();
- flush_rewrite_rules();
- }
- // }}}
- // {{{
- /**
- *
- */
- public function deactivate()
- {
- flush_rewrite_rules();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement