-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathredirection-settings.php
More file actions
145 lines (121 loc) · 3.08 KB
/
redirection-settings.php
File metadata and controls
145 lines (121 loc) · 3.08 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
<?php
/**
* @param string $plugin
* @return array<string, mixed>
*/
function red_get_plugin_data( string $plugin ): array {
if ( ! function_exists( 'get_plugin_data' ) ) {
include_once ABSPATH . '/wp-admin/includes/plugin.php';
}
return get_plugin_data( $plugin );
}
/**
* @param bool $full
* @return array<string, string>|list<string>
*/
function red_get_post_types( bool $full = true ): array {
$types = get_post_types( [ 'public' => true ], 'objects' );
$types[] = (object) array(
'name' => 'trash',
'label' => __( 'Trash', 'default' ),
);
$post_types = [];
foreach ( $types as $type ) {
if ( $type->name === 'attachment' ) {
continue;
}
if ( $full && strlen( $type->label ) > 0 ) {
$post_types[ $type->name ] = $type->label;
} else {
$post_types[] = $type->name;
}
}
return apply_filters( 'redirection_post_types', $post_types, $full );
}
/**
* Set options
*
* @param array<string, mixed> $settings Partial settings.
* @return array<string, mixed>
*/
function red_set_options( array $settings = [] ) {
return Red_Options::save( $settings );
}
/**
* @param string $url
* @return array<string, mixed>|false
*/
function red_parse_url( string $url ) {
$domain = filter_var( $url, FILTER_SANITIZE_URL );
if ( $domain === false ) {
return false;
}
if ( substr( $domain, 0, 5 ) !== 'http:' && substr( $domain, 0, 6 ) !== 'https:' ) {
$domain = ( is_ssl() ? 'https://' : 'http://' ) . $domain;
}
return wp_parse_url( $domain );
}
/**
* @param string $domain
* @return string
*/
function red_parse_domain_only( string $domain ): string {
$parsed = red_parse_url( $domain );
if ( $parsed !== false && isset( $parsed['host'] ) ) {
return $parsed['host'];
}
return '';
}
/**
* @param string $domain
* @return string
*/
function red_parse_domain_path( string $domain ): string {
$parsed = red_parse_url( $domain );
if ( $parsed !== false && isset( $parsed['host'] ) ) {
return $parsed['scheme'] . '://' . $parsed['host'] . ( isset( $parsed['path'] ) ? $parsed['path'] : '' );
}
return '';
}
/**
* Have redirects been disabled?
*
* @return boolean
*/
function red_is_disabled(): bool {
return ( defined( 'REDIRECTION_DISABLE' ) && REDIRECTION_DISABLE ) || file_exists( __DIR__ . '/redirection-disable.txt' );
}
/**
* Get Redirection options
*
* @return array<string, mixed>
*/
function red_get_options() {
return Red_Options::get();
}
/**
* Get the current REST API
*
* @param int|false $type Override with a specific API type.
* @return string
*/
function red_get_rest_api( $type = false ): string {
if ( $type === false ) {
$options = Red_Options::get();
$type = $options['rest_api'];
}
$url = get_rest_url(); // Red_Options::API_JSON
if ( $type === Red_Options::API_JSON_INDEX ) {
$url = home_url( '/?rest_route=/' );
} elseif ( $type === Red_Options::API_JSON_RELATIVE ) {
$relative = (string) wp_parse_url( $url, PHP_URL_PATH );
if ( $relative !== '' ) {
$url = $relative;
}
if ( $url === '/index.php' ) {
// No permalinks. Default to normal REST API
$url = home_url( '/?rest_route=/' );
}
}
return $url;
}