| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Presents the Add Manual Redirect modal on the Page Redirects admin table. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_RedirectAddModalPresenter { |
| 11 |
|
| 12 |
/** @var ABJ_404_Solution_Functions */ |
| 13 |
private $functions; |
| 14 |
|
| 15 |
/** @var ABJ_404_Solution_View_OptionsPresenter */ |
| 16 |
private $optionsPresenter; |
| 17 |
|
| 18 |
/** @var ABJ_404_Solution_View_RedirectTypeUI */ |
| 19 |
private $redirectTypeUI; |
| 20 |
|
| 21 |
/** @var ABJ_404_Solution_View_RedirectConditions */ |
| 22 |
private $redirectConditions; |
| 23 |
|
| 24 |
public function __construct(ABJ_404_Solution_Functions $functions, ABJ_404_Solution_View_OptionsPresenter $optionsPresenter, |
| 25 |
ABJ_404_Solution_View_RedirectTypeUI $redirectTypeUI, |
| 26 |
ABJ_404_Solution_View_RedirectConditions $redirectConditions) { |
| 27 |
$this->functions = $functions; |
| 28 |
$this->optionsPresenter = $optionsPresenter; |
| 29 |
$this->redirectTypeUI = $redirectTypeUI; |
| 30 |
$this->redirectConditions = $redirectConditions; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @param array<string, mixed> $tableOptions |
| 35 |
*/ |
| 36 |
public function render(array $tableOptions): string { |
| 37 |
$options = $this->optionsPresenter->getOptionsWithDefaults(); |
| 38 |
$link = wp_nonce_url($this->formUrl($tableOptions), 'abj404addRedirect'); |
| 39 |
$urlPlaceholder = parse_url(get_home_url(), PHP_URL_PATH) . '/example'; |
| 40 |
|
| 41 |
return $this->fillTpl('viewRedirectsTableAddModal.html', array( |
| 42 |
'{modal_title}' => esc_html__('Add Manual Redirect', '404-solution'), |
| 43 |
'{form_action}' => esc_url($link), |
| 44 |
'{url_label}' => esc_html__('URL', '404-solution'), |
| 45 |
'{url_placeholder}' => esc_attr($urlPlaceholder), |
| 46 |
'{url_help}' => esc_html__('The URL path that should be redirected (without domain)', '404-solution'), |
| 47 |
'{regex_label}' => esc_html__('Treat this URL as a regular expression', '404-solution'), |
| 48 |
'{explain_label}' => esc_html__('(Explain)', '404-solution'), |
| 49 |
'{regex_help_1}' => esc_html__('When checked, the text is treated as a regular expression. Note that including a bad regular expression or one that takes too long will break your website. So please use caution and test them elsewhere before trying them here. If you don\'t know what you\'re doing please don\'t use this option (as it\'s not necessary for the functioning of the plugin).', '404-solution'), |
| 50 |
'{example_label}' => esc_html__('Example:', '404-solution'), |
| 51 |
'{regex_help_2}' => esc_html__('/events/(.+) will match any URL that begins with /events/ and redirect to the specified page. Since a capture group is used, you can use a $1 replacement in the destination string of an external URL.', '404-solution'), |
| 52 |
'{regex_help_3}' => esc_html__('First, all of the normal "exact match" URLs are checked, then all of the regular expression URLs are checked.', '404-solution'), |
| 53 |
'{redirect_to_html}' => $this->redirectToHtml(), |
| 54 |
'{redirect_type_grid}' => $this->redirectTypeGrid($options), |
| 55 |
'{advanced_options}' => $this->advancedOptionsHtml(), |
| 56 |
'{cancel_label}' => esc_html__('Cancel', '404-solution'), |
| 57 |
'{add_redirect_label}' => esc_html__('Add', '404-solution'), |
| 58 |
)); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @param array<string, mixed> $tableOptions |
| 63 |
*/ |
| 64 |
private function formUrl(array $tableOptions): string { |
| 65 |
$url = '?page=' . ABJ404_PP . '&subpage=abj404_redirects'; |
| 66 |
$orderby = array_key_exists('orderby', $tableOptions) && is_string($tableOptions['orderby']) ? $tableOptions['orderby'] : 'url'; |
| 67 |
$order = array_key_exists('order', $tableOptions) && is_string($tableOptions['order']) ? $tableOptions['order'] : 'ASC'; |
| 68 |
if (!($orderby == 'url' && $order == 'ASC')) { |
| 69 |
$url .= '&orderby=' . sanitize_text_field($orderby) . '&order=' . sanitize_text_field($order); |
| 70 |
} |
| 71 |
$filter = array_key_exists('filter', $tableOptions) && is_scalar($tableOptions['filter']) ? $tableOptions['filter'] : 0; |
| 72 |
if ($filter != 0) { |
| 73 |
$url .= '&filter=' . $filter; |
| 74 |
} |
| 75 |
return $url; |
| 76 |
} |
| 77 |
|
| 78 |
private function redirectToHtml(): string { |
| 79 |
$redirectHtml = $this->tpl('addManualRedirectPageSearchDropdown.html'); |
| 80 |
$redirectHtml = $this->functions->str_replace( |
| 81 |
array( |
| 82 |
'{redirect_to_label}', |
| 83 |
'{TOOLTIP_POPUP_EXPLANATION_EMPTY}', |
| 84 |
'{TOOLTIP_POPUP_EXPLANATION_PAGE}', |
| 85 |
'{TOOLTIP_POPUP_EXPLANATION_CUSTOM_STRING}', |
| 86 |
'{TOOLTIP_POPUP_EXPLANATION_URL}', |
| 87 |
'{REDIRECT_TO_USER_FIELD_WARNING}', |
| 88 |
'{redirectPageTitle}', |
| 89 |
'{pageIDAndType}', |
| 90 |
'{data-url}', |
| 91 |
), |
| 92 |
array( |
| 93 |
esc_html__('Redirect to', '404-solution') . ' *', |
| 94 |
__('(Type a page name or an external URL)', '404-solution'), |
| 95 |
__('(A page has been selected.)', '404-solution'), |
| 96 |
__('(A custom string has been entered.)', '404-solution'), |
| 97 |
__('(An external URL will be used.)', '404-solution'), |
| 98 |
'', |
| 99 |
'', |
| 100 |
'', |
| 101 |
'admin-ajax.php?action=echoRedirectToPages&includeDefault404Page=true&includeSpecial=true&nonce=' . |
| 102 |
wp_create_nonce('abj404_ajax'), |
| 103 |
), |
| 104 |
$redirectHtml |
| 105 |
); |
| 106 |
return $this->functions->doNormalReplacements($redirectHtml); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @param array<string, mixed> $options |
| 111 |
*/ |
| 112 |
private function redirectTypeGrid(array $options): string { |
| 113 |
$rawDefault = $options['default_redirect'] ?? '301'; |
| 114 |
$defaultCode = is_string($rawDefault) ? $rawDefault : '301'; |
| 115 |
ob_start(); |
| 116 |
$this->redirectTypeUI->echoRedirectTypeButtonGrid($defaultCode); |
| 117 |
return (string)ob_get_clean(); |
| 118 |
} |
| 119 |
|
| 120 |
private function advancedOptionsHtml(): string { |
| 121 |
ob_start(); |
| 122 |
$this->redirectConditions->echoRedirectConditionsSection(); |
| 123 |
$conditionsSection = (string)ob_get_clean(); |
| 124 |
|
| 125 |
return $this->fillTpl('viewRedirectsTableAdvancedOptions.html', array( |
| 126 |
'{open_attr}' => '', |
| 127 |
'{advanced_options_label}' => esc_html__('Advanced Options', '404-solution'), |
| 128 |
'{active_from_label}' => esc_html__('Active From (optional)', '404-solution'), |
| 129 |
'{start_date_value}' => '', |
| 130 |
'{active_from_help}' => esc_html__('Leave blank to activate immediately', '404-solution'), |
| 131 |
'{active_until_label}' => esc_html__('Active Until (optional)', '404-solution'), |
| 132 |
'{end_date_value}' => '', |
| 133 |
'{active_until_help}' => esc_html__('Leave blank to never expire', '404-solution'), |
| 134 |
'{conditions_section}' => $conditionsSection, |
| 135 |
)); |
| 136 |
} |
| 137 |
|
| 138 |
/** @param array<string, string> $vars */ |
| 139 |
private function fillTpl(string $name, array $vars): string { |
| 140 |
return (string)$this->functions->str_replace(array_keys($vars), array_values($vars), $this->tpl($name)); |
| 141 |
} |
| 142 |
|
| 143 |
private function tpl(string $name): string { |
| 144 |
$raw = ABJ_404_Solution_FileSystemService::readFileContents(dirname(__DIR__) . '/html/' . $name, false); |
| 145 |
return rtrim((string)$raw, "\n"); |
| 146 |
} |
| 147 |
} |
| 148 |
|