-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol-enforcer.php
More file actions
151 lines (109 loc) · 4.13 KB
/
protocol-enforcer.php
File metadata and controls
151 lines (109 loc) · 4.13 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
150
151
<?php
/**
* Plugin Name: Protocol Enforcer
* Description: Plugin to allow user to specify by post, whether to force http or https.
* Author: KCPT
* Author URI: http://www.KCPT.org
* License: GPLv2 or later
* Version: 0.3
* Text Domain: protocolenforcer
*/
class KCPT_Protocol_Enforcer
{
public $slug = "kcpt-force-protocol";
public $url = false;
public function __construct()
{
add_action( 'add_meta_boxes', [ $this, 'metabox' ] );
add_action( 'save_post', [ $this, 'saveMetabox' ] );
add_action( 'template_redirect', [ $this, 'redirect' ] );
}
public function metabox()
{
add_meta_box( $this->slug, __( 'Force Protocol', 'protocolenforcer' ), [ $this, 'viewMetabox' ], null, 'side', 'high' );
}
public function redirect()
{
if (is_home() or is_category() or is_archive() or is_feed())
return;
global $post;
if ( ! isset( $post ) or ! isset( $post->ID ))
return;
$meta = get_post_custom( $post->ID );
$option = false;
$protocol = "http";
if (isset( $meta[ 'forceprotocol' ][ 0 ] ) and ! empty( $meta[ 'forceprotocol' ][ 0 ] )) {
$option = $meta[ 'forceprotocol' ][ 0 ];
}
if ( ! $option) return;
// var_dump( $GLOBALS ); die;
if (isset( $_SERVER[ 'HTTPS' ] ) and $_SERVER[ 'HTTPS' ] == "on")
$protocol = "https";
if (isset( $_SERVER[ 'REQUEST_SCHEME' ] ) and $_SERVER[ 'REQUEST_SCHEME' ] == "https")
$protocol = "https";
switch ( $option ) {
case 'http':
if ($protocol == "https") {
$URL = $option . '://' . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ];
$this->url = $URL;
wp_safe_redirect( $URL, 301 );
exit;
}
break;
case 'https':
if ($protocol == "http") {
$URL = $option . '://' . $_SERVER[ 'HTTP_HOST' ] . $_SERVER[ 'REQUEST_URI' ];
$this->url = $URL;
wp_safe_redirect( $URL, 301 );
exit;
}
break;
}
return;
}
public function saveMetabox( $postID )
{
global $post;
if ( ! isset( $_POST[ 'kcpt_metabox_protocol_nonce' ] ) || ! wp_verify_nonce( $_POST[ 'kcpt_metabox_protocol_nonce' ],
basename( __FILE__ ) )
) {
return $postID;
}
if (
( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ||
( defined( 'DOING_AJAX' ) && DOING_AJAX ) ||
isset( $_REQUEST[ 'bulk_edit' ] ) ||
( isset( $post->post_type ) && $post->post_type == 'revision' ) ||
( ! current_user_can( 'edit_post', $postID ) )
) {
return $postID;
}
if ( ! isset( $_POST[ 'forceprotocol' ] ))
return $postID;
$option = sanitize_text_field( $_POST[ 'forceprotocol' ] );
if ( ! empty( $option ))
update_post_meta( $postID, 'forceprotocol', $option );
return $postID;
}
public function viewMetabox()
{
global $post;
$meta = get_post_custom( $post->ID );
$option = "default";
if (isset( $meta[ 'forceprotocol' ][ 0 ] ) and ! empty( $meta[ 'forceprotocol' ][ 0 ] )) {
$option = $meta[ 'forceprotocol' ][ 0 ];
}
// var_dump( $meta );
?>
<input type="hidden" name="kcpt_metabox_protocol_nonce"
value="<?= wp_create_nonce( basename( __FILE__ ) ); ?>"/>
<select name="forceprotocol">
<option value="default" <?php if ( $option == "default" ): ?>selected="selected"<?php endif; ?>>Default
</option>
<option value="http" <?php if ( $option == "http" ): ?>selected="selected"<?php endif; ?>>HTTP</option>
<option value="https" <?php if ( $option == "https" ): ?>selected="selected"<?php endif; ?>>HTTPS</option>
</select>
<?php
}
}
$KCPTProtocolEnforcer = new KCPT_Protocol_Enforcer();