PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/magic-fields-2/mf_register.php

https://bitbucket.org/rossberenson/michael-karas
PHP | 146 lines | 96 code | 36 blank | 14 comment | 14 complexity | be8f94545efdf4d1c2280d18c85fb2e5 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. */
  5. class mf_register{
  6. public $name = 'mf_register';
  7. function __construct(){
  8. add_action('init', array( &$this, 'mf_register_custom_taxonomies' ) );
  9. add_action('init', array( &$this, 'mf_register_post_types' ) );
  10. }
  11. // register post type
  12. public function mf_register_post_types(){
  13. global $mf_pt_register,$mf_pt_unique;
  14. $post_types = $this->_get_post_types();
  15. foreach($post_types as $p){
  16. $p = unserialize($p['arguments']);
  17. $name = $p['core']['type'];
  18. $option = $p['option'];
  19. $option['show_in_menu'] = ($option['show_in_menu']) ? true : false;
  20. $option['query_var'] = ($option['query_var']) ? true : false;
  21. $option['exclude_from_search'] = ($option['exclude_from_search']) ? true : false;
  22. $option['labels'] = $p['label'];
  23. $option['with_front'] = (isset($option['with_front'])) ? $option['with_front'] : true;
  24. if( isset($p['support']) ){
  25. foreach($p['support'] as $k => $v){
  26. $option['supports'][] = $k;
  27. }
  28. }
  29. if( isset($p['taxonomy']) ){
  30. foreach($p['taxonomy'] as $k => $v){
  31. //register_taxonomy_for_object_type($k, $name);
  32. $option['taxonomies'][] = $k;
  33. }
  34. }
  35. if(isset($option['has_archive']) && $option['has_archive'] && isset($option['has_archive_slug']) && $option['has_archive_slug'])
  36. $option['has_archive'] = $option['has_archive_slug'];
  37. if($option['rewrite'] && $option['rewrite_slug'])
  38. $option['rewrite'] = array( 'slug' => $option['rewrite_slug'],'with_front' => $option['with_front']);
  39. unset($option['rewrite_slug']);
  40. unset($option['with_front']);
  41. array_push($mf_pt_register,$name);
  42. if($option['menu_position']){
  43. $option['menu_position'] = (int)$option['menu_position'];
  44. }
  45. //check Capability type
  46. trim($option['capability_type']);
  47. if(empty($option['capability_type'])){
  48. $option['capability_type'] = 'post';
  49. }elseif( !in_array($option['capability_type'],array('post','page')) ){
  50. $option['capabilities'] = $this->_get_cap($option['capability_type']);
  51. }
  52. //description
  53. $option['description'] = $p['core']['description'];
  54. register_post_type($name,$option);
  55. //add unique post type
  56. if ($p['core']['quantity']) {
  57. array_push($mf_pt_unique, "edit.php?post_type=".$name);
  58. }
  59. }
  60. }
  61. public function _get_cap($name){
  62. $caps = array(
  63. 'edit_post' => sprintf('edit_%s',$name),
  64. 'read_post' => sprintf('read_%s',$name),
  65. 'delete_post' => sprintf('delete_%s',$name),
  66. 'edit_posts' => sprintf('edit_%ss',$name),
  67. 'edit_others_posts' => sprintf('edit_others_%ss',$name),
  68. 'publish_posts' => sprintf('publish_%ss',$name),
  69. 'read_private_posts' => sprintf('read_private_%ss',$name)
  70. );
  71. return $caps;
  72. }
  73. public function mf_register_custom_taxonomies(){
  74. $taxonomies = $this->_get_custom_taxonomies();
  75. foreach($taxonomies as $tax){
  76. $tax = unserialize($tax['arguments']);
  77. $name = $tax['core']['type'];
  78. $option = $tax['option'];
  79. $option['show_in_nav_menus'] = ($option['show_in_nav_menus']) ? true : false;
  80. $option['query_var'] = ($option['query_var']) ? true : false;
  81. if( !$option['update_count_callback'] ){
  82. unset($option['update_count_callback']);
  83. }
  84. $option['labels'] = $tax['label'];
  85. $in = $tax['post_types'];
  86. if($option['rewrite'] && $option['rewrite_slug'])
  87. $option['rewrite'] = array( 'slug' => $option['rewrite_slug'] );
  88. unset($option['rewrite_slug']);
  89. register_taxonomy($name,$in, $option);
  90. }
  91. }
  92. /**
  93. * return all post types
  94. */
  95. private function _get_post_types(){
  96. global $wpdb;
  97. $query = sprintf('SELECT * FROM %s ORDER BY id',MF_TABLE_POSTTYPES);
  98. $posttypes = $wpdb->get_results( $query, ARRAY_A );
  99. return $posttypes;
  100. }
  101. /**
  102. * return all custom_taxonomy
  103. */
  104. private function _get_custom_taxonomies(){
  105. global $wpdb;
  106. $query = sprintf('SELECT * FROM %s ORDER BY id',MF_TABLE_CUSTOM_TAXONOMY);
  107. $custom_taxonomies = $wpdb->get_results( $query, ARRAY_A );
  108. return $custom_taxonomies;
  109. }
  110. }