: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Contains instance or null
private static $instance = null ;
* Returns instance of PS_Meta.
public static function get_instance()
if ( null === self::$instance ) {
self::$instance = new self();
public function __construct()
add_action( 'init', array( $this, 'register_meta_fields' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'add_meta_scripts' ) );
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) );
* Register meta fields in WordPress.
public function register_meta_fields()
register_meta( 'post', 'passster_activate_protection', array(
register_meta( 'post', 'passster_protect_child_pages', array(
register_meta( 'post', 'passster_protection_type', array(
register_meta( 'post', 'passster_password', array(
register_meta( 'post', 'passster_activate_overwrite_defaults', array(
register_meta( 'post', 'passster_headline', array(
register_meta( 'post', 'passster_instruction', array(
register_meta( 'post', 'passster_placeholder', array(
register_meta( 'post', 'passster_button', array(
register_meta( 'post', 'passster_id', array(
register_meta( 'post', 'passster_activate_misc_settings', array(
register_meta( 'post', 'passster_redirect_url', array(
register_meta( 'post', 'passster_hide', array(
register_meta( 'post', 'passster_area_shortcode', array(
* Add scripts to post edit screen.
public function add_meta_scripts()
$screen = get_current_screen();
if ( $screen->base !== 'post' ) {
PASSSTER_URL . '/inc/admin/build/index.js',
$options = get_option( 'passster' );
'passster_protection_type' => get_post_meta( $post_id, 'passster_protection_type', true ),
'passster_password' => get_post_meta( $post_id, 'passster_password', true ),
'passster_headline' => get_post_meta( $post_id, 'passster_headline', true ),
'passster_instruction' => get_post_meta( $post_id, 'passster_instruction', true ),
'passster_placeholder' => get_post_meta( $post_id, 'passster_placeholder', true ),
'passster_button' => get_post_meta( $post_id, 'passster_button', true ),
'passster_id' => get_post_meta( $post_id, 'passster_id', true ),
'passster_hide' => get_post_meta( $post_id, 'passster_hide', true ),
'passster_redirect_url' => get_post_meta( $post_id, 'passster_redirect_url', true ),
'passster_activate_protection' => get_post_meta( $post_id, 'passster_activate_protection', true ),
'passster_protect_child_pages' => get_post_meta( $post_id, 'passster_protect_child_pages', true ),
'passster_activate_overwrite_defaults' => get_post_meta( $post_id, 'passster_activate_overwrite_defaults', true ),
'passster_activate_misc_settings' => get_post_meta( $post_id, 'passster_activate_misc_settings', true ),
'is_pro' => \passster_fs()->is_plan_or_trial__premium_only( 'pro' ),
'post_type' => $this->get_current_post_type(),
// Optionally adding some settings.
if ( isset( $options['password_length'] ) ) {
$args['password_length'] = esc_html( $options['password_length'] );
$args['password_length'] = 6;
if ( isset( $options['include_uppercase'] ) ) {
$args['include_uppercase'] = esc_html( $options['include_uppercase'] );
$args['include_uppercase'] = false;
if ( isset( $options['include_numbers'] ) ) {
$args['include_numbers'] = esc_html( $options['include_numbers'] );
$args['include_numbers'] = false;
if ( isset( $options['global_protection_id'] ) ) {
$args['global_protection_id'] = esc_html( $options['global_protection_id'] );
wp_localize_script( 'passster-settings', 'options', $args );
// Make the blocks translatable.
if ( function_exists( 'wp_set_script_translations' ) ) {
wp_set_script_translations( 'passster-settings', 'content-protector', PASSSTER_PATH . '/languages' );
wp_enqueue_style( 'passster-settings-style', PASSSTER_URL . '/inc/admin/build/index.css', array( 'wp-components' ) );
* Adds the meta box container.
public function add_metaboxes()
$post_types = get_post_types( array(
'exclude_from_search' => false,
$post_types[] = 'protected_areas';
__( 'Passster', 'content-protector' ),
array( $this, 'render_settings' ),
apply_filters( 'passster_post_types', $post_types ),
public function render_settings()
<div id="passster-metabox"></div>
* Register custom Rest API routes.
public function rest_api_init()
register_rest_route( 'passster/v1', '/meta', array(
'callback' => [ $this, 'save_meta' ],
'permission_callback' => function () {
return current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) );
register_rest_route( 'passster/v1', '/meta', array(
'callback' => [ $this, 'get_meta' ],
'permission_callback' => function () {
return current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) );
register_rest_route( 'passster/v1', '/areas', array(
'callback' => [ $this, 'get_areas' ],
'permission_callback' => function () {
return current_user_can( apply_filters( 'passster_user_capability', 'manage_options' ) );
* Save meta via Rest API.
* @param object $request given request.
* @return false|string|void
public function save_meta( object $request )
if ( $request->get_params() ) {
$params = $request->get_params();
$post_id = esc_html( $params['post_id'] );
$meta_key = esc_html( $params['meta_key'] );
// Check which action to perform.
if ( isset( $params['meta_value'] ) ) {
$meta_value = sanitize_meta( $meta_key, $params['meta_value'], 'post' );
update_post_meta( $post_id, $meta_key, $meta_value );
if ( isset( $params['delete'] ) ) {
delete_post_meta( $post_id, $meta_key );
* @param object $request given request.
* @return false|string|void
public function get_meta( object $request )
if ( $request->get_params() ) {
$params = $request->get_params();
$post_id = esc_html( $params['post_id'] );
$meta_key = esc_html( $params['meta_key'] );
$meta = get_post_meta( $post_id, $meta_key, true );
"message" => "Empty value",
* Get areas via REST API.
public function get_areas() : array
'post_type' => 'protected_areas',
'post_status' => 'publish',
$areas = get_posts( $args );
$fetched_areas = array();
foreach ( $areas as $area ) {
$new_area = new \stdClass();
$new_area->title = $area->post_title;
$new_area->id = $area->ID;
$fetched_areas[] = $new_area;
* Get the current post type.
* @return mixed|string|\WP_Post_Type|null
public function get_current_post_type()
global $post, $typenow, $current_screen ;
if ( $post && $post->post_type ) {
} elseif ( $current_screen && $current_screen->post_type ) {
return $current_screen->post_type;