| 1 |
<?php |
| 2 |
|
| 3 |
namespace passster; |
| 4 |
|
| 5 |
class PS_Rest_Handler { |
| 6 |
|
| 7 |
/** |
| 8 |
* Contains instance or null |
| 9 |
* |
| 10 |
* @var object|null |
| 11 |
*/ |
| 12 |
private static $instance = null; |
| 13 |
|
| 14 |
/** |
| 15 |
* Returns instance of PS_Rest_Handler. |
| 16 |
* |
| 17 |
* @return object |
| 18 |
*/ |
| 19 |
public static function get_instance() { |
| 20 |
|
| 21 |
if ( null === self::$instance ) { |
| 22 |
self::$instance = new self(); |
| 23 |
} |
| 24 |
|
| 25 |
return self::$instance; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Constructor for PS_Rest_Handler |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
add_filter( 'rest_authentication_errors', array( $this, 'restrict_rest_access' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
public function restrict_rest_access( $result ) { |
| 36 |
|
| 37 |
// If a previous authentication check was applied, |
| 38 |
// pass that result along without modification. |
| 39 |
if ( true === $result || is_wp_error( $result ) ) { |
| 40 |
return $result; |
| 41 |
} |
| 42 |
|
| 43 |
// Check if request is coming from a frontend page builder. |
| 44 |
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' ) || is_plugin_active( 'oxygen/functions.php' ) || is_plugin_active( 'pagelayer/pagelayer.php' ) ) ) { |
| 45 |
return $result; |
| 46 |
} |
| 47 |
|
| 48 |
// Global protection activated? |
| 49 |
$settings = get_option( 'passster' ); |
| 50 |
$protection_enabled = $settings['activate_global_protection'] ?? false; |
| 51 |
|
| 52 |
// Check if access is allowed. |
| 53 |
$valid = false; |
| 54 |
|
| 55 |
if ( ! empty( $settings['global_protection_id'] ) ) { |
| 56 |
$page_id = esc_attr( $settings['global_protection_id'] ); |
| 57 |
$atts = array( 'password' => get_post_meta( $page_id, 'passster_password', true ) ); |
| 58 |
$valid = PS_Conditional::is_valid( $atts ); |
| 59 |
} |
| 60 |
|
| 61 |
if ( $protection_enabled && ! $valid && ! is_user_logged_in() ) { |
| 62 |
return new \WP_Error( |
| 63 |
'rest_not_logged_in', |
| 64 |
__( 'You are not allowed to access this content. Please authenticate with a password first.' ), |
| 65 |
array( 'status' => 401 ) |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
return $result; |
| 70 |
} |
| 71 |
} |
| 72 |
|