ColumnRequest
6 months ago
ListScreen
6 months ago
Middleware
6 months ago
AjaxColumnModalValue.php
6 months ago
AjaxColumnRequest.php
6 months ago
AjaxColumnValue.php
6 months ago
AjaxGeneralOptions.php
6 months ago
AjaxRequestCustomFieldKeys.php
6 months ago
AjaxScreenOptions.php
6 months ago
ColumnRequest.php
6 months ago
DefaultColumns.php
6 months ago
ListScreenRestoreColumns.php
6 months ago
RestoreSettingsRequest.php
6 months ago
TableListScreenSetter.php
6 months ago
ColumnRequest.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AC\Controller; |
| 4 | |
| 5 | use AC; |
| 6 | use AC\Column; |
| 7 | use AC\Column\Placeholder; |
| 8 | use AC\ListScreen; |
| 9 | use AC\Request; |
| 10 | use AC\View; |
| 11 | |
| 12 | abstract class ColumnRequest |
| 13 | { |
| 14 | |
| 15 | private $list_screen_factory; |
| 16 | |
| 17 | public function __construct(AC\ListScreenFactory $list_screen_factory) |
| 18 | { |
| 19 | $this->list_screen_factory = $list_screen_factory; |
| 20 | } |
| 21 | |
| 22 | abstract protected function get_column(Request $request, ListScreen $list_screen): ?Column; |
| 23 | |
| 24 | public function request(Request $request): void |
| 25 | { |
| 26 | $list_key = (string)$request->get('list_screen'); |
| 27 | |
| 28 | if ( ! $this->list_screen_factory->can_create($list_key)) { |
| 29 | exit; |
| 30 | } |
| 31 | |
| 32 | $list_screen = $this->list_screen_factory->create($list_key); |
| 33 | |
| 34 | $column = $this->get_column($request, $list_screen); |
| 35 | |
| 36 | if ( ! $column) { |
| 37 | wp_send_json_error([ |
| 38 | 'type' => 'message', |
| 39 | 'error' => sprintf( |
| 40 | __('Please visit the %s screen once to load all available columns', 'codepress-admin-columns'), |
| 41 | ac_helper()->html->link((string)$list_screen->get_table_url(), $list_screen->get_label()) |
| 42 | ), |
| 43 | ]); |
| 44 | } |
| 45 | |
| 46 | $current_original_columns = (array)json_decode($request->get('current_original_columns', ''), true); |
| 47 | |
| 48 | // Not cloneable message |
| 49 | if (in_array($column->get_type(), $current_original_columns, true)) { |
| 50 | wp_send_json_error([ |
| 51 | 'type' => 'message', |
| 52 | 'error' => sprintf( |
| 53 | __('%s column is already present and can not be duplicated.', 'codepress-admin-columns'), |
| 54 | '<strong>' . $column->get_label() . '</strong>' |
| 55 | ), |
| 56 | ]); |
| 57 | } |
| 58 | |
| 59 | // Placeholder message |
| 60 | if ($column instanceof Placeholder) { |
| 61 | wp_send_json_error([ |
| 62 | 'type' => 'message', |
| 63 | 'error' => $column->get_message(), |
| 64 | ]); |
| 65 | } |
| 66 | |
| 67 | wp_send_json_success($this->render_column($column)); |
| 68 | } |
| 69 | |
| 70 | private function render_column(Column $column): string |
| 71 | { |
| 72 | $view = new View([ |
| 73 | 'column' => $column, |
| 74 | ]); |
| 75 | |
| 76 | $view->set_template('admin/edit-column'); |
| 77 | |
| 78 | return $view->render(); |
| 79 | } |
| 80 | |
| 81 | } |