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