class = sprintf( '%s (id: %s)', $class, $value->id ); break; case ( $value instanceof WP_Site ): $class = sprintf( '%s (blog_id: %s)', $class, $value->blog_id ); break; case ( $value instanceof WP_Theme ): $class = sprintf( '%s (%s)', $class, $value->get_stylesheet() ); break; } return $class; } else { return gettype( $value ); } } /** * Shortens a fully qualified name to reduce the length of the names of long namespaced symbols. * * This initialises portions that do not form the first or last portion of the name. For example: * * Inpsyde\Wonolog\HookListener\HookListenersRegistry->hook_callback() * * becomes: * * Inpsyde\W\H\HookListenersRegistry->hook_callback() * * @param string $fqn A fully qualified name. * @return string A shortened version of the name. */ public static function shorten_fqn( $fqn ) { if ( substr_count( $fqn, '\\' ) < 3 ) { return $fqn; } return preg_replace_callback( '#\\\\[a-zA-Z0-9_\\\\]{4,}\\\\#', function( array $matches ) { preg_match_all( '#\\\\([a-zA-Z0-9_])#', $matches[0], $m ); return '\\' . implode( '\\', $m[1] ) . '\\'; }, $fqn ); } /** * Helper function for JSON encoding data and formatting it in a consistent manner. * * @param mixed $data The data to be JSON encoded. * @return string The JSON encoded data. */ public static function json_format( $data ) { // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_unescaped_slashesFound $json_options = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES; $json = json_encode( $data, $json_options ); if ( false === $json ) { return ''; } return $json; } /** * Returns the site editor URL for a given template or template part name. * * @param string $template The site template name, for example `twentytwentytwo//header-small-dark`. * @param string $type The template type, either 'wp_template_part' or 'wp_template'. * @return string The admin URL for editing the site template. */ public static function get_site_editor_url( string $template, string $type = 'wp_template_part' ): string { return add_query_arg( array( 'postType' => $type, 'postId' => urlencode( $template ), 'canvas' => 'edit', ), admin_url( 'site-editor.php' ) ); } /** * @deprecated * @param mixed $data * @return bool */ public static function is_stringy( $data ) { return ( is_string( $data ) || ( is_object( $data ) && method_exists( $data, '__toString' ) ) ); } /** * @param mixed[] $array * @param string $field * @return void */ public static function sort( array &$array, $field ) { usort( $array, function( array $a, array $b ) use ( $field ): int { return $a[ $field ] <=> $b[ $field ]; } ); } /** * @param mixed[] $array * @param string $field * @return void */ public static function rsort( array &$array, $field ) { usort( $array, function( array $a, array $b ) use ( $field ): int { return $b[ $field ] <=> $a[ $field ]; } ); } } }