*/ public function get(): array { if ( ! is_admin() ) { return $this->fallback_records(); } $cached = wp_cache_get( $this->cache_key(), self::CACHE_GROUP ); if ( is_array( $cached ) ) { $records = $this->normalize_cached_records( $cached ); if ( ! empty( $records ) ) { return $records; } } $records = $this->fetch_records(); wp_cache_set( $this->cache_key(), $records, self::CACHE_GROUP, self::CACHE_TTL ); return ! empty( $records ) ? $records : $this->fallback_records(); } private function cache_key(): string { return self::CACHE_KEY . '_' . md5( implode( '|', self::SLUGS ) ); } /** * @return array */ private function fetch_records(): array { if ( ! function_exists( 'plugins_api' ) ) { $core_file = ABSPATH . 'wp-admin/includes/plugin-install.php'; if ( is_readable( $core_file ) ) { require_once $core_file; } } if ( ! function_exists( 'plugins_api' ) ) { return array(); } $records = array(); foreach ( self::SLUGS as $slug ) { $response = plugins_api( 'plugin_information', array( 'slug' => $slug, 'locale' => get_user_locale(), 'is_ssl' => is_ssl(), 'fields' => $this->api_fields(), ) ); if ( is_wp_error( $response ) || ( ! is_object( $response ) && ! is_array( $response ) ) ) { continue; } $record = $this->normalize_api_record( $slug, (array) $response ); if ( null !== $record ) { $records[] = $record; } } return $records; } /** @return array */ private function api_fields(): array { return array( 'short_description' => true, 'description' => false, 'sections' => false, 'tested' => false, 'requires' => false, 'requires_php' => false, 'rating' => true, 'ratings' => false, 'downloaded' => false, 'downloadlink' => false, 'last_updated' => false, 'added' => false, 'tags' => false, 'compatibility' => false, 'homepage' => false, 'versions' => false, 'donate_link' => false, 'reviews' => false, 'banners' => false, 'icons' => true, 'active_installs' => true, 'contributors' => false, ); } /** * @param array $data Remote response data. * @return array{slug: string, name: string, short_description: string, plugin_url: string, icon_url: string, rating: int, active_installs: int}|null */ private function normalize_api_record( string $slug, array $data ): ?array { if ( isset( $data['slug'] ) && ( ! is_scalar( $data['slug'] ) || $slug !== sanitize_key( (string) $data['slug'] ) ) ) { return null; } if ( ! isset( $data['name'] ) || ! is_scalar( $data['name'] ) ) { return null; } $name = sanitize_text_field( wp_strip_all_tags( (string) $data['name'] ) ); if ( '' === $name ) { return null; } $description = isset( $data['short_description'] ) && is_scalar( $data['short_description'] ) ? sanitize_text_field( wp_strip_all_tags( (string) $data['short_description'] ) ) : ''; $description = wp_html_excerpt( $description, 160, '…' ); $icons = isset( $data['icons'] ) && ( is_array( $data['icons'] ) || is_object( $data['icons'] ) ) ? (array) $data['icons'] : array(); $rating = isset( $data['rating'] ) && is_numeric( $data['rating'] ) ? max( 0, min( 100, (int) $data['rating'] ) ) : 0; $installs = isset( $data['active_installs'] ) && is_numeric( $data['active_installs'] ) ? max( 0, (int) $data['active_installs'] ) : 0; return array( 'slug' => $slug, 'name' => wp_html_excerpt( $name, 80, '…' ), 'short_description' => $description, 'plugin_url' => $this->plugin_url( $slug ), 'icon_url' => $this->preferred_icon_url( $icons ), 'rating' => $rating, 'active_installs' => $installs, ); } /** * @param array $cached Cached normalized records. * @return array */ private function normalize_cached_records( array $cached ): array { $by_slug = array(); foreach ( $cached as $record ) { if ( ! is_array( $record ) || ! isset( $record['slug'] ) || ! is_scalar( $record['slug'] ) ) { continue; } $slug = sanitize_key( (string) $record['slug'] ); if ( in_array( $slug, self::SLUGS, true ) ) { $by_slug[ $slug ] = $record; } } $records = array(); foreach ( self::SLUGS as $slug ) { if ( empty( $by_slug[ $slug ]['name'] ) || ! is_scalar( $by_slug[ $slug ]['name'] ) ) { continue; } $record = $by_slug[ $slug ]; $name = sanitize_text_field( wp_strip_all_tags( (string) $record['name'] ) ); $description = isset( $record['short_description'] ) && is_scalar( $record['short_description'] ) ? sanitize_text_field( wp_strip_all_tags( (string) $record['short_description'] ) ) : ''; $icon_url = isset( $record['icon_url'] ) && is_scalar( $record['icon_url'] ) ? $this->trusted_icon_url( (string) $record['icon_url'] ) : ''; $rating = isset( $record['rating'] ) && is_numeric( $record['rating'] ) ? max( 0, min( 100, (int) $record['rating'] ) ) : 0; $installs = isset( $record['active_installs'] ) && is_numeric( $record['active_installs'] ) ? max( 0, (int) $record['active_installs'] ) : 0; if ( '' === $name ) { continue; } $records[] = array( 'slug' => $slug, 'name' => wp_html_excerpt( $name, 80, '…' ), 'short_description' => wp_html_excerpt( $description, 160, '…' ), 'plugin_url' => $this->plugin_url( $slug ), 'icon_url' => $icon_url, 'rating' => $rating, 'active_installs' => $installs, ); } return $records; } /** * @param array $icons Remote icon URLs. */ private function preferred_icon_url( array $icons ): string { foreach ( array( 'svg', '2x', '1x', 'default' ) as $size ) { if ( isset( $icons[ $size ] ) && is_scalar( $icons[ $size ] ) ) { $icon_url = $this->trusted_icon_url( (string) $icons[ $size ] ); if ( '' !== $icon_url ) { return $icon_url; } } } return ''; } private function trusted_icon_url( string $url ): string { $parts = wp_parse_url( trim( $url ) ); if ( ! is_array( $parts ) || 'https' !== ( $parts['scheme'] ?? '' ) || empty( $parts['host'] ) ) { return ''; } if ( isset( $parts['user'] ) || isset( $parts['pass'] ) || ( isset( $parts['port'] ) && 443 !== (int) $parts['port'] ) ) { return ''; } $host = strtolower( (string) $parts['host'] ); if ( 'ps.w.org' !== $host && 'wordpress.org' !== $host && ! str_ends_with( $host, '.wordpress.org' ) ) { return ''; } return esc_url_raw( $url, array( 'https' ) ); } /** * @return array */ private function fallback_records(): array { $records = array(); foreach ( self::SLUGS as $slug ) { $records[] = array( 'slug' => $slug, 'name' => $slug, 'short_description' => '', 'plugin_url' => $this->plugin_url( $slug ), 'icon_url' => '', 'rating' => 0, 'active_installs' => 0, ); } return $records; } private function plugin_url( string $slug ): string { return 'https://wordpress.org/plugins/' . rawurlencode( $slug ) . '/'; } }