post_name ) ? (string) $post->post_name : ''; $post_status = isset( $post->post_status ) ? (string) $post->post_status : 'publish'; $flags = Flags::NONE; $scripts = array(); // 1. Truncated detection: Raw post_name length >= 190 characters. if ( strlen( $raw_slug ) >= 190 ) { $flags |= Flags::TRUNCATED; } // 2. Malformed encoding: % sequences that do not decode to valid UTF-8, or double encoding such as %25e0. if ( \SlugH\Redirects\Normalizer::is_malformed( $raw_slug ) ) { $flags |= Flags::MALFORMED; } // Decode slug before script and character analysis. $decoded_slug = rawurldecode( $raw_slug ); // 3. Empty slug on published record. if ( 'publish' === $post_status && '' === trim( $decoded_slug ) ) { $flags |= Flags::EMPTY_SLUG; } // 4. Non-ASCII check. if ( preg_match( '/[^\x20-\x7E]/', $decoded_slug ) ) { $flags |= Flags::NON_ASCII; } // 5. Script detection (PCRE with u flag). $has_bengali = (bool) preg_match( '/\p{Bengali}/u', $decoded_slug ); $has_devanagari = (bool) preg_match( '/\p{Devanagari}/u', $decoded_slug ); $has_arabic = (bool) preg_match( '/\p{Arabic}/u', $decoded_slug ); if ( $has_bengali ) { $flags |= Flags::SCRIPT_BENGALI; $scripts[] = 'Bengali'; } if ( $has_devanagari ) { $flags |= Flags::SCRIPT_DEVANAGARI; $scripts[] = 'Devanagari'; } if ( $has_arabic ) { $flags |= Flags::SCRIPT_ARABIC; $scripts[] = 'Arabic'; } // 6. Non-Latin letters: Characters matching \p{L} but not \p{Latin}. // Accented Latin (e.g., é, ñ, ü) is \p{Latin} so it is NOT non-Latin. // Extract all unicode letters. preg_match_all( '/\p{L}/u', $decoded_slug, $letter_matches ); $has_non_latin = false; $has_other = false; if ( ! empty( $letter_matches[0] ) ) { foreach ( $letter_matches[0] as $letter ) { if ( ! preg_match( '/\p{Latin}/u', $letter ) ) { $has_non_latin = true; // Check if letter belongs to an unclassified non-Latin script. if ( ! preg_match( '/[\p{Bengali}\p{Devanagari}\p{Arabic}]/u', $letter ) ) { $has_other = true; } } } } if ( $has_non_latin ) { $flags |= Flags::NON_LATIN; } if ( $has_other ) { $flags |= Flags::SCRIPT_OTHER; $scripts[] = 'Other'; } // 7. Uppercase characters in decoded slug. if ( preg_match( '/\p{Lu}/u', $decoded_slug ) ) { $flags |= Flags::UPPERCASE; } // 8. Invalid characters: whitespace or characters that sanitize_title() changes. // Compare in decoded form: a correctly encoded Unicode slug is valid and must not be flagged. if ( '' !== $decoded_slug && ( preg_match( '/\s/u', $decoded_slug ) || rawurldecode( sanitize_title( $decoded_slug ) ) !== $decoded_slug ) ) { $flags |= Flags::INVALID_CHARS; } return array( 'flags' => $flags, 'scripts' => implode( ', ', array_unique( $scripts ) ), 'decoded_slug' => $decoded_slug, ); } /** * Permalink problems: a ?p= style URL while pretty permalinks are on, or a permalink * whose last segment is not the record's slug. * * @param \WP_Post $post Post. * @param string $permalink Permalink. * @return bool */ public static function has_permalink_problem( $post, $permalink ) { if ( '' === $permalink || preg_match( '/[?&](p|page_id|post_type)=/', $permalink ) ) { return true; } if ( '' === $post->post_name ) { return true; } // Structures without %postname% (e.g. /%post_id%/) legitimately end with something else. if ( 'post' === $post->post_type && false === strpos( (string) get_option( 'permalink_structure' ), '%postname%' ) ) { return false; } $path = \SlugH\Redirects\Normalizer::normalize_path( $permalink ); $leaf = substr( $path, strrpos( $path, '/' ) + 1 ); $expected = ltrim( \SlugH\Redirects\Normalizer::normalize_path( '/' . $post->post_name ), '/' ); return $leaf !== $expected; } }