| 1 |
<?php |
| 2 |
/** |
| 3 |
* Block: Manage Subscription |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @subpackage Core |
| 7 |
* @since 4.8.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace SimplePay\Core\Block; |
| 11 |
|
| 12 |
use SimplePay\Core\AntiSpam\Captcha\ScriptUtils; |
| 13 |
|
| 14 |
/** |
| 15 |
* ManageSubscriptionBlock class. |
| 16 |
* |
| 17 |
* @since 4.8.0 |
| 18 |
*/ |
| 19 |
class ManageSubscriptionsBlock extends AbstractBlock { |
| 20 |
|
| 21 |
/** |
| 22 |
* {@inheritdoc} |
| 23 |
*/ |
| 24 |
public function register() { |
| 25 |
|
| 26 |
$asset_file = SIMPLE_PAY_INC . 'pro/assets/js/dist/simpay-block-manage-subscriptions.asset.php'; |
| 27 |
|
| 28 |
if ( ! file_exists( $asset_file ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$script_data = require $asset_file; |
| 33 |
|
| 34 |
wp_register_script( |
| 35 |
'simpay-manage-subscriptions', |
| 36 |
SIMPLE_PAY_INC_URL . 'pro/assets/js/dist/simpay-block-manage-subscriptions.js', |
| 37 |
$script_data['dependencies'], |
| 38 |
$script_data['version'] |
| 39 |
); |
| 40 |
|
| 41 |
// Register the view script. |
| 42 |
wp_register_script( |
| 43 |
'simpay-manage-subscriptions-frontend', |
| 44 |
SIMPLE_PAY_INC_URL . 'pro/assets/js/dist/simpay-public-pro-manage-subscriptions.js', |
| 45 |
array( 'wp-api-fetch', 'simpay-shared' ), |
| 46 |
$script_data['version'], |
| 47 |
true |
| 48 |
); |
| 49 |
|
| 50 |
// Pass REST API url to frontend. |
| 51 |
wp_localize_script( |
| 52 |
'simpay-manage-subscriptions-frontend', |
| 53 |
'simpayManageSubscription', |
| 54 |
array( |
| 55 |
'rest_url' => 'wpsp/__internal__/send/subscriptions', |
| 56 |
'messages' => array( |
| 57 |
'wait_message' => __( 'Please wait...', 'stripe' ), |
| 58 |
'valid_email_warning' => __( 'Please enter a valid email address.', 'stripe' ), |
| 59 |
'request_error_message' => __( 'Request failed.', 'stripe' ), |
| 60 |
'captcha_error_message' => __( 'Invalid CAPTCHA. Please try again.', 'stripe' ), |
| 61 |
), |
| 62 |
) |
| 63 |
); |
| 64 |
|
| 65 |
register_block_type( |
| 66 |
'simpay/manage-subscriptions-block', |
| 67 |
array( |
| 68 |
'editor_script' => 'simpay-manage-subscriptions', |
| 69 |
'view_script' => 'simpay-manage-subscriptions-frontend', |
| 70 |
'render_callback' => array( $this, 'render' ), |
| 71 |
) |
| 72 |
); |
| 73 |
|
| 74 |
add_shortcode( 'simpay_manage_subscriptions', array( $this, 'render_shortcode' ) ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Returns the default attribute strings shared by the block render |
| 79 |
* callback and the shortcode handler. |
| 80 |
* |
| 81 |
* Keeping a single source of truth prevents the two entry points from |
| 82 |
* drifting out of sync over time. |
| 83 |
* |
| 84 |
* @since 4.17.3 |
| 85 |
* |
| 86 |
* @return array<string, string> Associative array of default strings keyed by |
| 87 |
* attribute identifier (`label`, |
| 88 |
* `email_placeholder`, `button_text`). |
| 89 |
*/ |
| 90 |
private function get_defaults() { |
| 91 |
return array( |
| 92 |
'label' => __( 'Purchase Email Address', 'stripe' ), |
| 93 |
'email_placeholder' => __( 'Enter your email', 'stripe' ), |
| 94 |
'button_text' => __( 'Manage Subscription', 'stripe' ), |
| 95 |
); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Renders the [simpay_manage_subscriptions] shortcode output. |
| 100 |
* |
| 101 |
* Supports the same optional attributes as the block: |
| 102 |
* label — email field label text |
| 103 |
* email_placeholder — email input placeholder |
| 104 |
* button_text — submit button label |
| 105 |
* |
| 106 |
* @since 4.17.3 |
| 107 |
* |
| 108 |
* @param array<string, string>|string $atts Shortcode attributes. |
| 109 |
* @return string Shortcode HTML output. |
| 110 |
*/ |
| 111 |
public function render_shortcode( $atts ) { |
| 112 |
$defaults = $this->get_defaults(); |
| 113 |
|
| 114 |
// WordPress passes an empty string for shortcodes used without |
| 115 |
// attributes; normalize to an array for shortcode_atts(). |
| 116 |
if ( ! is_array( $atts ) ) { |
| 117 |
$atts = array(); |
| 118 |
} |
| 119 |
|
| 120 |
$atts = shortcode_atts( |
| 121 |
$defaults, |
| 122 |
$atts, |
| 123 |
'simpay_manage_subscriptions' |
| 124 |
); |
| 125 |
|
| 126 |
wp_enqueue_script( 'simpay-manage-subscriptions-frontend' ); |
| 127 |
|
| 128 |
return $this->render( |
| 129 |
array( |
| 130 |
'label' => $atts['label'], |
| 131 |
'emailPlaceholder' => $atts['email_placeholder'], |
| 132 |
'buttonText' => $atts['button_text'], |
| 133 |
) |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Renders the block's output on the server. |
| 139 |
* |
| 140 |
* @since 4.7.11 |
| 141 |
* |
| 142 |
* @param array<mixed> $attributes The block attributes. |
| 143 |
* @return string Block content. |
| 144 |
*/ |
| 145 |
public function render( $attributes ) { |
| 146 |
$defaults = $this->get_defaults(); |
| 147 |
|
| 148 |
/** @var string $label */ |
| 149 |
$label = isset( $attributes['label'] ) ? $attributes['label'] : $defaults['label']; |
| 150 |
/** @var string $email_placeholder */ |
| 151 |
$email_placeholder = isset( $attributes['emailPlaceholder'] ) ? $attributes['emailPlaceholder'] : $defaults['email_placeholder']; |
| 152 |
/** @var string $button_text */ |
| 153 |
$button_text = isset( $attributes['buttonText'] ) ? $attributes['buttonText'] : $defaults['button_text']; |
| 154 |
|
| 155 |
// Enqueue captcha scripts. |
| 156 |
|
| 157 |
$action = 'manage-subscriptions'; |
| 158 |
ScriptUtils::enqueue_captcha_scripts(); |
| 159 |
$captcha_content = ScriptUtils::render_captcha( $action ); |
| 160 |
|
| 161 |
$form_format = ' |
| 162 |
<form id="simpay-manage-subscription-form" class="simpay-subscription-management-form"> |
| 163 |
<div id="messageContainer" class="form-message-container"> |
| 164 |
<div class="form-message"></div> |
| 165 |
</div> |
| 166 |
<p> |
| 167 |
<label for="simpay-ms-email">%1$s</label> |
| 168 |
<input class="form-input-email" id="simpay-ms-email" type="email" placeholder="%2$s" /> |
| 169 |
</p> |
| 170 |
' . $captcha_content . ' |
| 171 |
<p class="form-submit wp-block-button"> |
| 172 |
<input type="submit" id="simpay-ms-submit-btn" class="wp-block-button__link wp-element-button form-button" value="%3$s" /> |
| 173 |
</p> |
| 174 |
</form>'; |
| 175 |
|
| 176 |
return sprintf( |
| 177 |
$form_format, |
| 178 |
esc_html( $label ), |
| 179 |
esc_attr( $email_placeholder ), |
| 180 |
esc_html( $button_text ) |
| 181 |
); |
| 182 |
} |
| 183 |
} |
| 184 |
|