Advertisement
inhouse

Modified Order Bender for Breadcrumb NavXT

Sep 18th, 2012
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Order Bender
  4. Plugin URI: http://mtekk.us/code/
  5. Description: Modified plugin. Do not upgrade automatically. Adds a metabox that allows you to set a page as the parent of a post
  6. Version: 0.1.0
  7. Author: John Havlik
  8. Author URI: http://mtekk.us/
  9. License: GPL2
  10. TextDomain: mtekk-order-bender
  11. DomainPath: /languages/
  12. */
  13. /* Copyright 2012 John Havlik (email : mtekkmonkey@gmail.com)
  14.  
  15. This program is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation; either version 2 of the License, or
  18. (at your option) any later version.
  19.  
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with this program; if not, write to the Free Software
  27. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  28. */
  29. /**
  30. * The plugin class
  31. */
  32. class mtekk_order_bender
  33. {
  34. protected $version = '0.2.0';
  35. protected $full_name = 'Order Bender';
  36. protected $short_name = 'Order Bender';
  37. protected $access_level = 'manage_options';
  38. protected $identifier = 'mtekk_order_bender';
  39. protected $unique_prefix = 'mob';
  40. protected $plugin_basename = 'order-bender/order-bender.php';
  41. /**
  42. * mlba_video
  43. *
  44. * Class default constructor
  45. */
  46. function __construct()
  47. {
  48. //We set the plugin basename here, could manually set it, but this is for demonstration purposes
  49. $this->plugin_basename = plugin_basename(__FILE__);
  50. add_action('add_meta_boxes', array($this, 'meta_boxes'));
  51. add_filter('get_the_terms', array($this, 'reorder_terms'), 3, 10);
  52. add_action('save_post', array($this, 'save_post'));
  53. }
  54. /**
  55. * Function that fires on the add_meta_boxes action
  56. */
  57. function meta_boxes()
  58. {
  59. global $wp_post_types;
  60. foreach($wp_post_types as $post_type)
  61. {
  62. //We only want custom post types that are public
  63. if(!$post_type->_builtin && $post_type->public)
  64. {
  65. //Add our primary category metabox for the current post type
  66. add_meta_box('postparentdiv', __('Primary Cateogry', 'mtekk-order-bender'), array($this,'primary_category_meta_box'), $post_type->name, 'side', 'low');
  67. }
  68. }
  69. //Add our primary category metabox for posts
  70. add_meta_box('postparentdiv', __('Primary Cateogry', 'mtekk-order-bender'), array($this,'primary_category_meta_box'), 'post', 'side', 'low');
  71. }
  72. /**
  73. * This function outputs the primary category metabox
  74. *
  75. * @param WP_Post $post The post object for the post being edited
  76. */
  77. function primary_category_meta_box($post)
  78. {
  79. //Nonce this bad boy up
  80. wp_nonce_field($this->plugin_basename, $this->unique_prefix . '-category-prefered-nonce');
  81. $pref_id = get_post_meta($post->ID, $this->unique_prefix . '_category_prefered', true);
  82. //Need inline style to keep our category drop down from doing bad things width wise
  83. echo "<style>#primary_cat{max-width: 100%;}</style>";
  84. wp_dropdown_categories(array(
  85. 'name' => $this->unique_prefix . '_primary_cat',
  86. 'id' => 'primary_cat',
  87. 'echo' => 1,
  88. 'show_option_none' => __( '&mdash; Select &mdash;' ),
  89. 'option_none_value' => '0',
  90. 'taxonomy'=> 'mcm_category_destination',
  91. 'selected' => $pref_id));
  92. }
  93. /**
  94. * This function hooks into the save_post action and saves our prefered category
  95. *
  96. * @param int $post_id The ID of the post that was just saved
  97. */
  98. function save_post($post_id)
  99. {
  100. //Exit early if we don't have our data
  101. if(!isset($_POST[$this->unique_prefix . '_primary_cat']))
  102. {
  103. return;
  104. }
  105. //Exit early if the nonce fails
  106. if(!wp_verify_nonce($_POST[$this->unique_prefix . '-category-prefered-nonce'], $this->plugin_basename))
  107. {
  108. return;
  109. }
  110. //Grab the prefered category ID
  111. $prefered_category = absint($_POST[$this->unique_prefix . '_primary_cat']);
  112. //Save the prefered category as a postmeta
  113. update_post_meta($post_id, $this->unique_prefix . '_category_prefered', $prefered_category);
  114. }
  115. /**
  116. * This function changes the order of the input terms to place a prefered term at the top
  117. *
  118. * @param array $terms The array of WP_Term objects
  119. * @param int $post_id The ID of the post in question
  120. * @param string $taxonomy The taxonomy of the term in question
  121. */
  122. function reorder_terms($terms, $post_id, $taxonomy)
  123. {
  124. //Get the prefered category for the post here
  125. $pref_id = get_post_meta($post_id, $this->unique_prefix . '_' . $taxonomy . '_prefered', true);
  126. //Make sure that ID is in the array
  127. if(array_key_exists($pref_id, $terms))
  128. {
  129. //Store our prefered term
  130. $perf_term = array($pref_id => $terms[$pref_id]);
  131. //Remove it from the array
  132. unset($terms[$pref_id]);
  133. //Recombine the array
  134. $terms = $perf_term + $terms;
  135. }
  136. //Return the array
  137. return $terms;
  138. }
  139. }
  140. $mtekk_order_bender = new mtekk_order_bender();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement