File: /var/www/html/insiders/wp-load/wp-content/plugins/gutenmate/lib/util.php
<?php
/*//////////////////////////////////////
// Get gutenberg version
//////////////////////////////////////*/
if ( ! function_exists( 'gtm_get_gutenberg_version' ) ) {
function gtm_get_gutenberg_version() {
if ( defined( 'GUTENBERG_VERSION' ) ) {
return GUTENBERG_VERSION;
} else {
// Check version here https://developer.wordpress.org/block-editor/contributors/versions-in-wordpress/
$wp_versions = array_reverse( [
'6.0' => '12.8',
'5.9' => '11.9',
], true );
$current_gutenberg_version = '11.0';
foreach ( $wp_versions as $wp_version => $gutenberg_version ) {
if ( version_compare( $GLOBALS['wp_version'], $wp_version, '>=' ) ) {
$current_gutenberg_version = $gutenberg_version;
} else {
break;
}
}
}
return $current_gutenberg_version;
}
}
if ( ! function_exists( 'gtm_get_gutenberg_version_class' ) ) {
function gtm_get_gutenberg_version_class() {
$version = explode( '.', gtm_get_gutenberg_version() );
if ( ! empty( $version ) ) {
return sprintf( 'gtm-gutenberg-%s', $version[0] );
}
}
}
/*//////////////////////////////////////
// May be JSON
//////////////////////////////////////*/
if ( ! function_exists( 'gtm_maybe_json' ) ) {
function gtm_maybe_json( $string ) {
$parsed = json_decode( $string, true );
if ( JSON_ERROR_NONE === json_last_error() ) {
return $parsed;
}
return $string;
}
}
/*//////////////////////////////////////
// Get sidebar choice
//////////////////////////////////////*/
if ( ! function_exists( 'gtm_get_sidebar_choices' ) ) {
function gtm_get_sidebar_choices( $add_none_choice = false ) {
global $wp_registered_sidebars;
$sidebars = $wp_registered_sidebars;
$choices = [];
if ( $add_none_choice ) {
$choices = [
'' => '',
];
}
if ( ! empty( $sidebars ) && ! is_wp_error( $sidebars ) ) {
foreach ( $sidebars as $sidebar ) {
$choices[$sidebar['id']] = $sidebar['name'];
}
}
return apply_filters( 'gtm_get_sidebar_choices', $choices, $add_none_choice );
}
}
/*//////////////////////////////////////
// Get image sizes choices
//////////////////////////////////////*/
if ( ! function_exists( 'gtm_get_image_size_options' ) ) {
function gtm_get_image_size_options( $add_default_choice = false ) {
if ( function_exists( 'wp_get_additional_image_sizes' ) ) {
$registered_sizes = wp_get_additional_image_sizes(); // WP 4.7+
} else {
global $_wp_additional_image_sizes;
$registered_sizes = $_wp_additional_image_sizes;
}
$choices = [];
if ( $add_default_choice ) {
$choices = [
'' => esc_html__( 'Default', 'gutenmate' ),
];
}
$choices['full'] = esc_html__( 'Full size', 'gutenmate' );
foreach ( get_intermediate_image_sizes() as $size ) {
if ( in_array( $size, ['thumbnail', 'medium', 'medium_large', 'large'] ) ) {
$w = get_option( "{$size}_size_w" ) ?: esc_html__( 'auto', 'gutenmate' );
$h = get_option( "{$size}_size_h" ) ?: esc_html__( 'auto', 'gutenmate' );
$choices[$size] = sprintf( '%s - %s x %s', ucfirst( $size ), $w, $h );
} else {
$w = $registered_sizes[$size]['width'] ?: esc_html__( 'auto', 'gutenmate' );
$h = $registered_sizes[$size]['height'] ?: esc_html__( 'auto', 'gutenmate' );
$choices[$size] = sprintf( '%s - %s x %s', ucfirst( $size ), $w, $h );
}
}
return $choices;
}
}
/*//////////////////////////////////////
// CSS Class Functions
//////////////////////////////////////*/
if ( ! function_exists( 'gtm_class' ) ) {
function gtm_class( $classes ) {
if ( is_array( $classes ) ) {
$theclass = implode( ' ', $classes );
} else {
$theclass = $classes;
}
return $theclass . ' ';
}
}
if ( ! function_exists( 'gtm_the_class' ) ) {
function gtm_the_class( $classes ) {
echo esc_attr( ' ' . gtm_class( $classes ) . ' ' );
}
}
/*//////////////////////////////////////
// Inline Style Functions
//////////////////////////////////////*/
if ( ! function_exists( 'gtm_css' ) ) {
function gtm_css( $properties, $selectors = [] ) {
$output = '';
if ( empty( $properties ) ) {
return;
}
if ( ! empty( $selectors ) ) {
if ( ! is_array( $selectors ) ) {
$selectors = [$selectors];
}
$output .= wp_strip_all_tags( implode( ',', $selectors ) ) . ' { ';
}
foreach ( $properties as $prop => $val ) {
if ( is_numeric( $prop ) ) { // Support custom css
$output .= wp_strip_all_tags( $val ) . ';';
} else if ( $val !== '' && $val !== false && ! is_null( $val ) ) {
$output .= sprintf( ' %s: %s;', wp_strip_all_tags( $prop ), wp_strip_all_tags( $val ) );
}
}
if ( ! empty( $selectors ) ) {
$output .= ' } ';
}
return $output; // WPCS: XSS ok.
}
}
if ( ! function_exists( 'gtm_the_css' ) ) {
function gtm_the_css( $properties, $selectors, $echo = false ) {
if ( $echo ) {
// Print with style tag
echo '<style>';
echo gtm_css( $properties, $selectors );
echo '</style>';
} else {
wp_add_inline_style( 'gtm-custom-css', gtm_css( $properties, $selectors ) );
}
}
}
/*//////////////////////////////////////
// Setup handle for custom css
// Usage : wp_add_inline_style( 'gtm-custom-css', '.selector { color: white; }' )
//////////////////////////////////////*/
if ( ! function_exists( 'gtm_add_custom_css_handle' ) ) {
add_action( 'wp_enqueue_scripts', 'gtm_add_custom_css_handle', 9 );
add_action( 'admin_enqueue_scripts', 'gtm_add_custom_css_handle', 9 );
add_action( 'enqueue_block_editor_assets', 'gtm_add_custom_css_handle', 9 );
function gtm_add_custom_css_handle() {
if ( ! wp_style_is( 'gtm-custom-css', 'registered' ) ) {
wp_register_style( 'gtm-custom-css', false ); // False for dummy handle
}
}
}
if ( ! function_exists( 'gtm_setup_custom_css' ) ) {
add_action( 'wp_head', 'gtm_setup_custom_css' );
add_action( 'admin_head', 'gtm_setup_custom_css' );
function gtm_setup_custom_css() {
if ( ! wp_style_is( 'gtm-custom-css', 'enqueued' ) ) {
wp_enqueue_style( 'gtm-custom-css' );
}
}
}
/*//////////////////////////////////////
// Data Functions
//////////////////////////////////////*/
if ( ! function_exists( 'gtm_data' ) ) {
function gtm_data( $name, $value = '' ) {
if ( is_array( $name ) ) {
$data_string = '';
foreach ( $name as $name2 => $value2 ) {
$data_string .= gtm_data( $name2, $value2 );
}
return $data_string;
} else {
return ' data-' . esc_attr( $name ) . '="' . esc_attr( $value ) . '" '; // WPCS: XSS ok.
}
}
}
if ( ! function_exists( 'gtm_the_data' ) ) {
function gtm_the_data( $name, $value = '' ) {
echo gtm_data( $name, $value ); // WPCS: XSS ok.
}
}
/*//////////////////////////////////////
// Number prefixs
//////////////////////////////////////*/
if ( ! function_exists( 'gtm_number_prefixes' ) ) {
function gtm_number_prefixes( $number ) {
if ( $number > 1000000000 ) {
return number_format_i18n( round( ( $number / 1000000000 ), 1 ) ) . 'B';
} elseif ( $number > 1000000 ) {
return number_format_i18n( round( ( $number / 1000000 ), 1 ) ) . 'M';
} elseif ( $number > 1000 ) {
return number_format_i18n( round( ( $number / 1000 ), 1 ) ) . 'K';
} else {
return number_format_i18n( intval( $number ) );
}
}
}
/*//////////////////////////////////////
// Get theme support with default value
//////////////////////////////////////*/
if ( ! function_exists( 'gtm_theme_support' ) ) {
function gtm_theme_support( $feature, $defaults = [] ) {
$supported = get_theme_support( $feature );
if ( isset( $supported[0] ) ) {
return wp_parse_args( $supported[0], $defaults );
} else {
return $defaults;
}
}
}
/*//////////////////////////////////////
// Unit value for render css
//////////////////////////////////////*/
if ( ! function_exists( 'gtm_unit_val' ) ) {
function gtm_unit_val( $text, $default_unit = 'px' ) {
$text = trim( $text );
if ( preg_match( '/^[0-9.]+(%|px|em|vw|vh)$/', $text ) ) {
return $text;
} else if ( is_numeric( $text ) ) {
return $text . $default_unit;
}
return $text;
}
}
/*//////////////////////////////////////
// Admin Warning
//////////////////////////////////////*/
if ( ! function_exists( 'gtm_admin_warning' ) ) {
function gtm_admin_warning( $message ) {
echo gtm_get_admin_warning( $message );
}
}
if ( ! function_exists( 'gtm_get_admin_warning' ) ) {
function gtm_get_admin_warning( $message ) {
if ( gtm_current_user_can_edit() ) {
return sprintf( '<div class="gtm-admin-warning">⚠️ %s</div>', wp_kses_post( $message ) );
}
}
}
/*//////////////////////////////////////
// Get block support
//////////////////////////////////////*/
function gtm_get_block_support( $block_type, $feature, $default = false ) {
$block_support = $default;
// Retrieve block type if it is block name
if ( is_string( $block_type ) ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_type );
}
if ( $block_type && property_exists( $block_type, 'supports' ) ) {
$block_support = _wp_array_get( $block_type->supports, $feature, $default );
}
return $block_support;
}
/*//////////////////////////////////////
// Generate web font url
//////////////////////////////////////*/
function gtm_generate_web_font_handle( $family, $weight = '400' ) {
if ( is_array( $weight ) ) {
$weight = implode( '-', $weight );
}
return sanitize_title( "gtm-font-{$family}-{$weight}" );
}
function gtm_generate_web_font_url( $family, $weights = '400' ) {
if ( ! is_array( $weights ) ) {
$weights = [$weights];
}
$weight_string = 'ital,wght@' . implode( ';', $weights );
// Convert weight into format ital,wght
$weight_string = preg_replace( '|([0-9]+)|', '0,\1', $weight_string );
// Fix italic value
$weight_string = preg_replace( '|0,([0-9]+)i|', '1,\1', $weight_string );
return "https://fonts.googleapis.com/css2?family={$family}:{$weight_string}&display=swap";
}
/*//////////////////////////////////////
// String check
//////////////////////////////////////*/
function gtm_startsWith( $mystring, $findme ) {
if ( function_exists( 'str_starts_with' ) ) {
return str_starts_with( $mystring, $findme );
} else {
return substr_compare( $mystring, $findme, 0, strlen( $findme ) ) === 0;
}
}
function gtm_endsWith( $mystring, $findme ) {
if ( function_exists( 'str_ends_with' ) ) {
return str_ends_with( $mystring, $findme );
} else {
return substr_compare( $mystring, $findme, -strlen( $findme ) ) === 0;
}
}
/*//////////////////////////////////////
// Case convert
//////////////////////////////////////*/
/* Kebab stirng to camel string */
function gtm_to_camel( $string, $capitalizeFirstCharacter = false ) {
$str = str_replace( '-', '', ucwords( $string, '-' ) );
if ( ! $capitalizeFirstCharacter ) {
$str = lcfirst( $str );
}
return $str;
}
/* Kebab keys to camel keys */
function gtm_keys_to_camel( $arr ) {
$out = [];
foreach ( $arr as $key => $val ) {
if ( ! is_numeric( $key ) ) {
$out[gtm_to_camel( $key )] = $val;
} else {
$out[$key] = $val;
}
}
return $out;
}
/*//////////////////////////////////////
// Check block editor screen
//////////////////////////////////////*/
function gtm_is_block_editor_screen() {
// get_current_screen is available only backend
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false;
return is_object( $screen ) && property_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor;
}
/*//////////////////////////////////////
// Generate html attributes from array
//////////////////////////////////////*/
function gtm_html_attributes( $attrs ) {
$html = '';
foreach ( $attrs as $key => $val ) {
$html .= sprintf( ' %s="%s"', esc_html( $key ), esc_attr( $val ) );
}
return $html;
}
/*//////////////////////////////////////
// Register block type with styles
//////////////////////////////////////*/
function gtm_register_block_type( $slug, $args = [] ) {
$block = "build/block-library/{$slug}/";
wp_register_style( "gtm-{$slug}-style", GTM_URL . $block . 'style.css', ['gtm-block-style'], GTM_VERSION );
wp_style_add_data( "gtm-{$slug}-style", 'rtl', 'replace' );
wp_register_style( "gtm-{$slug}-editor-style", GTM_URL . $block . 'editor.css', [], GTM_VERSION );
wp_style_add_data( "gtm-{$slug}-editor-style", 'rtl', 'replace' );
register_block_type( GTM_DIR . $block, $args );
}
/*//////////////////////////////////////
// Check the current user is able to edit content
//////////////////////////////////////*/
function gtm_current_user_can_edit() {
return current_user_can( 'editor' ) || current_user_can( 'administrator' );
}
/*//////////////////////////////////////
// Generate global id
//////////////////////////////////////*/
function gtm_id( $scope = '' ) {
static $counter = [];
if ( empty( $scope ) ) {
$scope = 'gtm-id';
}
if ( empty( $counter[$scope] ) ) {
$counter[$scope] = 0;
}
return $scope . '-' . ( ++$counter[$scope] );
}
/*//////////////////////////////////////
// Replace the first occurrence stirng
//////////////////////////////////////*/
function gtm_str_replace_first( $search, $replace, $subject ) {
$pos = strpos( $subject, $search );
if ( $pos !== false ) {
return substr_replace( $subject, $replace, $pos, strlen( $search ) );
}
return $subject;
}
function gtm_get_post_format_icon( $post = null ) {
$format = get_post_format( $post ) ?: 'standard';
$icons = get_option( 'gtm_post_format_icons', [] );
if ( ! empty( $icons[$format] ) ) {
return $icons[$format];
} else if ( $format == 'video' ) {
// Defualt icon for video
return 'gtm-basic-icon-play';
} else if ( $format == 'audio' ) {
// Defualt icon for video
return 'gtm-basic-icon-headphones';
}
}
/*//////////////////////////////////////
// Update option proxy
// To support boolean value from this bug
// https://core.trac.wordpress.org/ticket/40007
//////////////////////////////////////*/
function gtm_update_option( $option, $value, $autoload = null ) {
// Force transform value to INT if it's false
if ( $value === false ) {
$value = 0;
}
$is_updated = update_option( $option, $value, $autoload );
}
/*//////////////////////////////////////
// Check WooCommerce pluing is activated
//////////////////////////////////////*/
function gtm_has_woocommerce() {
return class_exists( 'WooCommerce' );
}
/**
* Convert uri to local path
*
* @param [type] $url
* @return void
*/
function gtm_url_to_local_path( $url ) {
return str_replace( get_site_url(), ABSPATH, $url );
}
/**
* Find item in array that have $field === $value
*
* @param [type] $array
* @param [type] $field
* @param [type] $value
* @return void
*/
function gtm_find_array_item( $array, $field, $value ) {
if ( ! empty( $array ) ) {
foreach ( $array as $item ) {
if ( isset( $item[$field] ) && $item[$field] === $value ) {
return $item;
}
}
}
}
/**
* Translate aria-label in block content
*
* @param [type] $content
* @param [type] $label
* @return void
*/
function gtm_translate_aria_label( $content, $label ) {
$content = preg_replace( '|aria-label="[^"]*"|i', "aria-label=\"$label\"", $content );
return $content;
}
/**
* To get the current request url
*/
function gtm_get_current_url() {
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
return $current_url;
}