rozanchetta

WordPress - arquivo cpt.php

Oct 12th, 2011
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.63 KB | None | 0 0
  1. <?php
  2. /* referente ao tutorial:  http://rozanchetta.wordpress.com/2011/10/12/area-de-clientes-colunas-administracao-cpt */
  3.  
  4. // ARQUIVO PARA OS CUSTOM POST TYPES DO TEMA
  5.  
  6. /* CPT DOCUMENTOS */
  7. add_action( 'init', 'register_cpt_documentos' );
  8. function register_cpt_documentos() {
  9. $labels = array(
  10. 'name' => _x( 'Documentos', 'documentos' ),
  11. 'singular_name' => _x( 'Documento', 'documentos' ),
  12. 'add_new' => _x( 'Adicionar Documento', 'documentos' ),
  13. 'add_new_item' => _x( 'Adicionar Novo Documento', 'documentos' ),
  14. 'edit_item' => _x( 'Editar Documento', 'documentos' ),
  15. 'new_item' => _x( 'Novo Documento', 'documentos' ),
  16. 'view_item' => _x( 'Ver Documento', 'documentos' ),
  17. 'search_items' => _x( 'Localizar documentos', 'documentos' ),
  18. 'not_found' => _x( 'Nenhum Documento encontrado', 'documentos' ),
  19. 'not_found_in_trash' => _x( 'Não há documentos na Lixeira', 'documentos' ),
  20. 'parent_item_colon' => _x( 'Pai Documento:', 'documentos' ),
  21. 'menu_name' => _x( 'Documentos de Clientes', 'documentos' ), /* "Documentos de Clientes" será o texto que vai aparecer no menu do painel */
  22. );
  23. $args = array(
  24. 'labels' => $labels,
  25. 'hierarchical' => true,
  26. 'supports' => array( 'title', 'author', 'editor', 'comments' ),
  27. 'taxonomies' => array( 'cliente' ), /* "Cliente" será a taxonomia para o CPT documentos */
  28. 'public' => true,
  29. 'show_ui' => true,
  30. 'show_in_menu' => true,
  31. 'menu_position' => 5,
  32. 'menu_icon' => 'http://acharme.com.br/testesrozanchetta/wp-content/themes/tema-base/images/documentos-icone.gif', /* Crie uma imagem de 16x16 px e salve em seu tema */
  33. 'show_in_nav_menus' => true,
  34. 'publicly_queryable' => true,
  35. 'exclude_from_search' => true,
  36. 'has_archive' => true,
  37. 'query_var' => true,
  38. 'can_export' => true,
  39. 'rewrite' => true,
  40. 'capability_type' => 'post'
  41. );
  42. register_post_type( 'documentos', $args );
  43. }
  44.  
  45. //  T A X O N O M I A S
  46. /* TAX:CLIENTE - PARA O CPT DOCUMENTOS */
  47. add_action( 'init', 'register_taxonomy_cliente' );
  48. function register_taxonomy_cliente() {
  49. $labels = array(
  50. 'name' => _x( 'Clientes', 'cliente' ),
  51. 'singular_name' => _x( 'Cliente', 'cliente' ),
  52. 'search_items' => _x( 'Localizar Cliente', 'cliente' ),
  53. 'popular_items' => _x( 'Clientes Populares', 'cliente' ),
  54. 'all_items' => _x( 'Todos os Clientes', 'cliente' ),
  55. 'parent_item' => _x( 'Cliente Pai', 'cliente' ),
  56. 'parent_item_colon' => _x( 'Cliente Pai:', 'cliente' ),
  57. 'edit_item' => _x( 'Editar Cliente', 'cliente' ),
  58. 'update_item' => _x( 'Atualizar Cliente', 'cliente' ),
  59. 'add_new_item' => _x( 'Adicionar Novo Cliente', 'cliente' ),
  60. 'new_item_name' => _x( 'Novo Nome de Cliente', 'cliente' ),
  61. 'separate_items_with_commas' => _x( 'Separe por vírgulas', 'cliente' ),
  62. 'add_or_remove_items' => _x( 'Adicionar ou remover Cliente', 'cliente' ),
  63. 'choose_from_most_used' => _x( 'Escolha entre os mais usados', 'cliente' ),
  64. 'menu_name' => _x( 'Clientes', 'cliente' ),
  65. );
  66. $args = array(
  67. 'labels' => $labels,
  68. 'public' => true,
  69. 'show_in_nav_menus' => true,
  70. 'show_ui' => true,
  71. 'show_tagcloud' => true,
  72. 'hierarchical' => true,
  73. 'rewrite' => true,
  74. 'query_var' => true
  75. );
  76. register_taxonomy( 'cliente', array('documentos'), $args );
  77. }
  78.  
  79. // ADICIONAR COLUNAS AO PAINEL DO CPT DOCUMENTOS
  80. add_filter( 'manage_edit-documentos_columns', 'meutema_edit_documentos_columns' ) ;
  81. function meutema_edit_documentos_columns( $columns ) {
  82. $columns = array(
  83. 'cb' => '<input type="checkbox" />',
  84. 'title' => __( 'Documento' ),
  85. 'author' => __( 'Autor' ),
  86. 'cliente' => __( 'Cliente' ),
  87. 'restrito' => __( 'Restrito para' ),
  88. 'comments' => __( 'Com' ),
  89. 'date' => __( 'Data' )
  90. );
  91. return $columns;
  92. }
  93. add_action( 'manage_documentos_posts_custom_column', 'meutema_manage_documentos_columns', 10, 2 );
  94. function meutema_manage_documentos_columns( $column, $post_id ) {
  95. switch( $column ) {
  96. /* Adiciona coluna para a meta box */
  97. case 'restrito' :
  98. global $restrito_mb;
  99. $restrito_mb->the_meta();
  100. $restrito_mb->the_value('teste_restrito');
  101. break;
  102. /* Adiciona coluna para a taxonomia */
  103. case 'cliente' :
  104. global $post;
  105. $terms = get_the_terms( $post_id, 'cliente' );
  106. if ( !empty( $terms ) ) {
  107. $out = array();
  108. foreach ( $terms as $term ) {
  109. $out[] = sprintf( '<a href="%s">%s</a>',
  110. esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'cliente' => $term->slug ), 'edit.php' ) ),
  111. esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'cliente', 'display' ) )
  112. );
  113. }
  114. echo join( ', ', $out );
  115. }
  116. else {
  117. _e( 'Nenhum Cliente' );
  118. }
  119. break;
  120. default :
  121. break;
  122. }
  123. }
  124.  
  125. /* Habilita a coluna restrito para ser ordenada */
  126. function restrito_column_register_sortable( $columns ) {
  127. $columns['restrito'] = 'restrito';
  128. return $columns;
  129. }
  130. add_filter( 'manage_edit-documentos_sortable_columns', 'restrito_column_register_sortable' );
  131.  
  132. /* Classifica a coluna restrito */
  133. function restrito_column_orderby( $vars ) {
  134. global $restrito_mb;
  135. if ( isset( $vars['orderby'] ) && 'restrito' == $vars['orderby'] ) {
  136. $vars = array_merge( $vars, array(
  137. 'meta_key' => $restrito_mb->get_the_name('teste_restrito'),
  138. 'orderby' => 'meta_value'
  139. ) );
  140. }
  141. return $vars;
  142. }
  143. add_filter( 'request', 'restrito_column_orderby' );
  144.  
  145. /* Habilita a coluna cliente para ser ordenada */
  146. function cliente_column_register_sortable( $columns ) {
  147. $columns['cliente'] = 'cliente';
  148. return $columns;
  149. }
  150. add_filter( 'manage_edit-documentos_sortable_columns', 'cliente_column_register_sortable' );
  151.  
  152. /* Classifica a coluna cliente */
  153. function cliente_clauses_meutema( $clauses, $wp_query ) {
  154. global $wpdb;
  155. if ( isset( $wp_query->query['orderby'] ) && 'cliente' == $wp_query->query['orderby'] ) {
  156. $clauses['join'] .= <<<SQL
  157. LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
  158. LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
  159. LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
  160. SQL;
  161. $clauses['where'] .= " AND (taxonomy = 'cliente' OR taxonomy IS NULL)";
  162. $clauses['groupby'] = "object_id";
  163. $clauses['orderby']  = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";
  164. $clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';
  165. }
  166. return $clauses;
  167. }
  168. add_filter( 'posts_clauses', 'cliente_clauses_meutema', 10, 2 );
  169.  
  170. /* Adiciona os CPTs ao Box "Agora" no Painel de Administração */
  171. function wph_right_now_content_table_end() {
  172. $args = array(
  173. 'public' => true ,
  174. '_builtin' => false
  175. );
  176. $output = 'object';
  177. $operator = 'and';
  178. $post_types = get_post_types( $args , $output , $operator );
  179. foreach( $post_types as $post_type ) {
  180. $num_posts = wp_count_posts( $post_type->name );
  181. $num = number_format_i18n( $num_posts->publish );
  182. $text = _n( $post_type->labels->singular_name, $post_type->labels->name , intval( $num_posts->publish ) );
  183. if ( current_user_can( 'edit_posts' ) ) {
  184. $num = "<a href='edit.php?post_type=$post_type->name'>$num</a>";
  185. $text = "<a href='edit.php?post_type=$post_type->name'>$text</a>";
  186. }
  187. echo '<tr><td class="first b b-' . $post_type->name . '">' . $num . '</td>';
  188. echo '<td class="t ' . $post_type->name . '">' . $text . '</td></tr>';
  189. }
  190. $taxonomies = get_taxonomies( $args , $output , $operator );
  191. foreach( $taxonomies as $taxonomy ) {
  192. $num_terms  = wp_count_terms( $taxonomy->name );
  193. $num = number_format_i18n( $num_terms );
  194. $text = _n( $taxonomy->labels->singular_name, $taxonomy->labels->name , intval( $num_terms ));
  195. if ( current_user_can( 'manage_categories' ) ) {
  196. $num = "<a href='edit-tags.php?taxonomy=$taxonomy->name'>$num</a>";
  197. $text = "<a href='edit-tags.php?taxonomy=$taxonomy->name'>$text</a>";
  198. }
  199. echo '<tr><td class="first b b-' . $taxonomy->name . '">' . $num . '</td>';
  200. echo '<td class="t ' . $taxonomy->name . '">' . $text . '</td></tr>';
  201. }
  202. }
  203. add_action( 'right_now_content_table_end' , 'wph_right_now_content_table_end' );
  204.  
Advertisement
Add Comment
Please, Sign In to add comment