PluginProbe
Passster – Password Protect Pages and Content / 4.2.15
Passster – Password Protect Pages and Content v4.2.15
4.3.15 4.3.14 4.3.12 4.3.13 4.3.11 4.3.10 4.3.9 4.3.8 4.3.7 4.3.6 4.3.5 trunk 3.5.4 3.5.5.2 3.5.5.8 3.5.5.9 4.0 4.1.4 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.15 4.2.16 All 47 releases
content-protector / inc / class-ps-block-editor.php

class-ps-block-editor.php in Passster – Password Protect Pages and Content 4.2.15, at inc/class-ps-block-editor.php

170 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace passster;
4
5 class PS_Block_Editor {
6 /**
7 * Contains instance or null
8 *
9 * @var object|null
10 */
11 private static $instance = null;
12
13 /**
14 * Returns instance of PS_Block_Editor.
15 *
16 * @return object
17 */
18 public static function get_instance() {
19 if ( null === self::$instance ) {
20 self::$instance = new self();
21 }
22 return self::$instance;
23 }
24
25 /**
26 * Constructor for PS_Block_Editor
27 */
28 public function __construct() {
29 add_action( 'wp_enqueue_scripts', array($this, 'register_block_scripts') );
30 add_action( 'enqueue_block_editor_assets', array($this, 'add_block_editor_assets') );
31 add_action( 'init', array($this, 'register_area_block') );
32 }
33
34 /**
35 * Enqueue scripts for blocks.
36 *
37 * @return void
38 */
39 public function register_block_scripts() {
40 $area_asset_file = (include PASSSTER_PATH . '/build/area/index.asset.php');
41 wp_register_script(
42 'area-script',
43 PASSSTER_URL . '/build/area/index.js',
44 $area_asset_file['dependencies'],
45 $area_asset_file['version']
46 );
47 }
48
49 /**
50 * Add block editor scripts and styles.
51 *
52 * @return void
53 */
54 public function add_block_editor_assets() {
55 $area_asset_file = (include PASSSTER_PATH . '/build/area/index.asset.php');
56 wp_enqueue_script(
57 'area-script',
58 PASSSTER_URL . '/build/area/index.js',
59 $area_asset_file['dependencies'],
60 $area_asset_file['version']
61 );
62 wp_localize_script( 'area-script', 'area_data', array(
63 'is_pro' => \passster_fs()->is_plan_or_trial__premium_only( 'pro' ),
64 'create_area_url' => admin_url( 'post-new.php?post_type=protected_areas' ),
65 'options' => get_option( 'passster' ),
66 ) );
67 wp_enqueue_style( 'area-style', PASSSTER_URL . '/build/area/index.css' );
68 // Make the blocks translatable.
69 if ( function_exists( 'wp_set_script_translations' ) ) {
70 wp_set_script_translations( 'area-script', 'content-protector', PASSSTER_PATH . '/languages' );
71 }
72 }
73
74 /**
75 * Register area block in WordPress.
76 *
77 * @return void
78 */
79 public function register_area_block() {
80 $options = get_option( 'passster' );
81 $settings = array(
82 'render_callback' => array($this, 'render_area_block'),
83 'attributes' => array(
84 'area_id' => array(
85 'type' => 'string',
86 'default' => 0,
87 ),
88 'headline' => array(
89 'type' => 'string',
90 'default' => esc_html( $options['headline'] ),
91 ),
92 'instruction' => array(
93 'type' => 'string',
94 'default' => wp_kses_post( $options['instruction'] ),
95 ),
96 'buttonLabel' => array(
97 'type' => 'string',
98 'default' => esc_html( $options['button_label'] ),
99 ),
100 'placeholder' => array(
101 'type' => 'string',
102 'default' => esc_html( $options['placeholder'] ),
103 ),
104 ),
105 );
106 register_block_type( 'content-protector/area', array_merge( array(
107 'editor_script' => 'area-script',
108 'editor_style' => 'area-style',
109 ), $settings ) );
110 }
111
112 /**
113 * Returns the shortcode for an area based on block attributes.
114 *
115 * @param array $attributes the list attributes from the block.
116 *
117 * @return string
118 */
119 public function render_area_block( array $attributes ) {
120 $area_id = esc_attr( $attributes['area_id'] );
121 $area = get_post( $area_id );
122 if ( !$area ) {
123 return '';
124 }
125 // Build dynamic shortcode.
126 $content = $area->post_content;
127 $shortcode = '';
128 // texts.
129 $headline = esc_attr( $attributes['headline'] );
130 $instruction = wp_kses_post( $attributes['instruction'] );
131 $button = esc_attr( $attributes['buttonLabel'] );
132 $placeholder = esc_attr( $attributes['placeholder'] );
133 $id = get_post_meta( $area_id, 'passster_id', true );
134 // Redirection.
135 $redirection = get_post_meta( $area_id, 'passster_redirect_url', true );
136 $atts = array();
137 $password = get_post_meta( $area_id, 'passster_password', true );
138 $atts['password'] = $password;
139 $shortcode = '[passster password="' . $password . '" area="' . $area_id . '" ';
140 // Building the actual shortcode.
141 if ( !empty( $redirection ) ) {
142 $shortcode .= 'redirect="' . $redirection . '" ';
143 }
144 if ( !empty( $headline ) ) {
145 $shortcode .= 'headline="' . $headline . '" ';
146 }
147 if ( !empty( $instruction ) ) {
148 $shortcode .= 'instruction="' . $instruction . '" ';
149 }
150 if ( !empty( $placeholder ) ) {
151 $shortcode .= 'placeholder="' . $placeholder . '" ';
152 }
153 if ( !empty( $button ) ) {
154 $shortcode .= 'button="' . $button . '" ';
155 }
156 if ( !empty( $id ) ) {
157 $shortcode .= 'id="' . $id . '" ';
158 }
159 $shortcode = rtrim( $shortcode );
160 $shortcode .= ']';
161 // check if valid before restrict anything.
162 $valid = PS_Conditional::is_valid( $atts );
163 if ( $valid ) {
164 return $content;
165 }
166 return do_shortcode( $shortcode );
167 }
168
169 }
170