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; } /** * Quick and dirty way to mostly minify CSS. * * @author Gary Jones * * @since 0.0.2 * * @param string $css CSS to minify * * @return string */ function minify_css( string $css ): string { $css = preg_replace( '/\s+/', ' ', $css ); $css = preg_replace( '/(\s+)(\/\*(.*?)\*\/)(\s+)/', '$2', $css ); $css = preg_replace( '~/\*(?![!|*])(.*?)\*/~', '', $css ); $css = preg_replace( '/;(?=\s*})/', '', $css ); $css = preg_replace( '/(,|:|;|\{|}|\*\/|>) /', '$1', $css ); $css = preg_replace( '/ (,|;|\{|}|\(|\)|>)/', '$1', $css ); $css = preg_replace( '/(:| )0\.([0-9]+)(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}.${2}${3}', $css ); $css = preg_replace( '/(:| )(\.?)0(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}0', $css ); $css = preg_replace( '/0 0 0 0/', '0', $css ); $css = preg_replace( '/#([a-f0-9])\\1([a-f0-9])\\2([a-f0-9])\\3/i', '#\1\2\3', $css ); return trim( $css ); } /** * 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 null $default * * @return array */ function get_sub_config( string $sub_config, $default = null ): array { return get_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 ); }