'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 '
Welcome! Four quick steps and you are ready to collect donations on stream.
';
echo '';
foreach ( self::STEPS as $n => $label ) {
$cls = $n === $step ? 'is-current' : ( $n < $step || ( $connected && $n > 1 ) ? 'is-done' : '' );
echo '' . ( $connected ? '' : '' ) . esc_html( $label ) . ( $connected ? ' ' : '' ) . ' ';
}
echo ' ';
match ( $step ) {
2 => self::step2(),
3 => self::step3(),
4 => self::step4(),
default => self::step1( $connected ),
};
echo '
';
Admin::close();
}
private static function step1( bool $connected ): void {
echo 'Step 1: Connect PayPal ';
if ( $connected ) {
echo 'PayPal is connected. You can continue, or paste new credentials below to change them.
';
echo 'Continue to step 2
';
}
PayPalConnector::instructions();
Admin::form( 'sg_paypal_connect', array( 'return' => 'setup' ) );
PayPalConnector::fields();
echo 'Test connection
';
}
private static function step2(): void {
$cur = Admin::currency();
echo 'Step 2: Create your first goal Pick a starting point. You can change everything later on the Goals screen.
';
Admin::form( 'sg_wizard_goal' );
echo '';
$first = true;
foreach ( Templates::LABELS as $key => [ $label, $desc ] ) {
echo ' ' . esc_html( $label ) . ' ' . esc_html( $desc ) . ' ';
$first = false;
}
echo '
';
echo 'Create goal
';
}
private static function step3(): void {
echo 'Step 3: Add a donation page ';
$id = (int) get_option( 'sg_donate_page_id', 0 );
$post = $id ? get_post( $id ) : null;
if ( $post && 'trash' !== $post->post_status ) {
echo 'Your donation page exists: ' . esc_html( get_the_title( $post ) ) . ' (edit ).
';
} else {
echo 'Viewers donate through a page on your site. We can create it for you now. It simply contains this shortcode:
';
}
echo '[streamgoals_donate] Copy
';
echo 'You can paste that shortcode into any page or post. To show only one goal use [streamgoals_donate goal="ID"].
';
if ( ! $post || 'trash' === $post->post_status ) {
Admin::form( 'sg_wizard_page', array( 'mode' => 'create' ), 'sg-inline' );
echo 'Create my Donate page ';
}
Admin::form( 'sg_wizard_page', array( 'mode' => 'skip' ), 'sg-inline' );
echo '' . ( $post && 'trash' !== $post->post_status ? 'Continue' : 'Skip, I will add it myself' ) . ' ';
}
private static function step4(): void {
echo 'Step 4: Add the overlays to OBS ';
echo 'Overlays are small web pages OBS shows on top of your stream. Each one has its own link. Keep these links private : anyone with a link can see your live overlay (not your donor details or money info).
';
OverlaysPage::urlList();
echo 'How to add one in OBS ';
echo 'In OBS, under Sources click + and choose Browser . ';
echo 'Name it (for example "Goal bar") and click OK. ';
echo 'Paste the link into the URL box. Do not tick "Local file". ';
echo 'Set Width and Height to the recommended size shown next to the link. Leave the custom CSS box as it is. ';
echo 'Click OK, then move and resize it on your canvas. The background is transparent automatically. ';
echo 'Want different colors or fonts? Open the Overlays screen , which builds the link for you and lets you send a test donation. ';
echo 'Finish setup
';
}
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.' );
}
}