Plugin Directory

source: jetpack/trunk/extensions/blocks/donations/donations.php @ 3068647

Last change on this file since 3068647 was 3068647, checked in by zinigor, 3 months ago

Updating trunk to version 13.3.1

File size: 5.3 KB
Line 
1<?php
2/**
3 * Donations Block.
4 *
5 * @since 8.x
6 *
7 * @package automattic/jetpack
8 */
9
10namespace Automattic\Jetpack\Extensions\Donations;
11
12use Automattic\Jetpack\Blocks;
13use Jetpack_Gutenberg;
14
15/**
16 * Registers the block for use in Gutenberg
17 * This is done via an action so that we can disable
18 * registration if we need to.
19 */
20function register_block() {
21        Blocks::jetpack_register_block(
22                __DIR__,
23                array(
24                        'render_callback' => __NAMESPACE__ . '\render_block',
25                )
26        );
27}
28add_action( 'init', __NAMESPACE__ . '\register_block' );
29
30/**
31 * Donations block dynamic rendering.
32 *
33 * @param array  $attr    Array containing the Donations block attributes.
34 * @param string $content String containing the Donations block content.
35 *
36 * @return string
37 */
38function render_block( $attr, $content ) {
39        // Keep content as-is if rendered in other contexts than frontend (i.e. feed, emails, API, etc.).
40        if ( ! jetpack_is_frontend() ) {
41                return $content;
42        }
43
44        require_once JETPACK__PLUGIN_DIR . 'modules/memberships/class-jetpack-memberships.php';
45
46        // If stripe isn't connected don't show anything to potential donors - they can't actually make a donation.
47        if ( ! \Jetpack_Memberships::has_connected_account() ) {
48                return '';
49        }
50
51        Jetpack_Gutenberg::load_assets_as_required( __DIR__ );
52
53        require_once JETPACK__PLUGIN_DIR . '/_inc/lib/class-jetpack-currencies.php';
54
55        $donations = array(
56                'one-time' => array_merge(
57                        array(
58                                'title' => __( 'One-Time', 'jetpack' ),
59                                'class' => 'donations__one-time-item',
60                        ),
61                        $attr['oneTimeDonation']
62                ),
63        );
64        if ( $attr['monthlyDonation']['show'] ) {
65                $donations['1 month'] = array_merge(
66                        array(
67                                'title' => __( 'Monthly', 'jetpack' ),
68                                'class' => 'donations__monthly-item',
69                        ),
70                        $attr['monthlyDonation']
71                );
72        }
73        if ( $attr['annualDonation']['show'] ) {
74                $donations['1 year'] = array_merge(
75                        array(
76                                'title' => __( 'Yearly', 'jetpack' ),
77                                'class' => 'donations__annual-item',
78                        ),
79                        $attr['annualDonation']
80                );
81        }
82
83        $currency   = $attr['currency'];
84        $nav        = '';
85        $headings   = '';
86        $amounts    = '';
87        $extra_text = '';
88        $buttons    = '';
89        foreach ( $donations as $interval => $donation ) {
90                $plan_id = (int) $donation['planId'];
91                $plan    = get_post( $plan_id );
92                if ( ! $plan || is_wp_error( $plan ) ) {
93                        continue;
94                }
95
96                if ( count( $donations ) > 1 ) {
97                        if ( ! $nav ) {
98                                $nav .= '<div class="donations__nav">';
99                        }
100                        $nav .= sprintf(
101                                '<div role="button" tabindex="0" class="donations__nav-item" data-interval="%1$s">%2$s</div>',
102                                esc_attr( $interval ),
103                                esc_html( $donation['title'] )
104                        );
105                }
106                $headings .= sprintf(
107                        '<h4 class="%1$s">%2$s</h4>',
108                        esc_attr( $donation['class'] ),
109                        wp_kses_post( $donation['heading'] )
110                );
111                $amounts  .= sprintf(
112                        '<div class="donations__amounts %s">',
113                        esc_attr( $donation['class'] )
114                );
115                foreach ( $donation['amounts'] as $amount ) {
116                        $amounts .= sprintf(
117                                '<div class="donations__amount" data-amount="%1$s">%2$s</div>',
118                                esc_attr( $amount ),
119                                esc_html( \Jetpack_Currencies::format_price( $amount, $currency ) )
120                        );
121                }
122                $amounts    .= '</div>';
123                $extra_text .= sprintf(
124                        '<p class="%1$s">%2$s</p>',
125                        esc_attr( $donation['class'] ),
126                        wp_kses_post( $donation['extraText'] )
127                );
128                $buttons    .= sprintf(
129                        '<a class="wp-block-button__link donations__donate-button %1$s" href="%2$s">%3$s</a>',
130                        esc_attr( $donation['class'] ),
131                        esc_url( \Jetpack_Memberships::get_instance()->get_subscription_url( $plan_id ) ),
132                        wp_kses_post( $donation['buttonText'] )
133                );
134        }
135        if ( $nav ) {
136                $nav .= '</div>';
137        }
138
139        $custom_amount = '';
140        if ( $attr['showCustomAmount'] ) {
141                $custom_amount        .= sprintf(
142                        '<p>%s</p>',
143                        wp_kses_post( $attr['customAmountText'] )
144                );
145                $default_custom_amount = \Jetpack_Memberships::SUPPORTED_CURRENCIES[ $currency ] * 100;
146                $custom_amount        .= sprintf(
147                        '<div class="donations__amount donations__custom-amount">
148                                %1$s
149                                <div class="donations__amount-value" data-currency="%2$s" data-empty-text="%3$s"></div>
150                        </div>',
151                        esc_html( \Jetpack_Currencies::CURRENCIES[ $attr['currency'] ]['symbol'] ),
152                        esc_attr( $attr['currency'] ),
153                        esc_attr( \Jetpack_Currencies::format_price( $default_custom_amount, $currency, false ) )
154                );
155        }
156
157        return sprintf(
158                '
159<div class="%1$s">
160        <div class="donations__container">
161                %2$s
162                <div class="donations__content">
163                        <div class="donations__tab">
164                                %3$s
165                                <p>%4$s</p>
166                                %5$s
167                                %6$s
168                                <hr class="donations__separator">
169                                %7$s
170                                %8$s
171                        </div>
172                </div>
173        </div>
174</div>
175',
176                esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ),
177                $nav,
178                $headings,
179                $attr['chooseAmountText'],
180                $amounts,
181                $custom_amount,
182                $extra_text,
183                $buttons
184        );
185}
186
187/**
188 * Determine if AMP should be disabled on posts having Donations blocks.
189 *
190 * @param bool    $skip Skipped.
191 * @param int     $post_id Post ID.
192 * @param WP_Post $post Post.
193 *
194 * @return bool Whether to skip the post from AMP.
195 */
196function amp_skip_post( $skip, $post_id, $post ) {
197        // When AMP is on standard mode, there are no non-AMP posts to link to where the donation can be completed, so let's
198        // prevent the post from being available in AMP.
199        if ( function_exists( 'amp_is_canonical' ) && \amp_is_canonical() && has_block( Blocks::get_block_name( __DIR__ ), $post->post_content ) ) {
200                return true;
201        }
202        return $skip;
203}
204add_filter( 'amp_skip_post', __NAMESPACE__ . '\amp_skip_post', 10, 3 );
Note: See TracBrowser for help on using the repository browser.