Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Nexus\PostType;
  4. class nx_PostType {
  5.  
  6. protected $CPT_labels;
  7. protected $post_type;
  8. protected $post_name_singular;
  9. protected $post_name_plural;
  10. protected $taxonomy;
  11.  
  12.  
  13. // Register Custom Post Type
  14. public function nx_create_CPT() {
  15. register_post_type( $this->post_type, [
  16. 'label' => __( $this->post_name_singular, 'sage' ),
  17. 'labels' => $this->CPT_labels,
  18. 'supports' => ['title','editor','thumbnail'],
  19. 'taxonomies' => $this->taxonomy,
  20. 'hierarchical' => FALSE,
  21. 'public' => TRUE,
  22. 'show_ui' => TRUE,
  23. 'show_in_menu' => TRUE,
  24. 'menu_position' => 5,
  25. 'show_in_admin_bar' => TRUE,
  26. 'show_in_nav_menus' => TRUE,
  27. 'can_export' => TRUE,
  28. 'has_archive' => TRUE,
  29. 'exclude_from_search' => FALSE,
  30. 'publicly_queryable' => TRUE,
  31. 'capability_type' => 'post',
  32. ] );
  33.  
  34. }
  35.  
  36.  
  37. //create the labels for the new post
  38. protected function nx_CPT_labels( $post_name_singular, $post_name_plural ) {
  39. $labels = [
  40. 'name' => _x( $post_name_singular, 'Post Type General Name', 'sage' ),
  41. 'singular_name' => _x( $post_name_plural, 'Post Type Singular Name', 'sage' ),
  42. 'menu_name' => __( $post_name_plural, 'sage' ),
  43. 'name_admin_bar' => __( $post_name_singular, 'sage' ),
  44. 'archives' => __( $post_name_singular . ' Archives', 'sage' ),
  45. 'attributes' => __( $post_name_singular . ' Attributes', 'sage' ),
  46. 'parent_item_colon' => __( 'Parent ' . $post_name_singular . ':', 'sage' ),
  47. 'all_items' => __( 'All ' . $post_name_plural, 'sage' ),
  48. 'add_new_item' => __( 'Add New ' . $post_name_singular, 'sage' ),
  49. 'add_new' => __( 'Add New', 'sage' ),
  50. 'new_item' => __( 'New ' . $post_name_singular, 'sage' ),
  51. 'edit_item' => __( 'Edit ' . $post_name_singular, 'sage' ),
  52. 'update_item' => __( 'Update ' . $post_name_singular, 'sage' ),
  53. 'view_item' => __( 'View ' . $post_name_singular, 'sage' ),
  54. 'view_items' => __( 'View ' . $post_name_plural, 'sage' ),
  55. 'search_items' => __( 'Search ' . $post_name_singular, 'sage' ),
  56. 'not_found' => __( 'Not found', 'sage' ),
  57. 'not_found_in_trash' => __( 'Not found in Trash', 'sage' ),
  58. 'featured_image' => __( 'Featured Image', 'sage' ),
  59. 'set_featured_image' => __( 'Set featured image', 'sage' ),
  60. 'remove_featured_image' => __( 'Remove featured image', 'sage' ),
  61. 'use_featured_image' => __( 'Use as featured image', 'sage' ),
  62. 'insert_into_item' => __( 'Insert into ' . $post_name_singular, 'sage' ),
  63. 'uploaded_to_this_item' => __( 'Uploaded to this ' . $post_name_singular, 'sage' ),
  64. 'items_list' => __( $post_name_plural . ' list', 'sage' ),
  65. 'items_list_navigation' => __( $post_name_plural . ' list navigation', 'sage' ),
  66. 'filter_items_list' => __( 'Filter ' . $post_name_plural . ' list', 'sage' ),
  67. ];
  68.  
  69. return $labels;
  70. }
  71.  
  72. //check if we need create new taxonomy
  73. protected function nx_have_to_create_taxonomy() {
  74. $new_taxonomy = [];
  75. foreach ($this->taxonomy as $taxonomy_item){
  76. if (!taxonomy_exists($taxonomy_item)){
  77. $new_taxonomy[] = $taxonomy_item;
  78. }
  79. }
  80.  
  81. if ( count($new_taxonomy) > 0 ) {
  82. return $new_taxonomy;
  83. }
  84.  
  85. return FALSE;
  86. }
  87.  
  88. //Create new Taxonomy
  89. public function nx_new_taxonomy() {
  90. // Register Custom Taxonomy
  91. $taxonomies = $this->nx_have_to_create_taxonomy();
  92.  
  93. if (!$taxonomies)
  94. return FALSE;
  95.  
  96. foreach ( $taxonomies as $taxonomy ) {
  97. if (taxonomy_exists($taxonomy)){
  98. return FALSE;
  99. }
  100. $labels = $this->nx_CPT_labels($taxonomy,$taxonomy);
  101. register_taxonomy( $taxonomy, [ $this->post_type ], [
  102. 'labels' => $labels,
  103. 'hierarchical' => FALSE,
  104. 'public' => TRUE,
  105. 'show_ui' => TRUE,
  106. 'show_admin_column' => TRUE,
  107. 'show_in_nav_menus' => TRUE,
  108. 'show_tagcloud' => TRUE,
  109. ] );
  110. }
  111. }
  112.  
  113.  
  114. public function nx_register_taxonomy() {
  115. add_action( 'init', [ $this, 'nx_new_taxonomy' ], 0 );
  116. }
  117.  
  118. //Call the action
  119. public function nx_CPT_action() {
  120. add_action( 'init', [ $this, 'nx_create_CPT' ], 0, 1 );
  121. }
  122.  
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement