PluginProbe
404 Solution / trunk
404 Solution vtrunk
4.3.5 4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.2.0 4.1.19 4.1.18 4.1.17 4.1.16 4.1.15 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.1.7 4.1.6 4.1.5 4.1.4 4.1.3 trunk 2.30.0 All 109 releases
404-solution / includes / view / RedirectDestinationOptionsPresenter.php

RedirectDestinationOptionsPresenter.php in 404 Solution trunk, at includes/view/RedirectDestinationOptionsPresenter.php

166 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 /**
8 * Renders legacy redirect destination option groups for posts and taxonomies.
9 */
10 class ABJ_404_Solution_RedirectDestinationOptionsPresenter {
11
12 /**
13 * @param string $dest
14 * @param array<int, object> $rows
15 * @param callable|null $debugInfoWriter Receives debug text while rendering large option sets.
16 * @return string
17 */
18 public function buildPostOptions(string $dest, array $rows, ?callable $debugInfoWriter = null): string {
19 $content = array();
20 $rowCounter = 0;
21 $currentPostType = '';
22
23 foreach ($rows as $row) {
24 $rowCounter++;
25 /** @var object{id: int|string, post_type: string, depth?: int} $row */
26 $id = is_scalar($row->id) ? (string)$row->id : '';
27 $titleId = is_numeric($id) ? (int)$id : 0;
28 $theTitle = get_the_title($titleId);
29 $thisval = $id . "|" . ABJ404_TYPE_POST;
30
31 if ($debugInfoWriter !== null) {
32 $debugInfoWriter('Before row: ' . $rowCounter . ', Title: ' . $theTitle .
33 ', Post type: ' . $row->post_type);
34 }
35
36 if ($row->post_type != $currentPostType) {
37 if ($currentPostType != '') {
38 $content[] = "\n" . '</optgroup>' . "\n";
39 }
40
41 $postTypeLabel = $this->postTypeLabel((string)$row->post_type);
42 $content[] = "\n" . '<optgroup label="' . esc_attr($postTypeLabel) . '">' . "\n";
43 $currentPostType = $row->post_type;
44 }
45
46 $content[] = "\n <option value=\"";
47 $content[] = esc_attr($thisval);
48 $content[] = "\"";
49 $content[] = ($thisval == $dest) ? " selected" : "";
50 $content[] = ">";
51
52 $depth = property_exists($row, 'depth') ? intval($row->depth) : 0;
53 for ($i = 0; $i < $depth; $i++) {
54 $content[] = "&nbsp;&nbsp;&nbsp;";
55 }
56
57 $content[] = esc_html($this->postTypeLabel((string)$row->post_type));
58 $content[] = ": ";
59 $content[] = esc_html($theTitle);
60 $content[] = "</option>";
61
62 if ($debugInfoWriter !== null) {
63 $debugInfoWriter('After row: ' . $rowCounter . ', Title: ' . $theTitle .
64 ', Post type: ' . $row->post_type);
65 }
66 }
67
68 if ($currentPostType !== '') {
69 $content[] = "\n" . '</optgroup>' . "\n";
70 }
71
72 if ($debugInfoWriter !== null) {
73 $debugInfoWriter('Cleared after building redirect destination page list.');
74 }
75
76 return implode('', $content);
77 }
78
79 /**
80 * @param string $dest
81 * @param array<int, WP_Term|object> $categories
82 * @param array<int, WP_Term|object> $tags
83 * @param array<string, array<int, WP_Term|object>> $customCategories
84 * @return string
85 */
86 public function buildTaxonomyOptions(string $dest, array $categories, array $tags, array $customCategories): string {
87 $content = "";
88 $content .= "\n" . '<optgroup label="' . esc_attr(__('Categories', '404-solution')) . '">' . "\n";
89
90 foreach ($categories as $cat) {
91 /** @var WP_Term|object{term_id: int|string, name: string, taxonomy: string} $cat */
92 if ($cat->taxonomy != 'category') {
93 continue;
94 }
95 $content .= $this->buildTaxonomyOption(
96 (string)$cat->term_id,
97 ABJ404_TYPE_CAT,
98 __('Category', '404-solution'),
99 (string)$cat->name,
100 $dest
101 );
102 }
103 $content .= "\n" . '</optgroup>' . "\n";
104
105 $content .= "\n" . '<optgroup label="' . esc_attr(__('Tags', '404-solution')) . '">' . "\n";
106 foreach ($tags as $tag) {
107 /** @var WP_Term|object{term_id: int|string, name: string} $tag */
108 $content .= $this->buildTaxonomyOption(
109 (string)$tag->term_id,
110 ABJ404_TYPE_TAG,
111 __('Tag', '404-solution'),
112 (string)$tag->name,
113 $dest
114 );
115 }
116 $content .= "\n" . '</optgroup>' . "\n";
117
118 foreach ($customCategories as $taxonomy => $catRow) {
119 $content .= "\n" . '<optgroup label="' . esc_attr($taxonomy) . '">' . "\n";
120 foreach ($catRow as $cat) {
121 /** @var WP_Term|object{term_id: int|string, name: string} $cat */
122 $content .= $this->buildTaxonomyOption(
123 (string)$cat->term_id,
124 ABJ404_TYPE_CAT,
125 __('Custom', '404-solution'),
126 (string)$cat->name,
127 $dest
128 );
129 }
130 $content .= "\n" . '</optgroup>' . "\n";
131 }
132
133 return $content;
134 }
135
136 /**
137 * @param string $id
138 * @param int $type
139 * @param string $label
140 * @param string $title
141 * @param string $dest
142 * @return string
143 */
144 private function buildTaxonomyOption(string $id, int $type, string $label, string $title, string $dest): string {
145 $thisval = $id . "|" . $type;
146 $selected = ($thisval == $dest) ? " selected" : "";
147 return "\n<option value=\"" . esc_attr($thisval) . "\"" . $selected . ">" .
148 esc_html($label) . ": " . esc_html($title) . "</option>";
149 }
150
151 private function postTypeLabel(string $postType): string {
152 if (function_exists('get_post_type_object')) {
153 $postTypeObject = get_post_type_object($postType);
154 if (is_object($postTypeObject) && property_exists($postTypeObject, 'labels') && is_object($postTypeObject->labels)) {
155 $labels = $postTypeObject->labels;
156 $label = $labels->singular_name ?? $labels->name ?? '';
157 if (is_scalar($label) && (string)$label !== '') {
158 return (string)$label;
159 }
160 }
161 }
162
163 return ucwords(str_replace(array('_', '-'), ' ', $postType));
164 }
165 }
166