PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/mb-custom-taxonomy/inc/class-mb-custom-taxonomy-register.php

https://gitlab.com/code26/selah
PHP | 235 lines | 130 code | 22 blank | 83 comment | 4 complexity | 2f3b94516c6000bce7dddd46405a65ce MD5 | raw file
  1. <?php
  2. /**
  3. * Controls all operations of MB Custom Taxonomy extension for registering custom taxonomy.
  4. *
  5. * @package Meta Box
  6. * @subpackage MB Custom Taxonomy
  7. */
  8. /**
  9. * Controls all operations for registering custom taxonomy.
  10. */
  11. class MB_Custom_Taxonomy_Register {
  12. /**
  13. * Initializing
  14. */
  15. public function __construct() {
  16. // Register taxonomies.
  17. add_action( 'init', array( $this, 'register' ) );
  18. // Change the output of post/bulk post updated messages.
  19. add_filter( 'post_updated_messages', array( $this, 'updated_message' ), 10, 1 );
  20. add_filter( 'bulk_post_updated_messages', array( $this, 'bulk_updated_messages' ), 10, 2 );
  21. }
  22. /**
  23. * Register custom post type for taxonomies
  24. */
  25. public function register() {
  26. // Register post type of the plugin 'mb-taxonomy'.
  27. $labels = array(
  28. 'name' => _x( 'Taxonomies', 'Taxonomy General Name', 'mb-custom-taxonomy' ),
  29. 'singular_name' => _x( 'Taxonomy', 'Taxonomy Singular Name', 'mb-custom-taxonomy' ),
  30. 'menu_name' => __( 'Taxonomies', 'mb-custom-taxonomy' ),
  31. 'name_admin_bar' => __( 'Taxonomy', 'mb-custom-taxonomy' ),
  32. 'parent_item_colon' => __( 'Parent Taxonomy:', 'mb-custom-taxonomy' ),
  33. 'all_items' => __( 'All Taxonomies', 'mb-custom-taxonomy' ),
  34. 'add_new_item' => __( 'Add New Taxonomy', 'mb-custom-taxonomy' ),
  35. 'add_new' => __( 'Add New', 'mb-custom-taxonomy' ),
  36. 'new_item' => __( 'New Taxonomy', 'mb-custom-taxonomy' ),
  37. 'edit_item' => __( 'Edit Taxonomy', 'mb-custom-taxonomy' ),
  38. 'update_item' => __( 'Update Taxonomy', 'mb-custom-taxonomy' ),
  39. 'view_item' => __( 'View Taxonomy', 'mb-custom-taxonomy' ),
  40. 'search_items' => __( 'Search Taxonomy', 'mb-custom-taxonomy' ),
  41. 'not_found' => __( 'Not found', 'mb-custom-taxonomy' ),
  42. 'not_found_in_trash' => __( 'Not found in Trash', 'mb-custom-taxonomy' ),
  43. );
  44. $args = array(
  45. 'label' => __( 'Taxonomies', 'mb-custom-taxonomy' ),
  46. 'labels' => $labels,
  47. 'supports' => false,
  48. 'public' => false,
  49. 'show_ui' => true,
  50. 'show_in_menu' => true,
  51. 'menu_icon' => 'dashicons-exerpt-view',
  52. 'can_export' => true,
  53. 'rewrite' => false,
  54. 'query_var' => false,
  55. );
  56. register_post_type( 'mb-taxonomy', $args );
  57. // Get all registered custom taxonomies.
  58. $taxonomies = $this->get_taxonomies();
  59. foreach ( $taxonomies as $taxonomy => $args ) {
  60. if ( false !== $args['meta_box_cb'] ) {
  61. unset( $args['meta_box_cb'] );
  62. }
  63. register_taxonomy( $taxonomy, isset( $args['post_types'] ) ? $args['post_types'] : null, $args );
  64. }
  65. }
  66. /**
  67. * Get all registered taxonomies.
  68. *
  69. * @return array
  70. */
  71. public function get_taxonomies() {
  72. // This array stores all registered custom taxonomies.
  73. $taxonomies = array();
  74. // Get all post where where post_type = mb-taxonomy.
  75. $mb_taxonomies = get_posts( array(
  76. 'posts_per_page' => - 1,
  77. 'post_status' => 'publish',
  78. 'post_type' => 'mb-taxonomy',
  79. ) );
  80. foreach ( $mb_taxonomies as $taxonomy ) {
  81. list( $labels, $args ) = $this->get_taxonomy_data( $taxonomy->ID );
  82. $taxonomies[ $args['taxonomy'] ] = $this->set_up_taxonomy( $labels, $args );
  83. }
  84. return $taxonomies;
  85. }
  86. /**
  87. * Get new taxonomy data from mb custom taxonomy id.
  88. *
  89. * @param int $mb_cpt_id MB custom taxonomy id.
  90. * @return array Array contains label and args of new taxonomy.
  91. */
  92. public function get_taxonomy_data( $mb_cpt_id ) {
  93. // Get all post meta from current post.
  94. $post_meta = get_post_meta( $mb_cpt_id );
  95. // Create array that contains Labels of this current custom taxonomy.
  96. $labels = array();
  97. // Create array that contains arguments of this current custom taxonomy.
  98. $args = array();
  99. foreach ( $post_meta as $key => $value ) {
  100. if ( false !== strpos( $key, 'label' ) ) {
  101. // If post meta has prefix 'label' then add it to $labels.
  102. // @codingStandardsIgnoreLine
  103. $data = 1 == count( $value ) ? $value[0] : $value;
  104. $labels[ str_replace( 'label_', '', $key ) ] = $data;
  105. } elseif ( false !== strpos( $key, 'args' ) ) {
  106. // If post meta has prefix 'args' then add it to $args.
  107. // @codingStandardsIgnoreLine
  108. $data = 1 == count( $value ) ? $value[0] : $value;
  109. $data = is_numeric( $data ) ? ( 1 === intval( $data ) ? true : false ) : $data;
  110. $args[ str_replace( 'args_', '', $key ) ] = $data;
  111. }
  112. }
  113. return array( $labels, $args );
  114. }
  115. /**
  116. * Setup labels, arguments for a custom taxonomy
  117. *
  118. * @param array $labels Taxonomy labels.
  119. * @param array $args Taxonomy parameters.
  120. *
  121. * @return array
  122. */
  123. public function set_up_taxonomy( $labels = array(), $args = array() ) {
  124. $labels = wp_parse_args( $labels, array(
  125. 'menu_name' => $labels['name'],
  126. // translators: %s: Name of the taxonomy in plural form.
  127. 'all_items' => sprintf( __( 'All %s', 'mb-custom-taxonomy' ), $labels['name'] ),
  128. // translators: %s: Name of the taxonomy in singular form.
  129. 'edit_item' => sprintf( __( 'Edit %s', 'mb-custom-taxonomy' ), $labels['singular_name'] ),
  130. // translators: %s: Name of the taxonomy in singular form.
  131. 'view_item' => sprintf( __( 'View %s', 'mb-custom-taxonomy' ), $labels['singular_name'] ),
  132. // translators: %s: Name of the taxonomy in singular form.
  133. 'update_item' => sprintf( __( 'Update %s', 'mb-custom-taxonomy' ), $labels['singular_name'] ),
  134. // translators: %s: Name of the taxonomy in singular form.
  135. 'add_new_item' => sprintf( __( 'Add new %s', 'mb-custom-taxonomy' ), $labels['singular_name'] ),
  136. // translators: %s: Name of the taxonomy in singular form.
  137. 'new_item_name' => sprintf( __( 'New %s', 'mb-custom-taxonomy' ), $labels['singular_name'] ),
  138. // translators: %s: Name of the taxonomy in singular form.
  139. 'parent_item' => sprintf( __( 'Parent %s', 'mb-custom-taxonomy' ), $labels['singular_name'] ),
  140. // translators: %s: Name of the taxonomy in singular form.
  141. 'parent_item_colon' => sprintf( __( 'Parent %s:', 'mb-custom-taxonomy' ), $labels['singular_name'] ),
  142. // translators: %s: Name of the taxonomy in plural form.
  143. 'search_items' => sprintf( __( 'Search %s', 'mb-custom-taxonomy' ), $labels['name'] ),
  144. // translators: %s: Name of the taxonomy in plural form.
  145. 'popular_items' => sprintf( __( 'Popular %s', 'mb-custom-taxonomy' ), $labels['name'] ),
  146. // translators: %s: Name of the taxonomy in plural form.
  147. 'separate_items_with_commas' => sprintf( __( 'Separate %s with commas', 'mb-custom-taxonomy' ), $labels['name'] ),
  148. // translators: %s: Name of the taxonomy in plural form.
  149. 'add_or_remove_items' => sprintf( __( 'Add or remove %s', 'mb-custom-taxonomy' ), $labels['name'] ),
  150. // translators: %s: Name of the taxonomy in plural form.
  151. 'choose_from_most_used' => sprintf( __( 'Choose most used %s', 'mb-custom-taxonomy' ), $labels['name'] ),
  152. // translators: %s: Name of the taxonomy in plural form.
  153. 'not_found' => sprintf( __( 'No %s found', 'mb-custom-taxonomy' ), $labels['name'] ),
  154. ) );
  155. $args = wp_parse_args( $args, array(
  156. 'label' => $labels['name'],
  157. 'labels' => $labels,
  158. 'public' => true,
  159. ) );
  160. unset( $args['taxonomy'] );
  161. return $args;
  162. }
  163. /**
  164. * Custom post updated messages
  165. *
  166. * @param array $messages Post messages.
  167. *
  168. * @return array
  169. */
  170. public function updated_message( $messages ) {
  171. $post = get_post();
  172. $revision = filter_input( INPUT_GET, 'revision', FILTER_SANITIZE_NUMBER_INT );
  173. $messages['mb-taxonomy'] = array(
  174. 0 => '', // Unused. Messages start at index 1.
  175. 1 => __( 'Taxonomy updated.', 'mb-custom-taxonomy' ),
  176. 2 => __( 'Custom field updated.', 'mb-custom-taxonomy' ),
  177. 3 => __( 'Custom field deleted.', 'mb-custom-taxonomy' ),
  178. 4 => __( 'Taxonomy updated.', 'mb-custom-taxonomy' ),
  179. // translators: %s: Date and time of the revision.
  180. 5 => $revision ? sprintf( __( 'Taxonomy restored to revision from %s.', 'mb-custom-taxonomy' ), wp_post_revision_title( $revision, false ) ) : false,
  181. 6 => __( 'Taxonomy published.', 'mb-custom-taxonomy' ),
  182. 7 => __( 'Taxonomy saved.', 'mb-custom-taxonomy' ),
  183. 8 => __( 'Taxonomy submitted.', 'mb-custom-taxonomy' ),
  184. // translators: %s: Date and time of the revision.
  185. 9 => sprintf( __( 'Taxonomy scheduled for: <strong>%s</strong>.', 'mb-custom-taxonomy' ), date_i18n( __( 'M j, Y @ G:i', 'mb-custom-taxonomy' ), strtotime( $post->post_date ) ) ),
  186. 10 => __( 'Taxonomy draft updated.', 'mb-custom-taxonomy' ),
  187. );
  188. return $messages;
  189. }
  190. /**
  191. * Custom post management WordPress messages
  192. *
  193. * @param array $bulk_messages Post bulk messages.
  194. * @param array $bulk_counts Number of posts.
  195. *
  196. * @return array
  197. */
  198. public function bulk_updated_messages( $bulk_messages, $bulk_counts ) {
  199. $bulk_messages['mb-taxonomy'] = array(
  200. // translators: %s: Name of the taxonomy in singular and plural form.
  201. 'updated' => sprintf( _n( '%s taxonomy updated.', '%s taxonomies updated.', $bulk_counts['updated'], 'mb-custom-taxonomy' ), $bulk_counts['updated'] ),
  202. // translators: %s: Name of the taxonomy in singular and plural form.
  203. 'locked' => sprintf( _n( '%s taxonomy not updated, somebody is editing.', '%s taxonomies not updated, somebody is editing.', $bulk_counts['locked'], 'mb-custom-taxonomy' ), $bulk_counts['locked'] ),
  204. // translators: %s: Name of the taxonomy in singular and plural form.
  205. 'deleted' => sprintf( _n( '%s taxonomy permanently deleted.', '%s taxonomies permanently deleted.', $bulk_counts['deleted'], 'mb-custom-taxonomy' ), $bulk_counts['deleted'] ),
  206. // translators: %s: Name of the taxonomy in singular and plural form.
  207. 'trashed' => sprintf( _n( '%s taxonomy moved to the Trash.', '%s taxonomies moved to the Trash.', $bulk_counts['trashed'], 'mb-custom-taxonomy' ), $bulk_counts['trashed'] ),
  208. // translators: %s: Name of the taxonomy in singular and plural form.
  209. 'untrashed' => sprintf( _n( '%s taxonomy restored from the Trash.', '%s taxonomies restored from the Trash.', $bulk_counts['untrashed'], 'mb-custom-taxonomy' ), $bulk_counts['untrashed'] ),
  210. );
  211. return $bulk_messages;
  212. }
  213. }