Skip to main content

Approve or decline guests’ Guest portal changes from code, and change the approval emails

Premium Intermediate Guest Manager

Every change a guest sends through their form (see Restyle the guest’s own Guest portal form, or act when a guest sends changes) waits on the Guests screen’s Waiting for Approval list; the Guests list shows N waiting for approval with Review. All three routes are admin only (manage_options through benecaster_rest_permission_admin(), with the X-WP-Nonce header), with no show: a guest is the site’s.

  • GET /wp-json/benecaster-guest-manager/v1/approvals answers { items, total }, one item per guest with changes waiting (a deleted or trashed guest is left out): { guest_id, name, submitted, submitted_display, changes: [{ field, label, old, new }], photo }. submitted is ISO 8601 UTC. old and new are plain text (social links one per line, Platform: address); never draw them as HTML. photo is null, or { action: "replace" | "remove", old_url, new_url }, where new_url is the waiting photo as a data: address (the file itself is private and never served).
  • POST /approvals/{id}/approve applies the guest’s changes and answers { approved, guest, total } (guest as GET /guests/{id} gives it; total still waiting). It is an edit on the Guests screen: the waiting fields are cleaned again exactly as PUT, PATCH /guests/{id} cleans them (a value that no longer passes is that route’s 400, naming the field), then written between benecaster_guest_manager_before_save_guest and benecaster_guest_manager_after_save_guest ( $guest_id, $fields, false ) (see Read, create, change or delete guests from code, as the Guests screen does), so the feed’s cache clears. Only the fields the guest changed are written: a field the podcaster edited since keeps the podcaster’s value. A new photo joins the media library now, titled Photo of {Name} and attached to no post, and becomes the guest’s photo; Remove my photo sets none (the old image stays in the library). A photo that cannot be imported is 500 benecaster_guest_manager_photo_import_failed, and nothing is applied. The guest is emailed.
  • POST /approvals/{id}/decline throws the changes and any waiting photo away and answers { declined, total }. The guest is emailed.
  • Both answer 404 benecaster_guest_manager_not_found for no such guest and 404 benecaster_guest_manager_nothing_pending when nothing is waiting.

The hooks:

  • benecaster_guest_manager_before_approve_guest_changes( int $guest_id, array $pending ) and benecaster_guest_manager_after_approve_guest_changes( int $guest_id, array $fields ), the fields written (photo_id included when the photo changed).
  • benecaster_guest_manager_before_decline_guest_changes( int $guest_id, array $pending ) and benecaster_guest_manager_after_decline_guest_changes( int $guest_id ). $pending is { fields, photo, submitted }.

The emails are plain text, from the site’s title, and never quote what the guest sent.

  • To the admin on each submission: benecaster_guest_manager_approval_email_recipient( string $to, int $guest_id ), admin_email by default, '' sends nothing; benecaster_guest_manager_approval_email_subject( string $subject, int $guest_id ); benecaster_guest_manager_approval_email_message( string $message, int $guest_id, string $queue_url ).
  • To the guest on approve or decline, only when they have an email: benecaster_guest_manager_outcome_email_subject( string $subject, int $guest_id, bool $approved ) and benecaster_guest_manager_outcome_email_message( string $message, int $guest_id, bool $approved ).

Code

<?php
// Send submissions to the producer instead of the site's admin.
add_filter( 'benecaster_guest_manager_approval_email_recipient', static fn(): string => 'producer@example.com' );

// Approve every waiting change from code, e.g. a trusted guest list in a cron job.
$waiting = rest_do_request( new WP_REST_Request( 'GET', '/benecaster-guest-manager/v1/approvals' ) )->get_data();
foreach ( $waiting['items'] as $item ) {
    rest_do_request( new WP_REST_Request( 'POST', '/benecaster-guest-manager/v1/approvals/' . $item['guest_id'] . '/approve' ) );
}

View on GitHub →

Hooks Used

Need this built rather than just documented? See our services →