Skip to content

Commit ad17414

Browse files
Merge pull request #275 from JLG-WOCFR-DEV/codex/add-apply-redirect-button-and-ajax-handler
Add AJAX workflow to apply detected redirects
2 parents eaafb1f + f8621e9 commit ad17414

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

liens-morts-detector-jlg/includes/class-blc-links-list-table.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,10 @@ protected function get_row_actions($item) {
597597

598598
$data_attributes = implode(' ', $data_attributes);
599599

600-
$edit_nonce = wp_create_nonce('blc_edit_link_nonce');
601-
$ignore_nonce = wp_create_nonce('blc_ignore_link_nonce');
602-
$unlink_nonce = wp_create_nonce('blc_unlink_nonce');
603-
$recheck_nonce = wp_create_nonce('blc_recheck_link_nonce');
604-
$apply_redirect_nonce = wp_create_nonce('blc_apply_detected_redirect_nonce');
600+
$edit_nonce = wp_create_nonce('blc_edit_link_nonce');
601+
$ignore_nonce = wp_create_nonce('blc_ignore_link_nonce');
602+
$unlink_nonce = wp_create_nonce('blc_unlink_nonce');
603+
$recheck_nonce = wp_create_nonce('blc_recheck_link_nonce');
605604

606605
$actions['edit_link'] = sprintf(
607606
'<button type="button" class="button button-small button-link blc-edit-link" %s data-nonce="%s">%s</button>',
@@ -618,10 +617,12 @@ protected function get_row_actions($item) {
618617
);
619618

620619
if ($detected_target !== '') {
620+
$apply_redirect_nonce = wp_create_nonce('blc_apply_detected_redirect_nonce');
621+
621622
$actions['apply_redirect'] = sprintf(
622623
'<button type="button" class="button button-small button-link blc-apply-redirect" %s data-nonce="%s">%s</button>',
623624
$data_attributes,
624-
$apply_redirect_nonce,
625+
esc_attr($apply_redirect_nonce),
625626
esc_html__('Appliquer la redirection détectée', 'liens-morts-detector-jlg')
626627
);
627628
}
@@ -1776,4 +1777,20 @@ private function build_internal_url_condition() {
17761777

17771778
return $this->internal_url_condition_cache;
17781779
}
1780+
1781+
/**
1782+
* Render the HTML markup for a single row using the table's column definitions.
1783+
*
1784+
* @param array $item Row data to render.
1785+
* @return string
1786+
*/
1787+
public function render_row_html(array $item) {
1788+
$this->_column_headers = [$this->get_columns(), [], []];
1789+
$this->items = [$item];
1790+
1791+
ob_start();
1792+
$this->single_row($item);
1793+
1794+
return (string) ob_get_clean();
1795+
}
17791796
}

liens-morts-detector-jlg/liens-morts-detector-jlg.php

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,50 @@ function blc_perform_link_update(array $args) {
937937
return $success_payload;
938938
}
939939

940+
/**
941+
* Retrieve a single broken link row with contextual data.
942+
*
943+
* @param int $row_id Identifier of the broken link row.
944+
* @return array|null
945+
*/
946+
function blc_get_broken_link_row($row_id) {
947+
global $wpdb;
948+
949+
$row_id = absint($row_id);
950+
if ($row_id <= 0) {
951+
return null;
952+
}
953+
954+
$table_name = $wpdb->prefix . 'blc_broken_links';
955+
$posts_table = $wpdb->posts;
956+
957+
$query = $wpdb->prepare(
958+
"SELECT links.id, links.occurrence_index, links.url, links.anchor, links.redirect_target_url, links.context_html, links.context_excerpt, links.post_id, links.post_title, links.http_status, links.last_checked_at, links.ignored_at, posts.post_type AS post_type FROM {$table_name} AS links LEFT JOIN {$posts_table} AS posts ON links.post_id = posts.ID WHERE links.id = %d AND links.type = %s",
959+
$row_id,
960+
'link'
961+
);
962+
963+
$row = $wpdb->get_row($query, ARRAY_A);
964+
965+
return is_array($row) ? $row : null;
966+
}
967+
968+
/**
969+
* Render the HTML markup for a single broken link row.
970+
*
971+
* @param array $row Row data as returned by blc_get_broken_link_row().
972+
* @return string
973+
*/
974+
function blc_render_broken_link_row_html(array $row) {
975+
if (!class_exists('BLC_Links_List_Table')) {
976+
require_once BLC_PLUGIN_PATH . 'includes/class-blc-links-list-table.php';
977+
}
978+
979+
$list_table = new BLC_Links_List_Table();
980+
981+
return $list_table->render_row_html($row);
982+
}
983+
940984
// Gère la modification d'une URL
941985
add_action('wp_ajax_blc_edit_link', 'blc_ajax_edit_link_callback');
942986
function blc_ajax_edit_link_callback() {
@@ -1033,11 +1077,18 @@ function blc_ajax_apply_detected_redirect_callback() {
10331077
}
10341078

10351079
$response = [
1036-
'message' => $result['message'] ?? __('La redirection détectée a été appliquée.', 'liens-morts-detector-jlg'),
1080+
'message' => $result['message'] ?? __('La redirection détectée a été appliquée.', 'liens-morts-detector-jlg'),
10371081
'announcement' => $result['announcement'] ?? ($result['message'] ?? ''),
1038-
'rowRemoved' => !empty($result['row_removed']),
1082+
'rowRemoved' => !empty($result['row_removed']),
10391083
];
10401084

1085+
if (empty($result['purged']) && empty($result['row_removed'])) {
1086+
$refreshed_row = blc_get_broken_link_row($row_id);
1087+
if (is_array($refreshed_row)) {
1088+
$response['rowHtml'] = blc_render_broken_link_row_html($refreshed_row);
1089+
}
1090+
}
1091+
10411092
if (!empty($result['purged'])) {
10421093
$response['purged'] = true;
10431094
}

0 commit comments

Comments
 (0)