Advertisement
Menekko

Untitled

Oct 24th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.32 KB | None | 0 0
  1. //class-dportfolio-ptype.php
  2.  
  3. <?php
  4.  
  5. if ( ! defined( 'ABSPATH' ) ) exit;
  6.  
  7. class DPortfolioPType {
  8.  
  9.     private static $_instance = null;
  10.  
  11.     public $parent = null;
  12.     public $post_type;
  13.  
  14.     public function __construct ( $parent ) {
  15.  
  16.         $this->parent = $parent;
  17.         $this->post_type = 'dportfolio';
  18.  
  19.         add_action( 'init', array( $this, 'register_post_type' ) );
  20.         add_action( 'init', array( $this, 'register_taxonomy' ) );
  21.  
  22.         if ( is_admin() ) {
  23.  
  24.             add_action( 'admin_menu', array( $this, 'meta_box_setup' ), 20 );
  25.             add_action( 'save_post', array( $this, 'meta_box_save' ) );
  26.  
  27.             add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) );
  28.  
  29.         }
  30.  
  31.         add_shortcode( 'dportfolio', array( $this, 'dportfolio_shortcode' ) );
  32.  
  33.     }
  34.  
  35.     public function register_post_type () {
  36.  
  37.         $labels = array(
  38.             'name' => _x( 'DPortfolio', 'post type general name' , 'dportfolio' ),
  39.             'singular_name' => _x( 'Single DPortfolio', 'post type singular name' , 'dportfolio' ),
  40.             'add_new' => _x( 'Add New', $this->post_type , 'dportfolio' ),
  41.             'add_new_item' => sprintf( __( 'Add New %s' , 'dportfolio' ), __( 'Post' , 'dportfolio' ) ),
  42.             'edit_item' => sprintf( __( 'Edit %s' , 'dportfolio' ), __( 'Post' , 'dportfolio' ) ),
  43.             'new_item' => sprintf( __( 'New %s' , 'dportfolio' ), __( 'Post' , 'dportfolio' ) ),
  44.             'all_items' => sprintf( __( 'All %s' , 'dportfolio' ), __( 'DPortfolio' , 'dportfolio' ) ),
  45.             'view_item' => sprintf( __( 'View %s' , 'dportfolio' ), __( 'Post' , 'dportfolio' ) ),
  46.             'search_items' => sprintf( __( 'Search %a' , 'dportfolio' ), __( 'DPortfolio' , 'dportfolio' ) ),
  47.             'not_found' =>  sprintf( __( 'No %s Found' , 'dportfolio' ), __( 'DPortfolio' , 'dportfolio' ) ),
  48.             'not_found_in_trash' => sprintf( __( 'No %s Found In Trash' , 'dportfolio' ), __( 'DPortfolio' , 'dportfolio' ) ),
  49.             'parent_item_colon' => '',
  50.             'menu_name' => __( 'DPortfolio' , 'dportfolio' )
  51.         );
  52.  
  53.         $args = array(
  54.             'labels' => $labels,
  55.             'public' => true,
  56.             'publicly_queryable' => true,
  57.             'exclude_from_search' => true,
  58.             'show_ui' => true,
  59.             'show_in_menu' => true,
  60.             'show_in_nav_menus' => true,
  61.             'query_var' => true,
  62.             'rewrite' => array( 'slug' => $this->post_type ),
  63.             'capability_type' => 'post',
  64.             'has_archive' => false,
  65.             'hierarchical' => false,
  66.             'supports' => array( 'title', 'editor', 'thumbnail' ),
  67.             'menu_position' => 5,
  68.             'menu_icon' => 'dashicons-portfolio'
  69.         );
  70.  
  71.         register_post_type( $this->post_type, $args );
  72.  
  73.     }
  74.  
  75.     public function register_taxonomy () {
  76.  
  77.         $labels = array(
  78.             'name' => __( 'Categories' , 'dportfolio' ),
  79.             'singular_name' => __( 'Category', 'dportfolio' ),
  80.             'search_items' =>  __( 'Search Categories' , 'dportfolio' ),
  81.             'all_items' => __( 'All Categories' , 'dportfolio' ),
  82.             'parent_item' => __( 'Parent Category' , 'dportfolio' ),
  83.             'parent_item_colon' => __( 'Parent Category:' , 'dportfolio' ),
  84.             'edit_item' => __( 'Edit Category' , 'dportfolio' ),
  85.             'update_item' => __( 'Update Category' , 'dportfolio' ),
  86.             'add_new_item' => __( 'Add New Category' , 'dportfolio' ),
  87.             'new_item_name' => __( 'New Category Name' , 'dportfolio' ),
  88.             'menu_name' => __( 'Categories' , 'dportfolio' ),
  89.         );
  90.  
  91.         $args = array(
  92.             'public' => true,
  93.             'hierarchical' => true,
  94.             'rewrite' => true,
  95.             'labels' => $labels,
  96.             'show_admin_column' => true
  97.         );
  98.  
  99.         register_taxonomy( 'dportfolio_categories' , $this->post_type , $args );
  100.     }
  101.  
  102.  
  103.     public function meta_box_setup () {
  104.  
  105.         add_meta_box( 'post-data', __( 'DPortfolio Details' , 'dportfolio' ), array( $this, 'meta_box_content' ), $this->post_type, 'normal', 'high' );
  106.    
  107.     }
  108.  
  109.     public function meta_box_content () {
  110.  
  111.         global $post_id;
  112.         $fields = get_post_custom( $post_id );
  113.         $field_data = $this->get_custom_fields_settings();
  114.  
  115.         $html = '';
  116.  
  117.         $html .= '<input type="hidden" name="' . $this->post_type . '_nonce" id="' . $this->post_type . '_nonce" value="' . wp_create_nonce( plugin_basename( $this->parent->dir ) ) . '" />';
  118.  
  119.         if ( 0 < count( $field_data ) ) {
  120.             $html .= '<table class="form-table">' . "\n";
  121.             $html .= '<tbody>' . "\n";
  122.  
  123.             foreach ( $field_data as $k => $v ) {
  124.                 $data = $v['default'];
  125.  
  126.                 if ( isset( $fields[$k] ) && isset( $fields[$k][0] ) ) {
  127.                     $data = $fields[$k][0];
  128.                 }
  129.  
  130.                 if( $v['type'] == 'checkbox' ) {
  131.                     $html .= '<tr valign="top"><th scope="row">' . $v['name'] . '</th><td><input name="' . esc_attr( $k ) . '" type="checkbox" id="' . esc_attr( $k ) . '" ' . checked( 'on' , $data , false ) . ' /> <label for="' . esc_attr( $k ) . '"><span class="description">' . $v['description'] . '</span></label>' . "\n";
  132.                     $html .= '</td></tr>' . "\n";
  133.                 } else {
  134.                     $html .= '<tr valign="top"><th scope="row"><label for="' . esc_attr( $k ) . '">' . $v['name'] . '</label></th><td><input name="' . esc_attr( $k ) . '" type="text" id="' . esc_attr( $k ) . '" class="regular-text" value="' . esc_attr( $data ) . '" />' . "\n";
  135.                     $html .= '<p class="description">' . $v['description'] . '</p>' . "\n";
  136.                     $html .= '</td></tr>' . "\n";
  137.                 }
  138.  
  139.             }
  140.  
  141.             $html .= '</tbody>' . "\n";
  142.             $html .= '</table>' . "\n";
  143.         }
  144.  
  145.         echo $html;
  146.     }
  147.  
  148.  
  149.     public function meta_box_save ( $post_id ) {
  150.  
  151.         global $post, $messages;
  152.  
  153.         // Verify nonce
  154.         if ( ( get_post_type() != $this->post_type ) || ! wp_verify_nonce( $_POST[ $this->post_type . '_nonce'], plugin_basename( $this->parent->dir ) ) ) {
  155.             return $post_id;
  156.         }
  157.  
  158.         // Verify user permissions
  159.         if ( ! current_user_can( 'edit_post', $post_id ) ) {
  160.             return $post_id;
  161.         }
  162.  
  163.         // Handle custom fields
  164.         $field_data = $this->get_custom_fields_settings();
  165.         $fields = array_keys( $field_data );
  166.  
  167.         foreach ( $fields as $f ) {
  168.  
  169.             if( isset( $_POST[$f] ) ) {
  170.                 ${$f} = strip_tags( trim( $_POST[$f] ) );
  171.             }
  172.  
  173.             // Escape the URLs.
  174.             if ( 'url' == $field_data[$f]['type'] ) {
  175.                 ${$f} = esc_url( ${$f} );
  176.             }
  177.  
  178.             if ( ${$f} == '' ) {
  179.                 delete_post_meta( $post_id , $f , get_post_meta( $post_id , $f , true ) );
  180.             } else {
  181.                 update_post_meta( $post_id , $f , ${$f} );
  182.             }
  183.         }
  184.  
  185.     }
  186.  
  187.     public function updated_messages ( $messages ) {
  188.  
  189.       global $post, $post_ID;
  190.  
  191.       $messages[$this->post_type] = array(
  192.  
  193.         0 => '', // Unused. Messages start at index 1.
  194.         1 => sprintf( __( 'DPortfolio updated. %sView dportfolio%s.' , 'dportfolio' ), '<a href="' . esc_url( get_permalink( $post_ID ) ) . '">', '</a>' ),
  195.         2 => __( 'DPortfolio details updated.' , 'dportfolio' ),
  196.         3 => __( 'DPortfolio details deleted.' , 'dportfolio' ),
  197.         4 => __( 'DPortfolio.' , 'dportfolio' ),
  198.         /* translators: %s: date and time of the revision */
  199.         5 => isset($_GET['revision']) ? sprintf( __( 'DPortfolio restored to revision from %s.' , 'dportfolio' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
  200.         6 => sprintf( __( 'DPortfolio published. %sView dportfolio%s.' , 'dportfolio' ), '<a href="' . esc_url( get_permalink( $post_ID ) ) . '">', '</a>' ),
  201.         7 => __( 'DPortfolio saved.' , 'dportfolio' ),
  202.         8 => sprintf( __( 'DPortfolio submitted. %sPreview dportfolio%s.' , 'dportfolio' ), '<a target="_blank" href="' . esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ) . '">', '</a>' ),
  203.         9 => sprintf( __( 'DPortfolio scheduled for: %1$s. %2$sPreview dportfolio%3$s.' , 'dportfolio' ), '<strong>' . date_i18n( __( 'M j, Y @ G:i' , 'dportfolio' ), strtotime( $post->post_date ) ) . '</strong>', '<a target="_blank" href="' . esc_url( get_permalink( $post_ID ) ) . '">', '</a>' ),
  204.         10 => sprintf( __( 'DPortfolio draft updated. %sPreview dportfolio%s.' , 'dportfolio' ), '<a target="_blank" href="' . esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ) . '">', '</a>' ),
  205.      
  206.       );
  207.  
  208.       return $messages;
  209.  
  210.     }
  211.  
  212.     public function get_custom_fields_settings () {
  213.  
  214.         $fields = array();
  215.  
  216.         $fields['_client'] = array(
  217.  
  218.             'name' => __( 'Client:' , 'dportfolio' ),
  219.             'description' => '',
  220.             'type' => 'text',
  221.             'default' => '',
  222.             'section' => 'plugin-data'
  223.  
  224.         );
  225.  
  226.         $fields['_website'] = array(
  227.  
  228.             'name' => __( 'Website:' , 'dportfolio' ),
  229.             'description' => '',
  230.             'type' => 'url',
  231.             'default' => '',
  232.             'section' => 'plugin-data'
  233.  
  234.         );
  235.  
  236.         return $fields;
  237.     }
  238.  
  239.     /**
  240.     * [dportfolio]
  241.     * [dportfolio view="gallery" limit=-1 filter="true" filter_type="list"]
  242.     */
  243.     public function dportfolio_shortcode( $atts ) {
  244.        
  245.         $a = shortcode_atts( array(
  246.  
  247.             'view' => 'gallery',
  248.             'limit' => -1,
  249.             'filter' => 'true',
  250.             'filter_type' => 'list'
  251.  
  252.         ), $atts );
  253.  
  254.         switch ($a['view']) {
  255.  
  256.             case 'single':
  257.  
  258.                 $content = '<div class="dportfolio-container-main">';
  259.  
  260.                     global $post;
  261.  
  262.                     $client = get_post_meta( $post->ID, '_client', true );
  263.                     $website = get_post_meta( $post->ID, '_website', true );       
  264.  
  265.                     if( $client != '' || $website != '' ) {
  266.  
  267.                         $content .='<div class="dportfolio-header">';
  268.  
  269.                             $content .='<div class="dportfolio-single-details">';
  270.  
  271.                                 if( $client != '' ) {                    
  272.  
  273.                                     $content .='<p>'. $client .'</p>';
  274.  
  275.                                 }
  276.  
  277.                                 if( $website != '' ) {                   
  278.  
  279.                                     $content .='<p><a href="'. $website .'" target="_blank">'. __( 'Website' , 'dportfolio' ) .'</a></p>';
  280.  
  281.                                 }                          
  282.  
  283.                             $content .='</div>';
  284.  
  285.                         $content .='</div>';               
  286.  
  287.                     }                                      
  288.  
  289.                 $content .='</div>';
  290.  
  291.                 return $content;
  292.  
  293.                 break;
  294.            
  295.             default:
  296.  
  297.                 $content = '<div class="dportfolio-container-main">';
  298.  
  299.                     $args = array(
  300.                         'post_type' => $this->post_type,
  301.                         'post_status' => 'publish',
  302.                         'posts_per_page'=> $a['limit']
  303.                     );
  304.                    
  305.                     $the_query = new WP_Query( $args );
  306.  
  307.                             $content .= '<div class="dportfolio-header">';
  308.  
  309.                                     if( $a['filter'] == 'true') {
  310.  
  311.                                         if( $a['filter_type'] == 'checkbox') {
  312.  
  313.                                             $terms = get_terms( 'dportfolio_categories' );
  314.  
  315.                                             $content .= '<div class="dportfolio-filters">';
  316.  
  317.                                                 $content .= '<div data-toggle="dportfolio-buttons">';
  318.        
  319.                                                     foreach ( $terms as $term ) {
  320.  
  321.                                                         $content .= '<label class="dportfolio-btn">';
  322.                                                             $content .= '<input type="checkbox" value="'. $term->slug .'">';
  323.                                                             $content .= $term->name;
  324.                                                         $content .= '</label>';
  325.  
  326.                                                     }                                          
  327.  
  328.                                                 $content .= '</div>';
  329.  
  330.                                             $content .= '</div>';                    
  331.  
  332.                                         } else {
  333.                                            
  334.                                             $terms = get_terms( 'dportfolio_categories' );
  335.  
  336.                                             $content .= '<ul class="dportfolio-filters">';                                         
  337.                                                 $content .= '<li data-filter="all">All</li>';
  338.  
  339.                                                 foreach ( $terms as $term ) {
  340.  
  341.                                                     $content .= '<li data-filter="'. $term->slug .'">'. $term->name .'</li>';
  342.  
  343.                                                 }
  344.  
  345.                                             $content .= '</ul>';
  346.  
  347.                                         }
  348.  
  349.                                     }
  350.  
  351.                             $content .= '</div>';
  352.  
  353.                             if( $a['filter_type'] == 'checkbox') {
  354.  
  355.                                 $content .= '<div id="dportfolio-container">';
  356.  
  357.                             } else {
  358.  
  359.                                 $content .= '<div id="dportfolio-container-list">';
  360.  
  361.                             }                          
  362.  
  363.                             if ( $the_query->have_posts() ) {
  364.                                        
  365.                                 while ( $the_query->have_posts() ) {
  366.  
  367.                                     $the_query->the_post();
  368.                                     global $post;
  369.  
  370.                                         $terms = get_the_terms( $post->ID, 'dportfolio_categories' );
  371.  
  372.                                         if ( $terms && ! is_wp_error( $terms ) ) {
  373.  
  374.                                             foreach ( $terms as $term ) {
  375.  
  376.                                                 $category = $term->slug;
  377.  
  378.                                             }
  379.  
  380.                                         }
  381.                      
  382.                                             if( $a['filter_type'] == 'checkbox') {
  383.  
  384.                                                  $content .='<div class="dportfolio-item '. $category .'">';
  385.  
  386.                                             } else {
  387.  
  388.                                                  $content .='<div class="dportfolio-item-list '. $category .'">';
  389.  
  390.                                             }
  391.  
  392.                                                
  393.                                                     $content .='<a href="'. get_the_permalink() .'">'; 
  394.  
  395.                                                         $content .= get_the_post_thumbnail( $post->ID, 'large');
  396.  
  397.                                                         $content .='<div class="dportfolio-item-info">';
  398.                                                             $content .='<h3>'. get_the_title() .'</h3>';
  399.                                                         $content .='</div>';
  400.  
  401.                                                     $content .='</a>';
  402.  
  403.                                                 $content .='</div>';                                         
  404.  
  405.  
  406.                                 }
  407.  
  408.                             }
  409.  
  410.                             wp_reset_postdata();
  411.  
  412.                             $content .='</div>';
  413.  
  414.                 $content .='</div>';
  415.  
  416.                 return $content;
  417.  
  418.                 break;
  419.  
  420.         }
  421.  
  422.     }
  423.  
  424.     public static function instance ( $parent ) {
  425.  
  426.         if ( is_null( self::$_instance ) ) {
  427.  
  428.             self::$_instance = new self( $parent );
  429.  
  430.         }
  431.  
  432.         return self::$_instance;
  433.     }
  434.  
  435.  
  436.     public function __clone () {
  437.  
  438.         _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?' ), $this->parent->_version );
  439.  
  440.     }
  441.  
  442.  
  443.     public function __wakeup () {
  444.  
  445.         _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?' ), $this->parent->_version );
  446.  
  447.     }
  448.  
  449.  
  450. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement