| 1 |
<?php |
| 2 |
|
| 3 |
namespace passster; |
| 4 |
|
| 5 |
class PS_Admin |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Contains instance or null |
| 9 |
* |
| 10 |
* @var object|null |
| 11 |
*/ |
| 12 |
private static $instance = null ; |
| 13 |
/** |
| 14 |
* Returns instance of PS_Admin. |
| 15 |
* |
| 16 |
* @return object |
| 17 |
*/ |
| 18 |
public static function get_instance() |
| 19 |
{ |
| 20 |
if ( null === self::$instance ) { |
| 21 |
self::$instance = new self(); |
| 22 |
} |
| 23 |
return self::$instance; |
| 24 |
} |
| 25 |
|
| 26 |
public function __construct() |
| 27 |
{ |
| 28 |
// Check permissions before include settings. |
| 29 |
if ( !current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) ) ) { |
| 30 |
return; |
| 31 |
} |
| 32 |
add_action( 'init', array( $this, 'register_password_areas' ) ); |
| 33 |
add_filter( 'manage_protected_areas_posts_columns', array( $this, 'set_area_columns' ) ); |
| 34 |
add_filter( 'manage_post_posts_columns', array( $this, 'set_post_columns' ) ); |
| 35 |
add_filter( 'manage_page_posts_columns', array( $this, 'set_post_columns' ) ); |
| 36 |
add_action( |
| 37 |
'manage_post_posts_custom_column', |
| 38 |
array( $this, 'set_post_columns_content' ), |
| 39 |
10, |
| 40 |
2 |
| 41 |
); |
| 42 |
add_action( |
| 43 |
'manage_page_posts_custom_column', |
| 44 |
array( $this, 'set_post_columns_content' ), |
| 45 |
10, |
| 46 |
2 |
| 47 |
); |
| 48 |
add_action( |
| 49 |
'manage_protected_areas_posts_custom_column', |
| 50 |
array( $this, 'set_area_columns_content' ), |
| 51 |
10, |
| 52 |
2 |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Register post type "protected areas" |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function register_password_areas() |
| 62 |
{ |
| 63 |
$labels = array( |
| 64 |
'name' => _x( 'Protected Areas', 'post type general name', 'content-protector' ), |
| 65 |
'singular_name' => _x( 'Protected Area', 'post type singular name', 'content-protector' ), |
| 66 |
'menu_name' => _x( 'Protected Areas', 'admin menu', 'content-protector' ), |
| 67 |
'name_admin_bar' => _x( 'Protected Area', 'add new on admin bar', 'content-protector' ), |
| 68 |
'add_new' => _x( 'Add New', 'content-protector' ), |
| 69 |
'add_new_item' => __( 'Add New Protected Area', 'content-protector' ), |
| 70 |
'new_item' => __( 'New Protected Area', 'content-protector' ), |
| 71 |
'edit_item' => __( 'Edit Protected Area', 'content-protector' ), |
| 72 |
'view_item' => __( 'View Protected Area', 'content-protector' ), |
| 73 |
'all_items' => __( 'Protected Areas', 'content-protector' ), |
| 74 |
'search_items' => __( 'Search Protected Areas', 'content-protector' ), |
| 75 |
'parent_item_colon' => __( 'Parent Protected Area', 'content-protector' ), |
| 76 |
'not_found' => __( 'No Protected Areas found.', 'content-protector' ), |
| 77 |
'not_found_in_trash' => __( 'No Protected Areas found in Trash.', 'content-protector' ), |
| 78 |
); |
| 79 |
$args = array( |
| 80 |
'labels' => $labels, |
| 81 |
'description' => __( 'Manageable protected areas', 'content-protector' ), |
| 82 |
'public' => false, |
| 83 |
'publicly_queryable' => false, |
| 84 |
'show_ui' => true, |
| 85 |
'show_in_menu' => 'passster', |
| 86 |
'query_var' => true, |
| 87 |
'rewrite' => false, |
| 88 |
'capability_type' => 'post', |
| 89 |
'has_archive' => false, |
| 90 |
'hierarchical' => false, |
| 91 |
'menu_position' => null, |
| 92 |
'supports' => array( 'title', 'editor' ), |
| 93 |
'show_in_rest' => true, |
| 94 |
); |
| 95 |
|
| 96 |
if ( current_user_can( 'administrator' ) && (is_plugin_active( 'elementor/elementor.php' ) || is_plugin_active( 'livecanvas/livecanvas-plugin-index.php' ) || is_plugin_active( 'divi-builder/divi-builder.php' )) ) { |
| 97 |
$args['public'] = true; |
| 98 |
$args['publicly_queryable'] = true; |
| 99 |
} |
| 100 |
|
| 101 |
$theme = wp_get_theme(); |
| 102 |
|
| 103 |
if ( current_user_can( 'administrator' ) && 'Divi' == $theme->name || 'Divi' == $theme->parent_theme ) { |
| 104 |
$args['public'] = true; |
| 105 |
$args['publicly_queryable'] = true; |
| 106 |
} |
| 107 |
|
| 108 |
register_post_type( 'protected_areas', $args ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Set column headers for pages/post |
| 113 |
* |
| 114 |
* @param array $columns array of columns. |
| 115 |
* |
| 116 |
* @return array |
| 117 |
*/ |
| 118 |
public function set_post_columns( array $columns ) : array |
| 119 |
{ |
| 120 |
$columns['protected'] = __( 'Password Protection', 'content-protector' ); |
| 121 |
return $columns; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Add content to registered columns for posts/pages |
| 126 |
* |
| 127 |
* @param string $column name of the column. |
| 128 |
* @param int $post_id current id. |
| 129 |
* |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
public function set_post_columns_content( string $column, int $post_id ) |
| 133 |
{ |
| 134 |
switch ( $column ) { |
| 135 |
case 'protected': |
| 136 |
$protected = get_post_meta( $post_id, 'passster_activate_protection', true ); |
| 137 |
|
| 138 |
if ( $protected ) { |
| 139 |
?> |
| 140 |
<div style="background:#007cba;color:#fff;padding:10px 15px;text-align:center;font-weight:bold;max-width: 120px;margin: 10px 0;}"> |
| 141 |
<span style="position:relative;top: -2px;}" class="dashicons dashicons-lock"></span> |
| 142 |
<?php |
| 143 |
esc_html_e( 'Protected', 'content-protector' ); |
| 144 |
?> |
| 145 |
</div> |
| 146 |
<?php |
| 147 |
} |
| 148 |
|
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Set column headers password lists post type |
| 155 |
* |
| 156 |
* @param array $columns array of columns. |
| 157 |
* |
| 158 |
* @return array |
| 159 |
*/ |
| 160 |
public function set_area_columns( array $columns ) : array |
| 161 |
{ |
| 162 |
$columns['shortcode'] = __( 'Shortcode', 'content-protector' ); |
| 163 |
$columns['protection-type'] = __( 'Protection Type', 'content-protector' ); |
| 164 |
$columns['password'] = __( 'Password', 'content-protector' ); |
| 165 |
$columns['redirect'] = __( 'Redirect', 'content-protector' ); |
| 166 |
unset( $columns['date'] ); |
| 167 |
return $columns; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Add content to registered columns for password list post type. |
| 172 |
* |
| 173 |
* @param string $column name of the column. |
| 174 |
* @param int $post_id current id. |
| 175 |
* |
| 176 |
* @return void |
| 177 |
*/ |
| 178 |
public function set_area_columns_content( string $column, int $post_id ) |
| 179 |
{ |
| 180 |
switch ( $column ) { |
| 181 |
case 'shortcode': |
| 182 |
$shortcode = get_post_meta( $post_id, 'passster_area_shortcode', true ); |
| 183 |
?> |
| 184 |
<?php |
| 185 |
|
| 186 |
if ( !empty($shortcode) ) { |
| 187 |
?> |
| 188 |
<button type="button" |
| 189 |
class="components-button components-clipboard-button is-primary area-<?php |
| 190 |
echo esc_html( $post_id ) ; |
| 191 |
?>" |
| 192 |
data-shortcode="<?php |
| 193 |
echo esc_html( $shortcode ) ; |
| 194 |
?>" |
| 195 |
onclick="copyShortcode(this)" |
| 196 |
> |
| 197 |
<?php |
| 198 |
esc_html_e( 'Copy Shortcode', 'content-protector' ); |
| 199 |
?> |
| 200 |
</button> |
| 201 |
<script> |
| 202 |
function copyShortcode(element) { |
| 203 |
// Copy to clipboard. |
| 204 |
navigator.clipboard.writeText(element.getAttribute('data-shortcode')).then(function () { |
| 205 |
element.innerHTML = "<?php |
| 206 |
esc_html_e( 'Copied!', 'content-protector' ); |
| 207 |
?>"; |
| 208 |
|
| 209 |
setTimeout(function () { |
| 210 |
element.innerHTML = "<?php |
| 211 |
esc_html_e( 'Copy Shortcode', 'content-protector' ); |
| 212 |
?>" |
| 213 |
}, 1500); |
| 214 |
|
| 215 |
}, function (err) { |
| 216 |
console.error('Could not copy shortcode: ', err); |
| 217 |
}); |
| 218 |
} |
| 219 |
</script> |
| 220 |
<style> |
| 221 |
button { |
| 222 |
white-space: nowrap; |
| 223 |
text-shadow: none; |
| 224 |
outline: 1px solid transparent; |
| 225 |
width: auto; |
| 226 |
display: inline-flex; |
| 227 |
text-decoration: none; |
| 228 |
font-family: inherit; |
| 229 |
font-weight: normal; |
| 230 |
font-size: 13px; |
| 231 |
margin: 0; |
| 232 |
border: 0; |
| 233 |
cursor: pointer; |
| 234 |
-webkit-appearance: none; |
| 235 |
background: #007cba; |
| 236 |
transition: box-shadow 0.1s linear; |
| 237 |
height: 36px; |
| 238 |
align-items: center; |
| 239 |
box-sizing: border-box; |
| 240 |
padding: 6px 12px; |
| 241 |
border-radius: 2px; |
| 242 |
color: #fff; |
| 243 |
} |
| 244 |
|
| 245 |
button:hover { |
| 246 |
background: #006ba1; |
| 247 |
} |
| 248 |
</style> |
| 249 |
<?php |
| 250 |
} |
| 251 |
|
| 252 |
?> |
| 253 |
<?php |
| 254 |
break; |
| 255 |
case 'protection-type': |
| 256 |
$type = get_post_meta( $post_id, 'passster_protection_type', true ); |
| 257 |
$protection_types = array( |
| 258 |
'password' => __( 'Password', 'content-protector' ), |
| 259 |
'password_list' => __( 'Password List', 'content-protector' ), |
| 260 |
'passwords' => __( 'Passwords', 'content-protector' ), |
| 261 |
'recaptcha' => __( 'ReCaptcha', 'content-protector' ), |
| 262 |
); |
| 263 |
echo esc_html( $protection_types[$type] ) ; |
| 264 |
break; |
| 265 |
case 'password': |
| 266 |
$type = get_post_meta( $post_id, 'passster_protection_type', true ); |
| 267 |
$protection_types = array( |
| 268 |
'password' => get_post_meta( $post_id, 'passster_password', true ), |
| 269 |
'password_list' => get_post_meta( $post_id, 'passster_password_list', true ), |
| 270 |
'passwords' => get_post_meta( $post_id, 'passster_passwords', true ), |
| 271 |
'recaptcha' => '-', |
| 272 |
); |
| 273 |
// Build the edit link if it's a password list. |
| 274 |
|
| 275 |
if ( 'password_list' === $type ) { |
| 276 |
$edit_link = get_edit_post_link( $protection_types[$type] ); |
| 277 |
?> |
| 278 |
<a target="_blank" |
| 279 |
href="<?php |
| 280 |
echo esc_url( $edit_link ) ; |
| 281 |
?>"><?php |
| 282 |
echo esc_html( $protection_types[$type] ) ; |
| 283 |
?></a> |
| 284 |
<?php |
| 285 |
} else { |
| 286 |
echo esc_html( $protection_types[$type] ) ; |
| 287 |
} |
| 288 |
|
| 289 |
break; |
| 290 |
case 'user-restriction': |
| 291 |
$restriction = get_post_meta( $post_id, 'passster_activate_user_restriction', true ); |
| 292 |
$type = get_post_meta( $post_id, 'passster_user_restriction_type', true ); |
| 293 |
$value = get_post_meta( $post_id, 'passster_user_restriction', true ); |
| 294 |
// Get available user roles. |
| 295 |
global $wp_roles ; |
| 296 |
$roles = $wp_roles->roles; |
| 297 |
|
| 298 |
if ( $restriction ) { |
| 299 |
|
| 300 |
if ( 'user-role' === $type ) { |
| 301 |
echo esc_html( $roles[$value]['name'] ) ; |
| 302 |
} elseif ( 'username' === $type ) { |
| 303 |
echo esc_html( $value ) ; |
| 304 |
} else { |
| 305 |
echo '-' ; |
| 306 |
} |
| 307 |
|
| 308 |
} else { |
| 309 |
echo '-' ; |
| 310 |
} |
| 311 |
|
| 312 |
break; |
| 313 |
case 'redirect': |
| 314 |
$redirect = get_post_meta( $post_id, 'passster_redirect_url', true ); |
| 315 |
|
| 316 |
if ( !empty($redirect) ) { |
| 317 |
echo '<a href="' . esc_url( $redirect ) . '">' . esc_url( $redirect ) . '</a>' ; |
| 318 |
} else { |
| 319 |
echo '-' ; |
| 320 |
} |
| 321 |
|
| 322 |
break; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
} |