query("DELETE FROM $wpdb->options WHERE `option_name` LIKE ('_transient_tf_menu_%')");
return Themify_Storage::deleteByPrefix('tf_menu_');
}
/**
* Image Helper - Echoes themify_get_image
* @param string $args Format string.
*/
function themify_image($args) {//deprecated use themify_get_image
echo themify_get_image($args);
}
function themify_find_nearest_image_size(?int $attachment_id,int $width,int $height ) : string {
$image_meta = wp_get_attachment_metadata( $attachment_id );
$size = '';
if ( ! empty( $image_meta['sizes'] ) ) {
$last_pixel_count = 0;
foreach ( $image_meta['sizes'] as $key => $value ) {
/* image size is smaller than what we need, skip */
if ( $width > $value['width'] || $height > $value['height'] ) {
continue;
}
/* find smallest size */
$pixels = $value['width'] * $value['height'];
if ( $last_pixel_count === 0 || $pixels < $last_pixel_count ) {
$last_pixel_count = $pixels;
$size = $key;
}
}
}
return $size;
}
/**
* Returns the post image, either from Themify Custom Panel fields or from WordPress Featured Image.
* @param string|array $args Format string.
* @return string String with
tag and optional content prepended and/or appended
*/
function themify_get_image($args):string {
global $themify;
/**
* List of parameters
* @var array
*/
$defaults = array(
'src' => '',
'class' => '',
'style' => '',
'preload' => false,
'prefetch' => false,
'lazy_load' => true,
'is_slider' => false,
'disable_responsive' => false,
'w' => '', // width
'h' => '', // height
'before' => '',
'after' => '',
'alt' => '',
'alt_fallback' => true,
'title' => '',
'crop' => true,
'field_name' => 'post_image,image,wp_thumb,feature_image',
'urlonly' => false,
'image_size' => '',
'image_meta' => false,
'f_image' => false,
'attr' => array(),
'fallback' => ''//when there is no image
);
if (is_string($args)) {
$arr = array();
parse_str($args, $arr);
$args = array();
foreach ($arr as $k => $v) {
if ($v === 'true') {
$args[$k] = true;
} elseif ($v === 'false') {
$args[$k] = false;
} else {
$args[$k] = $v;
}
}
}
$args+=$defaults;
unset($defaults);
/**
* Post ID for single, query or archive views.
* Page ID is stored separately in $themify->page_id.
* @var string
*/
$post_id = get_the_ID();
/**
* URL of the image to use
* @var string
*/
$img_url = '';
/**
* Image width
* @var string
*/
$width = $args['w'];
if ($width !== '') {
$width = (int) $width;
}
/**
* Image height
* @var string
*/
$height = $args['h'];
if ($height !== '') {
$height = (int) $height;
}
$attachment_id = 0;
$is_disabled = themify_is_image_script_disabled();
if (is_numeric($args['src'])) {
$attachment_id = $args['src'];
$img = wp_get_attachment_image_src($attachment_id, 'large');
$args['src'] = !empty($img[0]) ? $img[0] : '';
unset($img);
} elseif ($args['fallback'] !== '' && empty($args['src']) && !has_post_thumbnail()) {
$args['src'] = $args['fallback'];
}
if ($is_disabled === true) { // Use WP standard image sizes
if (!empty($args['image_size'])) { // If image_size parameter is set
$feature_size = $args['image_size'];
} elseif (!empty($themify->image_size)) { // or if Themify::image_size is set
$feature_size = $themify->image_size;
} elseif (empty($themify->is_shortcode) && in_the_loop()) { // Main query area
if (is_single()) {
$feature_size = get_post_meta($post_id, 'feature_size', true);
if (empty($feature_size) || 'blank' === $feature_size) {
$feature_size = themify_get('setting-image_post_single_feature_size', null, true);
}
} elseif (!empty($themify->page_id) && !empty($themify->query_post_type) && themify_is_query_page()) {
$feature_size = get_post_meta($themify->page_id, $themify->query_post_type . 'feature_size_page', true);
if (empty($feature_size)) {
$feature_size = null;
}
} elseif (is_archive() || is_tax() || is_search() || is_home()) {
$feature_size = themify_get('setting-image_post_feature_size', null, true);
}
}
if (!empty($args['src'])) {
$attachment_id = themify_get_attachment_id_from_url($args['src']);
}
if (!isset($feature_size) || 'blank' === $feature_size) {
$feature_size = $attachment_id && ! empty( $width ) && ! empty( $height ) ? themify_find_nearest_image_size( $attachment_id, $width, $height ) : '';
if ( $feature_size === '' ) {
$feature_size = apply_filters('themify_global_feature_size', themify_get('setting-global_feature_size', 'large', true));
}
}
if (!empty($attachment_id)) {
$tmp = wp_get_attachment_image_src($attachment_id, $feature_size);
if (!empty($tmp)) {
$img_url = $tmp[0];
}
unset($tmp);
}
if ($img_url === '') {
// Set URL to use for final output.
$img_url = empty($args['src']) ? themify_image_url(false, $feature_size) : $args['src'];
$img_url = trim($img_url);
// Fix for showing feature_image when Themify Image Script is disabled
if ('' === $img_url) {
foreach (explode(',', $args['field_name']) as $field) {
if ($img_url = get_post_meta($post_id, trim($field), true))
break;
}
}
}
} else { // Use Image Script
if (empty($args['src'])) {
if (has_post_thumbnail()) {
$img_url = $width !== '' && $height !== '' ? ((int) get_post_thumbnail_id()) : get_the_post_thumbnail_url(); /* Image script works with thumbnail IDs as well as URLs, use ID which is faster */
} elseif ('attachment' === get_post_type()) {
$img_url = wp_get_attachment_url($post_id);
} else {
foreach (explode(',', $args['field_name']) as $field) {
if ($img_url = get_post_meta($post_id, trim($field), true)) {
break;
}
}
}
} else {
$img_url = $args['src'];
}
if ($height !== '' && 0 === ((int) $height )) {
$height = 0;
$args['crop'] = false;
}
/** filter $img_url before it goes off to themify_do_img for processing * */
$img_url = apply_filters('themify_get_image_before_do_img', $img_url, $width, $height, $args);
// Set URL to use for final output.
$temp = themify_do_img((empty($attachment_id) ? $img_url : $attachment_id), $width, $height, (bool) $args['crop']);
$img_url = $temp['url'];
if ($temp['width'] !== '' && $temp['height'] !== '') {
$width = (int) $temp['width'];
$height = (int) $temp['height'];
}
// Get title/alt text by attachment id if it was returned.
if (!$attachment_id && isset($temp['attachment_id'])) {
$attachment_id = $temp['attachment_id'];
}
}
if ($attachment_id) {
$attachment_id = themify_maybe_translate_object_id($attachment_id);
}
// No image was defined, parse content to find the first image.
if (empty($img_url) && ($args['f_image'] || themify_check('setting-auto_featured_image', true) )) {
$content = get_the_content();
$upload_dir = themify_upload_dir('baseurl');
foreach ( [ 'img', 'iframe' ] as $tag) {
$count = substr_count($content, '<' . $tag);
if ($count >= 1) {
$start = strpos($content, '<' . $tag, 0);
$pos = substr($content, $start);
$end = strpos($pos, '>');
$temp = themify_prep_image(substr($pos, 0, $end + 1));
$src = $temp['src'];
$parse = parse_url($src);
if (!empty($parse['query'])) {
$src = str_replace('?' . $parse['query'], '', $src);
}
$auto_image_url = isset($temp['src']) ? $temp['src'] : '';
if (isset($temp['class'])) {
$args['class'] .= ' ' . $temp['class'];
}
$args['alt'] = $temp['alt'];
if ($is_disabled === true) {
$img_url = themify_image_url(false, $feature_size, themify_get_attachment_id_from_url($auto_image_url, $upload_dir));
if (empty($img_url)) {
$img_url = esc_url($auto_image_url);
}
} elseif ($temp = themify_do_img($auto_image_url, $width, $height, (bool) $args['crop'])) {
$img_url = $temp['url'];
}
break;
}
}
unset($content, $upload_dir);
}
if (!empty($img_url)) {
if (isset($temp['is_large']) && current_user_can('edit_post', $post_id)) {
$args['class'] .= ' tf_large_img';
} else {
if ($args['preload'] !== false || $args['prefetch'] !== false) {
Themify_Enqueue_Assets::addPreLoadMedia($img_url, $args['preload'] !== false ? 'preload' : 'prefetch');
}
themify_generateWebp($img_url);
}
unset($temp);
if ($args['urlonly']) {
$out = $img_url;
} else {
// Build final image
$out = '
post_title : '';
$p = null;
}
if (!$out_alt) {
$out_alt = the_title_attribute('echo=0');
}
$title = $out_alt;
}
}
if($args['title'] !== false ){
if ($title === '') {
if (!empty($args['title'])) {
$title = $args['title'];
} elseif ($attachment_id) {
$p = get_post($attachment_id);
if (!empty($p)) {
$title = $p->post_title;
}
$p = null;
}
}
// Add title attribute only if explicitly set in $args
if (!empty($title)) {
$out .= ' title="' . esc_attr($title) . '"';
}
}
if ($args['lazy_load'] === false || $args['lazy_load'] === 'eager') {
$out .= ' data-tf-not-load="1"';
if ($args['lazy_load'] === 'eager') {
$out .= ' loading="eager" decoding="auto"';
}
}
if (!empty($args['attr'])) {
foreach ($args['attr'] as $k => $v) {
$out .= ' ' . $k . '="' . esc_attr($v) . '"';
}
}
$out .= ' alt="' . esc_attr( (string) $out_alt ) . '"';
$out .= '>';
if ($attachment_id && $args['disable_responsive'] === false) {
$out = function_exists('wp_filter_content_tags') ? wp_img_tag_add_srcset_and_sizes_attr($out, null, $attachment_id) // WP 5.5
: wp_make_content_images_responsive($out);
}
if (($args['is_slider'] === true || $themify->post_layout === 'slider') && $args['lazy_load'] === true) {
$out = themify_make_lazy($out, false);
}
if ($args['image_meta'] == true) {
$out .= "" . $out;
}
$out = $args['before'] . $out . $args['after'];
}
} else {
$out = '';
}
return $out;
}
if (!function_exists('themify_edit_link')) {
function themify_edit_link(string $title = '') : bool {
static $is = null;
if ($is === null) {
$is = is_user_logged_in() && (!class_exists('Themify_Builder_Model',false) || !Themify_Builder::$frontedit_active === true) && !themify_is_rest();
}
if ($is === true) {
if ($title === '') {
$title = sprintf( __('Edit %s', 'themify'), '' . get_post_type_object( get_post_type() )->labels->singular_name . '' );
}
edit_post_link($title, '', '');
}
return $is;
}
}
if (!function_exists('themify_image_url')) {
/**
* Returns the featured image url
* @param bool $echo Specify to echo or return the url
* @param string $size The image size to return
* @param null|int $attachment_id ID of image to load.
* @return void|string
*/
function themify_image_url(bool $echo = false, string $size = 'full', $attachment_id = null):string {
$url = '';
if (has_post_thumbnail()) {
$image = wp_get_attachment_image_src(get_post_thumbnail_id(), $size);
$url = $image === false ? '' : $image[0];
} elseif ($attachment_id !== null) {
$image = wp_get_attachment_image_src($attachment_id, $size);
if ($image) {
$url = $image[0];
}
}
$url = apply_filters('themify_image_url', $url);
if ($echo) {
echo esc_url($url);
return '';
}
return $url;
}
}
/**
* Image Helper - Prep Image
* @param $tag
* @return array
*/
function themify_prep_image(string $tag):array {
$image = [];
preg_match_all( '/(alt|title|src|class)=(("|\')[^("|\')]*("|\'))/i', $tag, $image_reg );
foreach ( $image_reg[1] as $index => $attribute ) {
$image[ $attribute ] = substr( $image_reg[2][ $index ], 1, -1 );
}
$image += [ 'src' => '', 'alt' => '', 'title' => '' ];
if (strpos($image['src'], 'youtube.com') !== false || strpos($image['src'], 'vimeo.com') !== false) {
$image['src'] = themify_video_image($image['src']);
}
$image['src'] = preg_replace('/(-\d+x\d+)(?=\.\w{3,4})/', '', $image['src']);
return $image;
}
/**
* Vimeo / Youtube Thumbnail grab
*
* @param $url
*
* @return string
*/
function themify_video_image(string $url):string {
$image_url = parse_url($url);
$return_url = '';
if ($image_url['host'] === 'www.youtube.com' || $image_url['host'] === 'youtube.com') {
parse_str($image_url['query'], $query);
if (!empty($query['v'])) {
$id = $query['v'];
} else {
$path = explode('/', $image_url['path']);
$id = $path[count($path) - 1];
}
$return_url = 'https://img.youtube.com/vi/' . $id . '/hqdefault.jpg';
} elseif ($image_url['host'] === 'www.vimeo.com' || $image_url['host'] === 'vimeo.com' || $image_url['host'] === 'player.vimeo.com') {
parse_str($image_url['query'], $query);
if (!empty($query['clip_id'])) {
$id = $query['clip_id'];
} else {
$path = explode('/', $image_url['path']);
$id = $path[(count($path) - 1)];
}
$content = Themify_Filesystem::get_contents('https://vimeo.com/api/v2/video/' . $id . '.php');
if (!empty($content)) {
$hash = themify_maybe_unserialize( $content );
if ( ! is_array( $hash ) ) {
$hash = array();
}
if (!empty($hash[0])) {
$return_url = $hash[0]["thumbnail_large"];
}
}
}
return $return_url;
}
/**
* Checks if a value referenced by $var exists in theme settings or post meta data.
* @param $var
* @return bool
*/
function themify_check(string $var, bool $data_only = false):bool {
$val = themify_get($var, null, $data_only);
return $val !== null && $val !== '' && $val !== 'off';
}
/**
* Returns a value referenced by $var checking in theme settings or post meta data.
* @param $var
* @return mixed
*/
function themify_get(string $var, $default = null, bool $data_only = false) {
$data = themify_get_data();
if (isset($data[$var]) && $data[$var] !== '') {
return $data[$var];
} elseif ($data_only !== true) {
global $post;
if (themify_is_shop() && !in_the_loop()) {
$id = themify_shop_pageId();
} elseif (is_object($post)) {
$id = $post->ID;
} else {
$id = false;
}
$val = ( $id !== false && $id !== 0 ) ? get_post_meta($id, $var, true) : '';
if ($val !== '') {
return $val;
}
}
return $default;
}
/**
* Returns a value referenced by $meta,$var checking in theme settings or post meta data.
* @param $meta - post meta
* @param $var - theme settings
* @param $default
* @return mixed
*/
function themify_get_both(string $meta, string $var, $default = null) {
$val = themify_get($meta);
if ($val === null || $val === 'default') {
$val = themify_get($var, $default, true);
}
return $val;
}
/**
* Get a color value, uses themify_get and sanitizes the value
*
* @return string
*/
function themify_get_color_both(string $meta, string $var, $default = null) {
$val = themify_get_color($meta);
if ($val === null || $val === 'default') {
$val = themify_get_color($var, $default, true);
}
return $val;
}
/**
* Returns a value referenced by $meta,$var checking in theme settings or post meta data.
* @param $meta - post meta
* @param $var - theme settings
* @return mixed
*/
function themify_check_both(string $meta, string $var):bool {
$val = themify_get($meta, null);
if ($val === null || $val === 'default') {
$val = themify_get($var, null, true);
}
return $val !== null && $val !== '' && $val !== 'off';
}
function themify_shop_pageId() {
static $id = null;
if ($id === null) {
if (themify_is_woocommerce_active() && function_exists( 'wc_get_page_id' ) ) {
$id = wc_get_page_id('shop');
if ($id <= 0) {//wc bug, page id isn't from wc settings,the default should be page with slug 'shop'
$page = get_page_by_path('shop');
$id = !empty($page) ? (int) $page->ID : false;
}
} else {
$id = false;
}
}
return $id;
}
/**
* Get a color value, uses themify_get and sanitizes the value
*
* @return string
*/
function themify_get_color(string $var, $default = null, bool $data_only = false) {
$value = themify_get($var, $default, $data_only);
if ( $value === null || false === $value || '' === $value ) {
return $value;
}
$value = trim( (string) $value );
if ( '' === $value ) {
return $value;
}
if ( 0 === stripos( $value, 'var(' ) ) {
return $value;
}
if ( 0 === strpos( $value, '--' ) ) {
return 'var(' . $value . ')';
}
return themify_sanitize_hex_color( $value );
}
/**
* Sanitize a string to ensure it's valid hex color code
*
* @since 3.1.2
*/
function themify_sanitize_hex_color($value) {
if ($value===null || false === $value) {
return $value;
}
$value = ltrim($value, '#');
/* match 3 to 6 hex digits */
if (preg_match('|^([A-Fa-f0-9]{3}){1,2}$|', $value)) {
$value = '#' . $value;
}
return $value;
}
/**
* Sanitize space-separated CSS class names for safe HTML class attributes.
*
* @since 7.0
*/
function themify_sanitize_css_classes( $classes ) {
if ( $classes === '' || $classes === null ) {
return '';
}
if ( ! is_string( $classes ) ) {
return '';
}
$parts = preg_split( '/\s+/', trim( $classes ) );
$sanitized = array();
foreach ( $parts as $part ) {
if ( $part === '' ) {
continue;
}
$clean = sanitize_html_class( $part );
if ( $clean !== '' ) {
$sanitized[] = $clean;
}
}
return implode( ' ', $sanitized );
}
/**
* Sanitize a CSS length value with an allowed unit.
*
* @since 7.0
*/
function themify_sanitize_css_size( $value, $unit = 'px' ) {
$value = preg_replace( '/[^0-9.]/', '', (string) $value );
if ( $value === '' ) {
return '';
}
$allowed_units = array( 'px', '%', 'em', 'rem', 'vw', 'vh', 'vmin', 'vmax' );
if ( ! in_array( $unit, $allowed_units, true ) ) {
$unit = 'px';
}
return $value . $unit;
}
/**
* Sanitize a CSS unit token.
*
* @since 7.0
*/
function themify_sanitize_css_unit( $unit, $default = 'px' ) {
$allowed_units = array( 'px', '%', 'em', 'rem', 'vw', 'vh', 'vmin', 'vmax' );
$unit = strtolower( trim( (string) $unit ) );
if ( ! in_array( $unit, $allowed_units, true ) ) {
$unit = in_array( $default, $allowed_units, true ) ? $default : 'px';
}
return $unit;
}
/**
* Sanitize slider height option for data-height attributes.
*
* @since 7.0
*/
function themify_sanitize_slider_height( $value ) {
$value = (string) $value;
if ( in_array( $value, array( 'variable', 'auto', '' ), true ) ) {
return $value;
}
if ( preg_match( '/^([0-9.]+)(px|%|em|rem|vw|vh|vmin|vmax)$/i', $value, $matches ) ) {
return themify_sanitize_css_size( $matches[1], strtolower( $matches[2] ) );
}
return 'variable';
}
/**
* Sanitize CSS border-style values.
*
* @since 7.0
*/
function themify_sanitize_border_style( $value, $default = 'solid' ) {
$allowed = array( 'solid', 'dashed', 'dotted', 'double', 'none' );
$value = strtolower( trim( (string) $value ) );
return in_array( $value, $allowed, true ) ? $value : $default;
}
/**
* Sanitize inline CSS declarations for style attributes.
*
* @since 7.0
*/
function themify_sanitize_inline_css( $css ) {
if ( $css === '' || $css === null ) {
return '';
}
$css = wp_strip_all_tags( (string) $css );
$css = preg_replace( '/expression\s*\(/i', '', $css );
$css = preg_replace( '/javascript\s*:/i', '', $css );
$css = preg_replace( '/@import/i', '', $css );
$css = preg_replace( '/<\/?style/i', '', $css );
return trim( $css );
}
/**
* Sanitize builder custom CSS before writing to a stylesheet file.
*
* @since 7.0
*/
function themify_sanitize_builder_custom_css( $css ) {
if ( ! is_string( $css ) || $css === '' ) {
return '';
}
$css = preg_replace( '/<\/style/i', '', $css );
$css = preg_replace( '/