forked from zeen101/leaky-paywall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetaboxes.php
203 lines (156 loc) · 8.02 KB
/
metaboxes.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
<?php
/**
* Random metaboxes
*
* @package zeen101's Leaky Paywall
* @since 2.0.0
*/
if ( !function_exists( 'leaky_paywall_general_metaboxes' ) ) {
function leaky_paywall_general_metaboxes() {
$hidden_post_types = array( 'attachment', 'revision', 'nav_menu_item' );
$post_types = get_post_types( array(), 'objects' );
foreach ( $post_types as $post_type ) {
if ( in_array( $post_type->name, $hidden_post_types ) )
continue;
add_meta_box( 'leaky_paywall_content_visibility', __( 'Leaky Paywall Visibility', 'issuem-leaky-paywall' ), 'leaky_paywall_content_visibility', $post_type->name, 'side' );
}
do_action( 'leaky_paywall_general_metaboxes' );
}
add_action( 'add_meta_boxes', 'leaky_paywall_general_metaboxes' );
}
if ( !function_exists( 'leaky_paywall_content_visibility' ) ) {
function leaky_paywall_content_visibility( $post ) {
$settings = get_leaky_paywall_settings();
$visibility = get_post_meta( $post->ID, '_issuem_leaky_paywall_visibility', true );
$defaults = array(
'visibility_type' => 'default',
'only_visible' => array(),
'only_always_visible' => array(),
'always_visible' => array(),
);
$visibility = wp_parse_args( $visibility, $defaults );
echo '<label for="leaky-paywall-visibility">' . sprintf( __( 'This %s should...', 'issuem-leaky-paywall' ), $post->post_type ) . '</label> ';
echo '<select id="issuem-leaky-paywall-visibility-type" name="leaky_paywall_visibility_type">';
echo ' <option value="default" ' . selected( $visibility['visibility_type'], 'default', true ) . '>' . __( "obey Leaky Paywall's defaults.", 'issuem-leaky-paywall' ) . '</option>';
echo ' <option value="only" ' . selected( $visibility['visibility_type'], 'only', true ) . '>' . __( 'only be visible to...', 'issuem-leaky-paywall' ) . '</option>';
echo ' <option value="always" ' . selected( $visibility['visibility_type'], 'always', true ) . '>' . __( 'always be visible to...', 'issuem-leaky-paywall' ) . '</option>';
echo ' <option value="onlyalways" ' . selected( $visibility['visibility_type'], 'onlyalways', true ) . '>' . __( 'only and always be visible to...', 'issuem-leaky-paywall' ) . '</option>';
echo '</select>';
if ( 'only' !== $visibility['visibility_type'] )
$only_visible = 'style="display: none;"';
else
$only_visible = '';
if ( !empty( $settings['levels'] ) ) {
echo '<select id="issuem-leaky-paywall-only-visible" name="leaky_paywall_only_visible[]" ' . $only_visible . ' multiple="multiple">';
foreach( $settings['levels'] as $key => $level ) {
echo ' <option value="' . $key . '" ' . selected( in_array( $key, $visibility['only_visible'] ), true, false ) . '>' . $level['label'] . '</option>';
}
echo '</select>';
}
if ( 'always' !== $visibility['visibility_type'] )
$always_visible = 'style="display: none;"';
else
$always_visible = '';
if ( !empty( $settings['levels'] ) ) {
echo '<select id="issuem-leaky-paywall-always-visible" name="leaky_paywall_always_visible[]" ' . $always_visible . ' multiple="multiple">';
echo ' <option value="-1" ' . selected( in_array( '-1', $visibility['always_visible'] ), true, false ) . '>' . __( 'Everyone', 'issuem-leaky-paywall' ) . '</option>';
foreach( $settings['levels'] as $key => $level ) {
echo ' <option value="' . $key . '" ' . selected( in_array( $key, $visibility['always_visible'] ), true, false ) . '>' . $level['label'] . '</option>';
}
echo '</select>';
}
if ( 'onlyalways' !== $visibility['visibility_type'] )
$only_always_visible = 'style="display: none;"';
else
$only_always_visible = '';
if ( !empty( $settings['levels'] ) ) {
echo '<select id="issuem-leaky-paywall-only-always-visible" name="leaky_paywall_only_always_visible[]" ' . $only_always_visible . ' multiple="multiple">';
foreach( $settings['levels'] as $key => $level ) {
echo ' <option value="' . $key . '" ' . selected( in_array( $key, $visibility['only_always_visible'] ), true, false ) . '>' . $level['label'] . '</option>';
}
echo '</select>';
}
echo '<p class="description">' . __( 'Hint:', 'issuem-leaky-paywall' ) . '</p>';
echo '<p class="description">' . sprintf( __( '"Only" means that only the selected subscription levels can see this %s, if they have not reached their %s limit.', 'issuem-leaky-paywall' ), $post->post_type, $post->post_type ) . '</p>';
echo '<p class="description">' . sprintf( __( '"Always" means that the selected subscription levels can see this %s, even if they have reached their %s limit.', 'issuem-leaky-paywall' ), $post->post_type, $post->post_type ) . '</p>';
echo '<p class="description">' . sprintf( __( '"Only and Always" means that only the selected subscription levels can see this %s, even if they have reached their %s limit.', 'issuem-leaky-paywall' ), $post->post_type, $post->post_type ) . '</p>';
wp_nonce_field( 'leaky_paywall_content_visibility_meta_box', 'leaky_paywall_content_visibility_meta_box_nonce' );
}
}
if ( !function_exists( 'save_leaky_paywall_content_visibility' ) ) {
function save_leaky_paywall_content_visibility( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['leaky_paywall_content_visibility_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['leaky_paywall_content_visibility_meta_box_nonce'], 'leaky_paywall_content_visibility_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( ! ( current_user_can( 'edit_page', $post_id ) || current_user_can( 'edit_post', $post_id ) ) ) {
return;
}
/* OK, it's safe for us to save the data now. */
if ( !empty( $_POST['leaky_paywall_visibility_type'] ) ) {
$visibility['visibility_type'] = $_POST['leaky_paywall_visibility_type'];
switch ( $visibility['visibility_type'] ) {
case 'only':
if ( !empty( $_POST['leaky_paywall_only_visible'] ) ) {
$visibility['only_visible'] = $_POST['leaky_paywall_only_visible'];
$visibility['only_always_visible'] = array();
$visibility['always_visible'] = array();
} else {
$visibility['only_visible'] = array();
$visibility['only_always_visible'] = array();
$visibility['always_visible'] = array();
}
break;
case 'onlyalways':
if ( !empty( $_POST['leaky_paywall_only_always_visible'] ) ) {
$visibility['only_visible'] = array();
$visibility['only_always_visible'] = $_POST['leaky_paywall_only_always_visible'];
$visibility['always_visible'] = array();
} else {
$visibility['only_visible'] = array();
$visibility['only_always_visible'] = array();
$visibility['always_visible'] = array();
}
break;
case 'always':
if ( !empty( $_POST['leaky_paywall_always_visible'] ) ) {
$visibility['only_visible'] = array();
$visibility['only_always_visible'] = array();
if ( in_array( '-1', $_POST['leaky_paywall_always_visible'] ) )
$visibility['always_visible'] = array( '-1' );
else
$visibility['always_visible'] = $_POST['leaky_paywall_always_visible'];
} else {
$visibility['only_visible'] = array();
$visibility['only_always_visible'] = array();
$visibility['always_visible'] = array();
}
break;
default:
if ( !empty( $_POST['leaky_paywall_always_visible'] ) ) {
$visibility['only_visible'] = array();
$visibility['only_always_visible'] = array();
$visibility['always_visible'] = array();
}
break;
}
update_post_meta( $post_id, '_issuem_leaky_paywall_visibility', $visibility );
} else {
delete_post_meta( $post_id, '_issuem_leaky_paywall_visibility' );
}
}
add_action( 'save_post', 'save_leaky_paywall_content_visibility' );
}