PluginProbe ʕ •ᴥ•ʔ
Admin Columns / 7.1.4
Admin Columns v7.1.4
7.1.4 7.0.19 2.3.5 2.4 2.4.1 2.4.10 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.6.1 2.5.6.2 2.5.6.3 2.5.6.4 3.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.7 3.1 3.1.1 3.1.10 3.1.2 3.1.3 3.1.5 3.2.3 3.2.7 3.3.1 3.4.1 3.4.6 3.4.8 4.0.1 4.0.3 4.1.6 4.2.2 4.2.5 4.3 4.3.2 4.4.1 4.4.4 4.4.5 4.5.5 4.6.1 4.7.18 4.7.19 4.7.20 4.7.7 7.0.13 7.0.14 7.0.16 trunk 1.0 1.1 1.1.3 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.5.1 1.4.6 1.4.6.1 1.4.6.2 1.4.6.3 1.4.6.4 1.4.7 1.4.8 1.4.9 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2 2.2.1 2.2.1.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.5.1 2.2.6 2.2.6.1 2.2.6.2 2.2.6.3 2.2.6.4 2.2.7 2.2.8 2.2.8.1 2.2.9 2.3.1 2.3.2 2.3.3
codepress-admin-columns / classes / Response / JsonListScreenSettingsFactory.php
codepress-admin-columns / classes / Response Last commit date
Json.php 1 day ago JsonColumnFactory.php 1 day ago JsonListScreenSettingsFactory.php 1 day ago
JsonListScreenSettingsFactory.php
216 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace AC\Response;
6
7 use AC;
8 use AC\Admin\Banner\BannerContextResolver;
9 use AC\Helper\Mbstring;
10 use AC\ListScreen;
11 use AC\Setting\Encoder;
12 use AC\Storage\Encoder\BaseEncoder;
13 use AC\Storage\EncoderFactory;
14 use AC\Type\StartingPrice;
15 use AC\Type\Url\Preview;
16 use AC\Type\Url\Site;
17 use AC\Type\Url\UtmTags;
18 use LogicException;
19
20 class JsonListScreenSettingsFactory
21 {
22 private EncoderFactory $encoder_factory;
23
24 private AC\ColumnTypeRepository $type_repository;
25
26 private AC\ColumnGroups $column_groups;
27
28 private BannerContextResolver $banner_context_resolver;
29
30 public function __construct(
31 EncoderFactory $encoder_factory,
32 AC\ColumnTypeRepository $type_repository,
33 AC\ColumnGroups $column_groups,
34 BannerContextResolver $banner_context_resolver
35 ) {
36 $this->encoder_factory = $encoder_factory;
37 $this->type_repository = $type_repository;
38 $this->column_groups = $column_groups;
39 $this->banner_context_resolver = $banner_context_resolver;
40 }
41
42 public function create(ListScreen $list_screen, bool $is_stored = true, bool $is_template = false): Json
43 {
44 $encoder = $this->encoder_factory->create();
45
46 if (! $encoder instanceof BaseEncoder) {
47 throw new LogicException('Encoder does not support setting a list screen.');
48 }
49
50 $encoder->set_list_screen($list_screen);
51
52 $table_screen = $list_screen->get_table_screen();
53
54 $context = $this->banner_context_resolver->resolve($table_screen);
55
56 return (new Json())->set_parameters([
57 'read_only' => $list_screen->is_read_only(),
58 'table_url' => $is_template
59 ? (string)new Preview($list_screen->get_table_url(), 'columns')
60 : (string)$list_screen->get_table_url(),
61 'settings' => $encoder->encode(),
62 'column_types' => $this->get_column_types($table_screen),
63 'column_settings' => $this->encode_column_settings($list_screen->get_columns()),
64 'is_stored' => $is_stored,
65 'is_template' => $is_template,
66 'labels' => [
67 'singular' => $table_screen->get_labels()->get_singular(),
68 'plural' => $table_screen->get_labels()->get_plural(),
69 ],
70 'pro_banner' => $context
71 ? $context->get_arguments($table_screen)
72 : $this->get_default_banner_arguments($table_screen),
73 ]);
74 }
75
76 private function get_column_types(AC\TableScreen $table_screen): array
77 {
78 $column_types = [];
79
80 $original_types = $this->get_original_types($table_screen);
81
82 foreach ($this->type_repository->find_all($table_screen) as $column) {
83 $group = $this->column_groups->find($column->get_group());
84 $label = $this->get_clean_label($column);
85
86 $column_type = [
87 'label' => $label,
88 'value' => $column->get_type(),
89 'group' => $group ? $group->get_label() : __('Default', 'codepress-admin-columns'),
90 'group_key' => $column->get_group(),
91 'original' => in_array($column->get_type(), $original_types, true),
92 'searchable_label' => $label,
93 ];
94
95 if ('custom' !== $column->get_group()) {
96 $column_type['searchable_label'] .= ' ' . $column_type['group'];
97 }
98
99 $description = $column->get_description();
100 if ($description !== null) {
101 $column_type['description'] = $description;
102 }
103
104 $column_types[] = $column_type;
105 }
106
107 usort($column_types, function ($a, $b) {
108 // ignore original columns
109 if ($a['original'] || $b['original']) {
110 return 0;
111 }
112
113 return strcasecmp($a['label'], $b['label']);
114 });
115
116 return $column_types;
117 }
118
119 private function get_original_types(AC\TableScreen $table_screen): array
120 {
121 $types = [];
122 foreach ($this->type_repository->find_all_original($table_screen) as $column) {
123 $types[] = $column->get_type();
124 }
125
126 return $types;
127 }
128
129 private function get_clean_label(AC\Column $column): string
130 {
131 $label = $column->get_label();
132
133 if (strip_tags($label) === '') {
134 $label = ucfirst(str_replace('_', ' ', $column->get_type()));
135 }
136
137 return strip_tags($label);
138 }
139
140 private function encode_column_settings(AC\ColumnIterator $columns): array
141 {
142 $settings = [];
143
144 foreach ($columns as $column) {
145 $settings[(string)$column->get_id()] = (new Encoder($column->get_settings()))->encode();
146 }
147
148 return $settings;
149 }
150
151 private function get_default_banner_arguments(AC\TableScreen $table_screen): array
152 {
153 $upgrade_url = new UtmTags(Site::create_admin_columns_pro(), 'banner');
154
155 $plural = $table_screen->get_labels()->get_plural();
156 $singular = $table_screen->get_labels()->get_singular();
157
158 if (Mbstring::strlen($plural) > 30) {
159 $plural = __('content', 'codepress-admin-columns');
160 $singular = __('item', 'codepress-admin-columns');
161 }
162
163 $plural_lower = Mbstring::strtolower($plural);
164 $singular_lower = Mbstring::strtolower($singular);
165
166 return [
167 'title' => sprintf(
168 __('Manage your %s faster', 'codepress-admin-columns'),
169 $plural_lower
170 ),
171 'description' => sprintf(
172 __('Turn your %1$s overview into a workspace for sorting, editing, filtering, and exporting - without opening a single %2$s.', 'codepress-admin-columns'),
173 $plural_lower,
174 $singular_lower
175 ),
176 'upgrade_cta' => sprintf(__('Manage your %s faster', 'codepress-admin-columns'), $plural_lower),
177 'upgrade_cta_price' => sprintf(
178 '%s · %s',
179 sprintf(
180 /* translators: %s: price (e.g. $79) */
181 __('from %s/year', 'codepress-admin-columns'),
182 StartingPrice::get()
183 ),
184 __('all features included', 'codepress-admin-columns')
185 ),
186 'features' => [
187 [
188 'url' => $upgrade_url->with_content('usp-editing')->get_url(),
189 'label' => __('Inline edit directly in the table', 'codepress-admin-columns'),
190 ],
191 [
192 'url' => $upgrade_url->with_content('usp-sorting')->get_url(),
193 'label' => __('Sort and filter on any column', 'codepress-admin-columns'),
194 ],
195 [
196 'url' => $upgrade_url->with_content('usp-bulk-edit')->get_url(),
197 'label' => sprintf(
198 __('Bulk edit hundreds of %s at once', 'codepress-admin-columns'),
199 $plural_lower
200 ),
201 ],
202 [
203 'url' => $upgrade_url->with_content('usp-export')->get_url(),
204 'label' => __('Export table data to CSV', 'codepress-admin-columns'),
205 ],
206 [
207 'url' => $upgrade_url->with_content('usp-column-sets')->get_url(),
208 'label' => __('Multiple views per screen', 'codepress-admin-columns'),
209 ],
210 ],
211 'promo_url' => $upgrade_url->get_url(),
212 ];
213 }
214
215 }
216