PluginProbe
404 Solution / 4.3.0
404 Solution v4.3.0
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 / View_RedirectForms.php

View_RedirectForms.php in 404 Solution 4.3.0, at includes/view/View_RedirectForms.php

198 lines 8.8 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 * Standalone Add/Edit redirect form rendering. Owns the legacy
9 * "Add Manual Redirect" form, the Edit Redirect form (active-from/until +
10 * conditions + cancel/submit row), and the default destination <optgroup>
11 * shared by the redirect-to dropdowns. The modern Add Redirect modal lives
12 * on View_RedirectsTable (it is rendered as part of the Redirects page).
13 *
14 * Outside callers (via the View facade __call dispatch):
15 * - PluginLogic admin edit flow (echoEditRedirect)
16 * - Legacy add-redirect paths (echoAddManualRedirect)
17 * - Redirect-to dropdown builders (echoRedirectDestinationOptionsDefaults)
18 */
19 class ABJ_404_Solution_View_RedirectForms extends ABJ_404_Solution_ViewComponent {
20
21 private function tpl(string $name): string {
22 $raw = ABJ_404_Solution_FileSystemService::readFileContents(dirname(__DIR__) . '/html/' . $name, false);
23 return rtrim((string)$raw, "\n");
24 }
25
26 /** @param array<string,string> $vars */
27 private function fillTpl(string $name, array $vars): string {
28 return (string)$this->f->str_replace(array_keys($vars), array_values($vars), $this->tpl($name));
29 }
30
31 /**
32 * @param array<string, mixed> $tableOptions
33 * @return void
34 */
35 public function echoAddManualRedirect($tableOptions) {
36
37 $options = $this->optionsPresenter->getOptionsWithDefaults();
38
39 $url = "?page=" . ABJ404_PP . "&subpage=abj404_redirects";
40 $orderby = array_key_exists('orderby', $tableOptions) && is_string($tableOptions['orderby']) ? $tableOptions['orderby'] : 'url';
41 $order = array_key_exists('order', $tableOptions) && is_string($tableOptions['order']) ? $tableOptions['order'] : 'ASC';
42 if (!($orderby == "url" && $order == "ASC")) {
43 $url .= "&orderby=" . sanitize_text_field($orderby) . "&order=" . sanitize_text_field($order);
44 }
45 $filter = array_key_exists('filter', $tableOptions) && is_scalar($tableOptions['filter']) ? $tableOptions['filter'] : 0;
46 if ($filter != 0) {
47 $url .= "&filter=" . $filter;
48 }
49 $link = wp_nonce_url($url, "abj404addRedirect");
50
51 $urlPlaceholder = parse_url(get_home_url(), PHP_URL_PATH) . "/example";
52 if (isset($_POST['url']) && $_POST['url'] != '') {
53 $postedURL = esc_url($_POST['url']);
54 } else {
55 $postedURL = $urlPlaceholder;
56 }
57
58 $selected301 = ($options['default_redirect'] == '301') ? ' selected ' : '';
59 $selected302 = ($options['default_redirect'] == '302') ? ' selected ' : '';
60 $selected307 = ($options['default_redirect'] == '307') ? ' selected ' : '';
61 $selected308 = ($options['default_redirect'] == '308') ? ' selected ' : '';
62 $selected410 = '';
63 $selected451 = '';
64 $selected0 = '';
65
66 // read the html content.
67 $html = ABJ_404_Solution_FileSystemService::readFileContents(dirname(__DIR__) . "/html/addManualRedirectTop.html");
68 $html .= ABJ_404_Solution_FileSystemService::readFileContents(dirname(__DIR__) .
69 "/html/addManualRedirectPageSearchDropdown.html");
70
71 $html = $this->f->str_replace(
72 array('{redirect_to_label}', '{TOOLTIP_POPUP_EXPLANATION_EMPTY}', '{TOOLTIP_POPUP_EXPLANATION_PAGE}',
73 '{TOOLTIP_POPUP_EXPLANATION_CUSTOM_STRING}', '{TOOLTIP_POPUP_EXPLANATION_URL}',
74 '{REDIRECT_TO_USER_FIELD_WARNING}', '{redirectPageTitle}', '{pageIDAndType}', '{data-url}'),
75 array(__('Redirect to', '404-solution'),
76 __('(Type a page name or an external URL)', '404-solution'),
77 __('(A page has been selected.)', '404-solution'),
78 __('(A custom string has been entered.)', '404-solution'),
79 __('(An external URL will be used.)', '404-solution'),
80 '', '', '',
81 "admin-ajax.php?action=echoRedirectToPages&includeDefault404Page=true&includeSpecial=true&nonce=" . wp_create_nonce('abj404_ajax')),
82 $html
83 );
84
85 $html .= ABJ_404_Solution_FileSystemService::readFileContents(dirname(__DIR__) . "/html/addManualRedirectBottom.html");
86 $html = $this->f->str_replace(
87 array('{addManualRedirectAction}', '{urlPlaceholder}', '{postedURL}',
88 '{301selected}', '{302selected}', '{307selected}', '{308selected}',
89 '{410selected}', '{451selected}', '{0selected}'),
90 array($link, esc_attr($urlPlaceholder), esc_attr($postedURL),
91 $selected301, $selected302, $selected307, $selected308,
92 $selected410, $selected451, $selected0),
93 $html
94 );
95
96 // constants and translations.
97 $html = $this->f->doNormalReplacements($html);
98
99 echo $html;
100 }
101
102 /** This is used both to add and to edit a redirect.
103 * @param ABJ_404_Solution_EditRedirectFormContext $ctx
104 * @return void
105 */
106 public function echoEditRedirect(ABJ_404_Solution_EditRedirectFormContext $ctx) {
107 $codeselected = $ctx->codeSelected;
108 $label = $ctx->label;
109 $source_page = $ctx->sourcePage;
110 $filter = $ctx->filter;
111 $orderby = $ctx->orderby;
112 $order = $ctx->order;
113 $startDate = $ctx->startDate;
114 $endDate = $ctx->endDate;
115 // allow-em-dash: comment-only context describing button grid section
116 // Redirect type button grid with hidden input
117 $this->redirectTypeUI->echoRedirectTypeButtonGrid((string)$codeselected);
118
119 // Advanced Options: Active From/Until + Conditions (collapsed by default)
120 $redirectId = 0;
121 $getRedirectId = $_GET['id'] ?? null;
122 $postRedirectId = $_POST['id'] ?? null;
123 if (is_scalar($getRedirectId) && $this->f->regexMatch('[0-9]+', (string)$getRedirectId)) {
124 $redirectId = absint($getRedirectId);
125 } elseif (is_scalar($postRedirectId) && $this->f->regexMatch('[0-9]+', (string)$postRedirectId)) {
126 $redirectId = absint($postRedirectId);
127 }
128 $hasExistingConditions = ($redirectId > 0) && !empty($this->redirectsRepository->getRedirectConditions($redirectId));
129 $hasAdvancedValues = ($startDate !== '' || $endDate !== '' || $hasExistingConditions);
130 $openAttr = $hasAdvancedValues ? ' open' : '';
131
132 ob_start();
133 $this->redirectConditions->echoRedirectConditionsSection();
134 $conditionsSection = (string)ob_get_clean();
135
136 echo $this->fillTpl('viewRedirectsTableAdvancedOptions.html', array(
137 '{open_attr}' => $openAttr,
138 '{advanced_options_label}' => esc_html__('Advanced Options', '404-solution'),
139 '{active_from_label}' => esc_html__('Active From (optional)', '404-solution'),
140 '{start_date_value}' => esc_attr($startDate),
141 '{active_from_help}' => esc_html__('Leave blank to activate immediately', '404-solution'),
142 '{active_until_label}' => esc_html__('Active Until (optional)', '404-solution'),
143 '{end_date_value}' => esc_attr($endDate),
144 '{active_until_help}' => esc_html__('Leave blank to never expire', '404-solution'),
145 '{conditions_section}' => $conditionsSection,
146 ));
147
148 // Cancel button URL
149 $cancelUrl = '?page=' . ABJ404_PP;
150 if ($source_page) {
151 $cancelUrl .= '&subpage=' . esc_attr($source_page);
152 }
153 if ($filter !== null) {
154 $cancelUrl .= '&filter=' . esc_attr($filter);
155 }
156 if ($orderby !== null) {
157 $cancelUrl .= '&orderby=' . esc_attr($orderby);
158 }
159 if ($order !== null) {
160 $cancelUrl .= '&order=' . esc_attr($order);
161 }
162
163 echo $this->f->str_replace(
164 array('{cancel_url}', '{cancel_label}', '{submit_label}'),
165 array(esc_url($cancelUrl), esc_html__('Cancel', '404-solution'), esc_html($label)),
166 $this->tpl('viewRedirectsTableEditButtonGroup.html')
167 );
168 }
169
170 /**
171 * @param string $currentlySelected
172 * @return string
173 */
174 public function echoRedirectDestinationOptionsDefaults($currentlySelected) {
175 $content = "";
176 $content .= "\n" . '<optgroup label="' . __('Special', '404-solution') . '">' . "\n";
177
178 $selected = "";
179 if ($currentlySelected == ABJ404_TYPE_EXTERNAL) {
180 $selected = " selected";
181 }
182 $content .= "\n<option value=\"" . ABJ404_TYPE_EXTERNAL . "|" . ABJ404_TYPE_EXTERNAL . "\"" . $selected . ">" .
183 __('External Page', '404-solution') . "</option>";
184
185 if ($currentlySelected == ABJ404_TYPE_HOME) {
186 $selected = " selected";
187 } else {
188 $selected = "";
189 }
190 $content .= "\n<option value=\"" . ABJ404_TYPE_HOME . "|" . ABJ404_TYPE_HOME . "\"" . $selected . ">" .
191 __('Home Page', '404-solution') . "</option>";
192
193 $content .= "\n" . '</optgroup>' . "\n";
194
195 return $content;
196 }
197 }
198