-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanify.php
More file actions
149 lines (117 loc) · 2.98 KB
/
cleanify.php
File metadata and controls
149 lines (117 loc) · 2.98 KB
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
<?php
/**
* Plugin Name: Cleanify
* Description: Remove slug from custom post types permalinks
* Author: Fredrik Forsmo
* Author URI: http://forsmo.me
* Version: 1.0.0
* Plugin URI: https://github.com/frozzare/cleanify
*/
final class Cleanify {
/**
* The Cleanify class instance.
*
* @var object
*/
private static $instance;
/**
* Get the Cleanify class instance.
*
* @return object
*/
public static function instance() {
if ( !isset( self::$instance ) ) {
self::$instance = new self;
self::$instance->setup_actions();
self::$instance->setup_filters();
}
return self::$instance;
}
/**
* Get post types.
*
* @return array
*/
private function get_post_types() {
$post_types = ['post'];
$cpts = (array) apply_filters( 'cleanify/cpts', [] );
foreach ( $cpts as $cpt ) {
if ( is_string( $cpt ) ) {
$post_types[] = $cpt;
}
}
return array_merge( $post_types, ['page'] );
}
/**
* Setup all actions.
*/
private function setup_actions() {
add_action( 'pre_get_posts', [$this, 'parse_request'] );
add_action( 'template_redirect', [$this, 'redirect'] );
}
/**
* Setup all filters.
*/
private function setup_filters() {
add_filter( 'post_type_link', [$this, 'remove_post_type_slug'], 10, 3 );
}
/**
* Redirect to right permalink if the database
* permalink don't match the server request uri.
*/
public function redirect() {
global $wp_query;
if ( ! isset( $wp_query->post ) ) {
return;
}
$post_types = $this->get_post_types();
if ( ! in_array( $wp_query->post->post_type, $post_types ) ) {
return;
}
$permalink = get_permalink( $wp_query->post->ID );
$parts = parse_url( $permalink );
$req_uri = $_SERVER['REQUEST_URI'];
if ( $parts['path'] != $req_uri ) {
wp_safe_redirect( $permalink );
exit;
}
}
/**
* Remove post type slug.
*
* @param string $post_type
* @param object $post
* @param string $leavename
*
* @return string
*/
public function remove_post_type_slug( $post_link, $post, $leavename ) {
if ( empty( $post ) || $post->post_type === 'post' || $post->post_type === 'page' || $post->post_status != 'publish' ) {
return $post_link;
}
$post_types = $this->get_post_types();
if ( ! in_array( $post->post_type, $post_types ) ) {
return $post_link;
}
return str_replace( '/' . $post->post_type . '/', '/', $post_link );
}
/**
* Parse request and replace post types.
*
* @param object $query
*/
public function parse_request( $query ) {
if ( ! $query->is_main_query() ) {
return;
}
if ( count( $query->query ) !== 2 || ! isset( $query->query['page'] ) ) {
return;
}
if ( ! empty( $query->query['name'] ) ) {
$query->set( 'post_type', $this->get_post_types() );
}
}
}
add_action( 'plugins_loaded', function () {
return Cleanify::instance();
} );