| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Presents the Page Redirects admin page wrapper and table controls. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_RedirectsTablePagePresenter { |
| 11 |
|
| 12 |
/** @var ABJ_404_Solution_Functions */ |
| 13 |
private $functions; |
| 14 |
|
| 15 |
/** @var ABJ_404_Solution_PluginLogic */ |
| 16 |
private $logic; |
| 17 |
|
| 18 |
/** @var ABJ_404_Solution_View_ListTableChrome */ |
| 19 |
private $listTableChrome; |
| 20 |
|
| 21 |
/** @var ABJ_404_Solution_RedirectAddModalPresenter */ |
| 22 |
private $addModalPresenter; |
| 23 |
|
| 24 |
public function __construct(ABJ_404_Solution_Functions $functions, ABJ_404_Solution_PluginLogic $logic, |
| 25 |
ABJ_404_Solution_View_ListTableChrome $listTableChrome, |
| 26 |
ABJ_404_Solution_RedirectAddModalPresenter $addModalPresenter) { |
| 27 |
$this->functions = $functions; |
| 28 |
$this->logic = $logic; |
| 29 |
$this->listTableChrome = $listTableChrome; |
| 30 |
$this->addModalPresenter = $addModalPresenter; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
public function render(): string { |
| 37 |
$sub = 'abj404_redirects'; |
| 38 |
$tableOptions = $this->logic->settingsUpdate()->sanitizePostData( |
| 39 |
$this->logic->settingsUpdate()->getTableOptions($sub) |
| 40 |
); |
| 41 |
$currentFilter = $this->currentFilter($tableOptions); |
| 42 |
$paginationState = $this->paginationState($tableOptions); |
| 43 |
$refresh = $this->listTableChrome->paginationRefreshStrings(); |
| 44 |
|
| 45 |
$html = $this->fillTpl('viewRedirectsTableRedirectsPageWrapper.html', array( |
| 46 |
'{data-health-bar-ajax-url}' => esc_attr(admin_url('admin-ajax.php')), |
| 47 |
'{data-health-bar-nonce}' => esc_attr(wp_create_nonce('abj404_refreshHealthBar')), |
| 48 |
'{loading_status_text}' => esc_html__('Loading status...', '404-solution'), |
| 49 |
'{redirects_title}' => esc_html__('Page Redirects', '404-solution'), |
| 50 |
'{subtitle}' => $this->subtitleHtml(), |
| 51 |
'{add_redirect_button}' => $this->addRedirectButtonHtml($currentFilter), |
| 52 |
'{subsubsub}' => $this->subsubsubHtml($sub, $tableOptions), |
| 53 |
'{data-pagination-ajax-url}' => esc_attr(admin_url('admin-ajax.php')), |
| 54 |
'{data-lazy-backfill-ajax-url}' => esc_attr(admin_url('admin-ajax.php')), |
| 55 |
'{data-lazy-backfill-nonce}' => esc_attr(wp_create_nonce('abj404_runLazyBackfill')), |
| 56 |
'{data-pagination-ajax-subpage}' => esc_attr($sub), |
| 57 |
'{data-pagination-ajax-nonce}' => esc_attr(wp_create_nonce('abj404_updatePaginationLink')), |
| 58 |
'{data-pagination-current-orderby}' => esc_attr($paginationState['orderby']), |
| 59 |
'{data-pagination-current-order}' => esc_attr($paginationState['order']), |
| 60 |
'{data-pagination-current-filter}' => esc_attr((string)$currentFilter), |
| 61 |
'{data-pagination-current-paged}' => esc_attr((string)$paginationState['paged']), |
| 62 |
'{data-pagination-current-score-range}' => esc_attr($paginationState['scoreRange']), |
| 63 |
'{data-pagination-auto-refresh}' => esc_attr('1'), |
| 64 |
'{data-pagination-refresh-available-text}' => esc_attr($refresh['available']), |
| 65 |
'{search_placeholder}' => esc_attr__('Type to filter redirects... (press Enter)', '404-solution'), |
| 66 |
'{filter_text}' => esc_attr($this->filterText($tableOptions)), |
| 67 |
'{rows_per_page_label}' => esc_html__('Rows per page:', '404-solution'), |
| 68 |
'{perpage_options}' => $this->listTableChrome->buildPerpageOptions($this->perPage($tableOptions)), |
| 69 |
'{confidence_label}' => esc_html__('Confidence:', '404-solution'), |
| 70 |
'{score_range_base_url_js}' => esc_js(esc_url($this->scoreRangeBaseUrl($currentFilter))), |
| 71 |
'{score_range_options}' => $this->listTableChrome->buildScoreRangeOptions($paginationState['scoreRange']), |
| 72 |
'{form_action}' => esc_url($this->listTableChrome->getBulkOperationsFormURL($sub, $tableOptions)), |
| 73 |
'{selected_label}' => esc_html__('redirects selected', '404-solution'), |
| 74 |
'{bulk_actions_label}' => esc_html__('Bulk Actions', '404-solution'), |
| 75 |
'{bulk_action_options}' => $this->listTableChrome->buildRedirectsBulkActionOptions($currentFilter), |
| 76 |
'{apply_label}' => esc_html__('Apply', '404-solution'), |
| 77 |
'{clear_selection_label}' => esc_html__('Clear Selection', '404-solution'), |
| 78 |
'{warmup_placeholder}' => ABJ_404_Solution_FileSystemService::readFileContents(dirname(__DIR__) . '/html/tableWarmupPlaceholder.html'), |
| 79 |
'{loading_badge_text}' => esc_html__('Loading...', '404-solution'), |
| 80 |
'{empty_trash_form}' => $currentFilter == ABJ404_TRASH_FILTER ? $this->listTableChrome->buildEmptyTrashForm($sub) : '', |
| 81 |
)); |
| 82 |
|
| 83 |
if ($currentFilter != ABJ404_TRASH_FILTER) { |
| 84 |
$html .= $this->addModalPresenter->render($tableOptions); |
| 85 |
} |
| 86 |
return $html; |
| 87 |
} |
| 88 |
|
| 89 |
/** @param array<string, mixed> $tableOptions */ |
| 90 |
private function currentFilter(array $tableOptions): int { |
| 91 |
$rawFilter = $tableOptions['filter'] ?? 0; |
| 92 |
return is_scalar($rawFilter) ? (int)$rawFilter : 0; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* @param array<string, mixed> $tableOptions |
| 97 |
* @return array{orderby: string, order: string, paged: int, scoreRange: string} |
| 98 |
*/ |
| 99 |
private function paginationState(array $tableOptions): array { |
| 100 |
$rawPaged = $tableOptions['paged'] ?? 1; |
| 101 |
$paged = is_scalar($rawPaged) ? intval($rawPaged) : 1; |
| 102 |
$rawOrderby = $tableOptions['orderby'] ?? ''; |
| 103 |
$rawOrder = $tableOptions['order'] ?? ''; |
| 104 |
$rawScoreRange = $tableOptions['score_range'] ?? ''; |
| 105 |
|
| 106 |
return array( |
| 107 |
'orderby' => is_string($rawOrderby) ? $rawOrderby : 'url', |
| 108 |
'order' => is_string($rawOrder) ? $rawOrder : 'ASC', |
| 109 |
'paged' => max(1, $paged), |
| 110 |
'scoreRange' => is_string($rawScoreRange) && $rawScoreRange !== '' ? $rawScoreRange : 'all', |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
private function subtitleHtml(): string { |
| 115 |
if (abj_service('settings_mode_preference')->getMode() !== 'simple') { |
| 116 |
return ''; |
| 117 |
} |
| 118 |
return $this->functions->str_replace( |
| 119 |
'{text}', |
| 120 |
esc_html__('The plugin creates these automatically. You only need to act when the status bar above says so.', '404-solution'), |
| 121 |
$this->tpl('viewRedirectsTableSubtitle.html') |
| 122 |
) . "\n"; |
| 123 |
} |
| 124 |
|
| 125 |
private function addRedirectButtonHtml(int $currentFilter): string { |
| 126 |
if ($currentFilter == ABJ404_TRASH_FILTER) { |
| 127 |
return ''; |
| 128 |
} |
| 129 |
return $this->functions->str_replace( |
| 130 |
'{label}', |
| 131 |
esc_html__('Add Redirect', '404-solution'), |
| 132 |
$this->tpl('viewRedirectsTableAddRedirectButton.html') |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
/** @param array<string, mixed> $tableOptions */ |
| 137 |
private function subsubsubHtml(string $sub, array $tableOptions): string { |
| 138 |
return $this->listTableChrome->buildSubsubsubFilters($sub, array( |
| 139 |
array(0, __('All', '404-solution')), |
| 140 |
array(ABJ404_STATUS_MANUAL, __('Manual', '404-solution')), |
| 141 |
array(ABJ404_STATUS_AUTO, __('Automatic', '404-solution')), |
| 142 |
array(ABJ404_TRASH_FILTER, __('Trash', '404-solution')), |
| 143 |
), $tableOptions); |
| 144 |
} |
| 145 |
|
| 146 |
/** @param array<string, mixed> $tableOptions */ |
| 147 |
private function filterText(array $tableOptions): string { |
| 148 |
return is_string($tableOptions['filterText'] ?? '') ? (string)($tableOptions['filterText'] ?? '') : ''; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* @param array<string, mixed> $tableOptions |
| 153 |
* @return int|string |
| 154 |
*/ |
| 155 |
private function perPage(array $tableOptions) { |
| 156 |
$perPage = $tableOptions['perpage'] ?? 25; |
| 157 |
return (is_int($perPage) || is_string($perPage)) ? $perPage : 25; |
| 158 |
} |
| 159 |
|
| 160 |
private function scoreRangeBaseUrl(int $currentFilter): string { |
| 161 |
return '?page=' . ABJ404_PP . '&subpage=abj404_redirects&filter=' . $currentFilter; |
| 162 |
} |
| 163 |
|
| 164 |
/** @param array<string, string> $vars */ |
| 165 |
private function fillTpl(string $name, array $vars): string { |
| 166 |
return (string)$this->functions->str_replace(array_keys($vars), array_values($vars), $this->tpl($name)); |
| 167 |
} |
| 168 |
|
| 169 |
private function tpl(string $name): string { |
| 170 |
$raw = ABJ_404_Solution_FileSystemService::readFileContents(dirname(__DIR__) . '/html/' . $name, false); |
| 171 |
return rtrim((string)$raw, "\n"); |
| 172 |
} |
| 173 |
} |
| 174 |
|