| 1 |
<?php |
| 2 |
|
| 3 |
namespace gutenify; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class Popup { |
| 8 |
public static function init() { |
| 9 |
add_action( 'init', array( __CLASS__, 'register_block' ) ); |
| 10 |
} |
| 11 |
|
| 12 |
public static function register_block() { |
| 13 |
if ( ! self::is_active() ) { |
| 14 |
return; |
| 15 |
} |
| 16 |
|
| 17 |
register_block_type( |
| 18 |
__DIR__, |
| 19 |
array( |
| 20 |
'render_callback' => array( __CLASS__, 'render_callback' ), |
| 21 |
) |
| 22 |
); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Whether this PRO-origin block should register — see |
| 27 |
* Helpers::is_pro_block_active() for the actual PRO-active-or-theme- |
| 28 |
* support check and why it must run on `init` or later. |
| 29 |
* |
| 30 |
* @return bool |
| 31 |
*/ |
| 32 |
private static function is_active() { |
| 33 |
return Helpers::is_pro_block_active(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* save() (see save.js) still emits the complete, already-correct markup — |
| 38 |
* trigger button, popup wrapper, and inner blocks via |
| 39 |
* `<InnerBlocks.Content />` — for a single, standalone instance of this |
| 40 |
* block. That's fine when the block only ever appears once per page, but |
| 41 |
* inside a Post Template / Query Loop the identical saved HTML is |
| 42 |
* rendered once per post, so every post ends up with the SAME popup DOM |
| 43 |
* id and the SAME `data-popup-id` selector. jQuery's `$( popupId )` |
| 44 |
* (used by view-script.js via Magnific Popup) then always resolves to |
| 45 |
* the FIRST match on the page, so every post's button opens post #1's |
| 46 |
* popup content. |
| 47 |
* |
| 48 |
* This callback does not regenerate any markup (all the per-instance |
| 49 |
* button/popup styling logic stays in save.js, untouched) — it only |
| 50 |
* swaps the popup id / data-popup-id selector inside the already |
| 51 |
* rendered $content so each post gets its own unique pairing. |
| 52 |
* |
| 53 |
* Outside of any resolvable post ($post_id stays 0 — e.g. a block |
| 54 |
* rendered somewhere with no current/global post and no `postId` |
| 55 |
* block context) $content is returned completely untouched, so a |
| 56 |
* previously-saved, standalone quick-view block keeps rendering the |
| 57 |
* exact same DOM id / classes / structure as before this fix. |
| 58 |
*/ |
| 59 |
public static function render_callback( $attributes, $content, $block = null ) { |
| 60 |
$block_client_id = ! empty( $attributes['blockClientId'] ) ? $attributes['blockClientId'] : ''; |
| 61 |
|
| 62 |
if ( '' === $block_client_id || empty( $content ) ) { |
| 63 |
return $content; |
| 64 |
} |
| 65 |
|
| 66 |
$post_id = 0; |
| 67 |
|
| 68 |
if ( $block && isset( $block->context['postId'] ) && $block->context['postId'] ) { |
| 69 |
$post_id = (int) $block->context['postId']; |
| 70 |
} elseif ( get_the_ID() ) { |
| 71 |
$post_id = (int) get_the_ID(); |
| 72 |
} |
| 73 |
|
| 74 |
if ( ! $post_id ) { |
| 75 |
return $content; |
| 76 |
} |
| 77 |
|
| 78 |
$base_id = 'gutenify-quick-view-content-' . $block_client_id; |
| 79 |
$new_id = $base_id . '-' . $post_id; |
| 80 |
|
| 81 |
return str_replace( |
| 82 |
array( |
| 83 |
'id="' . $base_id . '"', |
| 84 |
'data-popup-id="#' . $base_id . '"', |
| 85 |
), |
| 86 |
array( |
| 87 |
'id="' . $new_id . '"', |
| 88 |
'data-popup-id="#' . $new_id . '"', |
| 89 |
), |
| 90 |
$content |
| 91 |
); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
Popup::init(); |
| 96 |
|