public function lookForBrokenLinks($postId = null, $url = null)
    {
        \BrokenLinkDetector\App::checkInstall();
        $foundUrls = array();

        if ($url) {
            $url = "REGEXP ('.*(href=\"{$url}\").*')";
        } else {
            $url = "RLIKE ('href=*')";
        }

        global $wpdb;
        $sql = "
            SELECT ID, post_content
            FROM $wpdb->posts
            WHERE
                post_content {$url}
                AND post_type NOT IN ('attachment', 'revision', 'acf', 'acf-field', 'acf-field-group')
                AND post_status IN ('publish', 'private', 'password')
        ";

        if (is_numeric($postId)) {
            $sql .= " AND ID = $postId";
        }

        $posts = $wpdb->get_results($sql);

        if(is_array($posts) && !empty($posts)) {
            foreach ($posts as $post) {
                preg_match_all('/<a[^>]+href=([\'"])(http|https)(.+?)\1[^>]*>/i', $post->post_content, $m);

                if (!isset($m[3]) || count($m[3]) > 0) {
                    foreach ($m[3] as $key => $url) {
                        $url = $m[2][$key] . $url;

                        // Replace whitespaces in url
                        if (preg_match('/\s/', $url)) {
                            $newUrl = preg_replace('/ /', '%20', $url);
                            $wpdb->query(
                                $wpdb->prepare(
                                    "UPDATE $wpdb->posts
                                        SET post_content = REPLACE(post_content, %s, %s)
                                        WHERE post_content LIKE %s
                                        AND ID = %d",
                                    $url,
                                    $newUrl,
                                    '%' . $wpdb->esc_like($url) . '%',
                                    $post->ID
                                )
                            );
                            $url = $newUrl;
                        }

                        if ($postId !== 'internal' && !$this->isBroken($url)) {
                            continue;
                        }

                        $foundUrls[] = array(
                            'post_id' => $post->ID,
                            'url' => $url
                        );
                    }
                }
            }
        }

        $this->saveBrokenLinks($foundUrls, $postId);
    }
public function lookForBrokenLinks($postId = null, $url = null)
    {
        \BrokenLinkDetector\App::checkInstall();
        $foundUrls = array();

        if ($url) {
            $url = "REGEXP ('.*(href=\"{$url}\").*')";
        } else {
            $url = "RLIKE ('href=*')";
        }

        global $wpdb;
        $sql = "
            SELECT ID, post_content
            FROM $wpdb->posts
            WHERE
                post_content {$url}
                AND post_type NOT IN ('attachment', 'revision', 'acf', 'acf-field', 'acf-field-group')
                AND post_status IN ('publish', 'private', 'password')
        ";

        if (is_numeric($postId)) {
            $sql .= " AND ID = $postId";
        }

        $posts = $wpdb->get_results($sql);

        if(is_array($posts) && !empty($posts)) {
            foreach ($posts as $post) {
                preg_match_all('/<a[^>]+href=([\'"])(http|https)(.+?)\1[^>]*>/i', $post->post_content, $m);

                if (!isset($m[3]) || count($m[3]) > 0) {
                    foreach ($m[3] as $key => $url) {
                        $url = $m[2][$key] . $url;

                        // Replace whitespaces in url
                        if (preg_match('/\s/', $url)) {
                            $newUrl = preg_replace('/ /', '%20', $url);
                            $wpdb->query(
                                $wpdb->prepare(
                                    "UPDATE $wpdb->posts
                                        SET post_content = REPLACE(post_content, %s, %s)
                                        WHERE post_content LIKE %s
                                        AND ID = %d",
                                    $url,
                                    $newUrl,
                                    '%' . $wpdb->esc_like($url) . '%',
                                    $post->ID
                                )
                            );
                            $url = $newUrl;
                        }

                        if ($postId !== 'internal' && !$this->isBroken($url)) {
                            continue;
                        }

                        $foundUrls[] = array(
                            'post_id' => $post->ID,
                            'url' => $url
                        );
                    }
                }
            }
        }

        $this->saveBrokenLinks($foundUrls, $postId);
    }
public function lookForBrokenLinks ( $ postId = null , $ url = null ) { \ BrokenLinkDetector \ App :: checkInstall ( ) ; $ foundUrls = array ( ) ; if ( $ url ) { $ url = "REGEXP ('.*(href=\"{$url}\").*')" ; } else { $ url = "RLIKE ('href=*')" ; } global $ wpdb ; $ sql = "
            SELECT ID, post_content
            FROM $wpdb->posts
            WHERE
                post_content {$url}
                AND post_type NOT IN ('attachment', 'revision', 'acf', 'acf-field', 'acf-field-group')
                AND post_status IN ('publish', 'private', 'password')
        " ; if ( is_numeric ( $ postId ) ) { $ sql .= " AND ID = $postId" ; } $ posts = $ wpdb -> get_results ( $ sql ) ; if ( is_array ( $ posts ) && ! empty ( $ posts ) ) { foreach ( $ posts as $ post ) { preg_match_all ( '/<a[^>]+href=([\'"])(http|https)(.+?)\1[^>]*>/i' , $ post -> post_content , $ m ) ; if ( ! isset ( $ m [ 3 ] ) || count ( $ m [ 3 ] ) > 0 ) { foreach ( $ m [ 3 ] as $ key => $ url ) { $ url = $ m [ 2 ] [ $ key ] . $ url ; // Replace whitespaces in url if ( preg_match ( '/\s/' , $ url ) ) { $ newUrl = preg_replace ( '/ /' , '%20' , $ url ) ; $ wpdb -> query ( $ wpdb -> prepare ( "UPDATE $wpdb->posts
                                        SET post_content = REPLACE(post_content, %s, %s)
                                        WHERE post_content LIKE %s
                                        AND ID = %d" , $ url , $ newUrl , '%' . $ wpdb -> esc_like ( $ url ) . '%' , $ post -> ID ) ) ; $ url = $ newUrl ; } if ( $ postId !== 'internal' && ! $ this -> isBroken ( $ url ) ) { continue ; } $ foundUrls [ ] = array ( 'post_id' => $ post -> ID , 'url' => $ url ) ; } } } } $ this -> saveBrokenLinks ( $ foundUrls , $ postId ) ; }
Look for broken links in post_content

@param  integer $post_id Optional post_id to update broken links for
@return void
Look for broken links in post_content