prefix . 'langferry_pages'; } /** * A page's key: its path relative to the home path ("/blog/about/" is "/about/" when WordPress * lives in /blog), starting with "/", without the fragment. On pretty permalinks the query goes * too, as in hreflang (Router::canonical_url_in()): "/about/?utm_source=news" is "/about/". On * plain ones only the query vars that name content stay, sorted by name: "/?p=5" and * "/?page_id=7" are two pages, "/?p=5&order=asc" is "/?p=5". * * @param string $path A full URL, or a path with its query. * @param string $lang The language of a translated address: its prefix ("/es/") goes, and with * translated slugs on, they map back to the source. '' for a source address, * such as the one WordPress serves while it renders a translated page. */ public static function key( string $path, string $lang = '' ): string { $parts = wp_parse_url( $path ); $rel = '/' . ltrim( is_array( $parts ) && isset( $parts['path'] ) ? (string) $parts['path'] : '', '/' ); $query = is_array( $parts ) && isset( $parts['query'] ) ? (string) $parts['query'] : ''; $home = Router::instance()->home_path(); if ( '' !== $home && ( $rel === $home || 0 === strpos( $rel, $home . '/' ) ) ) { $rel = '/' . ltrim( substr( $rel, strlen( $home ) ), '/' ); } $config = Settings::effective(); if ( '' !== $lang && $lang !== $config['default_language'] ) { if ( 'subdirectory' === $config['url_mode'] ) { $prefix = '/' . Languages::url_slug( $lang ); if ( $rel === $prefix || 0 === strpos( $rel, $prefix . '/' ) ) { $rel = '/' . ltrim( substr( $rel, strlen( $prefix ) ), '/' ); } } if ( $config['seo']['translate_slugs'] ) { $rel = Slugs::to_source( $rel, $lang ); } } return Slugs::normalize( $rel ) . self::content_query( $query ); } /** * Record a render's completeness. A busy page doesn't write on every view: only its first * render, pre-translation (whose time the worker reads), a coverage change of 5 points or more, * a move across the noindex threshold, pending text appearing or going, and a person's first * render that leaves text pending (asked_at, the order's tier 2) write. * * @param string $path The source address rendered (see key()). * @param string $lang The language rendered. * @param array $stats Translator::translate()'s stats: chars, translated_chars, pending. * @param string $requester Traffic's reason: person, search_engine, pretranslate, … */ public static function record( string $path, string $lang, array $stats, string $requester ): void { // Search results, 404s and bots never write: their addresses can be made up, so they could // grow the table without end or overwrite a page's real row. if ( '' === $lang || strlen( $lang ) > 12 || ! in_array( $requester, self::RECORDED_FOR, true ) ) { return; } $key = self::key( $path ); if ( mb_strlen( $key ) > self::MAX_PATH ) { return; } $chars = max( 0, (int) ( $stats['chars'] ?? 0 ) ); $now = array( 'chars' => $chars, 'translated_chars' => min( $chars, max( 0, (int) ( $stats['translated_chars'] ?? 0 ) ) ), 'pending' => max( 0, (int) ( $stats['pending'] ?? 0 ) ), ); $row = self::rows( $key )[ $lang ] ?? null; $asked = 'person' === $requester && $now['pending'] > 0 && ( null === $row || null === $row['asked_at'] ); if ( null !== $row && 'pretranslate' !== $requester && ! $asked && ! self::changed( $row, array_merge( $stats, $now ) ) ) { return; } global $wpdb; $time = current_time( 'mysql', true ); // attempts is assigned first, while translated_chars is still the stored value. NULLIF: // prepare() can't write NULL, so an empty asked_at stays NULL and COALESCE keeps the first. $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; the listing's cache is cleared below. $wpdb->prepare( "INSERT INTO %i (path_hash, path, lang, chars, translated_chars, pending, asked_at, attempts, updated_at) VALUES (%s, %s, %s, %d, %d, %d, NULLIF(%s, ''), 0, %s) ON DUPLICATE KEY UPDATE attempts = IF(VALUES(translated_chars) > translated_chars, 0, attempts), chars = VALUES(chars), translated_chars = VALUES(translated_chars), pending = VALUES(pending), asked_at = COALESCE(asked_at, VALUES(asked_at)), updated_at = VALUES(updated_at)", self::table(), self::hash( $key ), $key, $lang, $now['chars'], $now['translated_chars'], $now['pending'], $asked ? $time : '', $time ) ); self::flush(); } /** * A pair's row as it is in the table now (never from the cache, so the worker sees the write of * the render it asked for), or null. * * @return array{path: string, lang: string, chars: int, translated_chars: int, pending: int, asked_at: ?string, attempts: int, set_aside_until: ?string, reason: string, updated_at: string}|null */ public static function get( string $key, string $lang ): ?array { global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( 'SELECT path, lang, chars, translated_chars, pending, asked_at, attempts, set_aside_until, reason, updated_at FROM %i WHERE path_hash = %s AND lang = %s', self::table(), self::hash( $key ), $lang ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; read fresh on purpose. return is_array( $row ) ? self::shape( $row ) : null; } /** * Whether a pair with this row is listed (hreflang and the sitemaps): with no row, or while "Hide * incomplete pages" is off, always; otherwise exactly when it wouldn't be noindex as incomplete. The * worker compares it before and after its request (Queue, ruling R64). * * @param array|null $row The pair's row (get()), or a render's stats; null for none. */ public static function listed( ?array $row ): bool { return null === $row || ! self::hides_incomplete() || self::is_listed( $row ); } /** * The languages a page's translation is listed in (hreflang): those with no row, and those whose * coverage wouldn't be hidden as incomplete. One query for the page's rows, through the cache. * * @param string[] $langs * @return string[] In $langs' order. */ public static function listed_languages( string $key, array $langs ): array { if ( ! self::hides_incomplete() ) { return array_values( $langs ); } $rows = self::rows( $key ); $out = array(); foreach ( $langs as $lang ) { if ( ! isset( $rows[ $lang ] ) || self::is_listed( $rows[ $lang ] ) ) { $out[] = $lang; } } return $out; } /** * The languages each of a sitemap file's pages is listed in, which give both the file's own * addresses and each one's alternates. One query for every key's rows in every language, through * the cache. * * @param string[] $keys * @param string[] $langs * @return array Each key => the languages of $langs it is listed in, in $langs' order. */ public static function listed_matrix( array $keys, array $langs ): array { $langs = array_values( array_unique( array_map( 'strval', $langs ) ) ); $out = array(); if ( ! self::hides_incomplete() || ! $langs || ! $keys ) { foreach ( $keys as $key ) { $out[ (string) $key ] = $langs; } return $out; } $hashes = array(); foreach ( $keys as $key ) { $hashes[ self::hash( (string) $key ) ] = true; } $hashes = array_keys( $hashes ); sort( $hashes ); $sorted = $langs; sort( $sorted ); $cache = 'matrix:' . wp_cache_get_last_changed( self::CACHE_GROUP ) . ':' . md5( implode( ',', $sorted ) . ':' . implode( ',', $hashes ) ); $rows = wp_cache_get( $cache, self::CACHE_GROUP ); if ( ! is_array( $rows ) ) { global $wpdb; $found = $wpdb->get_results( $wpdb->prepare( 'SELECT path_hash, lang, chars, translated_chars, pending FROM %i WHERE path_hash IN (' . implode( ',', array_fill( 0, count( $hashes ), '%s' ) ) . ') AND lang IN (' . implode( ',', array_fill( 0, count( $sorted ), '%s' ) ) . ')', array_merge( array( self::table() ), $hashes, $sorted ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; cached until the next write. $rows = array(); foreach ( (array) $found as $row ) { $rows[ $row['path_hash'] ][ $row['lang'] ] = self::shape( $row ); } wp_cache_set( $cache, $rows, self::CACHE_GROUP, HOUR_IN_SECONDS ); } foreach ( $keys as $key ) { $hash = self::hash( (string) $key ); $listed = array(); foreach ( $langs as $lang ) { if ( ! isset( $rows[ $hash ][ $lang ] ) || self::is_listed( $rows[ $hash ][ $lang ] ) ) { $listed[] = $lang; } } $out[ (string) $key ] = $listed; } return $out; } /** * The order's view of these pages in these languages (Priority): whether each pair is pending and * until when it is set aside. Read from the table, never from the cache, because the worker writes * between two reads. One query per 100 keys, for every language at once. * * @param string[] $keys * @param string[] $langs * @return array> Key => language => its row's state; a pair with no row is missing. */ public static function states( array $keys, array $langs ): array { $langs = array_values( array_unique( array_map( 'strval', $langs ) ) ); $by_hash = array(); foreach ( $keys as $key ) { $by_hash[ self::hash( (string) $key ) ] = (string) $key; } $out = array(); if ( ! $by_hash || ! $langs ) { return $out; } global $wpdb; foreach ( array_chunk( array_keys( $by_hash ), 100 ) as $hashes ) { $found = $wpdb->get_results( $wpdb->prepare( 'SELECT path_hash, lang, pending, set_aside_until FROM %i WHERE path_hash IN (' . implode( ',', array_fill( 0, count( $hashes ), '%s' ) ) . ') AND lang IN (' . implode( ',', array_fill( 0, count( $langs ), '%s' ) ) . ')', array_merge( array( self::table() ), $hashes, $langs ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; read fresh on purpose. foreach ( (array) $found as $row ) { if ( isset( $by_hash[ $row['path_hash'] ] ) ) { $out[ $by_hash[ $row['path_hash'] ] ][ (string) $row['lang'] ] = array( 'pending' => (int) $row['pending'], 'set_aside_until' => null === $row['set_aside_until'] ? null : (string) $row['set_aside_until'], ); } } } return $out; } /** * The pairs a person asked for (the order's tier 2) that are still pending in these languages and * not set aside at $at: earliest asked first, $limit of them from $offset. * * @param string[] $langs * @param string $at The time, in UTC ('Y-m-d H:i:s'). * @return array */ public static function asked( array $langs, string $at, int $offset, int $limit ): array { $langs = array_values( array_unique( array_map( 'strval', $langs ) ) ); if ( ! $langs || $limit < 1 ) { return array(); } global $wpdb; $found = $wpdb->get_results( $wpdb->prepare( 'SELECT path, lang FROM %i WHERE asked_at IS NOT NULL AND pending > 0 AND ( set_aside_until IS NULL OR set_aside_until <= %s ) AND lang IN (' . implode( ',', array_fill( 0, count( $langs ), '%s' ) ) . ') ORDER BY asked_at, id LIMIT %d, %d', array_merge( array( self::table(), $at ), $langs, array( max( 0, $offset ), $limit ) ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; read fresh on purpose. $out = array(); foreach ( (array) $found as $row ) { $out[] = array( 'path' => (string) $row['path'], 'lang' => (string) $row['lang'], ); } return $out; } /** * The pairs set aside at $at in these languages, still pending: those coming back first first, * $limit of them from $offset, with the reason they were set aside. * * @param string[] $langs * @param string $at The time, in UTC ('Y-m-d H:i:s'). * @return array */ public static function set_aside( array $langs, string $at, int $offset, int $limit ): array { $langs = array_values( array_unique( array_map( 'strval', $langs ) ) ); if ( ! $langs || $limit < 1 ) { return array(); } global $wpdb; $found = $wpdb->get_results( $wpdb->prepare( 'SELECT path, lang, reason FROM %i WHERE set_aside_until > %s AND pending > 0 AND lang IN (' . implode( ',', array_fill( 0, count( $langs ), '%s' ) ) . ') ORDER BY set_aside_until, id LIMIT %d, %d', array_merge( array( self::table(), $at ), $langs, array( max( 0, $offset ), $limit ) ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; read fresh on purpose. $out = array(); foreach ( (array) $found as $row ) { $out[] = array( 'path' => (string) $row['path'], 'lang' => (string) $row['lang'], 'reason' => (string) $row['reason'], ); } return $out; } /** * How many pairs in these languages are set aside at $at and still pending: the rows * set_aside() reads, counted (paths since excluded included). * * @param string[] $langs * @param string $at The time, in UTC ('Y-m-d H:i:s'). */ public static function set_aside_count( array $langs, string $at ): int { $langs = array_values( array_unique( array_map( 'strval', $langs ) ) ); if ( ! $langs ) { return 0; } global $wpdb; return max( 0, (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE set_aside_until > %s AND pending > 0 AND lang IN (' . implode( ',', array_fill( 0, count( $langs ), '%s' ) ) . ')', array_merge( array( self::table(), $at ), $langs ) ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; read fresh on purpose. } /** * How many pairs in these languages are complete (nothing pending). * * @param string[] $langs */ public static function complete_count( array $langs ): int { $langs = array_values( array_unique( array_map( 'strval', $langs ) ) ); if ( ! $langs ) { return 0; } global $wpdb; return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE pending = 0 AND lang IN (' . implode( ',', array_fill( 0, count( $langs ), '%s' ) ) . ')', array_merge( array( self::table() ), $langs ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; one count an hour, for the report. } /** * A failed attempt at a pair (the worker, Queue): one more in a row, with its reason. The one that * makes $limit in a row sets the pair aside until $until; when it comes back, its next failed * attempt starts a new count. A pair with no row gets one, with something pending, so the order * keeps it waiting (and never reads it as complete when it comes back). Its time is $now; an * existing row's time stays the render's, which the worker compares with its next request. * * @param string $now The time, in UTC ('Y-m-d H:i:s'). * @param string $until When a pair set aside comes back, in UTC. */ public static function failed( string $key, string $lang, string $reason, int $limit, string $now, string $until ): void { global $wpdb; // attempts is assigned first, so set_aside_until reads the new count. $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; the listing's cache is cleared below. $wpdb->prepare( 'INSERT INTO %i (path_hash, path, lang, chars, translated_chars, pending, attempts, set_aside_until, reason, updated_at) VALUES (%s, %s, %s, 0, 0, 1, 1, IF(1 >= %d, %s, NULL), %s, %s) ON DUPLICATE KEY UPDATE attempts = IF(attempts >= %d, 1, attempts + 1), set_aside_until = IF(attempts >= %d, %s, set_aside_until), reason = VALUES(reason)', self::table(), self::hash( $key ), $key, $lang, $limit, $until, $reason, $now, $limit, $limit, $until ) ); self::flush(); } /** * A complete pair has no failed attempts in a row: its count and reason go (the worker, Queue). */ public static function clear_attempts( string $key, string $lang ): void { global $wpdb; $wpdb->query( $wpdb->prepare( "UPDATE %i SET attempts = 0, reason = '' WHERE path_hash = %s AND lang = %s", self::table(), self::hash( $key ), $lang ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; the listing's cache is cleared below. self::flush(); } /** * The worker's run lock (Queue): a row in the options table named $name, holding $value ("expiry: * token"), taken only when there is none. The insert is atomic on the option name's unique key, so * of two runs only one takes it. A lock past its expiry (a run that died) is taken over by * replacing the value read, which only one run can do. Every query of the worker lives in this class. * The DB upgrade takes one too (Installer::UPGRADE_LOCK, ruling R61), so only one request schedules * the worker. * * @param int $now A Unix time: a lock whose expiry is at or before it is broken. */ public static function lock( string $name, string $value, int $now ): bool { global $wpdb; if ( $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO %i (option_name, option_value, autoload) VALUES (%s, %s, 'off')", $wpdb->options, $name, $value ) ) ) { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- add_option() isn't atomic (it updates on a duplicate); the lock must be. return true; } $held = $wpdb->get_var( $wpdb->prepare( 'SELECT option_value FROM %i WHERE option_name = %s', $wpdb->options, $name ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- read fresh: another run writes it. if ( ! is_string( $held ) || (int) $held > $now ) { return false; } return 1 === (int) $wpdb->query( $wpdb->prepare( 'UPDATE %i SET option_value = %s WHERE option_name = %s AND option_value = %s', $wpdb->options, $value, $name, $held ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- compare and swap: only one run replaces the value it read. } /** * Keep holding the lock: $held, the value this run wrote, becomes $value. False when it isn't * $held any more (another run took it over past its expiry). */ public static function relock( string $name, string $held, string $value ): bool { global $wpdb; if ( $value === $held ) { return $held === $wpdb->get_var( $wpdb->prepare( 'SELECT option_value FROM %i WHERE option_name = %s', $wpdb->options, $name ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- read fresh: another run writes it. } return 1 === (int) $wpdb->query( $wpdb->prepare( 'UPDATE %i SET option_value = %s WHERE option_name = %s AND option_value = %s', $wpdb->options, $value, $name, $held ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- compare and swap, as in lock(). } /** * Release the lock, only if it is still $held: never one another run has taken over. */ public static function unlock( string $name, string $held ): void { global $wpdb; $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE option_name = %s AND option_value = %s', $wpdb->options, $name, $held ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- only this run's own lock. } /** * Delete a language's rows ("Delete translations"), or every row for null (or ''). Those pages * count as not recorded, so listed, until they are rendered again; the order's sitemap cursor * starts again in that language, as the pages it had passed have work again. */ public static function forget( ?string $lang ): void { global $wpdb; $lang = (string) $lang; // '' = every language. $wpdb->query( $wpdb->prepare( "DELETE FROM %i WHERE ( %s = '' OR lang = %s )", self::table(), $lang, $lang ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; the listing's cache is cleared below. self::flush(); Priority::restart( '' === $lang ? null : $lang ); } /** * Delete one pair's row: a page a person asked for (the order's tier 2) that answers 404, 410 or a * redirect isn't there any more (the worker, Queue; ruling R41). The pair counts as not recorded, * so listed, until it is rendered again. */ public static function forget_pair( string $key, string $lang ): void { global $wpdb; $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE path_hash = %s AND lang = %s', self::table(), self::hash( $key ), $lang ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; the listing's cache is cleared below. self::flush(); } private static function hash( string $key ): string { return sha1( $key ); } /** * Whether a row (or a render's stats) is listed: exactly when the page wouldn't be noindex as * incomplete, so listing and noindex agree by construction. */ private static function is_listed( array $row ): bool { return ! Seo::should_noindex_incomplete( $row ); } /** * Whether the listing leaves anything out: only while "Hide incomplete pages" is on. With it off * no page is noindex, so every translation is listed, as in 2.7, and listing and noindex still * agree. record() keeps writing threshold crossings either way (is_listed()), so the rows are * right the moment the setting is switched on. */ private static function hides_incomplete(): bool { return ! empty( Settings::effective()['seo']['noindex_incomplete'] ); } /** * Whether a render differs enough from its row to be written. * * @param array $row The stored row. * @param array $now The render's stats, with chars, translated_chars and pending as stored. */ private static function changed( array $row, array $now ): bool { $before = $row['chars'] > 0 ? $row['translated_chars'] / $row['chars'] : 1.0; $after = $now['chars'] > 0 ? $now['translated_chars'] / $now['chars'] : 1.0; // 0.85 - 0.80 is 0.04999… in floating point; a hair of slack keeps 5 points 5 points. return abs( $after - $before ) >= self::MOVE - 1e-9 || self::is_listed( $row ) !== self::is_listed( $now ) || ( $row['pending'] > 0 ) !== ( $now['pending'] > 0 ); } /** * A page's rows by language, through the cache: hreflang and record() on the same view share * one query. * * @return array */ private static function rows( string $key ): array { $cache = 'rows:' . wp_cache_get_last_changed( self::CACHE_GROUP ) . ':' . self::hash( $key ); $rows = wp_cache_get( $cache, self::CACHE_GROUP ); if ( is_array( $rows ) ) { return $rows; } global $wpdb; $found = $wpdb->get_results( $wpdb->prepare( 'SELECT lang, chars, translated_chars, pending, asked_at, attempts FROM %i WHERE path_hash = %s', self::table(), self::hash( $key ) ), ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- the plugin's own table; cached until the next write. $rows = array(); foreach ( (array) $found as $row ) { $rows[ $row['lang'] ] = self::shape( $row ); } wp_cache_set( $cache, $rows, self::CACHE_GROUP, HOUR_IN_SECONDS ); return $rows; } /** * Every cached read is filed under the group's last-changed time; a new one retires them all. */ private static function flush(): void { wp_cache_delete( 'last_changed', self::CACHE_GROUP ); } private static function shape( array $row ): array { foreach ( array( 'chars', 'translated_chars', 'pending', 'attempts' ) as $column ) { if ( isset( $row[ $column ] ) ) { $row[ $column ] = (int) $row[ $column ]; } } return $row; } /** * On plain permalinks, the query vars that name content (CONTENT_VARS and those of public post * types and taxonomies), sorted by name, as "?a=1&b=2", or ''. Each value is written as WordPress * reads it (content_value()), so "?p=5abc" is "?p=5". On pretty permalinks the path names the * page: ''. A made-up value of anything else (?order=…, an e-mail address) renders the same page, * so it adds no row. */ private static function content_query( string $query ): string { if ( '' === $query || '' !== (string) get_option( 'permalink_structure' ) ) { return ''; } $vars = self::CONTENT_VARS; foreach ( array_merge( get_post_types( array( 'public' => true ), 'objects' ), get_taxonomies( array( 'public' => true ), 'objects' ) ) as $type ) { if ( is_string( $type->query_var ) && '' !== $type->query_var ) { $vars[] = $type->query_var; } } wp_parse_str( $query, $args ); $kept = array(); foreach ( array_unique( $vars ) as $var ) { $value = isset( $args[ $var ] ) && is_scalar( $args[ $var ] ) ? self::content_value( $var, (string) $args[ $var ] ) : ''; if ( '' !== $value ) { $kept[ $var ] = $value; } } if ( ! $kept ) { return ''; } ksort( $kept ); return '?' . http_build_query( $kept ); } /** * A content var's value in the form WordPress reads it, or '' when that names nothing: * - whole numbers: "5abc" is "5"; * - cat and author: the IDs listed, once each and sorted. An exclusion ("-5") goes: one of an * ID that doesn't exist still shows every post, so made-up ones would each make a row; * - m: its digits, at most 14 (YYYYMMDDHHMMSS), in pairs after the year as WordPress reads them; * - slugs (tag, name, post types' and taxonomies' query vars, …): sanitize_title_for_query(), * "/" by "/" as get_page_by_path() reads a page path ("parent/child"). */ private static function content_value( string $var, string $value ): string { if ( in_array( $var, self::NUMBER_VARS, true ) ) { $number = absint( $value ); return $number > 0 ? (string) $number : ''; } if ( in_array( $var, self::ID_LIST_VARS, true ) ) { $ids = array(); foreach ( explode( ',', (string) preg_replace( '/[^0-9,-]/', '', $value ) ) as $id ) { if ( (int) $id > 0 ) { $ids[ (int) $id ] = true; } } ksort( $ids ); return implode( ',', array_keys( $ids ) ); } if ( 'm' === $var ) { $digits = substr( (string) preg_replace( '/[^0-9]/', '', $value ), 0, 14 ); return strlen( $digits ) > 4 && 1 === strlen( $digits ) % 2 ? substr( $digits, 0, -1 ) : $digits; } $slugs = array(); foreach ( explode( '/', $value ) as $slug ) { $slug = sanitize_title_for_query( $slug ); if ( '' !== $slug ) { $slugs[] = $slug; } } return implode( '/', $slugs ); } }