。JS は二重送信の抑止だけを足す。
* - 「これまでに N人から」の実績表示はフェーズ5(集計)の担当。ここでは出さない。
*/
class ACSCW_Tipping {
/** ブロック名。 */
const BLOCK_NAME = 'acscw/tipping';
/**
* 投げ銭ブロックを描画する。
*
* @param array $attributes ブロック属性。
* @param string $content 内容(未使用)。
* @param WP_Block|null $block ブロックインスタンス。
* @return string
*/
public function render( $attributes, $content = '', $block = null ) {
unset( $content, $block );
$attributes = (array) $attributes;
$post_id = ACSCW_Paywall::current_post_id();
$buttons = $this->build_buttons( $post_id );
if ( empty( $buttons ) ) {
// 送れる金額が1つも無いなら、空の箱を置かない。
return '';
}
ACSCW_Blocks::ensure_assets();
$title = $this->get_attribute( $attributes, 'title', __( 'Support this article', 'acs-content-for-woocommerce' ) );
$lede = $this->get_attribute( $attributes, 'description', __( 'If you enjoyed it, just pick an amount to send.', 'acs-content-for-woocommerce' ) );
$html = '
get_wrapper_attributes() . '>';
$html .= sprintf( '
%s
', esc_html( $title ) );
if ( '' !== $lede ) {
$html .= sprintf( '
%s
', esc_html( $lede ) );
}
$html .= '
';
foreach ( $buttons as $button ) {
$html .= sprintf(
'
%2$s',
esc_url( $button['url'] ),
wp_kses_post( $button['label'] )
);
}
$html .= '
';
$html .= sprintf(
'
%s
',
esc_html__( 'Pick an amount and you go straight to checkout.', 'acs-content-for-woocommerce' )
);
$html .= '
';
return $html;
}
/**
* 金額ボタンの一覧を作る。
*
* @param int $post_id 戻り先のコンテンツID。
* @return array
*/
private function build_buttons( $post_id ) {
$buttons = array();
foreach ( ACSCW_Products::get_tip_amounts() as $amount ) {
$url = ACSCW_Checkout::get_tip_url( $amount, $post_id );
if ( '' === $url ) {
continue;
}
$buttons[] = array(
'url' => $url,
'label' => ACSCW_Products::format_price( $amount ),
);
}
return $buttons;
}
/**
* ブロック属性を文字列で取り出す。
*
* @param array $attributes ブロック属性。
* @param string $key キー。
* @param string $fallback 既定値。
* @return string
*/
private function get_attribute( $attributes, $key, $fallback = '' ) {
if ( ! isset( $attributes[ $key ] ) || ! is_scalar( $attributes[ $key ] ) ) {
return $fallback;
}
$value = trim( (string) $attributes[ $key ] );
return ( '' === $value ) ? $fallback : $value;
}
/**
* ラッパー要素の属性を返す。
*
* @return string
*/
private function get_wrapper_attributes() {
if ( function_exists( 'get_block_wrapper_attributes' ) ) {
return get_block_wrapper_attributes( array( 'class' => 'acscw-tip' ) );
}
return 'class="acscw-tip"';
}
}