forked from WebberZone/crp-taxonomy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrp-taxonomy.php
334 lines (270 loc) · 9.36 KB
/
crp-taxonomy.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<?php
/**
* Contextual Related Posts Taxonomy Extender
*
* CRP Taxomy Extender adds extra options to CRP settings that allow you to
* restrict posts to the category or tag of the current post.
*
* @package CRP_Taxonomy
* @author Ajay D'Souza <[email protected]>
* @license GPL-2.0+
* @link http://ajaydsouza.com
* @copyright 2014-2015 Ajay D'Souza
*
* @wordpress-plugin
* Plugin Name: Contextual Related Posts Taxonomy Extender
* Plugin URI: http://ajaydsouza.com/wordpress/plugins/crp-taxonomy/
* Description: Adds new settings to Contextual Related Posts that allows you to restrict posts to the category or tag of the current post
* Version: 1.1.0
* Author: Ajay D'Souza
* Author URI: http://ajaydsouza.com
* Text Domain: crp-taxonomy
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Domain Path: /languages
* GitHub Plugin URI: https://github.com/ajaydsouza/crp-taxonomy/
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Initialises text domain for l10n.
*
* @since 1.0.0
*/
function crpt_lang_init() {
load_plugin_textdomain( 'crp-taxonomy' , false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
add_action( 'plugins_loaded', 'crpt_lang_init' );
/**
* Filter JOIN clause of CRP query to add taxonomy tables.
*
* @since 1.0.0
*
* @param mixed $join
* @return string Filtered CRP JOIN clause
*/
function crpt_crp_posts_join( $join ) {
global $wpdb, $crp_settings;
if ( $crp_settings['crpt_tag'] || $crp_settings['crpt_category'] || $crp_settings['crpt_taxes'] ) {
$sql = $join . "\n";
$sql .= "INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)\n";
$sql .= "INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)\n";
if ( $crp_settings['crpt_match_all'] ) {
$sql .= "INNER JOIN $wpdb->terms ON ($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)\n";
}
return $sql;
} else {
return $join;
}
}
add_filter( 'crp_posts_join', 'crpt_crp_posts_join' );
/**
* Filter WHERE clause of CRP query to limit posts only to category of current post.
*
* @since 1.0.0
*
* @param mixed $where
* @return string Filtered CRP WHERE clause
*/
function crpt_crp_posts_where( $where ) {
global $wpdb, $post, $crp_settings;
$term_ids = $category_ids = $tag_ids = $taxonomies = array();
if ( !$crp_settings['crpt_tag'] && !$crp_settings['crpt_category'] && !$crp_settings['crpt_taxes'] ) {
return $where;
}
// Temp variable used in crpt_crp_posts_having()
$crp_settings['crpt_taxonomy_count'] = 0;
if ( $crp_settings['crpt_category'] ) {
$taxonomies[] = 'category';
++$crp_settings['crpt_taxonomy_count'];
}
if ( $crp_settings['crpt_tag'] ) {
$taxonomies[] = 'post_tag';
++$crp_settings['crpt_taxonomy_count'];
}
if ( $crp_settings['crpt_taxes'] ) {
$crpt_taxes = explode( ',', $crp_settings['crpt_taxes'] );
$crp_settings['crpt_taxonomy_count'] += count( $crpt_taxes );
$taxonomies = array_merge( $taxonomies, $crpt_taxes );
}
// Get the terms for the current post
$terms = wp_get_object_terms( $post->ID, $taxonomies );
if ( is_wp_error( $terms ) || empty( $terms ) ) {
$crp_settings['crpt_taxonomy_count'] = 0;
return $where;
} else {
$sql = '';
if ( $crp_settings['crpt_match_all'] ) {
// Limit to posts matching all current taxonomy terms
$term_strings = array();
$selected_taxonomy_slugs = explode( ',', $crp_settings['crpt_taxes'] );
if ( count( $selected_taxonomy_slugs ) ) {
// Find the matching selected taxonomies
// Then add the term_id for that tax to the array
foreach ( $terms as $term ) {
if ( in_array( $term->taxonomy, $selected_taxonomy_slugs ) ) {
$term_strings[] = $wpdb->prepare( '%s', $term->taxonomy . '/' . $term->term_id );
}
}
}
// This SQL will match all posts that have the specified term_id in a particular taxonomy by converting
// the data into a string and matching against that. Now this does return nearly the same results
// as if crpt_match_all was false, however a HAVING statement added later reduces the dataset down
// to only posts that match at least 1 term in each the taxonomy.
// Credit for solution: http://wordpress.stackexchange.com/questions/8503/optimize-multiple-taxonomy-term-mysql-query
$term_count = count( $term_strings );
if ( $term_count ) {
$sql .= "AND CONCAT($wpdb->term_taxonomy.taxonomy, '/', $wpdb->terms.term_id) IN (";
$sql .= implode( ',', $term_strings );
$sql .= ')';
} else {
$crp_settings['crpt_taxonomy_count'] = 0;
}
} else {
// Limit to posts matching any current taxonomy term
$term_ids = array_unique( wp_list_pluck( $terms, 'term_id' ) );
if ( count( $term_ids ) ) {
$tax_ids = implode( ',', $term_ids );
$sql .= "AND $wpdb->term_taxonomy.term_id IN ($tax_ids)";
}
}
return $where . ' ' . $sql;
}
}
add_filter( 'crp_posts_where', 'crpt_crp_posts_where' );
/**
* Filter GROUP BY clause of CRP query.
*
* @since 1.1.1
*
* @param mixed $groupby
* @return string Filtered CRP GROUP BY clause
*/
function crpt_crp_posts_groupby( $groupby ) {
global $wpdb, $crp_settings;
if ( $crp_settings['crpt_match_all'] && ( $crp_settings['crpt_tag'] || $crp_settings['crpt_category'] || $crp_settings['crpt_taxes'] ) ) {
$groupby .= " $wpdb->posts.ID";
}
return $groupby;
}
add_filter( 'crp_posts_groupby', 'crpt_crp_posts_groupby' );
/**
* Filter HAVING clause of CRP query.
*
* @since 1.1.1
*
* @param mixed $having
* @return string Filtered CRP HAVING clause
*/
function crpt_crp_posts_having( $having ) {
global $wpdb, $crp_settings;
if ( $crp_settings['crpt_match_all'] && isset( $crp_settings['crpt_taxonomy_count'] ) && $crp_settings['crpt_taxonomy_count'] ) {
$having .= $wpdb->prepare( " COUNT(DISTINCT $wpdb->term_taxonomy.taxonomy) = %d", $crp_settings['crpt_taxonomy_count'] );
}
return $having;
}
add_filter( 'crp_posts_having', 'crpt_crp_posts_having', 10, 1);
/**
* Add options to CRP Settings array.
*
* @since 1.1.0
*
* @param array $crp_settings CRP Settings
* @return array Filtered array of CRP Settings
*/
function crpt_crp_posts_match( $match, $stuff, $postid ) {
global $crp_settings;
$post_type = get_post_type( $postid );
if ( $crp_settings['crpt_disable_contextual'] ) {
/* If post or page and we're not disabling custom post types */
if ( ( 'post' == $post_type || 'page' == $post_type ) && ( $crp_settings['crpt_disable_contextual_cpt'] ) ) {
return $match;
}
return ' ';
}
return $match;
}
add_filter( 'crp_posts_match', 'crpt_crp_posts_match', 10, 3 );
/**
* Add options to CRP Settings array.
*
* @since 1.0.0
*
* @param array $crp_settings CRP Settings
* @return array Filtered array of CRP Settings
*/
function crpt_crp_default_options( $crp_settings ) {
$more_options = array(
'crpt_tag' => false, // Restrict to current post's tags
'crpt_category' => false, // Restrict to current post's categories
'crpt_taxes' => '', // Restrict to custom taxonomies
'crpt_match_all' => false, // Require all or only one of the taxonomy terms to match
'crpt_disable_contextual' => false, // Disable contextual matching on all posts
'crpt_disable_contextual_cpt' => true, // Disable contextual matching on custom post types only
);
return array_merge( $more_options, $crp_settings );
}
add_filter( 'crp_default_options', 'crpt_crp_default_options' );
/**
* Fired for each blog when the plugin is activated.
*
* @since 1.0.0
*
* @param boolean $network_wide True if WPMU superadmin uses
* "Network Activate" action, false if
* WPMU is disabled or plugin is
* activated on an individual blog.
*/
function crpt_activate( $network_wide ) {
global $wpdb;
if ( is_multisite() && $network_wide ) {
// Get all blogs in the network and activate plugin on each one
$blog_ids = $wpdb->get_col( "
SELECT blog_id FROM $wpdb->blogs
WHERE archived = '0' AND spam = '0' AND deleted = '0'
" );
foreach ( $blog_ids as $blog_id ) {
switch_to_blog( $blog_id );
crpt_single_activate();
}
// Switch back to the current blog
restore_current_blog();
} else {
crp_single_activate();
}
}
register_activation_hook( __FILE__, 'crpt_activate' );
/**
* Fired for each blog when the plugin is activated.
*
* @since 1.0.0
*/
function crpt_single_activate() {
// Loop through crp_read_options to ensure that our options are added across the network
$crp_settings = crp_read_options();
}
/**
* Fired when a new site is activated with a WPMU environment.
*
* @since 1.0.0
*
* @param int $blog_id ID of the new blog.
*/
function crpt_activate_new_site( $blog_id ) {
if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
return;
}
switch_to_blog( $blog_id );
crp_single_activate();
restore_current_blog();
}
add_action( 'wpmu_new_blog', 'crpt_activate_new_site' );
/*----------------------------------------------------------------------------*
* Dashboard and Administrative Functionality
*----------------------------------------------------------------------------*/
if ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
require_once( plugin_dir_path( __FILE__ ) . 'admin/admin.php' );
} // End admin.inc
?>