AdminGeneralOptionsGet.php
3 months ago
AdminGeneralOptionsPersist.php
3 months ago
CustomFieldKeys.php
3 months ago
EditorMenuFavorites.php
3 months ago
EditorMenuStatus.php
3 months ago
ExtendedValue.php
3 months ago
IntegrationToggle.php
3 months ago
Integrations.php
3 months ago
ListScreenAddColumn.php
3 months ago
ListScreenDelete.php
3 months ago
ListScreenOriginalColumns.php
3 months ago
ListScreenSave.php
3 months ago
ListScreenSelectColumn.php
3 months ago
ListScreenSettings.php
3 months ago
NetworkPostStati.php
3 months ago
NumberFormat.php
3 months ago
RestoreSettingsRequest.php
3 months ago
ScreenOptions.php
3 months ago
ListScreenSettings.php
127 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\RequestHandler\Ajax; |
| 6 | |
| 7 | use AC; |
| 8 | use AC\Admin\Preference; |
| 9 | use AC\Capabilities; |
| 10 | use AC\Form\NonceFactory; |
| 11 | use AC\ListScreen; |
| 12 | use AC\ListScreenRepository\Storage; |
| 13 | use AC\Request; |
| 14 | use AC\RequestAjaxHandler; |
| 15 | use AC\Type\TableId; |
| 16 | use InvalidArgumentException; |
| 17 | |
| 18 | class ListScreenSettings implements RequestAjaxHandler |
| 19 | { |
| 20 | |
| 21 | protected Storage $storage; |
| 22 | |
| 23 | protected AC\TableScreenFactory\Aggregate $table_factory; |
| 24 | |
| 25 | protected Preference\EditorPreference $editor_preference; |
| 26 | |
| 27 | protected AC\Response\JsonListScreenSettingsFactory $response_factory; |
| 28 | |
| 29 | private AC\Type\ListScreenIdGenerator $list_screen_id_generator; |
| 30 | |
| 31 | public function __construct( |
| 32 | Storage $storage, |
| 33 | AC\TableScreenFactory\Aggregate $table_factory, |
| 34 | Preference\EditorPreference $preference, |
| 35 | AC\Response\JsonListScreenSettingsFactory $response_factory, |
| 36 | AC\Type\ListScreenIdGenerator $list_screen_id_generator |
| 37 | ) { |
| 38 | $this->storage = $storage; |
| 39 | $this->table_factory = $table_factory; |
| 40 | $this->editor_preference = $preference; |
| 41 | $this->response_factory = $response_factory; |
| 42 | $this->list_screen_id_generator = $list_screen_id_generator; |
| 43 | } |
| 44 | |
| 45 | protected function validate(): void |
| 46 | { |
| 47 | $response = new AC\Response\Json(); |
| 48 | |
| 49 | if ( ! current_user_can(Capabilities::MANAGE)) { |
| 50 | $response->error(); |
| 51 | } |
| 52 | |
| 53 | if ( ! NonceFactory::create_ajax()->verify(new Request())) { |
| 54 | $response->error(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | protected function set_editor_preference(ListScreen $list_screen): void |
| 59 | { |
| 60 | $this->editor_preference->save( |
| 61 | $list_screen->get_table_screen()->get_id(), |
| 62 | $list_screen->get_id() |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | protected function is_template(ListScreen $list_screen): bool |
| 67 | { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | protected function add_middleware(Request $request, AC\TableScreen $table_screen): void |
| 72 | { |
| 73 | $request->add_middleware( |
| 74 | new Request\Middleware\ListScreenAdmin( |
| 75 | $this->storage, |
| 76 | $table_screen, |
| 77 | $this->editor_preference, |
| 78 | ) |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | public function handle(): void |
| 83 | { |
| 84 | $this->validate(); |
| 85 | |
| 86 | $request = new Request(); |
| 87 | |
| 88 | $table_id = new TableId((string)$request->get('list_key')); |
| 89 | |
| 90 | if ( ! $this->table_factory->can_create($table_id)) { |
| 91 | throw new InvalidArgumentException('Invalid table screen.'); |
| 92 | } |
| 93 | |
| 94 | $table_screen = $this->table_factory->create($table_id); |
| 95 | |
| 96 | $this->add_middleware($request, $table_screen); |
| 97 | |
| 98 | $list_screen = $request->get('list_screen'); |
| 99 | |
| 100 | // List Screen and context properties |
| 101 | $is_template = false; |
| 102 | $is_stored = false; |
| 103 | $title = $table_screen->get_labels()->get_singular(); |
| 104 | |
| 105 | if ($list_screen instanceof ListScreen) { |
| 106 | $this->set_editor_preference($list_screen); |
| 107 | |
| 108 | if ( ! trim($list_screen->get_title())) { |
| 109 | $list_screen->set_title($title); |
| 110 | } |
| 111 | |
| 112 | $is_template = $this->is_template($list_screen); |
| 113 | $is_stored = true; |
| 114 | } else { |
| 115 | $list_screen = new ListScreen( |
| 116 | $this->list_screen_id_generator->generate(), |
| 117 | $title, |
| 118 | $table_screen |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | $this->response_factory |
| 123 | ->create($list_screen, $is_stored, $is_template) |
| 124 | ->success(); |
| 125 | } |
| 126 | |
| 127 | } |