Repository navigation
Expand file tree
/
Copy pathadmin.php
More file actions
231 lines (212 loc) · 8.62 KB
/
Copy pathadmin.php
File metadata and controls
231 lines (212 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
// This file contains the HTML, CSS, and JS for the admin page.
// It's included by the main plugin file.
global $wpdb;
// Table names from the Redirection plugin
$log_table = $wpdb->prefix . 'redirection_404';
$redirect_table = $wpdb->prefix . 'redirection_items';
// Get the 50 most recent 404s that don't already have a redirect
$query =
"SELECT DISTINCT l.url, l.created, COALESCE(l.ip, 'Unknown') as ip
FROM {$log_table} l
LEFT JOIN {$redirect_table} r ON l.url = r.url
WHERE r.id IS NULL
ORDER BY l.created DESC
LIMIT 50";
$results = $wpdb->get_results( $query );
?>
<style>
.lar-table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.lar-table th, .lar-table td {
padding: 12px 15px;
border: 1px solid #ddd;
text-align: left;
}
.lar-table th {
background-color: #f4f4f4;
}
.lar-table tr:nth-child(even) {
background-color: #f9f9f9;
}
.lar-suggestion-col {
display: flex;
align-items: center;
gap: 10px;
}
.lar-suggestion-col input {
flex-grow: 1;
}
.lar-status {
font-style: italic;
color: #666;
}
.spinner {
display: none;
vertical-align: middle;
margin-left: 5px;
}
</style>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<p>Automate 301 redirects by getting intelligent suggestions from a Large Language Model (LLM).</p>
<!-- Settings Form -->
<form method="post" action="options.php">
<?php
settings_fields( 'lar_settings_group' );
do_settings_sections( 'llm-auto-redirect' );
submit_button();
?>
</form>
<hr>
<h2>Recent 404 Errors without Redirects</h2>
<p>Here are the latest 404 errors recorded by the Redirection plugin. Click "Suggest" to ask the LLM for a smart redirect target.</p>
<?php if ( empty(get_option('lar_gemini_api_key')) ) : ?>
<div class="notice notice-warning"><p><strong>Please enter and save your Gemini API key above to enable suggestions.</strong></p></div>
<?php endif; ?>
<?php if ( ! empty( $results ) ) : ?>
<table class="lar-table">
<thead>
<tr>
<th>404 Source URL</th>
<th>Date</th>
<th>Suggest & Create Redirect</th>
</tr>
</thead>
<tbody>
<?php foreach ( $results as $row ) : ?>
<tr id="lar-row-<?php echo md5($row->url); ?>">
<td>
<code><?php echo esc_html( $row->url ); ?></code>
<br>
<small>From IP: <?php echo esc_html( $row->ip ); ?></small>
</td>
<td><?php echo esc_html( date( 'Y-m-d H:i:s', strtotime($row->created) ) ); ?></td>
<td>
<div class="lar-suggestion-col">
<button class="button lar-suggest-btn" data-source-url="<?php echo esc_attr( $row->url ); ?>">Suggest</button>
<input type="text" class="large-text lar-target-input" placeholder="LLM suggestion will appear here...">
<button class="button button-primary lar-create-btn" disabled>Create</button>
<span class="spinner"></span>
</div>
<div class="lar-status"></div>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>No new 404 errors found in the Redirection log. Great job!</p>
<?php endif; ?>
</div>
<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function() {
const suggestButtons = document.querySelectorAll('.lar-suggest-btn');
const createButtons = document.querySelectorAll('.lar-create-btn');
suggestButtons.forEach(button => {
button.addEventListener('click', function(e) {
e.preventDefault();
const row = this.closest('tr');
const sourceUrl = this.dataset.sourceUrl;
const targetInput = row.querySelector('.lar-target-input');
const createBtn = row.querySelector('.lar-create-btn');
const statusDiv = row.querySelector('.lar-status');
const spinner = row.querySelector('.spinner');
this.disabled = true;
createBtn.disabled = true;
spinner.style.display = 'inline-block';
statusDiv.textContent = 'Asking the LLM for a suggestion...';
targetInput.value = '';
fetch(ajaxurl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
action: 'lar_get_llm_suggestion',
nonce: '<?php echo wp_create_nonce("lar_ajax_nonce"); ?>',
source_url: sourceUrl
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
targetInput.value = data.data.suggestion;
statusDiv.textContent = 'Suggestion received!';
statusDiv.style.color = 'green';
createBtn.disabled = false;
} else {
statusDiv.textContent = 'Error: ' + data.data;
statusDiv.style.color = 'red';
}
})
.catch(error => {
statusDiv.textContent = 'Request failed: ' + error;
statusDiv.style.color = 'red';
})
.finally(() => {
this.disabled = false;
spinner.style.display = 'none';
});
});
});
createButtons.forEach(button => {
button.addEventListener('click', function(e) {
e.preventDefault();
const row = this.closest('tr');
const sourceUrl = row.querySelector('.lar-suggest-btn').dataset.sourceUrl;
const targetUrl = row.querySelector('.lar-target-input').value;
const statusDiv = row.querySelector('.lar-status');
const spinner = row.querySelector('.spinner');
if (!targetUrl) {
statusDiv.textContent = 'Target URL cannot be empty.';
statusDiv.style.color = 'red';
return;
}
this.disabled = true;
spinner.style.display = 'inline-block';
statusDiv.textContent = 'Creating redirect...';
fetch(ajaxurl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
action: 'lar_create_redirect',
nonce: '<?php echo wp_create_nonce("lar_ajax_nonce"); ?>',
source_url: sourceUrl,
target_url: targetUrl
})
})
.then(response => response.json())
.then(data => {
if (data.success) {
statusDiv.textContent = 'Success! Redirect created and 404 log cleared.';
statusDiv.style.color = 'green';
// Hide the row after a successful creation
setTimeout(() => {
row.style.transition = 'opacity 0.5s ease';
row.style.opacity = '0';
setTimeout(() => row.remove(), 500);
}, 2000);
} else {
statusDiv.textContent = 'Error: ' + data.data;
statusDiv.style.color = 'red';
this.disabled = false;
}
})
.catch(error => {
statusDiv.textContent = 'Request failed: ' + error;
statusDiv.style.color = 'red';
this.disabled = false;
})
.finally(() => {
spinner.style.display = 'none';
});
});
});
});
</script>