lcfirst( str_replace( ' ', '', $upper ) ), PASCAL_CASE => str_replace( ' ', '', $upper ), SNAKE_CASE => strtolower( implode( '_', $pieces ) ), ADA_CASE => str_replace( ' ', '_', $upper ), MACRO_CASE => strtoupper( implode( '_', $pieces ) ), KEBAB_CASE => strtolower( implode( '-', $pieces ) ), TRAIN_CASE => lcfirst( str_replace( ' ', '-', $upper ) ), COBOL_CASE => strtoupper( implode( '-', $pieces ) ), LOWER_CASE => strtolower( $string ), UPPER_CASE => strtoupper( $string ), TITLE_CASE => $upper, SENTENCE_CASE => ucfirst( $lower ), DOT_CASE => strtolower( implode( '.', $pieces ) ), ]; $string = $cases[ $case ] ?? $string; $string = in_array( $string, [ 'Wordpress' ] ) ? 'WordPress' : $string; return apply_filters( 'blockify_convert_case', $string ); } /** * Returns a formatted DOMDocument object from a given string. * * @since 0.0.2 * * @param string $html * * @return string */ function dom( string $html ): DOMDocument { $dom = new DOMDocument(); if ( ! $html ) { return $dom; } $libxml_previous_state = libxml_use_internal_errors( true ); $dom->preserveWhiteSpace = true; if ( defined( 'LIBXML_HTML_NOIMPLIED' ) && defined( 'LIBXML_HTML_NODEFDTD' ) ) { $options = LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD; } else if ( defined( 'LIBXML_HTML_NOIMPLIED' ) ) { $options = LIBXML_HTML_NOIMPLIED; } else if ( defined( 'LIBXML_HTML_NODEFDTD' ) ) { $options = LIBXML_HTML_NODEFDTD; } else { $options = 0; } $dom->loadHTML( mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' ), $options ); $dom->formatOutput = true; libxml_clear_errors(); libxml_use_internal_errors( $libxml_previous_state ); return $dom; } /** * Returns the final merged config. * * @since 0.0.9 * * @return array */ function get_config(): array { $defaults = require __DIR__ . '/config.php'; $theme = get_theme_support( SLUG )[0] ?? []; return apply_filters( SLUG, array_merge_recursive( $defaults, $theme ) ); } /** * Returns sub config. * * @since 0.0.14 * * @param string $sub_config * @param array $default * * @return array */ function get_sub_config( string $sub_config, $default = [] ): array { $config = get_config(); return isset( $config[ $sub_config ] ) && is_array( $config[ $sub_config ] ) ? $config[ $sub_config ] : $default; } /** * Returns an HTML element with a replaced tag. * * @since 0.0.20 * * @param DOMElement $node * @param string $name * * @return DOMElement */ function change_tag_name( DOMElement $node, string $name ): DOMElement { $child_nodes = []; foreach ( $node->childNodes as $child ) { $child_nodes[] = $child; } $new_node = $node->ownerDocument->createElement( $name ); foreach ( $child_nodes as $child ) { $child2 = $node->ownerDocument->importNode( $child, true ); $new_node->appendChild( $child2 ); } foreach ( $node->attributes as $attr_node ) { $attr_name = $attr_node->nodeName; $attr_value = $attr_node->nodeValue; $new_node->setAttribute( $attr_name, $attr_value ); } $node->parentNode->replaceChild( $new_node, $node ); return $new_node; } /** * Attempts to log WordPress PHP data to console. * * @since 0.0.2 * * @param mixed $data * * @return void */ function log( $data ): void { $data = json_encode( $data ); $script = ""; add_action( 'wp_footer', fn() => print $script, PHP_INT_MAX - 1 ); } /** * Converts array of CSS rules to string. * * @since 0.0.22 * * @param array $styles * * @return string */ function css_array_to_string( array $styles ): string { $css = ''; foreach ( $styles as $property => $value ) { $css .= $value ? "$property:$value;" : ''; } return $css; } /** * Converts string of CSS rules to an array. * * @since 0.0.2 * * @param string $css * * @return array */ function css_string_to_array( string $css ): array { $array = []; $elements = explode( ';', $css ); foreach ( $elements as $element ) { $parts = explode( ':', $element, 2 ); if ( isset( $parts[1] ) ) { $property = $parts[0]; $value = $parts[1]; $array[ $property ] = $value; } } return $array; } /** * Checks if we're on a wp.org pattern preview. * * @since 0.2.0 * * @param int $post_id Not always the current post. * * @return bool */ function is_pattern_preview( int $post_id ): bool { return get_post_meta( $post_id, '_wp_page_template', true ) === 'page-full' && $post_id === 2; } /** * Attempts to detect the active style variation. * * Requires a color palette and a background color set in theme.json. * * @since 0.2.0 * * @return void */ function get_style_variation(): string { $style_variation = 'default'; $global_settings = wp_get_global_settings(); $global_styles = wp_get_global_styles(); $palette_settings = $global_settings['color']['palette']['theme'] ?? []; $style_json_files = glob( get_stylesheet_directory() . '/styles/*.json' ); foreach ( $style_json_files as $style_json_file ) { $json = json_decode( file_get_contents( $style_json_file ), true ); $matches = true; if ( ( $json['settings']['color']['palette'] ?? [] ) !== $palette_settings ) { $matches = false; } $background = $json['styles']['color']['background'] ?? null; if ( $background && $background !== $global_styles['color']['background'] ) { $matches = false; } $style_variation = $matches ? basename( $style_json_file, '.json' ) : $style_variation; } return $style_variation; }