| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @param string $plugin |
| 5 |
* @return array<string, mixed> |
| 6 |
*/ |
| 7 |
function red_get_plugin_data( string $plugin ): array { |
| 8 |
if ( ! function_exists( 'get_plugin_data' ) ) { |
| 9 |
include_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 10 |
} |
| 11 |
|
| 12 |
return get_plugin_data( $plugin ); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* @param bool $full |
| 17 |
* @return array<string, string>|list<string> |
| 18 |
*/ |
| 19 |
function red_get_post_types( bool $full = true ): array { |
| 20 |
$types = get_post_types( [ 'public' => true ], 'objects' ); |
| 21 |
$types[] = (object) array( |
| 22 |
'name' => 'trash', |
| 23 |
'label' => __( 'Trash', 'default' ), |
| 24 |
); |
| 25 |
|
| 26 |
$post_types = []; |
| 27 |
foreach ( $types as $type ) { |
| 28 |
if ( $type->name === 'attachment' ) { |
| 29 |
continue; |
| 30 |
} |
| 31 |
|
| 32 |
if ( $full && strlen( $type->label ) > 0 ) { |
| 33 |
$post_types[ $type->name ] = $type->label; |
| 34 |
} else { |
| 35 |
$post_types[] = $type->name; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
return apply_filters( 'redirection_post_types', $post_types ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Set options |
| 44 |
* |
| 45 |
* @param array<string, mixed> $settings Partial settings. |
| 46 |
* @return array<string, mixed> |
| 47 |
*/ |
| 48 |
function red_set_options( array $settings = [] ) { |
| 49 |
return Red_Options::save( $settings ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* @param string $url |
| 54 |
* @return array<string, mixed>|false |
| 55 |
*/ |
| 56 |
function red_parse_url( string $url ) { |
| 57 |
$domain = filter_var( $url, FILTER_SANITIZE_URL ); |
| 58 |
if ( $domain === false ) { |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
if ( substr( $domain, 0, 5 ) !== 'http:' && substr( $domain, 0, 6 ) !== 'https:' ) { |
| 63 |
$domain = ( is_ssl() ? 'https://' : 'http://' ) . $domain; |
| 64 |
} |
| 65 |
|
| 66 |
return wp_parse_url( $domain ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @param string $domain |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
function red_parse_domain_only( string $domain ): string { |
| 74 |
$parsed = red_parse_url( $domain ); |
| 75 |
|
| 76 |
if ( $parsed !== false && isset( $parsed['host'] ) ) { |
| 77 |
return $parsed['host']; |
| 78 |
} |
| 79 |
|
| 80 |
return ''; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @param string $domain |
| 85 |
* @return string |
| 86 |
*/ |
| 87 |
function red_parse_domain_path( string $domain ): string { |
| 88 |
$parsed = red_parse_url( $domain ); |
| 89 |
|
| 90 |
if ( $parsed !== false && isset( $parsed['host'] ) ) { |
| 91 |
return $parsed['scheme'] . '://' . $parsed['host'] . ( isset( $parsed['path'] ) ? $parsed['path'] : '' ); |
| 92 |
} |
| 93 |
|
| 94 |
return ''; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Have redirects been disabled? |
| 99 |
* |
| 100 |
* @return boolean |
| 101 |
*/ |
| 102 |
function red_is_disabled(): bool { |
| 103 |
return ( defined( 'REDIRECTION_DISABLE' ) && REDIRECTION_DISABLE ) || file_exists( __DIR__ . '/redirection-disable.txt' ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Get Redirection options |
| 108 |
* |
| 109 |
* @return array<string, mixed> |
| 110 |
*/ |
| 111 |
function red_get_options() { |
| 112 |
return Red_Options::get(); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get the current REST API |
| 117 |
* |
| 118 |
* @param int|false $type Override with a specific API type. |
| 119 |
* @return string |
| 120 |
*/ |
| 121 |
function red_get_rest_api( $type = false ): string { |
| 122 |
if ( $type === false ) { |
| 123 |
$options = Red_Options::get(); |
| 124 |
$type = $options['rest_api']; |
| 125 |
} |
| 126 |
|
| 127 |
$url = get_rest_url(); // Red_Options::API_JSON |
| 128 |
|
| 129 |
if ( $type === Red_Options::API_JSON_INDEX ) { |
| 130 |
$url = home_url( '/?rest_route=/' ); |
| 131 |
} elseif ( $type === Red_Options::API_JSON_RELATIVE ) { |
| 132 |
$relative = (string) wp_parse_url( $url, PHP_URL_PATH ); |
| 133 |
|
| 134 |
if ( $relative !== '' ) { |
| 135 |
$url = $relative; |
| 136 |
} |
| 137 |
|
| 138 |
if ( $url === '/index.php' ) { |
| 139 |
// No permalinks. Default to normal REST API |
| 140 |
$url = home_url( '/?rest_route=/' ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return $url; |
| 145 |
} |
| 146 |
|