[ 'post', 'page' ], 'post_status' => 'publish', 'posts_per_page' => 50, 'orderby' => 'modified', 'order' => 'ASC', ] ); foreach ( $posts as $post ) { self::scan_post( $post->ID ); } } public static function ajax_scan_post() { check_ajax_referer( 'awbseo_admin', 'nonce' ); $post_id = (int) ( $_POST['post_id'] ?? 0 ); if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) ) wp_send_json_error( 'Forbidden' ); $results = self::scan_post( $post_id ); wp_send_json_success( $results ); } public static function ajax_dismiss_link() { check_ajax_referer( 'awbseo_admin', 'nonce' ); if ( ! current_user_can( 'edit_posts' ) ) wp_send_json_error( 'Forbidden' ); $id = (int) ( $_POST['id'] ?? 0 ); if ( ! $id ) wp_send_json_error( 'Missing id' ); self::delete_record( $id ); wp_send_json_success(); } public static function scan_post( $post_id ) { $post = get_post( $post_id ); $content = $post->post_content ?? ''; preg_match_all( '/href=["\']((https?:\/\/)[^"\']+)["\']/', $content, $matches ); $urls = array_unique( $matches[1] ?? [] ); $results = []; // Skip internal links and common tracker URLs. $home = parse_url( home_url(), PHP_URL_HOST ); $skip_patterns = [ '/localhost/', '/127\.0\.0\.1/', '/' . preg_quote( $home, '/' ) . '/' ]; foreach ( $urls as $url ) { $skip = false; foreach ( $skip_patterns as $pattern ) { if ( preg_match( $pattern, $url ) ) { $skip = true; break; } } if ( $skip ) continue; $code = self::check_url( $url ); self::save_result( $post_id, $url, $code ); if ( $code >= 400 || $code === 0 ) { $results[] = [ 'url' => $url, 'status' => $code ]; } } return $results; } private static function check_url( $url ) { $response = wp_remote_head( $url, [ 'timeout' => 10, 'user-agent' => 'AWBSEO-BrokenLinkScanner/1.0', 'sslverify' => true, 'redirection' => 5, // URLs here come from post content (any author who can edit a // post), not an admin setting - reject_unsafe_urls blocks // requests to private/reserved/link-local IPs so this scanner // can't be used to probe internal network services (SSRF). 'reject_unsafe_urls' => true, ] ); if ( is_wp_error( $response ) ) return 0; return (int) wp_remote_retrieve_response_code( $response ); } private static function save_result( $post_id, $url, $code ) { global $wpdb; $table = $wpdb->prefix . 'awb_broken_links'; $existing = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table} WHERE post_id = %d AND url = %s", $post_id, $url ) ); if ( $existing ) { $wpdb->update( $table, [ 'status_code' => $code, 'last_checked' => current_time( 'mysql' ) ], [ 'id' => $existing ], [ '%d', '%s' ], [ '%d' ] ); } else { $wpdb->insert( $table, [ 'post_id' => $post_id, 'url' => $url, 'status_code' => $code, 'last_checked' => current_time( 'mysql' ) ], [ '%d', '%s', '%d', '%s' ] ); } } public static function get_broken( $limit = 100 ) { global $wpdb; return $wpdb->get_results( $wpdb->prepare( "SELECT bl.*, p.post_title, p.ID as post_id FROM {$wpdb->prefix}awb_broken_links bl JOIN {$wpdb->posts} p ON p.ID = bl.post_id WHERE bl.status_code >= 400 OR bl.status_code = 0 ORDER BY bl.last_checked DESC LIMIT %d", $limit ) ); } public static function get_all( $limit = 100 ) { global $wpdb; return $wpdb->get_results( $wpdb->prepare( "SELECT bl.*, p.post_title FROM {$wpdb->prefix}awb_broken_links bl JOIN {$wpdb->posts} p ON p.ID = bl.post_id ORDER BY bl.last_checked DESC LIMIT %d", $limit ) ); } public static function delete_record( $id ) { global $wpdb; $wpdb->delete( $wpdb->prefix . 'awb_broken_links', [ 'id' => (int) $id ], [ '%d' ] ); } /** * Update the fix_status field for a single broken-link record. * * @param int $id Record ID in awb_broken_links. * @param string $status 'fixed' or 'not_fixed'. */ public static function update_fix_status( $id, $status ) { global $wpdb; $allowed = [ 'fixed', 'not_fixed' ]; $status = in_array( $status, $allowed, true ) ? $status : 'not_fixed'; $wpdb->update( $wpdb->prefix . 'awb_broken_links', [ 'fix_status' => $status ], [ 'id' => (int) $id ], [ '%s' ], [ '%d' ] ); } public static function ajax_set_fix_status() { check_ajax_referer( 'awbseo_admin', 'nonce' ); if ( ! current_user_can( 'edit_posts' ) ) { wp_send_json_error( 'Forbidden' ); } $id = (int) ( $_POST['id'] ?? 0 ); $status = sanitize_key( $_POST['fix_status'] ?? '' ); if ( ! $id ) { wp_send_json_error( 'Missing id' ); } self::update_fix_status( $id, $status ); wp_send_json_success(); } }