| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly. |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* Registers the block using the metadata loaded from the `block.json` file. |
| 8 |
* Behind the scenes, it registers also all assets so they can be enqueued |
| 9 |
* through the block editor in the corresponding context. |
| 10 |
* |
| 11 |
* @see https://developer.wordpress.org/reference/functions/register_block_type/ |
| 12 |
*/ |
| 13 |
register_block_type( __DIR__ . '/build/login', |
| 14 |
[ |
| 15 |
'render_callback' => function( $attributes, $content ) { |
| 16 |
ob_start(); |
| 17 |
do_action( 'wppb/login/render_callback', $attributes, $content ); |
| 18 |
return ob_get_clean(); |
| 19 |
}, |
| 20 |
] |
| 21 |
); |
| 22 |
|
| 23 |
add_action( |
| 24 |
'admin_enqueue_scripts', |
| 25 |
function () { |
| 26 |
$args = array( |
| 27 |
'post_type' => 'page', |
| 28 |
'posts_per_page' => -1 |
| 29 |
); |
| 30 |
|
| 31 |
if( function_exists( 'wc_get_page_id' ) ) |
| 32 |
$args['exclude'] = wc_get_page_id( 'shop' ); |
| 33 |
|
| 34 |
$all_pages = get_posts( $args ); |
| 35 |
|
| 36 |
$url_options[] = [ "label" => "", "value" => "" ]; |
| 37 |
if( !empty( $all_pages ) ) { |
| 38 |
foreach ( $all_pages as $page ) { |
| 39 |
$url_options[] = [ "label" => esc_html( $page->post_title ) , "value" => esc_url( get_page_link( $page->ID ) ) ]; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// Add pre-loaded data for my-namespace/my-block |
| 44 |
wp_add_inline_script('wppb-login-editor-script', 'window.wppbLoginBlockConfig = ' . json_encode(array( |
| 45 |
'wppb_paid' => defined( 'WPPB_PAID_PLUGIN_DIR' ), |
| 46 |
'is_2fa_active' => wppb_is_2fa_active(), |
| 47 |
'url_options' => $url_options, |
| 48 |
)), 'before'); |
| 49 |
} |
| 50 |
); |
| 51 |
|
| 52 |
/** |
| 53 |
* Render: PHP. |
| 54 |
* |
| 55 |
* @param array $attributes Optional. Block attributes. Default empty array. |
| 56 |
* @param string $content Optional. Block content. Default empty string. |
| 57 |
*/ |
| 58 |
add_action( |
| 59 |
'wppb/login/render_callback', |
| 60 |
function( $attributes, $content ) { |
| 61 |
if ( isset($attributes['is_preview']) && $attributes['is_preview'] === 'true' ) { |
| 62 |
echo ' |
| 63 |
<svg |
| 64 |
xmlns="http://www.w3.org/2000/svg" |
| 65 |
fill="none" |
| 66 |
viewBox="0 0 230 130" |
| 67 |
style="width: "100%";" |
| 68 |
> |
| 69 |
<title>Login Block Preview</title> |
| 70 |
<rect |
| 71 |
width="36.328499" |
| 72 |
height="10.030812" |
| 73 |
x="35.712097" |
| 74 |
y="82.629547" |
| 75 |
rx="1.9783683" |
| 76 |
id="rect4" |
| 77 |
style="fill:#a0a5aa;stroke-width:1.22376513" /> |
| 78 |
<rect |
| 79 |
width="32.748241" |
| 80 |
height="15" |
| 81 |
x="28.485535" |
| 82 |
y="27.950914" |
| 83 |
rx="2.6073439" |
| 84 |
id="rect6" |
| 85 |
style="fill:#a0a5aa;stroke-width:0.72212797" /> |
| 86 |
<rect |
| 87 |
width="6.177" |
| 88 |
height="6.177" |
| 89 |
x="28.503256" |
| 90 |
y="84.55645" |
| 91 |
rx="3" |
| 92 |
id="rect38" |
| 93 |
style="fill:#a0a5aa" /> |
| 94 |
<rect |
| 95 |
width="49.609749" |
| 96 |
height="10.030812" |
| 97 |
x="28.485535" |
| 98 |
y="53.187717" |
| 99 |
rx="2.7016351" |
| 100 |
id="rect4-3" |
| 101 |
style="fill:#a0a5aa;stroke-width:1.43007195" /> |
| 102 |
<rect |
| 103 |
width="47.265999" |
| 104 |
height="10.030812" |
| 105 |
x="28.485535" |
| 106 |
y="67.738503" |
| 107 |
rx="2.5739999" |
| 108 |
id="rect4-6" |
| 109 |
style="fill:#a0a5aa;stroke-width:1.39588225" /> |
| 110 |
<rect |
| 111 |
width="28.646679" |
| 112 |
height="15" |
| 113 |
x="28.485535" |
| 114 |
y="95.820312" |
| 115 |
rx="2.2807865" |
| 116 |
id="rect6-7" |
| 117 |
style="fill:#a0a5aa;stroke-width:0.67539418" /> |
| 118 |
<rect |
| 119 |
width="120.60584" |
| 120 |
height="10.030812" |
| 121 |
x="81.445129" |
| 122 |
y="53.187717" |
| 123 |
rx="6.5679221" |
| 124 |
id="rect4-3-5" |
| 125 |
style="fill:#a0a5aa;stroke-width:2.22976208" /> |
| 126 |
<rect |
| 127 |
width="120.60584" |
| 128 |
height="10.030812" |
| 129 |
x="81.445129" |
| 130 |
y="67.738503" |
| 131 |
rx="6.5679221" |
| 132 |
id="rect4-3-5-5" |
| 133 |
style="fill:#a0a5aa;stroke-width:2.22976208" /> |
| 134 |
</svg>'; |
| 135 |
} else { |
| 136 |
$atts = [ |
| 137 |
'redirect_url' => $attributes['redirect_url'] !== '' ? ' redirect_url="' . esc_url( $attributes['redirect_url'] ) . '"' : '', |
| 138 |
'logout_redirect_url' => $attributes['logout_redirect_url'] !== '' ? ' logout_redirect_url="' . esc_url( $attributes['logout_redirect_url'] ) . '"' : '', |
| 139 |
'register_url' => $attributes['register_url'] !== '' ? ' register_url="' . esc_url( $attributes['register_url'] ) . '"' : '', |
| 140 |
'lostpassword_url' => $attributes['lostpassword_url'] !== '' ? ' lostpassword_url="' . esc_url( $attributes['lostpassword_url'] ) . '"' : '', |
| 141 |
'show_2fa_field' => $attributes['auth_field'] ? ' show_2fa_field="yes"' : '', |
| 142 |
'block' => $attributes['is_editor'] ? ' block="true"' : '', |
| 143 |
'ajax' => $attributes['ajax'] ? ' ajax="true"' : '', |
| 144 |
]; |
| 145 |
echo '<div class="wppb-block-container">' . do_shortcode( '[wppb-login' . $atts['redirect_url'] . $atts['logout_redirect_url'] . $atts['register_url'] . $atts['lostpassword_url'] . $atts['show_2fa_field'] . $atts['block'] . $atts['ajax'] . ' ]' ) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 146 |
} |
| 147 |
} |
| 148 |
, 10, 2 ); |
| 149 |
|