Add goal progress bar to donate form and [streamgoals_progress] shortcode
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
353f7ca5be
commit
b80010f422
7 files changed
+397
-9
No files matched your search
@@ -6,6 +6,7 @@ namespace StreamGoals\Rest;
|
||||
use StreamGoals\Core\GoalTargetPolicy;
|
||||
use StreamGoals\Core\Money;
|
||||
use StreamGoals\Core\Settings;
|
||||
use StreamGoals\Overlay\Progress;
|
||||
use StreamGoals\PayPal\CaptureFinalizer;
|
||||
use StreamGoals\PayPal\Client;
|
||||
use StreamGoals\PayPal\DonationStore;
|
||||
@@ -27,6 +28,7 @@ final class DonateController {
|
||||
public static function register(): void {
|
||||
add_action( 'rest_api_init', array( self::class, 'routes' ) );
|
||||
add_shortcode( 'streamgoals_donate', array( self::class, 'shortcode' ) );
|
||||
add_shortcode( 'streamgoals_progress', array( self::class, 'progress_shortcode' ) );
|
||||
}
|
||||
|
||||
public static function routes(): void {
|
||||
@@ -57,10 +59,35 @@ final class DonateController {
|
||||
'permission_callback' => '__return_true',
|
||||
)
|
||||
);
|
||||
register_rest_route(
|
||||
self::NS,
|
||||
'/progress',
|
||||
array(
|
||||
'methods' => 'GET',
|
||||
'callback' => array( self::class, 'progress_route' ),
|
||||
'permission_callback' => '__return_true',
|
||||
'args' => array( 'goal' => array( 'sanitize_callback' => 'absint' ) ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- REST */
|
||||
|
||||
/** Public, read-only totals for the progress bar. Whitelisted goal figures only; never donor data. */
|
||||
public static function progress_route( \WP_REST_Request $req ) {
|
||||
if ( $limited = self::limit( 'progress', 60, 60 ) ) {
|
||||
return $limited;
|
||||
}
|
||||
$goal = DonationStore::goal( (int) $req->get_param( 'goal' ) );
|
||||
if ( ! $goal || ! in_array( (string) $goal['status'], array( 'active', 'complete' ), true ) ) {
|
||||
return new \WP_Error( 'sg_goal_not_found', __( 'Goal not found.', 'streamgoals-donate' ), array( 'status' => 404 ) );
|
||||
}
|
||||
$res = rest_ensure_response( self::progress_for( $goal ) );
|
||||
// Short shared cache lets a CDN collapse bursts; the page JS adds a cache-busting parameter after a donation.
|
||||
$res->header( 'Cache-Control', 'public, max-age=10, s-maxage=10' );
|
||||
return $res;
|
||||
}
|
||||
|
||||
public static function create_order( \WP_REST_Request $req ) {
|
||||
if ( $limited = self::limit( 'order', 10, 300 ) ) {
|
||||
return $limited;
|
||||
@@ -159,8 +186,9 @@ final class DonateController {
|
||||
/* ----------------------------------------------------------- Shortcode */
|
||||
|
||||
public static function shortcode( $atts ): string {
|
||||
$atts = shortcode_atts( array( 'goal' => '' ), (array) $atts, 'streamgoals_donate' );
|
||||
$atts = shortcode_atts( array( 'goal' => '', 'show_progress' => 'yes' ), (array) $atts, 'streamgoals_donate' );
|
||||
$pinned = (int) $atts['goal'];
|
||||
$showP = Progress::flag( $atts['show_progress'], true );
|
||||
$s = Settings::all();
|
||||
if ( '' === (string) $s['paypal_client_id'] ) {
|
||||
return '<p class="sg-donate-unavailable">' . esc_html__( 'Donations are not available right now.', 'streamgoals-donate' ) . '</p>';
|
||||
@@ -179,13 +207,15 @@ final class DonateController {
|
||||
'currency' => strtoupper( (string) $g['currency'] ),
|
||||
'exp' => Money::exponent( (string) $g['currency'] ),
|
||||
'actions' => $actions[ (int) $g['id'] ] ?? array(),
|
||||
);
|
||||
) + ( $showP ? array( 'progress' => self::progress_for( $g ) ) : array() );
|
||||
}
|
||||
if ( ! $goals ) {
|
||||
$reached = $pinned ? DonationStore::goal( $pinned ) : null;
|
||||
$reached = $pinned ? ( $reached && GoalTargetPolicy::isClosedAtTarget( $reached ) ) : (bool) DonationStore::reached_goals();
|
||||
if ( $reached ) {
|
||||
return '<p class="sg-donate-unavailable sg-donate-reached">' . esc_html__( 'This goal has been reached - thank you for your support!', 'streamgoals-donate' ) . '</p>';
|
||||
$rg = $pinned ? DonationStore::goal( $pinned ) : ( DonationStore::reached_goals()[0] ?? null );
|
||||
$bar = ( $showP && $rg ) ? self::progress_markup( self::progress_for( $rg ) ) : '';
|
||||
return $bar . '<p class="sg-donate-unavailable sg-donate-reached">' . esc_html__( 'This goal has been reached - thank you for your support!', 'streamgoals-donate' ) . '</p>';
|
||||
}
|
||||
return '<p class="sg-donate-unavailable">' . esc_html__( 'There is no active goal to donate to right now.', 'streamgoals-donate' ) . '</p>';
|
||||
}
|
||||
@@ -231,11 +261,68 @@ final class DonateController {
|
||||
);
|
||||
wp_add_inline_script( 'streamgoals-donate', 'window.StreamGoalsDonate=' . wp_json_encode( $config, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) . ';', 'before' );
|
||||
|
||||
return '<div class="sg-donate" data-goal="' . esc_attr( (string) $pinned ) . '"></div>';
|
||||
self::enqueue_progress();
|
||||
$first = $showP ? $goals[0]['progress'] : null;
|
||||
return '<div class="sg-donate" data-goal="' . esc_attr( (string) $pinned ) . '"' . ( $showP ? ' data-progress="1"' : '' ) . '>'
|
||||
. ( $first ? self::progress_markup( $first ) : '' ) . '</div>';
|
||||
}
|
||||
|
||||
/** [streamgoals_progress goal=""]: the same bar without the form. Server-rendered; JS refreshes it. */
|
||||
public static function progress_shortcode( $atts ): string {
|
||||
$atts = shortcode_atts( array( 'goal' => '' ), (array) $atts, 'streamgoals_progress' );
|
||||
$id = (int) $atts['goal'];
|
||||
$goal = $id ? DonationStore::goal( $id ) : ( DonationStore::active_goals()[0] ?? ( DonationStore::reached_goals()[0] ?? null ) );
|
||||
if ( ! $goal || ! in_array( (string) $goal['status'], array( 'active', 'complete' ), true ) ) {
|
||||
return '';
|
||||
}
|
||||
self::enqueue_progress();
|
||||
return '<div class="sg-donate sg-progress-only">' . self::progress_markup( self::progress_for( $goal ) ) . '</div>';
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------- Helpers */
|
||||
|
||||
/** @return array<string,mixed> Progress::payload() for a goal row (same raised basis and tiers as the overlay). */
|
||||
private static function progress_for( array $goal ): array {
|
||||
global $wpdb;
|
||||
$id = (int) $goal['id'];
|
||||
$tiers = array();
|
||||
try {
|
||||
$tiers = ( new \StreamGoals\Tiers\TierRepository( $wpdb ) )->forGoal( $id );
|
||||
} catch ( \Throwable $e ) {
|
||||
$tiers = array();
|
||||
}
|
||||
return Progress::payload( $goal, ( new \StreamGoals\Overlay\WpdbDataSource() )->raisedMinor( $goal ), $tiers ?: null );
|
||||
}
|
||||
|
||||
private static function progress_i18n(): array {
|
||||
return array(
|
||||
'reached' => __( 'Goal reached', 'streamgoals-donate' ),
|
||||
/* translators: 1: formatted amount raised, 2: formatted target */
|
||||
'of' => __( '%1$s of %2$s', 'streamgoals-donate' ),
|
||||
/* translators: 1: current tier name, 2: formatted tier target */
|
||||
'tier' => __( 'Current tier: %1$s (target %2$s)', 'streamgoals-donate' ),
|
||||
/* translators: %s: goal title */
|
||||
'label' => __( 'Progress towards %s', 'streamgoals-donate' ),
|
||||
);
|
||||
}
|
||||
|
||||
private static function progress_markup( array $p ): string {
|
||||
return Progress::html( $p, self::progress_i18n() );
|
||||
}
|
||||
|
||||
/** Enqueue assets and the (once per page) config the bar's JS needs. */
|
||||
private static function enqueue_progress(): void {
|
||||
static $done = false;
|
||||
wp_enqueue_style( 'streamgoals-donate', STREAMGOALS_URL . 'assets/donate/donate.css', array(), STREAMGOALS_VERSION );
|
||||
wp_enqueue_script( 'streamgoals-donate', STREAMGOALS_URL . 'assets/donate/donate.js', array(), STREAMGOALS_VERSION, true );
|
||||
if ( $done ) {
|
||||
return;
|
||||
}
|
||||
$done = true;
|
||||
$cfg = array( 'restUrl' => esc_url_raw( rest_url( self::NS ) ), 'i18n' => self::progress_i18n(), 'pollMs' => 30000 );
|
||||
wp_add_inline_script( 'streamgoals-donate', 'window.StreamGoalsProgress=' . wp_json_encode( $cfg, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) . ';', 'before' );
|
||||
}
|
||||
|
||||
/**
|
||||
* "Next unlock" hint for a tiered goal: the current (first unreached) tier's title and the amount still needed, in minor units.
|
||||
* Null for goals without tiers or with every tier reached. Computed when the page renders.
|
||||
|
||||
Reference in new issue
Block a user