Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/Admin/ExtendedValues/CommitteeExtendedValuesField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Amber\Admin\ExtendedValues;

// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}

use Unity\Committees\Interfaces\Committee;
use Unity\Committees\Interfaces\CommitteeRepository;
use Unity\Core\Interfaces\Configuration;
use WP_Term;

use function add_action;
use function current_user_can;

/**
* "Extended values" row on the committee edit screen.
*
* Committees are taxonomy terms, and a term edit screen has no meta boxes,
* so the same read-only table as {@see ExtendedValuesMetaBox} goes in as a
* row at the foot of the form instead.
*/
final class CommitteeExtendedValuesField
{
public function __construct(
Configuration $configuration,
private readonly CommitteeRepository $committees,
) {
$config = $configuration->getConfig(Committee::class) ?? [];
$taxonomy = $config['TAXONOMY'] ?? null;

if (is_string($taxonomy) && $taxonomy !== '') {
add_action($taxonomy . '_edit_form_fields', [$this, 'render'], 99);
}
}

public function render(WP_Term $term): void
{
if (!current_user_can('edit_term', $term->term_id)) {
return;
}

$values = $this->committees->findById($term->term_id)?->allUnityExtendedValues() ?? [];

echo '<tr class="form-field"><th scope="row">Extended values</th><td>'
. ExtendedValuesTable::html($values)
. '<p class="description">Kept by other plugins; read-only.</p></td></tr>';
}
}
90 changes: 90 additions & 0 deletions src/Admin/ExtendedValues/ExtendedValuesMetaBox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace Amber\Admin\ExtendedValues;

// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}

use Closure;
use Unity\Core\Interfaces\Configuration;
use Unity\Core\Interfaces\KeyValueStoreInterface;
use WP_Post;

use function add_action;
use function add_meta_box;
use function current_user_can;

/**
* "Extended values" box on the edit screen of every Unity model kept as a
* post: members, meetings, groups, positions and intergroup meetings.
*
* Lists the record's Unity extended values: what other plugins keep against
* it beyond Unity's own fields, such as Reach's `AppleUserId` on a member.
* They are stored outside ACF, so without this box an administrator has no
* way to see them at all.
*
* <p><b>Read-only.</b> Values are written by the plugin that owns them, which
* knows what they mean; an edit box here would let somebody break a sign-in
* link without knowing it was one.</p>
*
* <p>A model gets the box only when its implementation publishes a
* `POST_TYPE` in Unity's configuration. TSML publishes none for locations,
* which it edits inside the meeting screen, so locations have no box. A post
* type with no edit screen of its own simply never fires the hook.</p>
*/
final class ExtendedValuesMetaBox
{
public const BOX_ID = 'amber-extended-values';

/** @var array<string, Closure(int): ?KeyValueStoreInterface> Post type => find the record by id. */
private array $lookups = [];

/**
* @param array<class-string, Closure(int): ?KeyValueStoreInterface> $lookups
* Unity model interface => find that model by post id.
*/
public function __construct(Configuration $configuration, array $lookups)
{
foreach ($lookups as $model => $find) {
$config = $configuration->getConfig($model) ?? [];
$postType = $config['POST_TYPE'] ?? null;

if (is_string($postType) && $postType !== '') {
$this->lookups[$postType] = $find;
}
}

if ($this->lookups !== []) {
add_action('add_meta_boxes', [$this, 'addMetaBox'], 10, 2);
}
}

/**
* @param string $postType The screen's post type.
* @param mixed $post The post being edited; a comment on the comment screen.
*/
public function addMetaBox(string $postType, mixed $post): void
{
if (!isset($this->lookups[$postType]) || !$post instanceof WP_Post) {
return;
}

if (!current_user_can('edit_post', $post->ID)) {
return;
}

add_meta_box(self::BOX_ID, 'Extended values', [$this, 'render'], $postType, 'side', 'low');
}

public function render(WP_Post $post): void
{
$find = $this->lookups[$post->post_type] ?? null;
$values = $find !== null ? ($find($post->ID)?->allUnityExtendedValues() ?? []) : [];

echo ExtendedValuesTable::html($values);
}
}
41 changes: 41 additions & 0 deletions src/Admin/ExtendedValues/ExtendedValuesTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Amber\Admin\ExtendedValues;

// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}

use function esc_html;

/**
* The read-only key/value table both extended-values screens show.
*
* Every key and value is escaped: values are written by other plugins, and
* nothing guarantees what they hold.
*/
final class ExtendedValuesTable
{
private function __construct()
{
}

/** @param array<string, string> $values Already sorted by key, as the store returns them. */
public static function html(array $values): string
{
if ($values === []) {
return '<p class="description">No extended values.</p>';
}

$html = '<table class="widefat striped"><thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>';
foreach ($values as $key => $value) {
$html .= '<tr><td><code>' . esc_html($key) . '</code></td>'
. '<td style="word-break: break-all;"><code>' . esc_html($value) . '</code></td></tr>';
}

return $html . '</tbody></table>';
}
}
77 changes: 0 additions & 77 deletions src/Admin/Members/MemberValuesMetaBox.php

This file was deleted.

55 changes: 51 additions & 4 deletions src/Core/AmberServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
}

use Amber\Admin\DeveloperDashboard;
use Amber\Admin\ExtendedValues\CommitteeExtendedValuesField;
use Amber\Admin\ExtendedValues\ExtendedValuesMetaBox;
use Amber\Admin\IntergroupMeetings\IntergroupAttendanceAdmin;
use Amber\Admin\IntergroupMeetings\IntergroupMeetingAdmin;
use Amber\Admin\IntergroupMeetings\IntergroupMeetingAttendanceDashboard;
Expand All @@ -20,7 +22,6 @@
use Amber\Admin\Members\DirectoryDashboard;
use Amber\Admin\Members\MemberAdmin;
use Amber\Admin\Members\MemberPasswordAdmin;
use Amber\Admin\Members\MemberValuesMetaBox;
use Amber\Admin\Members\AnonymousNameValidator;
use Amber\Admin\Members\PersonalEmailValidator;
use Amber\Admin\Positions\PositionAdmin;
Expand All @@ -42,18 +43,24 @@
use Unity\Committees\Interfaces\CommitteeRepository;
use Unity\Core\Interfaces\Configuration;
use Unity\Core\Interfaces\Container;
use Unity\Core\Interfaces\KeyValueStoreInterface;
use Unity\Groups\Interfaces\Group;
use Unity\Groups\Interfaces\GroupFactory;
use Unity\Groups\Interfaces\GroupRepository;
use Unity\Groups\Interfaces\GroupViewFactory;
use Unity\IntergroupMeetings\Interfaces\IntergroupMeeting;
use Unity\IntergroupMeetings\Interfaces\IntergroupMeetingFactory;
use Unity\IntergroupMeetings\Interfaces\IntergroupMeetingRepository;
use Unity\IntergroupMeetings\Interfaces\IntergroupMeetingGroupAttendanceFactory;
use Unity\IntergroupMeetings\Interfaces\IntergroupMeetingGroupAttendanceRepository;
use Unity\IntergroupMeetings\Interfaces\IntergroupMeetingOfficerAttendanceFactory;
use Unity\IntergroupMeetings\Interfaces\IntergroupMeetingOfficerAttendanceRepository;
use Unity\Meetings\Interfaces\Meeting;
use Unity\Meetings\Interfaces\MeetingRepository;
use Unity\Members\Interfaces\Member;
use Unity\Members\Interfaces\MemberRepository;
use Unity\Members\Interfaces\MemberRevisor;
use Unity\Positions\Interfaces\Position;
use Unity\Positions\Interfaces\PositionFactory;
use Unity\Positions\Interfaces\PositionRepository;
use Unity\Positions\Interfaces\PositionViewFactory;
Expand Down Expand Up @@ -130,10 +137,36 @@ private function registerAdminServices(Container $container): void
);
});

$container->register(MemberValuesMetaBox::class, function (ContainerInterface $c) {
return new MemberValuesMetaBox(
// One box for every Unity model kept as a post. Each lookup is
// feature-detected on its repository binding, as the committee
// screens are, and resolved only when a box is drawn.
$container->register(ExtendedValuesMetaBox::class, function (ContainerInterface $c) {
$repositories = [
Member::class => MemberRepository::class,
Meeting::class => MeetingRepository::class,
Group::class => GroupRepository::class,
Position::class => PositionRepository::class,
IntergroupMeeting::class => IntergroupMeetingRepository::class,
];
$lookups = [];

foreach ($repositories as $model => $repository) {
if ($c->has($repository)) {
$lookups[$model] = static function (int $id) use ($c, $repository): ?KeyValueStoreInterface {
$found = self::resolve($c, $repository)?->findById($id);

return $found instanceof KeyValueStoreInterface ? $found : null;
};
}
}

return new ExtendedValuesMetaBox($c->get(Configuration::class), $lookups);
});

$container->register(CommitteeExtendedValuesField::class, function (ContainerInterface $c) {
return new CommitteeExtendedValuesField(
$c->get(Configuration::class),
$c->get(MemberRepository::class)
$c->get(CommitteeRepository::class)
);
});

Expand Down Expand Up @@ -305,4 +338,18 @@ private function registerReconciler(Container $container): void
);
});
}

/**
* A service from the container, or null when it is not the type asked for.
*
* @template T of object
* @param class-string<T> $id
* @return T|null
*/
private static function resolve(ContainerInterface $c, string $id): ?object
{
$service = $c->get($id);

return $service instanceof $id ? $service : null;
}
}
6 changes: 4 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
}

use Amber\Admin\DeveloperDashboard;
use Amber\Admin\ExtendedValues\CommitteeExtendedValuesField;
use Amber\Admin\ExtendedValues\ExtendedValuesMetaBox;
use Amber\Admin\IntergroupMeetings\IntergroupAttendanceAdmin;
use Amber\Admin\IntergroupMeetings\IntergroupMeetingAdmin;
use Amber\Admin\IntergroupMeetings\IntergroupMeetingDashboard;
Expand All @@ -19,7 +21,6 @@
use Amber\Admin\Members\DirectoryDashboard;
use Amber\Admin\Members\MemberPasswordAdmin;
use Amber\Admin\Members\MemberAdmin;
use Amber\Admin\Members\MemberValuesMetaBox;
use Amber\Admin\Members\AnonymousNameValidator;
use Amber\Admin\Members\PersonalEmailValidator;
use Amber\Admin\Positions\PositionAdmin;
Expand Down Expand Up @@ -120,7 +121,7 @@ public static function init(Container $unityContainer): void
self::$container->get(PositionAdmin::class);
self::$container->get(PositionNameValidator::class);
self::$container->get(MemberAdmin::class);
self::$container->get(MemberValuesMetaBox::class);
self::$container->get(ExtendedValuesMetaBox::class);
self::$container->get(AnonymousNameValidator::class);
self::$container->get(PersonalEmailValidator::class);
self::$container->get(MeetingAdmin::class);
Expand All @@ -146,6 +147,7 @@ public static function init(Container $unityContainer): void
if (self::$container->has(CommitteeRepository::class)) {
self::$container->get(CommitteeTree::class);
self::$container->get(CommitteeAssignmentController::class);
self::$container->get(CommitteeExtendedValuesField::class);
}

// Run metadata migrations once per version upgrade (deferred to admin_init
Expand Down
Loading