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 / ShortcodeSuggestionAdminDebugPresenter.php

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

126 lines 4.7 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 * Presents admin-only debug data for frontend shortcode suggestions.
9 */
10 class ABJ_404_Solution_ShortcodeSuggestionAdminDebugPresenter {
11
12 /**
13 * @param float $score
14 * @return string
15 */
16 public function renderScoreLink(float $score): string {
17 return $this->fillTemplate('shortcodeAdminDebugScoreLink.html', array(
18 'title' => esc_attr__('Click to view debug data for all suggestions', '404-solution'),
19 'score' => number_format($score, 2),
20 ));
21 }
22
23 /**
24 * @param array<int, array<string, mixed>> $adminDebugData
25 * @return string
26 */
27 public function renderScript(array $adminDebugData): string {
28 $allSuggestionsJson = wp_json_encode($adminDebugData);
29 if ($allSuggestionsJson === false) {
30 $allSuggestionsJson = '[]';
31 }
32 $jsContent = ABJ_404_Solution_FileSystemService::readFileContents(dirname(__DIR__) . '/js/suggestion-debug-modal.js');
33 return $this->fillTemplate('shortcodeAdminDebugScript.html', array(
34 'json' => $allSuggestionsJson,
35 'modal_script' => $jsContent,
36 ));
37 }
38
39 /**
40 * @param array<int|string, mixed> $permalinkSuggestions
41 * @param mixed $viewReadService
42 * @param mixed $functions
43 * @return array<string, array<string, mixed>>
44 */
45 public function collectExtraData(array $permalinkSuggestions, $viewReadService, $functions): array {
46 $extraDataById = [];
47 $postIDs = array_keys($permalinkSuggestions);
48 if (empty($postIDs) || !is_object($viewReadService) || !method_exists($viewReadService, 'getExtraDataToPermalinkSuggestions')) {
49 return $extraDataById;
50 }
51 foreach ($postIDs as $index => $id) {
52 $idStr = is_string($id) ? $id : (string)$id;
53 $pipePos = is_object($functions) && method_exists($functions, 'strpos') ? $functions->strpos($idStr, '|') : strpos($idStr, '|');
54 $postIDs[$index] = is_object($functions) && method_exists($functions, 'substr')
55 ? $functions->substr($idStr, 0, $pipePos !== false ? $pipePos : null)
56 : substr($idStr, 0, $pipePos !== false ? $pipePos : null);
57 }
58
59 $rawExtraData = $viewReadService->getExtraDataToPermalinkSuggestions($postIDs);
60 foreach ($rawExtraData as $dataItem) {
61 if (!is_array($dataItem)) {
62 continue;
63 }
64 $postIdRaw = isset($dataItem['post_id']) ? $dataItem['post_id'] : '';
65 $termIdRaw = isset($dataItem['term_id']) ? $dataItem['term_id'] : '';
66 $postIdVal = is_scalar($postIdRaw) ? (string)$postIdRaw : '';
67 $termIdVal = is_scalar($termIdRaw) ? (string)$termIdRaw : '';
68 $extraDataById['post_id_' . $postIdVal] = $dataItem;
69 $extraDataById['term_id_' . $termIdVal] = $dataItem;
70 }
71 return $extraDataById;
72 }
73
74 /**
75 * @param string $idAndTypeStr
76 * @param string $permTitle
77 * @param float $permScore
78 * @param string $permLink
79 * @param array<string, array<string, mixed>> $extraDataById
80 * @return array<string, mixed>
81 */
82 public function buildItemData(string $idAndTypeStr, string $permTitle, float $permScore,
83 string $permLink, array $extraDataById): array {
84 $currentSuggestionData = [
85 'Title' => $permTitle,
86 'Link' => $permLink,
87 'Score' => number_format($permScore, 2),
88 'ID_Type_Code' => $idAndTypeStr,
89 ];
90
91 $idParts = explode('|', $idAndTypeStr);
92 $currentId = isset($idParts[0]) ? $idParts[0] : null;
93 $typeCode = isset($idParts[1]) ? $idParts[1] : null;
94
95 if ($typeCode == '1') {
96 $extraKey = 'post_id_' . $currentId;
97 if (isset($extraDataById[$extraKey])) {
98 $currentSuggestionData = $currentSuggestionData + $extraDataById[$extraKey];
99 }
100 } else {
101 $extraKey = 'term_id_' . $currentId;
102 if (isset($extraDataById[$extraKey])) {
103 $currentSuggestionData = $currentSuggestionData + $extraDataById[$extraKey];
104 }
105 }
106
107 return $currentSuggestionData;
108 }
109
110 /**
111 * @param string $name
112 * @param array<string, string> $vars
113 * @return string
114 */
115 private function fillTemplate(string $name, array $vars): string {
116 $template = ABJ_404_Solution_FileSystemService::readFileContents(dirname(__DIR__) . '/html/' . $name, false);
117 $search = [];
118 $replace = [];
119 foreach ($vars as $key => $value) {
120 $search[] = '{' . $key . '}';
121 $replace[] = $value;
122 }
123 return str_replace($search, $replace, $template);
124 }
125 }
126