-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-page-option.php
278 lines (210 loc) · 10.2 KB
/
class-page-option.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
defined('ABSPATH') or die("you do not have access to this page!");
if (!class_exists('rsssl_page_option')) {
class rsssl_page_option
{
private static $_this;
function __construct()
{
if (isset(self::$_this))
wp_die(sprintf(__('%s is a singleton class and you cannot create a second instance.', 'really-simple-ssl'), get_class($this)));
self::$_this = $this;
// register the meta box
add_action('add_meta_boxes', array($this, 'register_https_option'));
add_action('save_post', array($this, 'save_option'));
add_action('wp_loaded', array($this, 'init'), 20, 3);
add_action('plugins_loaded', array($this, 'switch_page'));
add_action('admin_notices', array($this, 'bulk_action_admin_notice'));
}
static function this()
{
return self::$_this;
}
public function init()
{
$args = array(
'public' => true,
);
$post_types = get_post_types($args);
foreach ($post_types as $post_type) {
add_filter('bulk_actions-edit-' . $post_type, array($this, 'register_bulk_actions'));
add_filter('handle_bulk_actions-edit-' . $post_type, array($this, 'bulk_action_handler'), 10, 3);
add_filter('manage_' . $post_type . '_posts_columns', array($this, 'set_edit_field_columns'));
add_action('manage_' . $post_type . '_posts_custom_column', array($this, 'https_column'), 10, 2);
}
}
public function register_bulk_actions($bulk_actions)
{
$bulk_actions['rsssl_disable_https_bulk'] = __("Make HTTP", "really-simple-ssl-on-specific-pages");
$bulk_actions['rsssl_enable_https_bulk'] = __("Make HTTPS", "really-simple-ssl-on-specific-pages");
return $bulk_actions;
}
public function set_edit_field_columns($columns)
{
$columns['https'] = __('HTTPS', 'really-simple-ssl-on-specific-pages');
return $columns;
}
public function https_column($column, $post_id)
{
if ($column !== 'https') return;
$https = RSSSL()->rsssl_front_end->is_ssl_page($post_id);
$switch = $https ? 0 : 1;
$nonce = wp_create_nonce('rsssl_nonce');
$post_type = isset($_GET['post_type']) ? sanitize_title($_GET['post_type']) : false;
$args = array(
'post_type' => $post_type,
'rsssl_nonce' => $nonce,
'rsssl_switch_post_id' => $post_id,
'enable_https' => $switch
);
$url = add_query_arg($args, admin_url('edit.php'));
if ($https) {
$img = '<a href="'.$url.'"><img class="rsssl-icon rsssl-https-icon" title="' . __("This page is currently on HTTPS. Click to switch to HTTP", "really-simple-ssl-on-specific-pages") . '" src="' . rsssl_pp_url . 'img/https.png" ></a>';
} else {
$img = '<a href="'.$url.'"><img class="rsssl-icon rsssl-http-icon" title="' . __("This page is currently on HTTP. Click to switch to HTTPS", "really-simple-ssl-on-specific-pages") . '" src="' . rsssl_pp_url . 'img/http.png" ></a>';
}
echo $img;
}
function bulk_action_handler($redirect_to, $doaction, $post_ids)
{
global $really_simple_ssl;
if (($doaction !== 'rsssl_enable_https_bulk') && ($doaction !== 'rsssl_disable_https_bulk')) {
return $redirect_to;
}
$exclude = $really_simple_ssl->exclude_pages;
$enable = ($doaction === 'rsssl_enable_https_bulk') ? true : false;
$disable = ($doaction === 'rsssl_disable_https_bulk') ? true : false;
//set pages to https. if exclude_pages_from https is enabled, this means the array should not contain these items
if (($enable && !$exclude) || ($disable && $exclude)) {
//add these to array
foreach ($post_ids as $post_id) {
//handle frontpage differently
if (!RSSSL()->rsssl_front_end->is_home($post_id)) {
update_post_meta($post_id, "rsssl_ssl_page", true);
}
}
}
//set pages to http
if (($enable && $exclude) || ($disable && !$exclude)) {
//remove these
foreach ($post_ids as $post_id) {
//handle frontpage differently
if (!RSSSL()->rsssl_front_end->is_home($post_id)) {
update_post_meta($post_id, "rsssl_ssl_page", false);
}
}
}
//handle frontpage differently
foreach ($post_ids as $post_id) {
if (RSSSL()->rsssl_front_end->is_home($post_id)) {
$options = get_option('rlrsssl_options');
if ($enable) {
$options['home_ssl'] = true;
}
if ($disable) {
$options['home_ssl'] = false;
}
update_option('rlrsssl_options', $options);
}
}
$redirect_to = add_query_arg('change_type', $doaction, $redirect_to);
$redirect_to = add_query_arg('changed_items', count($post_ids), $redirect_to);
return $redirect_to;
}
function bulk_action_admin_notice()
{
//prevent showing the review on edit screen, as gutenberg removes the class which makes it editable.
$screen = get_current_screen();
if ( $screen->parent_base === 'edit' ) return;
if (!empty($_REQUEST['changed_items'])) {
$count = intval($_REQUEST['changed_items']);
$action = $_REQUEST['change_type'];
if ($action == 'rsssl_enable_https_bulk') {
$string = sprintf(__('Enabled https for %s items', 'really-simple-ssl-on-specific-pages'), $count);
} else {
$string = sprintf(__('Disabled https for %s items', 'really-simple-ssl-on-specific-pages'), $count);
}
printf('<div id="message" class="rsssl-bulk-message updated fade">' . $string . '</div>', $count);
}
}
public function register_https_option()
{
add_meta_box(
'rsssl', // this is HTML id of the box on edit screen
__('SSL settings', "really-simple-ssl-pro"), // title of the box
array($this, 'option_html'), // function to be called to display the checkboxes, see the function below
null,//'post', // on which edit screen the box should appear
'side', // part of page where the box should appear
'default' // priority of the box
);
}
// display the metabox
public function option_html($post_id)
{
wp_nonce_field('rsssl_nonce', 'rsssl_nonce');
$value = 0;
$checked = "";
global $really_simple_ssl;
$really_simple_ssl->get_admin_options();
global $post;
$current_page_id = $post->ID;
$option_label = __("This page on https", "really-simple-ssl-pro");
if (RSSSL()->rsssl_front_end->is_ssl_page($current_page_id)) {
$value = 1;
$checked = "checked";
}
echo '<input type="checkbox" ' . $checked . ' name="rsssl_page_on_https" value="' . $value . '" />' . $option_label . '<br />';
}
// save data from checkboxes
public function save_option()
{
global $really_simple_ssl;
// check if this isn't an auto save
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
// security check
if (!isset($_POST['rsssl_nonce']) || !wp_verify_nonce($_POST['rsssl_nonce'], 'rsssl_nonce'))
return;
global $post;
$current_page_id = $post->ID;
$enable_https = isset($_POST['rsssl_page_on_https']) ? true : false;
if (!RSSSL()->rsssl_front_end->is_home($current_page_id) && $really_simple_ssl->exclude_pages) {
$enable_https = !$enable_https;
}
if (RSSSL()->rsssl_front_end->is_home($current_page_id)) {
$options = get_option('rlrsssl_options');
$options['home_ssl'] = $enable_https;
update_option('rlrsssl_options', $options);
}
// now store data in custom fields based on checkboxes selected
update_post_meta($current_page_id, "rsssl_ssl_page", $enable_https);
}
public function switch_page()
{
global $really_simple_ssl;
// security check
if (!isset($_GET['rsssl_nonce']) || !wp_verify_nonce($_GET['rsssl_nonce'], 'rsssl_nonce'))
return;
if (!isset($_GET['rsssl_switch_post_id'])) return;
if (!isset($_GET['enable_https'])) return;
$current_page_id = intval($_GET['rsssl_switch_post_id']);
$enable_https = (intval($_GET['enable_https'])==1) ? true : false;
$post_type = isset($_GET['post_type']) ? sanitize_title($_GET['post_type']) : false;
if (!RSSSL()->rsssl_front_end->is_home($current_page_id) && $really_simple_ssl->exclude_pages) {
$enable_https = !$enable_https;
}
if (RSSSL()->rsssl_front_end->is_home($current_page_id)) {
$options = get_option('rlrsssl_options');
$options['home_ssl'] = $enable_https;
update_option('rlrsssl_options', $options);
}
// now store data in custom fields based on checkboxes selected
update_post_meta($current_page_id, "rsssl_ssl_page", $enable_https);
$args = array(
'post_type' => $post_type,
);
wp_redirect(add_query_arg($args, admin_url('edit.php')));
exit;
}
}//class closure
}