donations.php
265 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 | 'planId' => null, |
| 62 | 'title' => __( 'One-Time', 'jetpack' ), |
| 63 | 'class' => 'donations__one-time-item', |
| 64 | 'heading' => $default_texts['oneTimeDonation']['heading'], |
| 65 | 'buttonText' => $default_texts['oneTimeDonation']['buttonText'], |
| 66 | ), |
| 67 | $attr['oneTimeDonation'] |
| 68 | ), |
| 69 | ); |
| 70 | if ( $attr['monthlyDonation']['show'] ) { |
| 71 | $donations['1 month'] = array_merge( |
| 72 | array( |
| 73 | 'planId' => null, |
| 74 | 'title' => __( 'Monthly', 'jetpack' ), |
| 75 | 'class' => 'donations__monthly-item', |
| 76 | 'heading' => $default_texts['monthlyDonation']['heading'], |
| 77 | 'buttonText' => $default_texts['monthlyDonation']['buttonText'], |
| 78 | ), |
| 79 | $attr['monthlyDonation'] |
| 80 | ); |
| 81 | } |
| 82 | if ( $attr['annualDonation']['show'] ) { |
| 83 | $donations['1 year'] = array_merge( |
| 84 | array( |
| 85 | 'planId' => null, |
| 86 | 'title' => __( 'Yearly', 'jetpack' ), |
| 87 | 'class' => 'donations__annual-item', |
| 88 | 'heading' => $default_texts['annualDonation']['heading'], |
| 89 | 'buttonText' => $default_texts['annualDonation']['buttonText'], |
| 90 | ), |
| 91 | $attr['annualDonation'] |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | $choose_amount_text = isset( $attr['chooseAmountText'] ) && ! empty( $attr['chooseAmountText'] ) ? $attr['chooseAmountText'] : $default_texts['chooseAmountText']; |
| 96 | $custom_amount_text = isset( $attr['customAmountText'] ) && ! empty( $attr['customAmountText'] ) ? $attr['customAmountText'] : $default_texts['customAmountText']; |
| 97 | $currency = $attr['currency']; |
| 98 | $nav = ''; |
| 99 | $headings = ''; |
| 100 | $amounts = ''; |
| 101 | $extra_text = ''; |
| 102 | $buttons = ''; |
| 103 | foreach ( $donations as $interval => $donation ) { |
| 104 | $plan_id = (int) $donation['planId']; |
| 105 | $plan = get_post( $plan_id ); |
| 106 | if ( ! $plan || is_wp_error( $plan ) ) { |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | if ( count( $donations ) > 1 ) { |
| 111 | if ( ! $nav ) { |
| 112 | $nav .= '<div class="donations__nav">'; |
| 113 | } |
| 114 | $nav .= sprintf( |
| 115 | '<div role="button" tabindex="0" class="donations__nav-item" data-interval="%1$s">%2$s</div>', |
| 116 | esc_attr( $interval ), |
| 117 | esc_html( $donation['title'] ) |
| 118 | ); |
| 119 | } |
| 120 | $headings .= sprintf( |
| 121 | '<h4 class="%1$s">%2$s</h4>', |
| 122 | esc_attr( $donation['class'] ), |
| 123 | wp_kses_post( $donation['heading'] ) |
| 124 | ); |
| 125 | $amounts .= sprintf( |
| 126 | '<div class="donations__amounts %s">', |
| 127 | esc_attr( $donation['class'] ) |
| 128 | ); |
| 129 | foreach ( $donation['amounts'] as $amount ) { |
| 130 | $amounts .= sprintf( |
| 131 | '<div class="donations__amount" data-amount="%1$s">%2$s</div>', |
| 132 | esc_attr( $amount ), |
| 133 | esc_html( \Jetpack_Currencies::format_price( $amount, $currency ) ) |
| 134 | ); |
| 135 | } |
| 136 | $amounts .= '</div>'; |
| 137 | $extra_text .= sprintf( |
| 138 | '<p class="%1$s">%2$s</p>', |
| 139 | esc_attr( $donation['class'] ), |
| 140 | wp_kses_post( $donation['extraText'] ?? $default_texts['extraText'] ) |
| 141 | ); |
| 142 | $buttons .= sprintf( |
| 143 | '<a class="wp-block-button__link donations__donate-button %1$s" href="%2$s">%3$s</a>', |
| 144 | esc_attr( $donation['class'] ), |
| 145 | esc_url( \Jetpack_Memberships::get_instance()->get_subscription_url( $plan_id ) ), |
| 146 | wp_kses_post( $donation['buttonText'] ) |
| 147 | ); |
| 148 | } |
| 149 | if ( $nav ) { |
| 150 | $nav .= '</div>'; |
| 151 | } |
| 152 | |
| 153 | $custom_amount = ''; |
| 154 | if ( $attr['showCustomAmount'] ) { |
| 155 | $custom_amount .= sprintf( |
| 156 | '<p>%s</p>', |
| 157 | wp_kses_post( $custom_amount_text ) |
| 158 | ); |
| 159 | $default_custom_amount = ( \Jetpack_Memberships::SUPPORTED_CURRENCIES[ $currency ] ?? 1 ) * 100; |
| 160 | $custom_amount .= sprintf( |
| 161 | '<div class="donations__amount donations__custom-amount"> |
| 162 | %1$s |
| 163 | <div class="donations__amount-value" data-currency="%2$s" data-empty-text="%3$s"></div> |
| 164 | </div>', |
| 165 | esc_html( \Jetpack_Currencies::CURRENCIES[ $currency ]['symbol'] ?? '¤' ), |
| 166 | esc_attr( $currency ), |
| 167 | esc_attr( \Jetpack_Currencies::format_price( $default_custom_amount, $currency, false ) ) |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | return sprintf( |
| 172 | ' |
| 173 | <div class="%1$s"> |
| 174 | <div class="donations__container"> |
| 175 | %2$s |
| 176 | <div class="donations__content"> |
| 177 | <div class="donations__tab"> |
| 178 | %3$s |
| 179 | <p>%4$s</p> |
| 180 | %5$s |
| 181 | %6$s |
| 182 | <hr class="donations__separator"> |
| 183 | %7$s |
| 184 | %8$s |
| 185 | </div> |
| 186 | </div> |
| 187 | </div> |
| 188 | </div> |
| 189 | ', |
| 190 | esc_attr( Blocks::classes( Blocks::get_block_feature( __DIR__ ), $attr ) ), |
| 191 | $nav, |
| 192 | $headings, |
| 193 | $choose_amount_text, |
| 194 | $amounts, |
| 195 | $custom_amount, |
| 196 | $extra_text, |
| 197 | $buttons |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Get the default texts for the block. |
| 203 | * |
| 204 | * @return array |
| 205 | */ |
| 206 | function get_default_texts() { |
| 207 | return array( |
| 208 | 'chooseAmountText' => __( 'Choose an amount', 'jetpack' ), |
| 209 | 'customAmountText' => __( 'Or enter a custom amount', 'jetpack' ), |
| 210 | 'extraText' => __( 'Your contribution is appreciated.', 'jetpack' ), |
| 211 | 'oneTimeDonation' => array( |
| 212 | 'heading' => __( 'Make a one-time donation', 'jetpack' ), |
| 213 | 'buttonText' => __( 'Donate', 'jetpack' ), |
| 214 | ), |
| 215 | 'monthlyDonation' => array( |
| 216 | 'heading' => __( 'Make a monthly donation', 'jetpack' ), |
| 217 | 'buttonText' => __( 'Donate monthly', 'jetpack' ), |
| 218 | ), |
| 219 | 'annualDonation' => array( |
| 220 | 'heading' => __( 'Make a yearly donation', 'jetpack' ), |
| 221 | 'buttonText' => __( 'Donate yearly', 'jetpack' ), |
| 222 | ), |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Make default texts available to the editor. |
| 228 | */ |
| 229 | function load_editor_scripts() { |
| 230 | // Only relevant to the editor right now. |
| 231 | if ( ! is_admin() ) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | $data = array( |
| 236 | 'defaultTexts' => get_default_texts(), |
| 237 | ); |
| 238 | |
| 239 | wp_add_inline_script( |
| 240 | 'jetpack-blocks-editor', |
| 241 | 'var Jetpack_DonationsBlock = ' . wp_json_encode( $data, JSON_HEX_TAG | JSON_HEX_AMP ) . ';', |
| 242 | 'before' |
| 243 | ); |
| 244 | } |
| 245 | add_action( 'enqueue_block_assets', __NAMESPACE__ . '\load_editor_scripts' ); |
| 246 | |
| 247 | /** |
| 248 | * Determine if AMP should be disabled on posts having Donations blocks. |
| 249 | * |
| 250 | * @param bool $skip Skipped. |
| 251 | * @param int $post_id Post ID. |
| 252 | * @param WP_Post $post Post. |
| 253 | * |
| 254 | * @return bool Whether to skip the post from AMP. |
| 255 | */ |
| 256 | function amp_skip_post( $skip, $post_id, $post ) { |
| 257 | // When AMP is on standard mode, there are no non-AMP posts to link to where the donation can be completed, so let's |
| 258 | // prevent the post from being available in AMP. |
| 259 | if ( function_exists( 'amp_is_canonical' ) && \amp_is_canonical() && has_block( Blocks::get_block_name( __DIR__ ), $post->post_content ) ) { |
| 260 | return true; |
| 261 | } |
| 262 | return $skip; |
| 263 | } |
| 264 | add_filter( 'amp_skip_post', __NAMESPACE__ . '\amp_skip_post', 10, 3 ); |
| 265 |