-
Notifications
You must be signed in to change notification settings - Fork 0
/
ictuwp-plugin-thema-taxonomie.php
354 lines (271 loc) · 10.3 KB
/
ictuwp-plugin-thema-taxonomie.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
/**
* @link https://github.com/ICTU/ictuwp-plugin-thema-taxonomie
* @package ictuwp-plugin-thema-taxonomie
*
* @wordpress-plugin
* Plugin Name: ICTU / Gebruiker Centraal / Thema taxonomie
* Plugin URI: https://github.com/ICTU/ictuwp-plugin-thema-taxonomie
* Description: Plugin voor het aanmaken van de 'thema'-taxonomie
* Version: 2.2.0
* Version description: Return no Thema tax terms if ALL available are coupled to post
* Author: Paul van Buuren
* Author URI: https://github.com/ICTU/ictuwp-plugin-thema-taxonomie/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: gctheme
* Domain Path: /languages
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
//========================================================================================================
// Dutch slug for taxonomy
$slug = 'thema';
if ( get_bloginfo( 'language' ) !== 'nl-NL' ) {
// non Dutch slug for taxonomy
$slug = 'thema'; // TODO: see bug GC-575
}
defined( 'GC_THEMA_TAX' ) or define( 'GC_THEMA_TAX', $slug );
defined( 'GC_THEMA_TAX_OVERVIEW_TEMPLATE' ) or define( 'GC_THEMA_TAX_OVERVIEW_TEMPLATE', 'template-overview-themas.php' );
defined( 'GC_THEMA_TAX_DETAIL_TEMPLATE' ) or define( 'GC_THEMA_TAX_DETAIL_TEMPLATE', 'template-thema-detail.php' );
//========================================================================================================
// only this plugin should activate the GC_THEMA_TAX taxonomy
if ( ! taxonomy_exists( GC_THEMA_TAX ) ) {
add_action( 'plugins_loaded', array( 'ICTU_GC_thema_taxonomy', 'init' ), 10 );
}
//========================================================================================================
/**
* Begins execution of the plugin.
*
* Since everything within the plugin is registered via hooks,
* then kicking off the plugin from this point in the file does
* not affect the page life cycle.
*
* @since 0.0.1
*/
if ( ! class_exists( 'ICTU_GC_thema_taxonomy' ) ) :
class ICTU_GC_thema_taxonomy {
/** ----------------------------------------------------------------------------------------------------
* Init
*/
public static function init() {
$newtaxonomy = new self();
}
/** ----------------------------------------------------------------------------------------------------
* Constructor
*/
public function __construct() {
$this->fn_ictu_thema_setup_actions();
}
/** ----------------------------------------------------------------------------------------------------
* Hook this plugins functions into WordPress.
* Use priority = 20, to ensure that the taxonomy is registered for post types from other plugins,
* such as the podcasts plugin (seriously-simple-podcasting)
*/
private function fn_ictu_thema_setup_actions() {
add_action( 'init', array( $this, 'fn_ictu_thema_register_taxonomy' ), 20 );
// add page templates
add_filter( 'template_include', array( $this, 'fn_ictu_thema_append_template_locations' ) );
// filter the breadcrumbs
add_filter( 'wpseo_breadcrumb_links', array( $this, 'fn_ictu_thema_yoast_filter_breadcrumb' ) );
// check if the term has detail page attached
add_action( 'template_redirect', array( $this, 'fn_ictu_thema_check_redirect' ) );
// Hide the `metabox_posts_category` field for 'community' related posts
// (because we already filter on Community tax term)
add_filter( 'acf/prepare_field/name=metabox_posts_category', function( $field ) {
global $post;
if ( ! empty( $post ) ) {
// Check if we're currently editing a post
// with a Thema template.
// If so: *disable* the category selection (return false)
// because we use the Thema Tax...
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
if ( $page_template === GC_THEMA_TAX_DETAIL_TEMPLATE ) {
return false;
}
}
return $field;
} );
}
public function fn_ictu_thema_check_redirect() {
if ( ! function_exists( 'get_field' ) ) {
// we can't check if ACF is not active
return;
}
if ( is_tax( GC_THEMA_TAX ) ) {
// check if the current term has a value for 'thema_taxonomy_page'
$pageid = get_field( 'thema_taxonomy_page', GC_THEMA_TAX . '_' . get_queried_object()->term_id );
$page = get_post( $pageid );
if ( $page ) {
// cool, a page is selected for this term
// But is the page published?
if ( 'publish' === $page->post_status ) {
// good, it is published
// let's redirect to that page
wp_safe_redirect( get_permalink( $page->ID ) );
exit;
} else {
// bad, we only want published pages
$aargh = 'No published page attached to this thema';
if ( current_user_can( 'editor' ) ) {
$editlink = get_edit_term_link( get_queried_object()->term_id, get_queried_object()->taxonomy );
$aargh .= '<a href="' . $editlink . '">Please choose a published page for this term.</a>';
}
die( $aargh );
}
} else {
// no page is selected for this term
// for now, do nothing
}
}
}
/** ----------------------------------------------------------------------------------------------------
* Do actually register the post types we need
*
* @return void
*/
public function fn_ictu_thema_register_taxonomy() {
require_once plugin_dir_path( __FILE__ ) . 'includes/register-thema-taxonomy.php';
}
/**
* Checks if the template is assigned to the page
*
* @in: $template (string)
*
* @return: $template (string)
*
*/
public function fn_ictu_thema_append_template_locations( $template ) {
// Get global post
global $post;
$file = '';
$pluginpath = plugin_dir_path( __FILE__ );
if ( $post ) {
// Do we have a post of whatever kind at hand?
// Get template name; this will only work for pages, obviously
$page_template = get_post_meta( $post->ID, '_wp_page_template', true );
if ( ( GC_THEMA_TAX_OVERVIEW_TEMPLATE === $page_template ) || ( GC_THEMA_TAX_DETAIL_TEMPLATE === $page_template ) ) {
// these names are added by this plugin, so we return
// the actual file path for this template
$file = $pluginpath . $page_template;
} else {
// exit with the already set template
return $template;
}
} elseif ( is_tax( GC_THEMA_TAX ) ) {
// Are we dealing with a term for the GC_THEMA_TAX taxonomy?
$file = $pluginpath . 'taxonomy-thema.php';
} else {
// Not a post, not a term, return the template
return $template;
}
// Just to be safe, check if the file actually exists
if ( $file && file_exists( $file ) ) {
return $file;
} else {
// o dear, who deleted the file?
echo $file;
}
// If all else fails, return template
return $template;
}
/**
* Filter the Yoast SEO breadcrumb
*
* @in: $links (array)
*
* @return: $links (array)
*
*/
public function fn_ictu_thema_yoast_filter_breadcrumb( $links ) {
if ( is_tax( GC_THEMA_TAX ) ) {
// this filter is only for terms in GC_THEMA_TAX taxonomy
$term = get_queried_object();
// Append taxonomy if 1st-level child term only
// old: Home > Term
// new: Home > Taxonomy > Term
if ( ! $term->parent ) {
$thema_overview_page_id = $this->fn_ictu_thema_get_thema_overview_page();
if ( $thema_overview_page_id ) {
// Use this page as GC_THEMA_TAX term parent in the breadcrumb
// If not available,
// - [1] Do not display root
// - [2] OR fall back to Taxonomy Rewrite
$taxonomy_link = array(
'url' => get_permalink( $thema_overview_page_id ),
'text' => get_the_title( $thema_overview_page_id )
);
array_splice( $links, - 1, 0, array( $taxonomy_link ) );
} else {
// [1] .. do nothing...
// [2] OR .. use Taxonomy Rewrite as root
// $taxonomy = get_taxonomy( GC_THEMA_TAX );
// $taxonomy_link = [
// 'url' => get_home_url() . '/' . $taxonomy->rewrite['slug'],
// 'text' => $taxonomy->labels->archives,
// 'term_id' => get_queried_object_id(),
// ];
// array_splice( $links, -1, 0, [$taxonomy_link] );
}
}
}
return $links;
}
/**
* Retrieve a page that is the GC_THEMA_TAX overview page. This
* page shows all available GC_THEMA_TAX terms
*
* @in: $args (array)
*
* @return: $overview_page_id (integer)
*
*/
private function fn_ictu_thema_get_thema_overview_page( $args = array() ) {
$return = 0;
// TODO: discuss if we need to make this page a site setting
// there is no technical way to limit the number of pages with
// template GC_THEMA_TAX_OVERVIEW_TEMPLATE, so the page we find may not be
// the exact desired page for in the breadcrumb.
//
// Try and find 1 Page
// with the GC_THEMA_TAX_OVERVIEW_TEMPLATE template...
$page_template_query_args = array(
'number' => 1,
'sort_column' => 'post_date',
'sort_order' => 'DESC',
'meta_key' => '_wp_page_template',
'meta_value' => GC_THEMA_TAX_OVERVIEW_TEMPLATE
);
$overview_page = get_pages( $page_template_query_args );
if ( $overview_page && isset( $overview_page[0]->ID ) ) {
$return = $overview_page[0]->ID;
}
return $return;
}
}
endif;
//========================================================================================================
/**
* Load plugin textdomain.
* only load translations if we can safely assume the taxonomy is active
*/
add_action( 'init', 'fn_ictu_thema_load_plugin_textdomain' );
function fn_ictu_thema_load_plugin_textdomain() {
load_plugin_textdomain( 'gctheme', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
//========================================================================================================
/**
* Returns array of allowed page templates
*
* @return array with extra templates
*/
function fn_ictu_thema_add_templates() {
$return_array = array(
GC_THEMA_TAX_OVERVIEW_TEMPLATE => _x( '[Thema] alle thema\'s', 'label page template', 'gctheme' ),
GC_THEMA_TAX_DETAIL_TEMPLATE => _x( '[Thema] thema-detail', 'label page template', 'gctheme' )
);
return $return_array;
}
//========================================================================================================