| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Renders the static Redirect Logs admin page shell. |
| 9 |
*/ |
| 10 |
class ABJ_404_Solution_AdminLogsPageShell { |
| 11 |
|
| 12 |
/** @var ABJ_404_Solution_Functions */ |
| 13 |
private $f; |
| 14 |
/** @var ABJ_404_Solution_PluginLogic */ |
| 15 |
private $logic; |
| 16 |
/** @var ABJ_404_Solution_View_Shared */ |
| 17 |
private $shared; |
| 18 |
|
| 19 |
public function __construct(ABJ_404_Solution_Functions $functions, |
| 20 |
ABJ_404_Solution_PluginLogic $pluginLogic, ABJ_404_Solution_View_Shared $shared) { |
| 21 |
$this->f = $functions; |
| 22 |
$this->logic = $pluginLogic; |
| 23 |
$this->shared = $shared; |
| 24 |
} |
| 25 |
|
| 26 |
/** @return string */ |
| 27 |
public function render(): string { |
| 28 |
$sub = 'abj404_logs'; |
| 29 |
$tableOptions = $this->logic->settingsUpdate()->getTableOptions($sub); |
| 30 |
$tableOptions = $this->logic->settingsUpdate()->sanitizePostData($tableOptions); |
| 31 |
|
| 32 |
$perpage = array_key_exists('perpage', $tableOptions) && is_scalar($tableOptions['perpage']) ? $tableOptions['perpage'] : 25; |
| 33 |
$orderby = array_key_exists('orderby', $tableOptions) && is_string($tableOptions['orderby']) ? $tableOptions['orderby'] : 'timestamp'; |
| 34 |
$order = array_key_exists('order', $tableOptions) && is_string($tableOptions['order']) ? $tableOptions['order'] : 'DESC'; |
| 35 |
$paginationNonce = wp_create_nonce('abj404_updatePaginationLink'); |
| 36 |
|
| 37 |
$perpageOptionsHtml = $this->renderPerPageOptions($perpage); |
| 38 |
$searchForm = $this->renderSearchForm(); |
| 39 |
$warmup = ABJ_404_Solution_FileSystemService::readFileContents(__DIR__ . '/../html/tableWarmupPlaceholder.html'); |
| 40 |
|
| 41 |
return $this->f->str_replace( |
| 42 |
$this->wrapperTokens(), |
| 43 |
$this->wrapperValues($sub, $paginationNonce, $orderby, $order, |
| 44 |
$searchForm, $perpageOptionsHtml, $warmup), |
| 45 |
$this->tpl('viewLogsPageWrapper.html') |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** @return array<int, string> */ |
| 50 |
private function wrapperTokens(): array { |
| 51 |
return array( |
| 52 |
'{logs_title}', |
| 53 |
'{data-pagination-ajax-url}', |
| 54 |
'{data-pagination-ajax-subpage}', |
| 55 |
'{data-pagination-ajax-nonce}', |
| 56 |
'{data-pagination-current-orderby}', |
| 57 |
'{data-pagination-current-order}', |
| 58 |
'{data-pagination-current-logsid}', |
| 59 |
'{refresh_available_text}', |
| 60 |
'{search_form}', |
| 61 |
'{rows_per_page_label}', |
| 62 |
'{perpage_options}', |
| 63 |
'{warmup_placeholder}', |
| 64 |
'{loading_badge_text}', |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** @return array<int, mixed> */ |
| 69 |
private function wrapperValues(string $sub, string $paginationNonce, string $orderby, |
| 70 |
string $order, string $searchForm, string $perpageOptionsHtml, string $warmup): array { |
| 71 |
$currentLogsId = $this->shared->viewGetPostOrGetSanitize('redirect_to_data_field_id'); |
| 72 |
return array( |
| 73 |
__('Redirect Logs', '404-solution'), |
| 74 |
esc_attr(admin_url('admin-ajax.php')), |
| 75 |
esc_attr($sub), |
| 76 |
esc_attr($paginationNonce), |
| 77 |
esc_attr($orderby), |
| 78 |
esc_attr($order), |
| 79 |
esc_attr(is_scalar($currentLogsId) ? (string)$currentLogsId : ''), |
| 80 |
esc_attr(__('Refresh available', '404-solution')), |
| 81 |
$searchForm, |
| 82 |
__('Rows per page:', '404-solution'), |
| 83 |
$perpageOptionsHtml, |
| 84 |
$warmup, |
| 85 |
esc_html__('Loading...', '404-solution'), |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
/** @param mixed $perpage */ |
| 90 |
private function renderPerPageOptions($perpage): string { |
| 91 |
$optionTpl = $this->tpl('viewLogsPerpageOption.html'); |
| 92 |
$html = ''; |
| 93 |
foreach (array(10, 25, 50, 100, 250) as $opt) { |
| 94 |
$selected = ($perpage == $opt) ? ' selected' : ''; |
| 95 |
$html .= $this->f->str_replace( |
| 96 |
array('{value}', '{selected_attr}'), |
| 97 |
array((string)$opt, $selected), |
| 98 |
$optionTpl |
| 99 |
) . "\n"; |
| 100 |
} |
| 101 |
return $html; |
| 102 |
} |
| 103 |
|
| 104 |
private function renderSearchForm(): string { |
| 105 |
$searchBox = ABJ_404_Solution_FileSystemService::readFileContents(__DIR__ . '/../html/viewLogsForSearchBox.html'); |
| 106 |
$redirectPageTitle = $this->shared->viewGetPostOrGetSanitize('redirect_to_data_field_title'); |
| 107 |
$pageIDAndType = $this->shared->viewGetPostOrGetSanitize('redirect_to_data_field_id'); |
| 108 |
$searchBox = $this->f->str_replace('{redirect_to_label}', __('View logs for', '404-solution'), $searchBox); |
| 109 |
$searchBox = $this->f->str_replace('{TOOLTIP_POPUP_EXPLANATION_EMPTY}', __('(Begin typing a URL)', '404-solution'), $searchBox); |
| 110 |
$searchBox = $this->f->str_replace('{TOOLTIP_POPUP_EXPLANATION_PAGE}', __('(A page has been selected.)', '404-solution'), $searchBox); |
| 111 |
$searchBox = $this->f->str_replace('{TOOLTIP_POPUP_EXPLANATION_CUSTOM_STRING}', __('(A custom string has been entered.)', '404-solution'), $searchBox); |
| 112 |
$searchBox = $this->f->str_replace('{TOOLTIP_POPUP_EXPLANATION_URL}', __('(Please choose from the dropdown list instead of typing your own URL.)', '404-solution'), $searchBox); |
| 113 |
$searchBox = $this->f->str_replace('{pageIDAndType}', esc_attr($pageIDAndType), $searchBox); |
| 114 |
$searchBox = $this->f->str_replace('{redirectPageTitle}', esc_attr($redirectPageTitle), $searchBox); |
| 115 |
$searchBox = $this->f->str_replace('{data-url}', 'admin-ajax.php?action=echoViewLogsFor&nonce=' . wp_create_nonce('abj404_ajax'), $searchBox); |
| 116 |
$searchBox = $this->f->doNormalReplacements($searchBox); |
| 117 |
|
| 118 |
return $this->f->str_replace( |
| 119 |
array('{page_constant}', '{search_dropdown}'), |
| 120 |
array(ABJ404_PP, $searchBox), |
| 121 |
$this->tpl('viewLogsSearchFormShell.html') |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
private function tpl(string $name): string { |
| 126 |
$raw = ABJ_404_Solution_FileSystemService::readFileContents(__DIR__ . '/../html/' . $name, false); |
| 127 |
return rtrim((string)$raw, "\n"); |
| 128 |
} |
| 129 |
} |
| 130 |
|