-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathremove-http.php
147 lines (137 loc) · 7.47 KB
/
remove-http.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
<?php
/**
* Plugin Name: Remove HTTP
* Plugin URI: https://wordpress.org/plugins/remove-http/
* Description: Removes both HTTP and HTTPS protocols from links.
* Version: 2.2.0
* Author: Fact Maven
* Author URI: https://www.factmaven.com/
* License: GPLv3
*/
// If accessed directly, exit
if ( !defined( 'ABSPATH' ) ) exit;
class Fact_Maven_Remove_HTTP {
private $option, $plugin;
public function __construct() {
// Call the core Plugin API
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// Get plugin's metadata
$this->plugin = get_plugin_data( __FILE__ );
// If the plugin version is lower or not defined, remove plugin options
if ( ( get_option( 'factmaven_rhttp_version' ) < $this->plugin['Version'] ) || !get_option( 'factmaven_rhttp_version' ) ) {
// Remove options with the prefix "factmaven_rhttp_"
foreach ( wp_load_alloptions() as $option => $value ) {
if ( strpos( $option, 'factmaven_rhttp' ) === 0 ) delete_option( $option );
}
// Add options for new plugin version
update_option( 'factmaven_rhttp_version', $this->plugin['Version'] );
}
// Get plugin options
$this->option = get_option( 'factmaven_rhttp' );
// Set default options
if ( empty( $this->option['format'] ) ) {
$this->option['format'] = 'protocol-relative';
}
if ( empty( $this->option['ignore'] ) ) {
$this->option['ignore'] = '';
}
if ( empty( $this->option['external'] ) ) {
$this->option['external'] = '0';
}
// Add link to plugin settings
add_filter( 'plugin_action_links', array( $this, 'settings_link' ), 10, 2 );
// Add custom settings field
add_filter( 'admin_init', array( $this, 'settings_field' ), 10, 1 );
// Relocate settings field using jQuery
add_action( 'admin_footer', array( $this, 'settings_location' ), 10, 1 );
// Protocol is only removed on frontend, ignore Admin Dashboard
if ( !is_admin() && ( defined( 'DOING_AJAX' ) || 'DOING_AJAX' ) ) {
add_action( 'wp_loaded', array( $this, 'protocol_relative' ) , PHP_INT_MAX, 1 );
}
}
public function settings_link( $links, $file ) {
// Display settings link
if ( $file == plugin_basename( __FILE__ ) && current_user_can( 'manage_options' ) ) {
array_unshift( $links, '<a href="options-general.php#home">Settings</a>' );
}
// Return the settings link
return $links;
}
public function settings_field() {
// Register the settings
register_setting( 'general', 'factmaven_rhttp' );
// Add settings field
add_settings_field( 'protocol_relative', 'URL Format', array( $this, 'options' ), 'general' );
}
public function settings_location() {
// Insert the settings field after the 'Site Address (URL)'
?> <script type="text/javascript">
jQuery( '#format-description' ).closest( 'tr' ).insertAfter( jQuery( '#home' ).closest( 'tr' ) );
</script> <?php
}
public function options() {
// Display plugin settings field
?> <fieldset>
<label><input type="radio" name="factmaven_rhttp[format]" value="protocol-relative" <?php checked( 'protocol-relative', $this->option['format'] ); ?> checked="checked"> <span class="date-time-text">Protocol-Relative</span><code><?php echo preg_replace( '/^https?:/', '', home_url() ); ?>/sample-post/</code></label><br>
<label><input type="radio" name="factmaven_rhttp[format]" value="relative" <?php checked( 'relative', $this->option['format'] ); ?>> <span class="date-time-text">Relative</span><code>/sample-post/</code></label>
<p class="description" id="format-description">Relative format will only affect internal links.</p>
<br>
<!-- <label>Exclude the following domains and URLs, internal or external. Enter each link on a new line.</label>
<p><textarea name="factmaven_rhttp[ignore]" rows="5" cols="80" class="code" placeholder="<?php echo preg_replace( '/^https?:\/\//', '', home_url() ); ?>
<?php echo home_url(); ?>/sample-post"><?php echo $this->option['ignore']; ?></textarea></p> -->
<label><input name="factmaven_rhttp[external]" type="checkbox" value="1" <?php checked( '1', $this->option['external'] ); ?>>Ignore all external links</label>
<!-- <p class="description" id="external-description">This will override any external URLs entered in the textbox above.</p> -->
</fieldset> <?php
}
public function protocol_relative() {
// Enable output buffering
ob_start( function( $links ) {
// Check for 'Content-Type' headers only
$content_type = NULL;
foreach ( headers_list() as $header ) {
if ( strpos( strtolower( $header ), 'content-type:' ) === 0 ) {
$pieces = explode( ':', strtolower( $header ) );
$content_type = trim( $pieces[1] );
break;
}
}
// If the content-type is 'NULL' or 'text/html', apply rewrite
if ( is_null( $content_type ) || substr( $content_type, 0, 9 ) === 'text/html' ) {
// Get domain without protocol
$website = preg_replace( '/^https?:\/\//', '', home_url() );
$website = preg_replace( '/\/.*$/', '', $website );
// Ignore input tags link tags with 'rel=canonical'
$exceptions = '<(?:input\b[^<]*\bvalue=[\"\']https?:\/\/|link\b[^<]*?\brel=[\'\"]canonical[\'\"][^<]*?>)(*SKIP)(*F)';
// If 'Ignore external links' is selected, only apply changes to internal links
if ( $this->option['external'] == 1 ) {
if ( $this->option['format'] == 'relative' ) $links = preg_replace( '/' . $exceptions . '|https?:\/\/' . $website . '/', '', $links );
else $links = preg_replace( '/' . $exceptions . '|https?:\/\/' . $website . '/', '//$1' . $website, $links );
}
// If 'Ignore external links' is not selected and ignore textbox is not empty, only apply to non-excluded domains
/*elseif ( $this->option['external'] != 1 && $this->$option['ignore'] == '' ) {
$ignore_list = preg_split( '/\r\n|[\r\n]/', $option['ignore'] );
foreach ( $ignore_list as $ignore_url ) {
$parse_url = parse_url( $ignore_url );
if ( $parse_url['path'] != '' ) {
$host = $parse_url['path'];
}
else {
$host = $parse_url['host'];
}
if ( strpos( $url, $host ) !== false) {
return $url;
}
}
}*/
// Else, apply to all internal and external links
else {
if ( $this->option['format'] == 'relative' ) $links = preg_replace( '/' . $exceptions . '|https?:\/\/' . $website . '/', '', $links );
else $links = preg_replace( '/' . $exceptions . '|https?:\/\//', '//', $links );
}
}
// Return protocol relative links
return $links;
} );
}
}
// Instantiate the class
new Fact_Maven_Remove_HTTP();