列設定を適用済みの画面 */ private array $applied = []; public static function instance(): self { return self::$instance ??= new self(); } public function container(): Container { return $this->c; } public function boot(): void { if ($this->booted) { return; } $this->booted = true; $this->c = new Container(); $this->registerServices(); if (is_admin()) { Schema::maybeUpgrade(); } /** @var Feature[] $features */ $features = apply_filters('lime_columns/features', [ new Features\Display\DisplayFeature(), new Features\Sorting\SortingFeature(), new Features\DefaultSort\DefaultSortFeature(), new Features\Search\SearchFeature(), new Features\Export\ExportFeature(), ]); foreach ($features as $f) { $f->boot($this->c); } (new SettingsPage())->register(); (new AjaxController($this->c))->register(); if (is_admin()) { add_action('current_screen', [$this, 'onCurrentScreen'], 5); add_action('wp_ajax_inline-save', [$this, 'onInlineSave'], 0); // WordPress 本体の処理(優先度 1)より先に適用する add_action('admin_notices', [$this, 'coexistNotice']); } do_action('lime_columns/loaded', $this->c); } /** ACP が有効な間は、二重適用を避けるため一覧画面への適用を止める(設定UI・移行ツールは使える)。 */ public static function acpActive(): bool { return defined('ACP_FILE') || defined('AC_FILE'); } public static function appliesToLists(): bool { return !self::acpActive() || (bool) apply_filters('lime_columns/allow_coexist_with_acp', false); } public function onCurrentScreen(\WP_Screen $screen): void { if (!self::appliesToLists()) { return; } $ctx = $this->c->get('resolver')->fromScreen($screen); if ($ctx) { $this->applyContext($ctx); } } /** クイック編集は、画面を開かずに行を描き直すので、ここで同じ列設定を適用する。 */ public function onInlineSave(): void { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- 表示する列を決めるだけ。権限と nonce の確認、保存は WordPress 本体が行う $postType = isset($_POST['post_type']) && is_string($_POST['post_type']) ? sanitize_key(wp_unslash($_POST['post_type'])) : ''; if ($postType === '' || !self::appliesToLists()) { return; } $ctx = $this->c->get('resolver')->fromKey('post:' . $postType); if ($ctx) { $this->applyContext($ctx); } } /** 同じ画面の列設定は、1 回のリクエストで 1 度だけ適用する(セルの二重出力を防ぐ)。 */ private function applyContext(Context $ctx): void { $key = $ctx->screen->key(); if (isset($this->applied[$key])) { return; } $this->applied[$key] = true; do_action('lime_columns/context_ready', $ctx); } public function coexistNotice(): void { $screen = get_current_screen(); if (self::appliesToLists() || !$screen || $screen->id !== 'settings_page_lime-columns') { return; } echo '

' . esc_html__('Admin Columns Pro is active, so Lime Columns is not applied to list screens. Deactivate it to switch over (settings and the importer remain available).', 'lime-columns') . '

'; } private function registerServices(): void { $this->c->set('repository', static fn () => new ColumnSetRepository()); $this->c->set('integrations', static function (): array { /** @var Integration[] $all */ $all = apply_filters('lime_columns/integrations', [new AcfIntegration(), new WooCommerceIntegration()]); return array_values(array_filter($all, static fn (Integration $i) => $i->available())); }); $this->c->set('screens', static fn (Container $c) => new ListScreenRegistry($c->get('integrations'))); $this->c->set('types', function () { $r = new ColumnRegistry(); foreach ($this->builtinTypes() as $t) { $r->add($t); } foreach ($this->c->get('integrations') as $i) { $i->register($r); } do_action('lime_columns/register_column_types', $r); return $r; }); $this->c->set('resolver', fn (Container $c) => new ContextResolver($c->get('screens'), $c->get('repository'), $c->get('types'))); $this->c->set('importer', fn (Container $c) => new AcpImporter($c->get('repository'), $c->get('types'), $c->get('screens'))); } /** @return list */ private function builtinTypes(): array { return [ new Types\Post\Title(), new Types\Post\Author(), new Types\Post\Date(), new Types\Post\Comments(), new Types\Post\AuthorDisplay(), new Types\Post\Thumbnail(), new Types\Post\Taxonomy(), new Types\Post\LatestComment(), new Types\Post\PostField('post_id'), new Types\Post\PostField('slug'), new Types\Post\PostField('status'), new Types\Post\PostField('modified'), new Types\Post\PostField('excerpt'), new Types\Post\PostField('content'), new Types\User\NativeUserColumn('username'), new Types\User\NativeUserColumn('user_name'), new Types\User\NativeUserColumn('email'), new Types\User\NativeUserColumn('role'), new Types\User\NativeUserColumn('posts'), new Types\User\UserField('user_id'), new Types\User\UserField('registered'), new Types\User\UserField('user_url'), new Types\User\UserField('display_name'), new Types\Common\CustomField('post'), new Types\Common\CustomField('user'), new Types\Media\UploadedTo(), new Types\Media\MediaField('file_name'), new Types\Media\MediaField('file_size'), new Types\Media\MediaField('dimensions'), new Types\Media\MediaField('mime_type'), new Types\Media\MediaField('alt_text'), new Types\Common\Foreign(), ]; } }