array( 'page', 'post' ), 'post_status' => 'publish', 'posts_per_page' => $limit, 'orderby' => 'modified', 'order' => 'DESC', ) ); $patterns = array( '#href=["\']([^"\']*?/collection/?["\'])#i', '#href=["\']([^"\']*?/product/?["\'])#i', ); $home = untrailingslashit( home_url() ); foreach ( $pages as $page ) { $content = (string) $page->post_content; if ( '' === $content ) { continue; } $found = array(); foreach ( $patterns as $pattern ) { if ( preg_match_all( $pattern, $content, $matches ) ) { foreach ( $matches[1] as $raw ) { $url = trim( $raw, "\"'" ); // Ignore already-slugged collection/foo. $path = wp_parse_url( $url, PHP_URL_PATH ); if ( ! is_string( $path ) ) { continue; } $norm = untrailingslashit( $path ); if ( preg_match( '#/(collection|product)$#', $norm ) ) { $found[] = $url; } } } } // Also check Elementor data JSON for URLs. $el = get_post_meta( $page->ID, '_elementor_data', true ); if ( is_string( $el ) && $el ) { if ( preg_match_all( '#' . preg_quote( $home, '#' ) . '/(collection|product)/?(?:["\'\\\\]|$)#i', $el, $m ) ) { foreach ( $m[0] as $hit ) { $found[] = trim( $hit, "\"'\\" ); } } // Relative in JSON. if ( preg_match_all( '#"(/(?:collection|product)/?)"#', $el, $m2 ) ) { foreach ( $m2[1] as $rel ) { if ( preg_match( '#^/(collection|product)/?$#', $rel ) ) { $found[] = $rel; } } } } $found = array_values( array_unique( $found ) ); if ( $found ) { $issues[] = array( 'page_id' => (int) $page->ID, 'title' => $page->post_title, 'edit_url' => get_edit_post_link( $page->ID, 'raw' ), 'view_url' => get_permalink( $page->ID ), 'urls' => $found, 'post_type' => $page->post_type, ); } } return $issues; } /** * Status for health. * * @return array */ public function status() { $issues = $this->find_issues( 50 ); $count = count( $issues ); if ( $count > 0 ) { return array( 'status' => 'warn', 'label' => __( 'Empty shop links in content', '7labs-debug-branding-for-commerce7' ), 'detail' => sprintf( /* translators: %d: pages */ _n( '%d page/post links to empty /collection or /product.', '%d pages/posts link to empty /collection or /product.', $count, '7labs-debug-branding-for-commerce7' ), $count ), 'count' => $count, ); } return array( 'status' => 'ok', 'label' => __( 'Page content links OK', '7labs-debug-branding-for-commerce7' ), 'detail' => __( 'No empty C7 base URLs found in recent page content.', '7labs-debug-branding-for-commerce7' ), 'count' => 0, ); } }