Admin tables: scroll containers and phone cards; Alerts list flags missing media
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
afa768fa42
commit
fb200594ce
8 files changed
+190
-54
No files matched your search
@@ -48,6 +48,44 @@ final class AlertsPage {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Media column parts for one alert. $existing maps attachment id => true for ids still in the Media Library.
|
||||
*
|
||||
* @param array<string,mixed> $d
|
||||
* @param array<int,bool> $existing
|
||||
* @return array<int,array{0:string,1:bool}> list of [label, missing]
|
||||
*/
|
||||
public static function mediaParts( array $d, array $existing ): array {
|
||||
$parts = array();
|
||||
$vid = (int) ( $d['visual_id'] ?? 0 );
|
||||
if ( $vid > 0 ) {
|
||||
$video = 'video' === ( $d['visual_type'] ?? '' );
|
||||
$parts[] = isset( $existing[ $vid ] ) ? array( $video ? 'Video' : 'Picture/GIF', false ) : array( $video ? 'Video missing' : 'Picture missing', true );
|
||||
}
|
||||
$sid = (int) ( $d['sound_id'] ?? 0 );
|
||||
if ( $sid > 0 ) {
|
||||
$parts[] = isset( $existing[ $sid ] ) ? array( 'Sound', false ) : array( 'Sound missing', true );
|
||||
}
|
||||
return $parts;
|
||||
}
|
||||
|
||||
/** One query: which of the alerts' attachments still exist. @return array<int,bool> */
|
||||
private static function existingAttachments( array $defs ): array {
|
||||
$ids = array();
|
||||
foreach ( $defs as $d ) {
|
||||
foreach ( array( 'visual_id', 'sound_id' ) as $k ) {
|
||||
if ( (int) ( $d[ $k ] ?? 0 ) > 0 ) {
|
||||
$ids[ (int) $d[ $k ] ] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( ! $ids ) {
|
||||
return array();
|
||||
}
|
||||
$found = get_posts( array( 'post_type' => 'attachment', 'post_status' => 'any', 'post__in' => array_keys( $ids ), 'posts_per_page' => count( $ids ), 'fields' => 'ids', 'orderby' => 'none', 'no_found_rows' => true, 'update_post_meta_cache' => false, 'update_post_term_cache' => false ) );
|
||||
return array_fill_keys( array_map( 'intval', $found ), true );
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- render */
|
||||
|
||||
public static function render(): void {
|
||||
@@ -89,24 +127,22 @@ final class AlertsPage {
|
||||
echo '<p>No custom alerts yet.</p></div>';
|
||||
return;
|
||||
}
|
||||
echo '<table class="widefat striped sg-table"><thead><tr><th>Name</th><th>Plays when</th><th>Applies to</th><th>Media</th><th>Order</th><th>Actions</th></tr></thead><tbody>';
|
||||
echo Admin::tableOpen( array( 'Name', 'Plays when', 'Applies to', 'Media', 'Order', 'Actions' ), 'Your alerts' );
|
||||
$existing = self::existingAttachments( $defs );
|
||||
foreach ( $defs as $d ) {
|
||||
$id = (int) $d['id'];
|
||||
$when = 'reward' === $d['trigger_type']
|
||||
? 'Reward: ' . ( $rewards[ $d['action_id'] ] ?? 'a reward that was deleted' )
|
||||
: 'Donation of at least ' . Format::money( (int) $d['min_minor'], $cur );
|
||||
$media = array();
|
||||
if ( $d['visual_id'] ) {
|
||||
$media[] = 'video' === $d['visual_type'] ? 'Video' : 'Picture/GIF';
|
||||
foreach ( self::mediaParts( $d, $existing ) as [ $mLabel, $mMissing ] ) {
|
||||
$media[] = $mMissing ? '<span class="sga-missing">' . esc_html( $mLabel ) . '</span>' : esc_html( $mLabel );
|
||||
}
|
||||
if ( $d['sound_id'] ) {
|
||||
$media[] = 'Sound';
|
||||
}
|
||||
echo '<tr' . ( $d['enabled'] ? '' : ' class="sg-off"' ) . '><td><strong>' . esc_html( $d['title'] ) . '</strong></td>';
|
||||
echo '<td>' . esc_html( $when ) . '</td>';
|
||||
echo '<td>' . esc_html( 0 === $d['goal_id'] ? 'All goals' : ( $goals[ $d['goal_id'] ] ?? 'Goal #' . $d['goal_id'] ) ) . '</td>';
|
||||
echo '<td>' . esc_html( $media ? implode( ', ', $media ) : 'Text only' ) . '</td>';
|
||||
echo '<td>' . (int) $d['sort'] . '</td><td class="sg-actions">';
|
||||
echo '<tr' . ( $d['enabled'] ? '' : ' class="sg-off"' ) . '>' . Admin::td( 'Name' ) . '<strong>' . esc_html( $d['title'] ) . '</strong></td>';
|
||||
echo Admin::td( 'Plays when' ) . esc_html( $when ) . '</td>';
|
||||
echo Admin::td( 'Applies to' ) . esc_html( 0 === $d['goal_id'] ? 'All goals' : ( $goals[ $d['goal_id'] ] ?? 'Goal #' . $d['goal_id'] ) ) . '</td>';
|
||||
echo Admin::td( 'Media' ) . ( $media ? implode( ', ', $media ) : 'Text only' ) . '</td>';
|
||||
echo Admin::td( 'Order' ) . (int) $d['sort'] . '</td>' . Admin::td( 'Actions', 'sg-actions' );
|
||||
echo '<a class="button" href="' . esc_url( self::url( array( 'edit' => $id ) ) ) . '">Edit</a> ';
|
||||
Admin::form( 'sg_alert_toggle', array( 'alert_id' => $id, 'enabled' => $d['enabled'] ? 0 : 1 ), 'sg-inline' );
|
||||
echo '<button class="button">' . ( $d['enabled'] ? 'Turn off' : 'Turn on' ) . '</button></form> ';
|
||||
@@ -114,7 +150,7 @@ final class AlertsPage {
|
||||
echo '<button class="button button-link-delete" data-confirm="Delete this alert? Your media files stay in the Media Library.">Delete</button></form>';
|
||||
echo '</td></tr>';
|
||||
}
|
||||
echo '</tbody></table></div>';
|
||||
echo Admin::tableClose() . '</div>';
|
||||
}
|
||||
|
||||
private static function mediaField( string $name, string $label, string $kind, int $id, string $help ): void {
|
||||
|
||||
Reference in new issue
Block a user