ListScreen.php 11.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
<?php

declare(strict_types=1);

namespace AC;

use AC\Type\ListScreenId;
use AC\Type\Uri;
use AC\Type\Url;
use DateTime;
use LogicException;
use WP_User;

abstract class ListScreen
{

    /**
     * @var string
     */
    protected $key;

    /**
     * The unique ID of the screen.
     * @see   \WP_Screen::id
     * @var string
     */
    protected $screen_id;

    /**
     * @var string
     */
    protected $label;

    /**
     * @var string
     */
    protected $singular_label;

    /**
     * Meta type of list screen; post, user, comment. Mostly used for fetching metadata.
     * @var string
     */
    protected $meta_type;

    /**
     * Group slug. Used for menu.
     * @var string
     */
    protected $group = '';

    /**
     * @var Column[]
     */
    private $columns;

    /**
     * @var Column[]
     */
    private $column_types;

    /**
     * @var string|null
     */
    protected $layout_id;

    /**
     * @var array Column settings data
     */
    private $settings = [];

    /**
     * @var array ListScreen settings data
     */
    private $preferences = [];

    /**
     * @var bool True when column settings can not be overwritten
     */
    private $read_only = false;

    /**
     * @var string
     */
    private $title;

    /**
     * @var DateTime
     */
    private $updated;

    public function __construct(string $key, string $screen_id)
    {
        $this->key = $key;
        $this->screen_id = $screen_id;
    }

    public function has_id(): bool
    {
        return ListScreenId::is_valid_id($this->layout_id);
    }

    public function get_id(): ListScreenId
    {
        if ( ! $this->has_id()) {
            throw new LogicException('ListScreen has no identity.');
        }

        return new ListScreenId($this->layout_id);
    }

    abstract protected function register_column_types(): void;

    /**
     * Register column types from a list with (fully qualified) class names
     *
     * @param string[] $list
     */
    protected function register_column_types_from_list(array $list): void
    {
        foreach ($list as $column) {
            $this->register_column_type(new $column());
        }
    }

    public function get_heading_hookname(): string
    {
        return sprintf('manage_%s_columns', $this->get_screen_id());
    }

    public function get_key(): string
    {
        return $this->key;
    }

    public function get_label(): ?string
    {
        return $this->label;
    }

    protected function set_label(string $label): self
    {
        $this->label = $label;

        return $this;
    }

    public function get_singular_label(): ?string
    {
        return $this->singular_label ?: $this->label;
    }

    public function get_meta_type(): string
    {
        return $this->meta_type ?: '';
    }

    protected function set_meta_type(string $meta_type): self
    {
        $this->meta_type = $meta_type;

        return $this;
    }

    public function get_screen_id(): string
    {
        return $this->screen_id;
    }

    public function get_group(): string
    {
        return $this->group;
    }

    public function set_group(string $group): self
    {
        $this->group = $group;

        return $this;
    }

    public function get_title(): string
    {
        return $this->title;
    }

    public function set_title(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function get_storage_key(): string
    {
        return $this->key . $this->layout_id;
    }

    public function get_layout_id(): ?string
    {
        return $this->layout_id;
    }

    public function set_layout_id(string $layout_id): self
    {
        $this->layout_id = $layout_id;

        return $this;
    }

    public function get_table_attr_id(): string
    {
        return '#the-list';
    }

    public function is_read_only(): bool
    {
        return $this->read_only;
    }

    public function set_read_only(bool $read_only): self
    {
        $this->read_only = $read_only;

        return $this;
    }

    public function set_updated(DateTime $updated): self
    {
        $this->updated = $updated;

        return $this;
    }

    public function get_updated(): DateTime
    {
        return $this->updated ?: new DateTime();
    }

    abstract public function get_table_url(): Uri;

    public function get_editor_url(): Uri
    {
        return new Url\EditorColumns($this->key, $this->has_id() ? $this->get_id() : null);
    }

    /**
     * @return Column[]
     */
    public function get_columns(): array
    {
        if (null === $this->columns) {
            $this->set_columns();
        }

        return $this->columns;
    }

    /**
     * @return Column[]
     */
    public function get_column_types(): array
    {
        if (null === $this->column_types) {
            $this->set_column_types();
        }

        return $this->column_types;
    }

    public function get_column_by_name($name): ?Column
    {
        foreach ($this->get_columns() as $column) {
            // Do not do a strict comparison. All column names are stored as strings, even integers.
            if ($column->get_name() == $name) {
                return $column;
            }
        }

        return null;
    }

    public function get_column_by_type(string $type): ?Column
    {
        $column_types = $this->get_column_types();

        return $column_types[$type] ?? null;
    }

    public function get_class_by_type(string $type): ?string
    {
        $column = $this->get_column_by_type($type);

        return $column
            ? get_class($column)
            : null;
    }

    public function deregister_column_type(string $type): void
    {
        unset($this->column_types[$type]);
    }

    public function register_column_type(Column $column): void
    {
        if ( ! $column->get_type()) {
            return;
        }

        $column->set_list_screen($this);

        if ( ! $column->is_valid()) {
            return;
        }

        // Skip the custom registered columns which are marked 'original' but are not available for this list screen
        if ($column->is_original() && ! array_key_exists($column->get_type(), $this->get_original_columns())) {
            return;
        }

        $this->column_types[$column->get_type()] = $column;
    }

    public function get_original_label(string $type): ?string
    {
        $columns = $this->get_original_columns();

        $label = $columns[$type] ?? null;

        return is_string($label)
            ? $label
            : null;
    }

    public function get_original_columns(): array
    {
        return (new DefaultColumnsRepository())->get($this->get_key());
    }

    private function set_column_types(): void
    {
        $this->column_types = [];

        // Register default columns
        foreach ($this->get_original_columns() as $type => $label) {
            // Ignore the mandatory checkbox column
            if ('cb' === $type) {
                continue;
            }

            $column = new Column();
            $column->set_type($type)
                   ->set_original(true);

            $this->register_column_type($column);
        }

        // Load Custom columns
        $this->register_column_types();

        /**
         * Register column types
         *
         * @param ListScreen $this
         */
        do_action('ac/column_types', $this);
    }

    private function is_original_column(string $type): bool
    {
        $column = $this->get_column_by_type($type);

        return $column && $column->is_original();
    }

    public function deregister_column(string $column_name): void
    {
        unset($this->columns[$column_name]);
    }

    public function create_column(array $settings): ?Column
    {
        if ( ! isset($settings['type'])) {
            return null;
        }

        $class = $this->get_class_by_type((string)$settings['type']);

        if ( ! $class) {
            return null;
        }

        /**
         * @var Column $column
         */
        $column = new $class();
        $column->set_list_screen($this)
               ->set_type($settings['type']);

        if (isset($settings['name'])) {
            $column->set_name($settings['name']);
        }

        // Mark as original
        if ($this->is_original_column($settings['type'])) {
            $column->set_original(true);
            $column->set_name($settings['type']);
        }

        $column->set_options($settings);

        do_action('ac/list_screen/column_created', $column, $this);

        return $column;
    }

    protected function register_column(Column $column): void
    {
        $this->columns[$column->get_name()] = $column;

        /**
         * Fires when a column is registered to a list screen, i.e. when it is created. Can be used
         * to attach additional functionality to a column, such as exporting, sorting or filtering
         *
         * @param Column     $column      Column type object
         * @param ListScreen $list_screen List screen object to which the column was registered
         *
         * @since 3.0.5
         */
        do_action('ac/list_screen/column_registered', $column, $this);
    }

    public function set_settings(array $settings): self
    {
        $this->settings = $settings;

        return $this;
    }

    private function set_columns(): void
    {
        foreach ($this->get_settings() as $name => $data) {
            $data['name'] = $name;
            $column = $this->create_column($data);

            if ($column) {
                $this->register_column($column);
            }
        }

        // Nothing stored. Use WP default columns.
        if (null === $this->columns) {
            foreach ($this->get_original_columns() as $type => $label) {
                $column = $this->create_column(['type' => $type, 'original' => true]);

                if ( ! $column) {
                    continue;
                }

                $this->register_column($column);
            }
        }

        if (null === $this->columns) {
            $this->columns = [];
        }
    }

    public function get_settings(): array
    {
        return $this->settings;
    }

    public function is_user_allowed(WP_User $user): bool
    {
        return user_can($user, Capabilities::MANAGE) || $this->is_user_assigned($user);
    }

    public function is_user_assigned(WP_User $user): bool
    {
        $user_ids = $this->get_preference('users');
        $roles = $this->get_preference('roles');

        $user_ids = is_array($user_ids)
            ? array_filter(array_map('intval', $user_ids))
            : [];

        $roles = is_array($roles)
            ? array_filter(array_map('strval', $roles))
            : [];

        if ( ! $user_ids && ! $roles) {
            return true;
        }

        foreach ($roles as $role) {
            if ($user->has_cap($role)) {
                return true;
            }
        }

        return in_array($user->ID, $user_ids, true);
    }

    public function set_preferences(array $preferences): self
    {
        $this->preferences = apply_filters('ac/list_screen/preferences', $preferences, $this);

        return $this;
    }

    public function get_preferences(): array
    {
        return $this->preferences;
    }

    public function get_preference(string $key)
    {
        return $this->preferences[$key] ?? null;
    }

    public function get_screen_link(): string
    {
        _deprecated_function(__METHOD__, '4.6.5', 'AC\ListScreen::get_table_url()');

        return (string)$this->get_table_url();
    }

    public function get_edit_link(): string
    {
        _deprecated_function(__METHOD__, '4.6.5', 'AC\ListScreen::get_editor_url()');

        return (string)$this->get_editor_url();
    }

}