public function ajax_deredirect() { if (!current_user_can('edit_others_posts') || !check_ajax_referer('blc_deredirect', false, false)) { die(json_encode(array('error' => __("You're not allowed to do that!", 'broken-link-checker')))); } if (!isset($_POST['link_id']) || !is_numeric($_POST['link_id'])) { die(json_encode(array('error' => __("Error : link_id not specified", 'broken-link-checker')))); } $id = intval($_POST['link_id']); $link = new blcLink($id); if (!$link->valid()) { die(json_encode(array('error' => sprintf(__("Oops, I can't find the link %d", 'broken-link-checker'), $id)))); } //The actual task is simple; it's error handling that's complicated. $result = $link->deredirect(); if (is_wp_error($result)) { die(json_encode(array('error' => sprintf('%s [%s]', $result->get_error_message(), $result->get_error_code())))); } $link = $result['new_link']; $status = $link->analyse_status(); $response = array('url' => $link->url, 'escaped_url' => esc_url_raw($link->url), 'new_link_id' => $result['new_link_id'], 'status_text' => $status['text'], 'status_code' => $status['code'], 'http_code' => empty($link->http_code) ? '' : $link->http_code, 'redirect_count' => $link->redirect_count, 'final_url' => $link->final_url, 'cnt_okay' => $result['cnt_okay'], 'cnt_error' => $result['cnt_error'], 'errors' => array()); //Convert WP_Error's to simple strings. if (!empty($result['errors'])) { foreach ($result['errors'] as $error) { /** @var WP_Error $error */ $response['errors'][] = $error->get_error_message(); } } die(json_encode($response)); }