forked from bigwing/post-type-archive-mapping
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpost-type-archive-mapping.php
573 lines (525 loc) · 16 KB
/
post-type-archive-mapping.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
<?php // phpcs:ignore
/*
Plugin Name: Custom Query Blocks
Plugin URI: https://mediaron.com/custom-query-blocks/
Description: Map your post type and term archives to a page and use our Gutenberg blocks to show posts or terms.
Author: MediaRon LLC
Version: 5.3.1
Requires at least: 5.5
Author URI: https://mediaron.com
Contributors: MediaRon LLC
Text Domain: post-type-archive-mapping
Domain Path: /languages
License: GPL v2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Credit: Forked from https://github.com/bigwing/post-type-archive-mapping
Credit: Gutenberg block based on Atomic Blocks
Credit: Chris Logan for the initial idea.
Credit: Paal Joaquim for UX and Issue Triage.
*/
define( 'PTAM_VERSION', '5.3.1' );
define( 'PTAM_FILE', __FILE__ );
define( 'PTAM_SPONSORS_URL', 'https://github.com/sponsors/MediaRon' );
require_once 'autoloader.php';
use PTAM\Includes\Admin\Options as Options;
/**
* Main plugin class.
*/
class PostTypeArchiveMapping {
/**
* Holds the class instance.
*
* @var PostTypeArchiveMapping $instance
*/
private static $instance = null;
/**
* Holds the paged argument.
*
* @var int $paged
*/
private $paged = null;
/**
* Holds the paged reset argument.
*
* @var bool $paged_reset
*/
private $paged_reset = false;
/**
* Return an instance of the class
*
* Return an instance of the PostTypeArchiveMapping Class.
*
* @since 1.0.0
* @access public static
*
* @return PostTypeArchiveMapping class instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
} //end get_instance
/**
* Class constructor.
*
* Initialize plugin and load text domain for internationalization
*
* @since 1.0.0
* @access private
*/
private function __construct() {
add_action( 'init', array( $this, 'init' ), 9 );
load_plugin_textdomain( 'post-type-archive-mapping', false, basename( dirname( __FILE__ ) ) . '/languages' );
// Register scripts/styles for the plugin.
$this->enqueue = new PTAM\Includes\Enqueue();
$this->enqueue->run();
// Run if blocks are enabled.
if ( false === Options::is_blocks_disabled() ) {
// Register REST for the plugin.
$this->rest = new PTAM\Includes\Rest\Rest();
$this->rest->run();
// Register Custom Post Type Block.
$this->cpt_block_one = new PTAM\Includes\Blocks\Custom_Post_Types\Custom_Post_Types();
$this->cpt_block_one->run();
// Register Term Grid Block.
$this->term_grid = new PTAM\Includes\Blocks\Term_Grid\Terms();
$this->term_grid->run();
// Register Featured Post Block.
$this->featured_posts = new PTAM\Includes\Blocks\Featured_Posts\Posts();
$this->featured_posts->run();
// Gutenberg Helper which sets the block categories.
$this->gutenberg = new PTAM\Includes\Admin\Gutenberg();
$this->gutenberg->run();
}
/**
* Filter to disable archive mapping.
* This is useful if you want to use the blocks but not the archive mapping.
*
* @since 5.1.8
*/
$ptam_disabled = apply_filters( 'ptam_archive_mapping_disabled', false );
// Run if page columns are enabled.
if ( false === Options::is_page_columns_disabled() && false === Options::is_archive_mapping_disabled() && ! $ptam_disabled ) {
// Page columns.
$this->page_columns = new PTAM\Includes\Admin\Page_Columns();
$this->page_columns->run();
}
if ( ! $ptam_disabled ) {
// Yoast Compatibility.
$this->yoast = new PTAM\Includes\Yoast();
$this->yoast->run();
}
// Admin settings.
$this->admin_settings = new PTAM\Includes\Admin\Admin_Settings();
} //end constructor
/**
* Main plugin initialization
*
* Initialize admin menus, options,and scripts
*
* @since 1.0.0
* @access public
*
* @see __construct
*/
public function init() {
/**
* Filter to disable archive mapping.
* This is useful if you want to use the blocks but not the archive mapping.
*
* @since 5.1.8
*/
$ptam_disabled = apply_filters( 'ptam_archive_mapping_disabled', false );
// Check if archive mapping is disabled.
if ( false === Options::is_archive_mapping_disabled() && ! $ptam_disabled ) {
// Archive mapping settings.
add_action( 'admin_init', array( $this, 'init_admin_settings' ) );
add_action( 'pre_get_posts', array( $this, 'maybe_override_archive' ) );
// Output admin notices once when saving archive mapping.
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
// 404 page detection.
add_filter( 'template_include', array( $this, 'maybe_force_404_template' ), 1 );
}
} //end init
/**
* This is a catch-all. Any 404 error not caught will be directed here.
* If a 404 error is caught, will load the page template instead.
*
* @param string $template The regular template.
*
* @return string $template The updated template.
*/
public function maybe_force_404_template( $template ) {
if ( is_404() ) {
$page_id_404 = absint( get_option( 'post-type-archive-mapping-404', 0 ) );
if ( $page_id_404 > 0 ) {
$args = array(
'post_type' => 'page',
'page_id' => $page_id_404,
'post_status' => 'publish',
'posts_per_page' => 1,
);
/* I wise woman once told me to never use query_posts. Like NEVER. I had no choice here. */
query_posts( // phpcs:ignore
$args
);
return get_page_template();
}
}
return $template;
}
/**
* Add admin notices when things go wrong.
*
* @since 3.3.5
*/
public function admin_notices() {
// Check for any term errors.
if ( get_option( 'ptam_error_message', '' ) ) {
printf(
'<div class="notice error"><strong><p>%s</p></strong></div>',
esc_html( get_option( 'ptam_error_message', '' ) )
);
delete_option( 'ptam_error_message' );
}
}
/**
* Override an archive page based on passed query arguments.
*
* @param WP_Query $query The query to check.
*/
public function maybe_override_archive( $query ) {
if ( is_admin() ) {
return $query;
}
// Maybe Redirect.
if ( is_page() ) {
$object_id = get_queried_object_id();
$post_meta = get_post_meta( $object_id, '_post_type_mapped', true );
if ( $post_meta ) {
if ( $post_meta && ! get_query_var( 'redirected' ) ) {
wp_safe_redirect( get_post_type_archive_link( $post_meta ) );
exit;
}
} else {
if ( get_query_var( 'paged' ) ) {
$query->set( 'paged', get_query_var( 'paged' ) );
}
return;
}
}
// trigger this once after running the main query.
if ( true === $this->paged_reset ) {
$query->set( 'paged', $this->paged );
set_query_var( 'paged', $this->paged );
$this->paged_reset = false;
}
$post_types = get_option( 'post-type-archive-mapping', array() );
if ( empty( $post_types ) && is_admin() && ! is_tax() ) {
return;
}
// trigger this the first time to get the current page.
if ( is_null( $this->paged ) ) {
$this->paged = get_query_var( 'paged' );
}
if ( is_array( $post_types ) && ! empty( $post_types ) ) {
foreach ( $post_types as $post_type => $post_id ) {
if ( is_post_type_archive( $post_type ) && 'default' !== $post_id && $query->is_main_query() ) {
$post_id = absint( $post_id );
$post_id = apply_filters( 'wpml_object_id', $post_id, 'page', true );
$query->set( 'post_type', 'page' );
$query->set( 'page_id', $post_id );
$query->set( 'redirected', true );
$query->set( 'original_archive_type', 'page' );
$query->set( 'original_archive_id', $post_type );
$query->set( 'term_tax', '' );
$query->set( 'paged', $this->paged );
$query->is_archive = false;
$query->is_single = true;
$query->is_singular = true;
$query->is_post_type_archive = false;
$this->paged_reset = true;
}
}
}
if ( is_tax() || $query->is_category || $query->is_tag ) {
$post_id = get_term_meta( get_queried_object_id(), '_term_archive_mapping', true );
$term = get_queried_object();
if ( $post_id && 'default' !== $post_id ) {
$post_id = absint( $post_id );
$query->set( 'post_type', 'page' );
$query->set( 'page_id', $post_id );
$query->set( 'redirected', true );
$query->set( 'paged', $this->paged );
$query->set( 'original_archive_type', 'term' );
$query->set( 'original_archive_id', absint( $term->term_id ) );
$query->set( 'term_tax', sanitize_text_field( $term->taxonomy ) );
$query->is_page = true;
$query->is_archive = false;
$query->is_category = false;
$query->is_tag = false;
$query->is_tax = false;
$query->is_single = true;
$query->is_singular = true;
$query->is_post_type_archive = false;
$query->queried_object_id = $post_id;
$query->queried_object = get_post( $post_id, OBJECT );
$this->paged_reset = true;
}
}
}
/**
* Get an absolute path for a plugin asset.
*
* @param string $path The relative path to the asset.
*/
public static function get_plugin_dir( $path = '' ) {
$dir = rtrim( plugin_dir_path( __FILE__ ), '/' );
if ( ! empty( $path ) && is_string( $path ) ) {
$dir .= '/' . ltrim( $path, '/' );
}
return $dir;
}
/**
* Get an absolute path for a plugin asset.
*
* @param string $path The relative path to the asset.
*/
public static function get_plugin_url( $path = '' ) {
$dir = rtrim( plugin_dir_url( __FILE__ ), '/' );
if ( ! empty( $path ) && is_string( $path ) ) {
$dir .= '/' . ltrim( $path, '/' );
}
return $dir;
}
/**
* Save post meta if selected on the reading screen.
*
* @param array $args Post Type arguments.
*/
public function post_type_save( $args ) {
if ( ! is_array( $args ) ) {
return $args;
}
global $wpdb;
$query = "delete from {$wpdb->postmeta} where meta_key = '_post_type_mapped'";
$wpdb->query( $query ); // phpcs:ignore
foreach ( $args as $post_type => $page_id ) {
$maybe_mapped = get_post_meta( $page_id, '_term_mapped', true );
if ( $maybe_mapped ) {
update_option(
'ptam_error_message',
sprintf(
/* Translators: %s is the page title */
__( 'The page %s to map to a post type archive is already mapped to a term.', 'post-type-archive-mapping' ),
esc_html( get_the_title( $page_id ) )
)
);
unset( $args[ $post_type ] );
} else {
update_post_meta( $page_id, '_post_type_mapped', $post_type );
}
}
return $args;
}
/**
* Initialize options
*
* Initialize page settings, fields, and sections and their callbacks
*
* @since 1.0.0
* @access public
*
* @see init
*/
public function init_admin_settings() {
// Get taxonomies.
$taxonomies = get_taxonomies(
array(
'public' => true,
),
'objects'
);
foreach ( $taxonomies as $taxonomy ) {
add_action( "{$taxonomy->name}_edit_form", array( $this, 'map_term_interface' ) );
}
add_action( 'edit_term', array( $this, 'save_mapped_term' ) );
// Register post type mapping settings.
register_setting(
'reading',
'post-type-archive-mapping',
array(
'sanitize_callback' => array( $this, 'post_type_save' ),
)
);
register_setting(
'reading',
'post-type-archive-mapping-404',
array(
'sanitize_callback' => 'absint',
)
);
add_settings_section( 'post-type-archive-mapping', _x( 'Page/Item Mapping', 'plugin settings heading', 'post-type-archive-mapping' ), array( $this, 'settings_section' ), 'reading' );
add_settings_field(
'post-type-archive-mapping',
__( 'Post Type Archive Mapping', 'post-type-archive-mapping' ),
array( $this, 'add_settings_post_types' ),
'reading',
'post-type-archive-mapping'
);
add_settings_field(
'post-type-archive-mapping-404',
__( '404 Page', 'post-type-archive-mapping' ),
array( $this, 'add_settings_404_page' ),
'reading',
'post-type-archive-mapping'
);
}
/**
* Add post type options to Settings->Reading screen.
*
* @param array $args Post Type arguments.
*/
public function add_settings_post_types( $args ) {
$output = get_option( 'post-type-archive-mapping', array() );
$post_types = get_post_types(
array(
'public' => true,
'has_archive' => true,
)
);
if ( empty( $post_types ) ) {
?>
<p><?php esc_html_e( 'There are no post types to map.', 'post-type-archive-mapping' ); ?></p>
<?php
return;
}
foreach ( $post_types as $index => $post_type ) {
$selection = 'default';
if ( isset( $output[ $post_type ] ) ) {
$selection = $output[ $post_type ];
}
$post_type_label = $post_type;
$post_type_data = get_post_type_object( $post_type );
if ( isset( $post_type_data->label ) && ! empty( $post_type_data->label ) ) {
$post_type_label = $post_type_data->label;
}
?>
<div class="ptam-admin-reading-cpt">
<h3><?php esc_html_e( 'Post Type:', 'post-type-archive-mapping' ); ?> <?php echo esc_html( $post_type_label ); ?></h3>
<?php
wp_dropdown_pages(
array(
'selected' => esc_attr( $selection ),
'name' => esc_html( "post-type-archive-mapping[{$post_type}]" ),
'value_field' => 'ID',
'option_none_value' => 'default',
'show_option_none' => esc_html__( 'Default', 'post-type-archive-mapping' ),
)
);
?>
</div>
<?php
}
?>
<?php
}
/**
* Add 404 page options to Settings->Reading screen.
*
* @param array $args No idea what these are.
*/
public function add_settings_404_page( $args ) {
$page_id_404 = get_option( 'post-type-archive-mapping-404', 0 );
if ( ! $page_id_404 ) {
$page_id_404 = -1;
}
?>
<div class="ptam-admin-reading-cpt">
<p class="description"><?php esc_html_e( 'Please select a page to map to your 404 page.', 'post-type-archive-mapping' ); ?></p>
<?php
wp_dropdown_pages(
array(
'selected' => intval( $page_id_404 ),
'name' => 'post-type-archive-mapping-404',
'value_field' => 'ID',
'option_none_value' => 'default',
'show_option_none' => esc_html__( 'Default', 'post-type-archive-mapping' ),
)
);
?>
</div>
<?php
}
/**
* Map Term Archives to Posts Options.
*
* @param object $tag The term object.
* @param string $taxonomy The taxonomy.
*/
public function map_term_interface( $tag, $taxonomy = '' ) {
$post_id = get_term_meta( $tag->term_id, '_term_archive_mapping', true );
if ( ! $post_id ) {
$post_id = -1;
}
?>
<h2><?php esc_html_e( 'Archive Mapping', 'post-type-archive-mapping' ); ?></h2>
<?php
wp_dropdown_pages(
array(
'selected' => intval( $post_id ),
'name' => 'term_post_type',
'value_field' => 'ID',
'option_none_value' => 'default',
'show_option_none' => esc_html__( 'Default', 'post-type-archive-mapping' ),
)
);
?>
<p class="description"><?php esc_html_e( 'Map a term archive to a page.', 'post-type-archive-mapping' ); ?></p>
<?php
}
/**
* Map a saved term to a term ID.
*
* @param int $term_id The term ID to map.
*/
public function save_mapped_term( $term_id ) {
if ( current_user_can( 'edit_term', $term_id ) ) {
$maybe_post_id = filter_input( INPUT_POST, 'term_post_type' );
if ( 'default' === $maybe_post_id ) {
delete_post_meta( $maybe_post_id, '_term_mapped' );
delete_term_meta( $term_id, '_term_archive_mapping' );
} elseif ( $maybe_post_id ) {
update_post_meta( $maybe_post_id, '_term_mapped', $term_id );
update_term_meta( $term_id, '_term_archive_mapping', $maybe_post_id );
}
}
}
/**
* Output settings HTML
*
* Output any HTML required to go into a settings section
*
* @since 1.0.0
* @access public
*
* @see init_admin_settings
*/
public function settings_section() {
}
}
add_action(
'plugins_loaded',
function() {
PostTypeArchiveMapping::get_instance();
}
);
global $wp_embed;
add_filter( 'ptam_the_content', array( $wp_embed, 'run_shortcode' ), 8 );
add_filter( 'ptam_the_content', array( $wp_embed, 'autoembed' ), 8 );
add_filter( 'ptam_the_content', 'wptexturize' );
add_filter( 'ptam_the_content', 'convert_chars' );
add_filter( 'ptam_the_content', 'wpautop' );
add_filter( 'ptam_the_content', 'shortcode_unautop' );
add_filter( 'ptam_the_content', 'do_shortcode' );