Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. if( ! post_type_exists( $this->post_type_name ) )
  2. {
  3. add_action( 'init', array( &$this, 'register_post_type' ) );
  4. }
  5.  
  6. var_dump(post_type_exists( $this->post_type_name ));
  7.  
  8. require_once plugin_dir_path( __FILE__ ) . 'admin/class-custom-post-type.php';
  9.  
  10. $location = new LegendPluginWPClassesCustomPostType('Location');
  11. $project = new LegendPluginWPClassesCustomPostType('Project');
  12. $project->add_taxomony('Type');
  13. $project->add_taxomony('Sector');
  14.  
  15. namespace LegendPluginWPClasses;
  16.  
  17. class CustomPostType {
  18.  
  19. private $post_type_name;
  20. private $args;
  21. private $labels;
  22. private $textDomain;
  23.  
  24. public function __construct( $name, $args = array(), $labels = array() )
  25. {
  26. $this->post_type_name = strtolower( str_replace( ' ', '_', $name ) );
  27. $this->args = $args;
  28. $this->labels = $labels;
  29. // Add action to register the post type, if the post type does not already exist
  30.  
  31. var_dump(post_type_exists( $this->post_type_name ));
  32. if( ! post_type_exists( $this->post_type_name ) )
  33. {
  34. add_action( 'init', array( &$this, 'register_post_type' ) );
  35. }
  36. }
  37.  
  38. public function register_post_type()
  39. {
  40. $post_type_name = ucwords($this->post_type_name);
  41. $post_type_name_plural = $post_type_name . 's';
  42.  
  43. $labels = array_merge(
  44.  
  45. array(
  46. 'name' => _x( $post_type_name_plural, 'post type general name' ),
  47. 'singular_name' => _x( $post_type_name, 'post type singular name' ),
  48. 'add_new' => _x( 'Add New', strtolower( $post_type_name ) ),
  49. 'add_new_item' => __( 'Add New ' . $post_type_name ),
  50. 'edit_item' => __( 'Edit ' . $post_type_name ),
  51. 'new_item' => __( 'New ' . $post_type_name ),
  52. 'all_items' => __( 'All ' . $post_type_name_plural ),
  53. 'view_item' => __( 'View ' . $post_type_name ),
  54. 'search_items' => __( 'Search ' . $post_type_name_plural ),
  55. 'not_found' => __( 'No ' . strtolower( $post_type_name_plural ) . ' found'),
  56. 'not_found_in_trash' => __( 'No ' . strtolower( $post_type_name_plural ) . ' found in Trash'),
  57. 'parent_item_colon' => '',
  58. 'menu_name' => $post_type_name_plural
  59. ),
  60.  
  61. $this->labels
  62. );
  63.  
  64. $args = array_merge(
  65. array(
  66. 'label' => $post_type_name,
  67. 'labels' => $labels,
  68. 'public' => true,
  69. 'show_ui' => true,
  70. 'supports' => array( 'title', 'editor' ),
  71. 'show_in_nav_menus' => true,
  72. '_builtin' => false,
  73. ),
  74.  
  75. $this->args
  76. );
  77.  
  78. // Register the post type
  79. register_post_type( $this->post_type_name, $args );
  80.  
  81. // Flus the rewrite rules to prevent problems
  82. flush_rewrite_rules(false);
  83. }
  84.  
  85. public function add_taxomony( $name, $args = array(), $labels = array() )
  86. {
  87. if( ! empty( $name ) )
  88. {
  89. $post_type_name = $this->post_type_name;
  90.  
  91. // Taxonomy properties
  92. $taxonomy_name = strtolower( str_replace( ' ', '_', $name ) );
  93. $taxonomy_labels = $labels;
  94. $taxonomy_args = $args;
  95. }
  96.  
  97. if( ! taxonomy_exists( $taxonomy_name ) )
  98. {
  99. $name = ucwords( str_replace( '_', ' ', $name ) );
  100. $plural = $name . 's';
  101.  
  102. $labels = array_merge(
  103. array(
  104. 'name' => _x( $plural, 'taxonomy general name' ),
  105. 'singular_name' => _x( $name, 'taxonomy singular name' ),
  106. 'search_items' => __( 'Search ' . $plural ),
  107. 'all_items' => __( 'All ' . $plural ),
  108. 'parent_item' => __( 'Parent ' . $name ),
  109. 'parent_item_colon' => __( 'Parent ' . $name . ':' ),
  110. 'edit_item' => __( 'Edit ' . $name ),
  111. 'update_item' => __( 'Update ' . $name ),
  112. 'add_new_item' => __( 'Add New ' . $name ),
  113. 'new_item_name' => __( 'New ' . $name . ' Name' ),
  114. 'menu_name' => __( $plural ),
  115. ),
  116.  
  117. $taxonomy_labels
  118. );
  119.  
  120. $args = array_merge(
  121.  
  122. array(
  123. 'label' => $plural,
  124. 'labels' => $labels,
  125. 'public' => true,
  126. 'show_ui' => true,
  127. 'show_in_nav_menus' => true,
  128. '_builtin' => false,
  129. ),
  130.  
  131. $taxonomy_args
  132. );
  133.  
  134. // Add the taxonomy to the post type
  135. add_action( 'init', function() use( $taxonomy_name, $post_type_name, $args ) {
  136. register_taxonomy( $taxonomy_name, $post_type_name, $args );
  137. // Flus the rewrite rules to prevent problems
  138. flush_rewrite_rules(false);
  139. }
  140. );
  141.  
  142. }
  143. else
  144. {
  145. // Taxonomy is already registered
  146. // just attach it to the post type
  147. add_action( 'init', function() use( $taxonomy_name, $post_type_name ) {
  148. register_taxonomy_for_object_type( $taxonomy_name, $post_type_name );
  149. // Flus the rewrite rules to prevent problems
  150. flush_rewrite_rules(false);
  151. }
  152. );
  153. }
  154.  
  155. }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement