forked from mtekk/Order-Bender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorder-bender.php
214 lines (206 loc) · 7.35 KB
/
order-bender.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/*
Plugin Name: Order Bender
Plugin URI: http://mtekk.us/code/
Description: Adds a metabox that allows you to set the prefered hierarchical taxonomy term for a post.
Version: 0.7.2
Author: John Havlik
Author URI: http://mtekk.us/
License: GPL2
TextDomain: mtekk-order-bender
DomainPath: /languages/
*/
/* Copyright 2012-2016 John Havlik (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* The plugin class
*/
class mtekk_order_bender
{
protected $version = '0.7.2';
protected $full_name = 'Order Bender';
protected $short_name = 'Order Bender';
protected $access_level = 'manage_options';
protected $identifier = 'mtekk_order_bender';
protected $unique_prefix = 'mob';
protected $plugin_basename = 'order-bender/order-bender.php';
/**
* mlba_video
*
* Class default constructor
*/
function __construct()
{
//We set the plugin basename here, could manually set it, but this is for demonstration purposes
$this->plugin_basename = plugin_basename(__FILE__);
add_action('add_meta_boxes', array($this, 'meta_boxes'), 2, 10);
add_filter('get_the_terms', array($this, 'reorder_terms'), 3, 10);
add_filter('bcn_pick_post_term', array($this, 'pick_bcn_term'), 3, 10);
add_action('save_post', array($this, 'save_post'));
}
/**
* Function that fires on the add_meta_boxes action
*
* @param string $post_type The name of the post type which the current post being edited
* @param WP_Post $post The WP_Post object for the current post being edited
*/
function meta_boxes($post_type, $post)
{
global $wp_taxonomies;
foreach($wp_taxonomies as $taxonomy)
{
if($taxonomy->hierarchical && in_array($post_type, $taxonomy->object_type))
{
//Add our primary category metabox for the current post type
add_meta_box($this->unique_prefix . '_' . $taxonomy->name . '_primary_term_div', sprintf(__('Primary %s', 'mtekk-order-bender'), $taxonomy->labels->singular_name), array($this,'primary_taxonomy_term_meta_box'), $post_type, 'side', 'low', array($taxonomy));
}
}
}
/**
* This function outputs the primary category metabox
*
* @param WP_Post $post The post object for the post being edited
* @param array $callback_args The callback argument array
*/
function primary_taxonomy_term_meta_box($post, $callback_args)
{
//Grab our taxonomy from the args
$taxonomy = $callback_args['args'][0];
//Get all the terms the post belongs to
$terms = wp_get_post_terms($post->ID, $taxonomy->name);
$used_terms = array();
//Loop through the terms and collect their ids in the $used_terms array
foreach($terms as $term)
{
//Go up the hierarchy and if has parent, add the parent too to keep the hierarchical view
$parent_id = $term->parent;
if($parent_id)
{
while($parent_id)
{
$used_terms[] = $parent_id;
$parent_term = get_term($parent_id, $taxonomy->name);
$parent_id = $parent_term->parent;
}
}
$used_terms[] = $term->term_id;
}
//Nonce this bad boy up
wp_nonce_field($this->plugin_basename, $this->unique_prefix . '-' . $taxonomy->name . '-prefered-nonce');
$pref_id = get_post_meta($post->ID, $this->unique_prefix .'_' . $taxonomy->name . '_prefered', true);
wp_dropdown_categories(array(
'name' => $this->unique_prefix . '_' . $taxonomy->name . '_primary_term',
'id' => $this->unique_prefix . '_' . $taxonomy->name . '_primary_term',
'class' => $this->unique_prefix . '_primary_term',
'echo' => 1,
'depth' => 0,
'hierarchical' => 1,
'show_option_none' => __( '— Select —' ),
'option_none_value' => '0',
'selected' => $pref_id,
'include' => $used_terms,
'taxonomy' => $taxonomy->name));
}
/**
* This function hooks into the save_post action and saves our prefered category
*
* @param int $post_id The ID of the post that was just saved
*/
function save_post($post_id)
{
global $wp_post_types, $wp_taxonomies;
foreach($wp_post_types as $post_type)
{
foreach($wp_taxonomies as $taxonomy)
{
if($taxonomy->hierarchical && in_array($post_type->name, $taxonomy->object_type))
{
//Exit early if we don't have our data
if(!isset($_POST[$this->unique_prefix . '_' . $taxonomy->name . '_primary_term']))
{
continue;
}
//Exit early if the nonce fails
if(!wp_verify_nonce($_POST[$this->unique_prefix . '-' . $taxonomy->name . '-prefered-nonce'], $this->plugin_basename))
{
continue;
}
//Grab the prefered category ID
$prefered_term = absint($_POST[$this->unique_prefix . '_' . $taxonomy->name . '_primary_term']);
//Save the prefered category as a postmeta
update_post_meta($post_id, $this->unique_prefix .'_' . $taxonomy->name . '_prefered', $prefered_term);
}
}
}
}
/**
* This function changes the order of the input terms to place a prefered term at the top
*
* @param array $terms The array of WP_Term objects
* @param int $post_id The ID of the post in question
* @param string $taxonomy The taxonomy of the term in question
*
* @return array The reordered array of WP_Term objects
*/
function reorder_terms($terms, $post_id, $taxonomy)
{
//Get the prefered taxonomy term for the post here
$pref_id = get_post_meta($post_id, $this->unique_prefix . '_' . $taxonomy . '_prefered', true);
//Have to iterate over all of the terms, searching for the prefered term
foreach($terms as $key => $term)
{
//If the current term is the prefered term, move it to the front of the array
if($term->term_id === (int) $pref_id)
{
//Remove the term from the array
unset($terms[$key]);
//Place in the front of the array
array_unshift($terms, $term);
//And, now we're done
break;
}
}
//Return the array
return $terms;
}
/**
* This function overrides Breadcrumb NavXT's default term selection with the prefered term
*
* @param WP_Term $term The WP_Term object that Breadcrumb NavXT picked as the term for the post's hierarchy
* @param int $post_id The ID of the post in question
* @param string $post_type The post type of the post ID passed in
*
* @return WP_Term|bool The new term for Breadcrumb NavXT to use for the post's hierarchy
*/
function pick_bcn_term($term, $post_id, $post_type)
{
if($term)
{
//Get the prefered taxonomy term for the post here
$pref_id = get_post_meta($post_id, $this->unique_prefix . '_' . $term->taxonomy . '_prefered', true);
//Only can do something if the prefered taxonomy term ID was set
if($pref_id != false && $pref_id !== '')
{
$pref_term = get_term($pref_id, $term->taxonomy);
//Only use the prefered term if it is actually a term
if($pref_term instanceof WP_Term)
{
return $pref_term;
}
}
}
return $term;
}
}
$mtekk_order_bender = new mtekk_order_bender();