Add admin UI: setup wizard, goals, rules editor, donations, moderation, overlays, settings
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0b9e6e7cbb
commit
4efdef72f6
18 files changed
+2169
No files matched your search
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace StreamGoals\Admin;
|
||||
|
||||
/** The 4-step setup wizard: PayPal, first goal, donation page, overlays. */
|
||||
final class SetupPage {
|
||||
|
||||
private const STEPS = array( 1 => 'PayPal', 2 => 'First goal', 3 => 'Donation page', 4 => 'Overlays' );
|
||||
|
||||
public static function url( int $step = 1 ): string {
|
||||
$page = PayPalConnector::isConnected() ? 'streamgoals-setup' : Admin::HOME;
|
||||
return Admin::url( $page, array( 'step' => $step ) );
|
||||
}
|
||||
|
||||
public static function render(): void {
|
||||
Admin::open( 'StreamGoals setup' );
|
||||
$connected = PayPalConnector::isConnected();
|
||||
$step = Admin::get_int( 'step' );
|
||||
$step = isset( self::STEPS[ $step ] ) ? $step : 1;
|
||||
if ( ! $connected ) {
|
||||
$step = 1; // nothing else works before PayPal is connected
|
||||
}
|
||||
echo '<p class="sg-lead">Welcome! Four quick steps and you are ready to collect donations on stream.</p>';
|
||||
echo '<ol class="sg-progress">';
|
||||
foreach ( self::STEPS as $n => $label ) {
|
||||
$cls = $n === $step ? 'is-current' : ( $n < $step || ( $connected && $n > 1 ) ? 'is-done' : '' );
|
||||
echo '<li class="' . esc_attr( $cls ) . '">' . ( $connected ? '<a href="' . esc_url( self::url( $n ) ) . '">' : '' ) . esc_html( $label ) . ( $connected ? '</a>' : '' ) . '</li>';
|
||||
}
|
||||
echo '</ol><div class="sg-card">';
|
||||
match ( $step ) {
|
||||
2 => self::step2(),
|
||||
3 => self::step3(),
|
||||
4 => self::step4(),
|
||||
default => self::step1( $connected ),
|
||||
};
|
||||
echo '</div>';
|
||||
Admin::close();
|
||||
}
|
||||
|
||||
private static function step1( bool $connected ): void {
|
||||
echo '<h2>Step 1: Connect PayPal</h2>';
|
||||
if ( $connected ) {
|
||||
echo '<p class="sg-ok">PayPal is connected. You can continue, or paste new credentials below to change them.</p>';
|
||||
echo '<p><a class="button button-primary" href="' . esc_url( self::url( 2 ) ) . '">Continue to step 2</a></p>';
|
||||
}
|
||||
PayPalConnector::instructions();
|
||||
Admin::form( 'sg_paypal_connect', array( 'return' => 'setup' ) );
|
||||
PayPalConnector::fields();
|
||||
echo '<p><button type="submit" class="button button-primary button-hero">Test connection</button></p></form>';
|
||||
}
|
||||
|
||||
private static function step2(): void {
|
||||
$cur = Admin::currency();
|
||||
echo '<h2>Step 2: Create your first goal</h2><p>Pick a starting point. You can change everything later on the Goals screen.</p>';
|
||||
Admin::form( 'sg_wizard_goal' );
|
||||
echo '<div class="sg-templates">';
|
||||
$first = true;
|
||||
foreach ( Templates::LABELS as $key => [ $label, $desc ] ) {
|
||||
echo '<label class="sg-template"><input type="radio" name="template" value="' . esc_attr( $key ) . '"' . checked( $first, true, false ) . '> <strong>' . esc_html( $label ) . '</strong><br><span>' . esc_html( $desc ) . '</span></label>';
|
||||
$first = false;
|
||||
}
|
||||
echo '</div><table class="form-table" role="presentation">';
|
||||
echo '<tr><th scope="row"><label for="sg_title">Goal name</label></th><td><input type="text" id="sg_title" name="title" class="regular-text" maxlength="190" placeholder="New gaming PC"></td></tr>';
|
||||
echo '<tr><th scope="row"><label for="sg_target">Target (' . esc_html( $cur ) . ')</label></th><td><input type="text" id="sg_target" name="target" class="small-text" inputmode="decimal" value="' . esc_attr( 0 === \StreamGoals\Core\Money::exponent( $cur ) ? '10000' : '100.00' ) . '"> <span class="description">How much you want to raise.</span></td></tr></table>';
|
||||
echo '<p><button type="submit" class="button button-primary button-hero">Create goal</button></p></form>';
|
||||
}
|
||||
|
||||
private static function step3(): void {
|
||||
echo '<h2>Step 3: Add a donation page</h2>';
|
||||
$id = (int) get_option( 'sg_donate_page_id', 0 );
|
||||
$post = $id ? get_post( $id ) : null;
|
||||
if ( $post && 'trash' !== $post->post_status ) {
|
||||
echo '<p class="sg-ok">Your donation page exists: <a href="' . esc_url( (string) get_permalink( $post ) ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( get_the_title( $post ) ) . '</a> (<a href="' . esc_url( (string) get_edit_post_link( $post->ID, 'raw' ) ) . '">edit</a>).</p>';
|
||||
} else {
|
||||
echo '<p>Viewers donate through a page on your site. We can create it for you now. It simply contains this shortcode:</p>';
|
||||
}
|
||||
echo '<p><code>[streamgoals_donate]</code> <button type="button" class="button" data-sg-copy-text="[streamgoals_donate]">Copy</button></p>';
|
||||
echo '<p class="description">You can paste that shortcode into any page or post. To show only one goal use <code>[streamgoals_donate goal="ID"]</code>.</p>';
|
||||
if ( ! $post || 'trash' === $post->post_status ) {
|
||||
Admin::form( 'sg_wizard_page', array( 'mode' => 'create' ), 'sg-inline' );
|
||||
echo '<button type="submit" class="button button-primary button-hero">Create my Donate page</button></form> ';
|
||||
}
|
||||
Admin::form( 'sg_wizard_page', array( 'mode' => 'skip' ), 'sg-inline' );
|
||||
echo '<button type="submit" class="button">' . ( $post && 'trash' !== $post->post_status ? 'Continue' : 'Skip, I will add it myself' ) . '</button></form>';
|
||||
}
|
||||
|
||||
private static function step4(): void {
|
||||
echo '<h2>Step 4: Add the overlays to OBS</h2>';
|
||||
echo '<p>Overlays are small web pages OBS shows on top of your stream. Each one has its own link. <strong>Keep these links private</strong>: anyone with a link can see your live overlay (not your donor details or money info).</p>';
|
||||
OverlaysPage::urlList();
|
||||
echo '<h3>How to add one in OBS</h3><ol class="sg-steps">';
|
||||
echo '<li>In OBS, under <strong>Sources</strong> click <strong>+</strong> and choose <strong>Browser</strong>.</li>';
|
||||
echo '<li>Name it (for example "Goal bar") and click OK.</li>';
|
||||
echo '<li>Paste the link into the <strong>URL</strong> box. Do <em>not</em> tick "Local file".</li>';
|
||||
echo '<li>Set <strong>Width</strong> and <strong>Height</strong> to the recommended size shown next to the link. Leave the custom CSS box as it is.</li>';
|
||||
echo '<li>Click OK, then move and resize it on your canvas. The background is transparent automatically.</li>';
|
||||
echo '<li>Want different colors or fonts? Open the <a href="' . esc_url( Admin::url( 'streamgoals-overlays' ) ) . '">Overlays screen</a>, which builds the link for you and lets you send a test donation.</li></ol>';
|
||||
echo '<p><a class="button button-primary button-hero" href="' . esc_url( Admin::url( Admin::HOME ) ) . '">Finish setup</a></p>';
|
||||
}
|
||||
|
||||
public static function handleGoal(): void {
|
||||
Admin::guard( 'sg_wizard_goal' );
|
||||
$cur = Admin::currency();
|
||||
$key = Admin::post_str( 'template', 20 );
|
||||
$tgt = Input::amountToMinor( Admin::post_str( 'target', 30 ), $cur );
|
||||
if ( ! Templates::exists( $key ) ) {
|
||||
Admin::redirect( self::url( 2 ), 'error', 'Please choose a goal template.' );
|
||||
}
|
||||
if ( null === $tgt || $tgt <= 0 ) {
|
||||
Admin::redirect( self::url( 2 ), 'error', 'Please enter a target amount greater than zero, for example 100.00.' );
|
||||
}
|
||||
$built = Templates::build( $key, $cur, $tgt, Admin::post_str( 'title', 190 ) );
|
||||
$built['goal']['starts_at'] = null;
|
||||
$built['goal']['ends_at'] = null;
|
||||
$id = Store::goals()->create( $built['goal'] );
|
||||
if ( $id <= 0 ) {
|
||||
Admin::redirect( self::url( 2 ), 'error', 'The goal could not be saved. Please try again.' );
|
||||
}
|
||||
foreach ( $built['rules'] as $r ) {
|
||||
Store::saveRule( $id, $r['type'], $r['config'] );
|
||||
}
|
||||
update_option( 'sg_wizard_goal_id', $id, false );
|
||||
Admin::redirect( self::url( 3 ), 'success', 'Goal created.' );
|
||||
}
|
||||
|
||||
public static function handlePage(): void {
|
||||
Admin::guard( 'sg_wizard_page' );
|
||||
if ( 'create' === Admin::post_str( 'mode', 10 ) ) {
|
||||
if ( ! current_user_can( 'publish_pages' ) ) {
|
||||
wp_die( esc_html__( 'You do not have permission to do that.', 'streamgoals-donate' ), 403 );
|
||||
}
|
||||
$id = wp_insert_post( array(
|
||||
'post_type' => 'page',
|
||||
'post_status' => 'publish',
|
||||
'post_title' => 'Donate',
|
||||
'post_content' => '[streamgoals_donate]',
|
||||
), true );
|
||||
if ( is_wp_error( $id ) ) {
|
||||
Admin::redirect( self::url( 3 ), 'error', 'The page could not be created. You can add the shortcode to any page yourself.' );
|
||||
}
|
||||
update_option( 'sg_donate_page_id', (int) $id, false );
|
||||
Admin::redirect( self::url( 4 ), 'success', 'Donate page created and published.' );
|
||||
}
|
||||
Admin::redirect( self::url( 4 ), 'info', 'Skipped. Remember to add [streamgoals_donate] to a page.' );
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user