PluginProbe
Passster – Password Protect Pages and Content / 4.0
Passster – Password Protect Pages and Content v4.0
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.0, at inc/class-ps-block-editor.php

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