InlineStyle
1 day ago
ManageHeading
1 day ago
ManageValue
1 day ago
SaveHeading
1 day ago
Service
1 day ago
TableScreenRepository
1 day ago
AdminHeadStyle.php
1 day ago
ManageValueServiceFactory.php
1 day ago
PrimaryColumn.php
1 day ago
PrimaryColumnFactory.php
1 day ago
SaveHeadingFactory.php
1 day ago
Screen.php
1 day ago
ScreenTools.php
1 day ago
TableFormView.php
1 day ago
TablePreference.php
1 day ago
TableScreenCollection.php
1 day ago
TableScreenRepository.php
1 day ago
Screen.php
259 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Table; |
| 6 | |
| 7 | use AC; |
| 8 | use AC\Asset; |
| 9 | use AC\Asset\Location; |
| 10 | use AC\Capabilities; |
| 11 | use AC\ListScreen; |
| 12 | use AC\Registerable; |
| 13 | use AC\Renderable; |
| 14 | use AC\TableScreen; |
| 15 | use AC\Type\EditorUrlFactory; |
| 16 | |
| 17 | final class Screen implements Registerable |
| 18 | { |
| 19 | private Location $location; |
| 20 | |
| 21 | private AC\TableScreen $table_screen; |
| 22 | |
| 23 | private ?AC\ListScreen $list_screen; |
| 24 | |
| 25 | private array $screen_options = []; |
| 26 | |
| 27 | private AC\Settings\GeneralOption $option_storage; |
| 28 | |
| 29 | public function __construct( |
| 30 | AC\AdminColumns $plugin, |
| 31 | TableScreen $table_screen, |
| 32 | AC\Settings\GeneralOption $option_storage, |
| 33 | ?ListScreen $list_screen = null |
| 34 | ) { |
| 35 | $this->location = $plugin->get_location(); |
| 36 | $this->table_screen = $table_screen; |
| 37 | $this->list_screen = $list_screen; |
| 38 | $this->option_storage = $option_storage; |
| 39 | } |
| 40 | |
| 41 | public function register(): void |
| 42 | { |
| 43 | (new AC\Table\AdminHeadStyle())->register(); |
| 44 | |
| 45 | add_action('admin_enqueue_scripts', [$this, 'admin_scripts']); |
| 46 | add_action('admin_footer', [$this, 'admin_scripts_footer']); |
| 47 | add_filter('admin_body_class', [$this, 'admin_class']); |
| 48 | add_action('admin_footer', [$this, 'render_actions']); |
| 49 | add_filter('screen_settings', [$this, 'screen_options']); |
| 50 | } |
| 51 | |
| 52 | public function get_list_screen(): ?ListScreen |
| 53 | { |
| 54 | return $this->list_screen; |
| 55 | } |
| 56 | |
| 57 | private function show_edit_columns_action(): bool |
| 58 | { |
| 59 | if (! current_user_can(Capabilities::MANAGE)) { |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | return (new AC\Storage\Repository\EditButton($this->option_storage))->is_active(); |
| 64 | } |
| 65 | |
| 66 | public function render_actions(): void |
| 67 | { |
| 68 | ?> |
| 69 | <div id="ac-table-actions" class="ac-table-actions"> |
| 70 | <div class="ac-table-actions-buttons"> |
| 71 | </div> |
| 72 | </div> |
| 73 | <?php |
| 74 | } |
| 75 | |
| 76 | public function register_screen_option(Renderable $option): void |
| 77 | { |
| 78 | $this->screen_options[] = $option; |
| 79 | } |
| 80 | |
| 81 | public function screen_options($html): string |
| 82 | { |
| 83 | if (empty($this->screen_options)) { |
| 84 | return (string)$html; |
| 85 | } |
| 86 | |
| 87 | ob_start(); |
| 88 | ?> |
| 89 | |
| 90 | <fieldset class='acp-screen-option-prefs'> |
| 91 | <legend><?= __('Admin Columns', 'codepress-admin-columns'); ?></legend> |
| 92 | <div class="acp-so-container"> |
| 93 | <?php |
| 94 | |
| 95 | foreach ($this->screen_options as $option) { |
| 96 | echo $option->render(); |
| 97 | } |
| 98 | |
| 99 | ?> |
| 100 | </div> |
| 101 | </fieldset> |
| 102 | |
| 103 | <?php |
| 104 | |
| 105 | $html .= ob_get_clean(); |
| 106 | |
| 107 | return $html; |
| 108 | } |
| 109 | |
| 110 | private function get_edit_columns_url(): string |
| 111 | { |
| 112 | return (string)EditorUrlFactory::create( |
| 113 | $this->table_screen->get_id(), |
| 114 | $this->table_screen->is_network(), |
| 115 | $this->list_screen ? $this->list_screen->get_id() : null |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | public function admin_scripts_footer(): void |
| 120 | { |
| 121 | $count = $this->table_screen instanceof TableScreen\TotalItems |
| 122 | ? $this->table_screen->get_total_items() |
| 123 | : null; |
| 124 | |
| 125 | $formatted = $count |
| 126 | ? number_format_i18n($count) |
| 127 | : null; |
| 128 | ?> |
| 129 | <script> |
| 130 | const ac_table_total_items = <?= $count ?: 'null' ?>; |
| 131 | const ac_table_total_items_formatted = <?= wp_json_encode((string)$formatted) ?>; |
| 132 | </script> |
| 133 | <?php |
| 134 | } |
| 135 | |
| 136 | public function admin_scripts(): void |
| 137 | { |
| 138 | $style = new Asset\Style( |
| 139 | 'ac-table', |
| 140 | $this->location->with_suffix('assets/css/table.css'), |
| 141 | ['ac-ui', 'ac-material-symbols'] |
| 142 | ); |
| 143 | $style->enqueue(); |
| 144 | |
| 145 | $edit_columns_translation = __('Edit columns', 'codepress-admin-columns'); |
| 146 | $table_translation = Asset\Script\Localize\Translation::create([ |
| 147 | 'value_loading' => __('Loading...', 'codepress-admin-columns'), |
| 148 | 'edit' => __('Edit', 'codepress-admin-columns'), |
| 149 | 'view' => __('View', 'codepress-admin-columns'), |
| 150 | 'download' => __('Download', 'codepress-admin-columns'), |
| 151 | 'edit_columns' => $edit_columns_translation, |
| 152 | 'edit_columns_tooltip' => sprintf( |
| 153 | __("Show %s button on table screen.", 'codepress-admin-columns'), |
| 154 | $edit_columns_translation |
| 155 | ), |
| 156 | ]); |
| 157 | |
| 158 | $script = new Asset\Script( |
| 159 | 'ac-table', |
| 160 | $this->location->with_suffix('assets/js/table.js'), |
| 161 | ['jquery', Asset\Script\GlobalTranslationFactory::HANDLE], |
| 162 | ); |
| 163 | |
| 164 | $args = [ |
| 165 | 'layout' => '', |
| 166 | 'column_types' => '', |
| 167 | 'read_only' => false, |
| 168 | 'assets' => $this->location->with_suffix('assets/')->get_url(), |
| 169 | 'list_screen' => (string)$this->table_screen->get_id(), |
| 170 | 'ajax_nonce' => wp_create_nonce('ac-ajax'), |
| 171 | 'table_id' => $this->table_screen->get_attr_id(), |
| 172 | 'screen' => $this->table_screen->get_screen_id(), |
| 173 | 'list_screen_link' => $this->get_list_screen_clear_link(), |
| 174 | 'show_edit_columns' => $this->show_edit_columns_action(), |
| 175 | 'edit_columns_url' => $this->get_edit_columns_url(), |
| 176 | 'current_user_id' => get_current_user_id(), |
| 177 | 'number_format' => [ |
| 178 | 'decimal_point' => $this->get_local_number_format('decimal_point'), |
| 179 | 'thousands_sep' => $this->get_local_number_format('thousands_sep'), |
| 180 | ], |
| 181 | 'meta_type' => $this->table_screen instanceof AC\TableScreen\MetaType |
| 182 | ? (string)$this->table_screen->get_meta_type() |
| 183 | : '', |
| 184 | ]; |
| 185 | |
| 186 | if ($this->list_screen) { |
| 187 | $args['column_types'] = $this->get_column_types_mapping($this->list_screen); |
| 188 | $args['layout'] = (string)$this->list_screen->get_id(); |
| 189 | $args['read_only'] = $this->list_screen->is_read_only(); |
| 190 | |
| 191 | do_action('ac/table_scripts', $this->list_screen, $this); |
| 192 | } |
| 193 | |
| 194 | $script |
| 195 | ->add_inline_variable('ac_table', $args) |
| 196 | ->localize('ac_table_i18n', $table_translation) |
| 197 | ->enqueue(); |
| 198 | } |
| 199 | |
| 200 | public function admin_class($classes): string |
| 201 | { |
| 202 | $classes .= ' ac-' . $this->table_screen->get_id(); |
| 203 | |
| 204 | return (string)apply_filters('ac/table/body_class', $classes, $this); |
| 205 | } |
| 206 | |
| 207 | private function get_column_types_mapping(ListScreen $list_screen): array |
| 208 | { |
| 209 | $types = []; |
| 210 | |
| 211 | foreach ($list_screen->get_columns() as $column) { |
| 212 | $types[(string)$column->get_id()] = $column->get_type(); |
| 213 | } |
| 214 | |
| 215 | return $types; |
| 216 | } |
| 217 | |
| 218 | private function get_local_number_format(string $var) |
| 219 | { |
| 220 | global $wp_locale; |
| 221 | |
| 222 | return $wp_locale->number_format[$var] ?? null; |
| 223 | } |
| 224 | |
| 225 | private function get_list_screen_clear_link(): string |
| 226 | { |
| 227 | $url = $this->list_screen |
| 228 | ? $this->list_screen->get_table_url() |
| 229 | : $this->table_screen->get_url(); |
| 230 | |
| 231 | $query_args_whitelist = [ |
| 232 | 'layout', |
| 233 | 'orderby', |
| 234 | 'order', |
| 235 | ]; |
| 236 | |
| 237 | switch (true) { |
| 238 | case $this->table_screen instanceof AC\PostType: |
| 239 | $query_args_whitelist[] = 'post_status'; |
| 240 | break; |
| 241 | case $this->table_screen instanceof AC\TableScreen\User: |
| 242 | $query_args_whitelist[] = 'role'; |
| 243 | break; |
| 244 | case $this->table_screen instanceof AC\TableScreen\Comment: |
| 245 | $query_args_whitelist[] = 'comment_status'; |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | foreach ($query_args_whitelist as $query_arg) { |
| 250 | if (isset($_GET[$query_arg]) && is_string($_GET[$query_arg])) { |
| 251 | $url = $url->with_arg($query_arg, $_GET[$query_arg]); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return (string)$url; |
| 256 | } |
| 257 | |
| 258 | } |
| 259 |