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