self::lists(), 'always' => self::inbound(), ); } public static function lists() { global $wp_rewrite; $pretty = $wp_rewrite && $wp_rewrite->using_permalinks(); if ( ! $pretty ) { return array( array( 'path' => '/', 'param' => 'paged', 'style' => 'query', 'pages' => self::pages( self::published( 'post' ), self::per_page() ) ) ); } $base = $wp_rewrite->pagination_base ? $wp_rewrite->pagination_base : 'page'; $found = array(); $add = function ( $url, $count, $per ) use ( &$found, $base ) { $path = self::own_path( $url ); $pages = self::pages( $count, $per ); if ( null === $path || $pages < 2 || isset( $found[ $path ] ) || count( $found ) >= 50 ) { return; } $found[ $path ] = array( 'path' => $path, 'param' => $base, 'style' => 'path', 'pages' => $pages ); }; $per = self::per_page(); $posts_page = (int) get_option( 'page_for_posts' ); $add( 'page' === get_option( 'show_on_front' ) && $posts_page ? get_permalink( $posts_page ) : home_url( '/' ), self::published( 'post' ), $per ); foreach ( get_post_types( array( 'public' => true, 'has_archive' => true ), 'names' ) as $type ) { $add( get_post_type_archive_link( $type ), self::published( $type ), 'product' === $type ? self::shop_per_page() : $per ); } foreach ( array( 'category' => $per, 'product_cat' => self::shop_per_page() ) as $tax => $tax_per ) { if ( ! taxonomy_exists( $tax ) ) { continue; } $terms = get_terms( array( 'taxonomy' => $tax, 'hide_empty' => true, 'number' => 25, 'orderby' => 'count', 'order' => 'DESC' ) ); foreach ( is_array( $terms ) ? $terms : array() as $term ) { $link = get_term_link( $term ); if ( ! is_wp_error( $link ) ) { $add( $link, (int) $term->count, $tax_per ); } } } return array_values( $found ); } public static function inbound() { if ( ! function_exists( 'rest_get_server' ) ) { return array(); } $prefix = '/' . trim( rest_get_url_prefix(), '/' ); $out = array(); foreach ( array_keys( rest_get_server()->get_routes() ) as $route ) { if ( ! preg_match( self::INBOUND, $route ) || preg_match( self::OWN, $route ) ) { continue; } $plain = rtrim( preg_split( '/[(?]/', $route )[0], '/' ); if ( strlen( $plain ) > 1 ) { $out[ $prefix . $plain ] = true; } } return array_slice( array_keys( $out ), 0, 30 ); } private static function own_path( $url ) { if ( ! is_string( $url ) || '' === $url ) { return null; } $host = wp_parse_url( home_url( '/' ), PHP_URL_HOST ); if ( wp_parse_url( $url, PHP_URL_HOST ) !== $host ) { return null; } $path = (string) wp_parse_url( $url, PHP_URL_PATH ); $path = '/' . trim( $path, '/' ); return strlen( $path ) <= 200 ? $path : null; } private static function published( $type ) { $n = wp_count_posts( $type ); return isset( $n->publish ) ? (int) $n->publish : 0; } private static function per_page() { return max( 1, (int) get_option( 'posts_per_page', 10 ) ); } private static function shop_per_page() { if ( function_exists( 'wc_get_default_products_per_row' ) && function_exists( 'wc_get_default_product_rows_per_page' ) ) { return max( 1, (int) apply_filters( 'loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page() ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WooCommerce's own filter. } return self::per_page(); } private static function pages( $count, $per ) { return (int) ceil( max( 0, (int) $count ) / max( 1, (int) $per ) ); } }