| @@ -28,11 +28,40 @@ | ||
| 28 | 28 | public function __construct() { |
| 29 | 29 | add_action( 'wp_enqueue_scripts', array($this, 'register_block_scripts') ); |
| 30 | 30 | add_action( 'enqueue_block_editor_assets', array($this, 'add_block_editor_assets') ); |
| 31 | 31 | add_action( 'init', array($this, 'register_area_block') ); |
| 32 | + add_action( 'init', array($this, 'register_protected_content_block') ); | |
| 33 | + add_action( | |
| 34 | + 'save_post', | |
| 35 | + array($this, 'sync_protected_content_blocks'), | |
| 36 | + 10, | |
| 37 | + 2 | |
| 38 | + ); | |
| 39 | + add_filter( | |
| 40 | + 'block_categories_all', | |
| 41 | + array($this, 'register_block_category'), | |
| 42 | + 10, | |
| 43 | + 2 | |
| 44 | + ); | |
| 32 | 45 | } |
| 33 | 46 | |
| 34 | 47 | /** |
| 48 | + * Register Passster block category. | |
| 49 | + * | |
| 50 | + * @param array $categories Block categories. | |
| 51 | + * @param WP_Block_Editor_Context $context Block editor context. | |
| 52 | + * | |
| 53 | + * @return array Modified categories. | |
| 54 | + */ | |
| 55 | + public function register_block_category( $categories, $context ) { | |
| 56 | + return array_merge( array(array( | |
| 57 | + 'slug' => 'passster', | |
| 58 | + 'title' => __( 'Passster', 'content-protector' ), | |
| 59 | + 'icon' => 'lock', | |
| 60 | + )), $categories ); | |
| 61 | + } | |
| 62 | + | |
| 63 | + /** | |
| 35 | 64 | * Enqueue scripts for blocks. |
| 36 | 65 | * |
| 37 | 66 | * @return void |
| 38 | 67 | */ |
| @@ -51,8 +80,9 @@ | ||
| 51 | 80 | * |
| 52 | 81 | * @return void |
| 53 | 82 | */ |
| 54 | 83 | public function add_block_editor_assets() { |
| 84 | + // Area block. | |
| 55 | 85 | $area_asset_file = (include PASSSTER_PATH . '/build/area/index.asset.php'); |
| 56 | 86 | wp_enqueue_script( |
| 57 | 87 | 'area-script', |
| 58 | 88 | PASSSTER_URL . '/build/area/index.js', |
| @@ -64,8 +94,36 @@ | ||
| 64 | 94 | 'create_area_url' => admin_url( 'post-new.php?post_type=protected_areas' ), |
| 65 | 95 | 'options' => get_option( 'passster' ), |
| 66 | 96 | ) ); |
| 67 | 97 | wp_enqueue_style( 'area-style', PASSSTER_URL . '/build/area/index.css' ); |
| 98 | + // Protected Content block. | |
| 99 | + $protected_content_asset = PASSSTER_PATH . '/build/blocks/protected-content/index.asset.php'; | |
| 100 | + if ( file_exists( $protected_content_asset ) ) { | |
| 101 | + $protected_content_asset_file = (include $protected_content_asset); | |
| 102 | + wp_enqueue_script( | |
| 103 | + 'passster-protected-content-script', | |
| 104 | + PASSSTER_URL . '/build/blocks/protected-content/index.js', | |
| 105 | + $protected_content_asset_file['dependencies'], | |
| 106 | + $protected_content_asset_file['version'] | |
| 107 | + ); | |
| 108 | + wp_localize_script( 'passster-protected-content-script', 'passster_block_data', array( | |
| 109 | + 'is_pro' => \passster_fs()->is_plan_or_trial__premium_only( 'pro' ), | |
| 110 | + 'options' => get_option( 'passster' ), | |
| 111 | + 'date_format' => get_option( 'date_format' ), | |
| 112 | + 'time_format' => get_option( 'time_format' ), | |
| 113 | + 'timezone' => wp_timezone_string(), | |
| 114 | + 'now' => current_time( 'c' ), | |
| 115 | + ) ); | |
| 116 | + wp_enqueue_style( | |
| 117 | + 'passster-protected-content-style', | |
| 118 | + PASSSTER_URL . '/build/blocks/protected-content/index.css', | |
| 119 | + array(), | |
| 120 | + $protected_content_asset_file['version'] | |
| 121 | + ); | |
| 122 | + if ( function_exists( 'wp_set_script_translations' ) ) { | |
| 123 | + wp_set_script_translations( 'passster-protected-content-script', 'content-protector', PASSSTER_PATH . '/languages' ); | |
| 124 | + } | |
| 125 | + } | |
| 68 | 126 | // Make the blocks translatable. |
| 69 | 127 | if ( function_exists( 'wp_set_script_translations' ) ) { |
| 70 | 128 | wp_set_script_translations( 'area-script', 'content-protector', PASSSTER_PATH . '/languages' ); |
| 71 | 129 | } |
| @@ -144,9 +202,9 @@ | ||
| 144 | 202 | if ( !empty( $headline ) ) { |
| 145 | 203 | $shortcode .= 'headline="' . $headline . '" '; |
| 146 | 204 | } |
| 147 | 205 | if ( !empty( $instruction ) ) { |
| 148 | - $shortcode .= 'instruction="' . $instruction . '" '; | |
| 206 | + $shortcode .= 'instruction="' . base64_encode( $instruction ) . '" '; | |
| 149 | 207 | } |
| 150 | 208 | if ( !empty( $placeholder ) ) { |
| 151 | 209 | $shortcode .= 'placeholder="' . $placeholder . '" '; |
| 152 | 210 | } |
| @@ -163,7 +221,140 @@ | ||
| 163 | 221 | if ( $valid ) { |
| 164 | 222 | return $content; |
| 165 | 223 | } |
| 166 | 224 | return do_shortcode( $shortcode ); |
| 225 | + } | |
| 226 | + | |
| 227 | + /** | |
| 228 | + * Register protected content block. | |
| 229 | + * | |
| 230 | + * @return void | |
| 231 | + */ | |
| 232 | + public function register_protected_content_block() { | |
| 233 | + // Check if build files exist. | |
| 234 | + $block_path = PASSSTER_PATH . '/build/blocks/protected-content'; | |
| 235 | + if ( !file_exists( $block_path . '/block.json' ) ) { | |
| 236 | + return; | |
| 237 | + } | |
| 238 | + register_block_type( $block_path ); | |
| 239 | + } | |
| 240 | + | |
| 241 | + /** | |
| 242 | + * Sync protected content blocks with Protected Areas CPT. | |
| 243 | + * | |
| 244 | + * @param int $post_id Post ID being saved. | |
| 245 | + * @param WP_Post $post Post object. | |
| 246 | + * | |
| 247 | + * @return void | |
| 248 | + */ | |
| 249 | + public function sync_protected_content_blocks( $post_id, $post ) { | |
| 250 | + // Skip auto-saves and revisions. | |
| 251 | + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
| 252 | + return; | |
| 253 | + } | |
| 254 | + if ( wp_is_post_revision( $post_id ) ) { | |
| 255 | + return; | |
| 256 | + } | |
| 257 | + // Skip protected areas CPT to avoid loops. | |
| 258 | + if ( 'protected_areas' === $post->post_type ) { | |
| 259 | + return; | |
| 260 | + } | |
| 261 | + // Parse blocks from content. | |
| 262 | + $blocks = parse_blocks( $post->post_content ); | |
| 263 | + $protected_blocks = $this->find_protected_content_blocks( $blocks ); | |
| 264 | + // Get existing synced areas for this post. | |
| 265 | + $existing_areas = get_posts( array( | |
| 266 | + 'post_type' => 'protected_areas', | |
| 267 | + 'posts_per_page' => -1, | |
| 268 | + 'meta_query' => array(array( | |
| 269 | + 'key' => '_passster_source', | |
| 270 | + 'value' => 'block', | |
| 271 | + ), array( | |
| 272 | + 'key' => '_passster_source_post_id', | |
| 273 | + 'value' => $post_id, | |
| 274 | + )), | |
| 275 | + ) ); | |
| 276 | + $existing_block_ids = array(); | |
| 277 | + foreach ( $existing_areas as $area ) { | |
| 278 | + $block_id = get_post_meta( $area->ID, '_passster_block_id', true ); | |
| 279 | + $existing_block_ids[$block_id] = $area->ID; | |
| 280 | + } | |
| 281 | + $synced_block_ids = array(); | |
| 282 | + // Sync each protected block. | |
| 283 | + foreach ( $protected_blocks as $block ) { | |
| 284 | + $attrs = $block['attrs']; | |
| 285 | + $block_id = ( !empty( $attrs['blockId'] ) ? $attrs['blockId'] : '' ); | |
| 286 | + if ( empty( $block_id ) ) { | |
| 287 | + continue; | |
| 288 | + } | |
| 289 | + $synced_block_ids[] = $block_id; | |
| 290 | + // Prepare area data. | |
| 291 | + $area_title = ( !empty( $attrs['areaTitle'] ) ? $attrs['areaTitle'] : sprintf( | |
| 292 | + /* translators: %s: Post title */ | |
| 293 | + __( 'Protected block in %s', 'content-protector' ), | |
| 294 | + $post->post_title | |
| 295 | + ) ); | |
| 296 | + $area_data = array( | |
| 297 | + 'post_title' => $area_title, | |
| 298 | + 'post_type' => 'protected_areas', | |
| 299 | + 'post_status' => 'publish', | |
| 300 | + ); | |
| 301 | + // Check if area already exists. | |
| 302 | + if ( isset( $existing_block_ids[$block_id] ) ) { | |
| 303 | + $area_data['ID'] = $existing_block_ids[$block_id]; | |
| 304 | + $area_id = wp_update_post( $area_data ); | |
| 305 | + } else { | |
| 306 | + $area_id = wp_insert_post( $area_data ); | |
| 307 | + } | |
| 308 | + if ( !$area_id || is_wp_error( $area_id ) ) { | |
| 309 | + continue; | |
| 310 | + } | |
| 311 | + // Save source meta. | |
| 312 | + update_post_meta( $area_id, '_passster_source', 'block' ); | |
| 313 | + update_post_meta( $area_id, '_passster_source_post_id', $post_id ); | |
| 314 | + update_post_meta( $area_id, '_passster_block_id', $block_id ); | |
| 315 | + // Save protection settings. | |
| 316 | + $protection_type = ( !empty( $attrs['protectionType'] ) ? $attrs['protectionType'] : 'password' ); | |
| 317 | + update_post_meta( $area_id, 'passster_activate_protection', true ); | |
| 318 | + update_post_meta( $area_id, 'passster_protection_type', $protection_type ); | |
| 319 | + switch ( $protection_type ) { | |
| 320 | + case 'password': | |
| 321 | + update_post_meta( $area_id, 'passster_password', ( !empty( $attrs['password'] ) ? $attrs['password'] : '' ) ); | |
| 322 | + break; | |
| 323 | + case 'passwords': | |
| 324 | + update_post_meta( $area_id, 'passster_passwords', ( !empty( $attrs['passwords'] ) ? $attrs['passwords'] : '' ) ); | |
| 325 | + break; | |
| 326 | + case 'password_list': | |
| 327 | + update_post_meta( $area_id, 'passster_password_list', ( !empty( $attrs['passwordList'] ) ? absint( $attrs['passwordList'] ) : 0 ) ); | |
| 328 | + break; | |
| 329 | + } | |
| 330 | + } | |
| 331 | + // Delete areas for blocks that no longer exist. | |
| 332 | + foreach ( $existing_block_ids as $block_id => $area_id ) { | |
| 333 | + if ( !in_array( $block_id, $synced_block_ids, true ) ) { | |
| 334 | + wp_delete_post( $area_id, true ); | |
| 335 | + } | |
| 336 | + } | |
| 337 | + } | |
| 338 | + | |
| 339 | + /** | |
| 340 | + * Recursively find protected content blocks. | |
| 341 | + * | |
| 342 | + * @param array $blocks Parsed blocks. | |
| 343 | + * | |
| 344 | + * @return array Protected content blocks. | |
| 345 | + */ | |
| 346 | + private function find_protected_content_blocks( $blocks ) { | |
| 347 | + $found = array(); | |
| 348 | + foreach ( $blocks as $block ) { | |
| 349 | + if ( 'passster/content-lock' === $block['blockName'] ) { | |
| 350 | + $found[] = $block; | |
| 351 | + } | |
| 352 | + // Check inner blocks recursively. | |
| 353 | + if ( !empty( $block['innerBlocks'] ) ) { | |
| 354 | + $found = array_merge( $found, $this->find_protected_content_blocks( $block['innerBlocks'] ) ); | |
| 355 | + } | |
| 356 | + } | |
| 357 | + return $found; | |
| 167 | 358 | } |
| 168 | 359 | |
| 169 | 360 | } |