slug = $slug; $this->prefix = sprintf('_%1$s_', $this->slug); } function get_code($code='') { $format = empty($code) ? '%1$s' : '%1$s-%2$s'; return sprintf($format, $this->slug, $code); } function get_prefix() { return $this->prefix;} function get_metakey($fld) { return $this->prefix . $fld;} function get_home_meta_key() { return $this->get_metakey('home_meta'); } function get_post_meta_key() { return $this->get_metakey('post_meta'); } function get_term_meta_key() { return $this->get_metakey('term_meta'); } function get_user_meta_key() { return $this->get_metakey('user_meta'); } function is_html5() { if ($this->is_html5 == null) $this->is_html5 = function_exists('current_theme_supports') && current_theme_supports('html5'); return $this->is_html5; } function is_yoast_installed() { return defined('WPSEO_VERSION'); } function is_seo_framework_installed() { return defined('THE_SEO_FRAMEWORK_VERSION'); } function get_current_term() { if (is_tax() || is_category() || is_tag()) { if (is_category()) $term = get_term_by('slug',get_query_var('category_name'),'category') ; elseif (is_tag()) $term = get_term_by('slug',get_query_var('tag'),'post_tag') ; else { if ($obj = get_queried_object()) $term = get_term_by('slug', $obj->slug, $obj->taxonomy) ; else $term = false; } } else { $term = false; } return $term; } function get_term_id() { if (is_archive() && ($term = $this->get_current_term())) return $term->term_id; else return false; } function get_post_id() { global $post; if (is_object($post) && property_exists($post, 'ID') && ($post_id = $post->ID)) return $post_id ; else return false; } function get_meta($type, $id, $key = false, $result= false) { switch ($type) { case 'home': return $this->get_home_meta($key, $result); break; case 'term': return $this->get_term_meta($id, $key, $result); break; case 'user': return $this->get_user_meta($id, $key, $result); break; case 'post': default: return $this->get_post_meta($id, $key, $result); break; } return $result; } function update_meta( $type = 'post', $id = false, $metakey, $vals, $defaults = false) { if (!$defaults) $defaults = array(); if (is_array($vals)) { foreach ($vals as $k => $v) if (!is_array($v)) $vals[$k] = stripslashes(trim($v)); $vals = @serialize(wp_parse_args($vals, $defaults)); } else { $vals = stripslashes(trim($vals)); } switch ($type) { case 'home': return $this->update_home_meta( $metakey, $vals ); break; case 'term': return $this->update_term_meta( $id, $metakey, $vals ); break; case 'user': return $this->update_user_meta( $id, $metakey, $vals ); break; case 'post': default: return $this->update_post_meta( $id, $metakey, $vals ); break; } } function delete_meta( $type = 'post', $id = false, $metakey) { switch ($type) { case 'home': return $this->delete_home_meta( $metakey ); break; case 'term': return $this->delete_term_meta( $id, $metakey ); break; case 'user': return $this->delete_user_meta( $id, $metakey); break; case 'post': default: return $this->delete_post_meta( $id, $metakey ); break; } } function get_home_meta( $key = false, $result = array() ) { if ($meta = get_option($this->get_home_meta_key())) if ($key && ($key != $this->get_home_meta_key())) return isset($meta[$key]) ? (is_serialized($meta[$key]) ? @unserialize($meta[$key]) : $meta[$key]) : $result; else return $meta; else return $result; } function update_home_meta( $key, $vals) { $meta = $this->get_home_meta(); if ($key && ($key != $this->get_home_meta_key())) $meta[$key] = $vals; else $meta = $vals; update_option($this->get_home_meta_key(), $meta); } function delete_home_meta( $key) { delete_option($this->get_home_meta_key()); } function get_post_meta ($post_id, $key= false, $result = false) { if (!$post_id) $post_id = $this->get_post_id(); if (!$key) $key = $this->get_post_meta_key(); if ($post_id && $key && ($meta = get_post_meta($post_id, $key, true)) && ($options = (is_serialized($meta) ? @unserialize($meta) : $meta)) && (is_array($options) || is_string($options))) return $options; else return $result; } function get_post_meta_value($post_id, $key) { return get_post_meta($post_id, $key, true); } function update_post_meta( $post_id, $key = false, $values = false) { if (!$post_id) $post_id = $this->get_post_id(); if (!$key) $key = $this->get_post_meta_key(); return update_post_meta( $post_id, $key, $values); } function delete_post_meta ($post_id, $key= false) { if (!$post_id) $post_id = $this->get_post_id(); if (!$key) $key = $this->get_post_meta_key(); if ($post_id && $key) return delete_post_meta($post_id, $key); else return false; } function get_term_meta( $term_id, $key= false, $result = false ) { if (!$term_id) $term_id = $this->get_term_id(); if (function_exists('get_term_meta')) { if (!$key) $key = $this->get_term_meta_key(); if ($vals = get_term_meta( $term_id, $key, true)) return maybe_unserialize($vals); } else { $meta = get_option($this->get_term_meta_key()); if (!$meta) return $result; if ($key && ($key != $this->get_term_meta_key()) ) { if (isset($meta[$term_id][$key])) return $meta[$term_id][$key]; } else { if (isset($meta[$term_id])) return $meta[$term_id]; } } return $result; } function update_term_meta( $term_id, $key = false, $values = false) { $default_metakey = $this->get_term_meta_key(); if (function_exists('update_term_meta')) { if (!$key) $key = $default_metakey; return update_term_meta( $term_id, $key, $values); } else { $meta = get_option($default_metakey); if (!$meta) $meta = array(); if ($key && ($key != $default_metakey)) $meta[$term_id][$key] = $values; else $meta[$term_id] = $values; update_option($default_metakey, $meta); } } function delete_term_meta( $term_id, $key = false) { $default_metakey = $this->get_term_meta_key(); if (function_exists('delete_term_meta')) { if (!$key) $key = $default_metakey; return delete_term_meta( $term_id, $key); } else { delete_option($default_metakey); } } function get_user_meta ($user_id, $key= false, $result = false) { if (!$key) $key = $this->get_user_meta_key(); if ($user_id && $key && ($meta = get_user_meta($user_id, $key, true)) && ($options = (is_serialized($meta) ? @unserialize($meta) : $meta)) && (is_array($options) || is_string($options))) return $options; else return $result; } function update_user_meta( $user_id, $key = false, $values = false) { if (!$key) $key = $this->get_user_meta_key(); return update_user_meta( $user_id, $key, $values); } function delete_user_meta( $user_id, $key = false) { if (!$key) $key = $this->get_user_meta_key(); return delete_user_meta( $user_id, $key); } function get_toggle_post_meta_key($action, $item) { return sprintf('%1$s%2$s_%3$s', $this->prefix, $action, $item ); } function post_has_shortcode($shortcode, $attribute = false) { global $wp_query; if (isset($wp_query) && isset($wp_query->post) && isset($wp_query->post->post_content) && function_exists('has_shortcode') && has_shortcode($wp_query->post->post_content, $shortcode)) if ($attribute) return strpos($wp_query->post->post_content, $attribute) !== FALSE ; else return true; else return false; } function overrides($defaults, $atts) { $overrides = array(); foreach ($defaults as $key => $value) if (isset($atts[$key]) && ($atts[$key] || ($atts[$key] === false))) $overrides[$key] = $atts[$key]; else $overrides[$key] = $value; return $overrides; } function clean_css_classes($classes) { $classes = str_replace(array('{', '}', '[', ']', '(', ')'), '', $classes); $classes = str_replace(array(',', ';', ':'), ' ', $classes); return trim($classes); } function json_encode($params) { //fix numerics and booleans $pat = '/(\")([0-9]+)(\")/'; $rep = '\\2'; return str_replace (array('"false"','"true"'), array('false','true'), preg_replace($pat, $rep, json_encode($params))); } function is_mobile_device() { if (function_exists('wp_is_mobile')) return wp_is_mobile(); else return preg_match("/wap.|.wap/i", $_SERVER["HTTP_ACCEPT"]) || preg_match("/iphone|ipad/i", $_SERVER["HTTP_USER_AGENT"]) || preg_match("/android/i", $_SERVER["HTTP_USER_AGENT"]); } function is_landing_page($page_template='') { if (empty($page_template) && ($post_id = $this->get_post_id())) $page_template = get_post_meta($post_id,'_wp_page_template',TRUE); if (empty($page_template)) return false; $landing_pages = (array) apply_filters('diy_landing_page_templates', array('page_landing.php')); return in_array($page_template, $landing_pages ); } function read_more_link($link_text='Read More', $class='', $spacer = '') { $classes = empty($class) ? '' : (' ' . $class); return sprintf('%1$s%4$s', $spacer, $classes, get_permalink(), $link_text); } function register_tooltip_styles() { wp_register_style('diy-tooltip', plugins_url('styles/tooltip.css',dirname(__FILE__)), array(), null); } function enqueue_tooltip_styles() { wp_enqueue_style('diy-tooltip'); wp_enqueue_style('dashicons'); } function selector($fld_id, $fld_name, $value, $options, $multiple = false) { $input = ''; if (is_array($options)) { foreach ($options as $optkey => $optlabel) $input .= sprintf('%3$s', $multiple ? selected(in_array($optkey, (array)$value), true, false) : selected($optkey, $value, false), $optkey, $optlabel); } else { $input = $options; } return sprintf('', $fld_id, $fld_name . ($multiple?'[]':''), $input, $multiple ? ' multiple="multiple"':''); } function form_field($fld_id, $fld_name, $label, $value, $type, $options = array(), $args = array(), $wrap = false) { if ($args) extract($args); $input = ''; $label = sprintf('', $fld_id, __($label)); switch ($type) { case 'number': case 'password': case 'range': case 'text': case 'url': $input .= sprintf(' %13$s', $type, $fld_id, $fld_name, $value, isset($readonly) ? (' readonly="'.$readonly.'"') : '', isset($size) ? (' size="'.$size.'"') : '', isset($maxlength) ? (' maxlength="'.$maxlength.'"') : '', isset($class) ? (' class="'.$class.'"') : '', isset($min) ? (' min="'.$min.'"') : '', isset($max) ? (' max="'.$max.'"') : '', isset($step) ? (' step="'.$step.'"') : '', isset($pattern) ? (' pattern="'.$pattern.'"') : '', isset($suffix) ? $suffix : ''); break; case 'file': $input .= sprintf('', $fld_id, $fld_name, $value, isset($size) ? ('size="'.$size.'"') : '', isset($maxlength) ? (' maxlength="'.$maxlength.'"') : '', isset($class) ? (' class="'.$class.'"') : ''); break; case 'textarea': $input .= sprintf('', $fld_id, $fld_name, isset($readonly) ? (' readonly="'.$readonly.'"') : '', isset($rows) ? (' rows="'.$rows.'"') : '', isset($cols) ? (' cols="'.$cols.'"') : '', isset($class) ? (' class="'.$class.'"') : '', stripslashes($value)); break; case 'checkbox': if (is_array($options) && (count($options) > 0)) { if (isset($legend)) $input .= sprintf('%1$s', $legend); if (!isset($separator)) $separator = ''; foreach ($options as $optkey => $optlabel) $input .= sprintf('%6$s', $fld_id, $fld_name, str_replace('\'','"',checked($optkey, $value, false)), $optkey, $optlabel, $separator); $input = sprintf('
%1$s
',$input); } else { $input .= sprintf('', $fld_id, $fld_name, checked($value, '1', false)); } break; case 'checkboxes': $values = (array) $value; $options = (array) $options; if (isset($legend)) $input .= sprintf('%1$s', $legend); foreach ($options as $optkey => $optlabel) $input .= sprintf('
  • ', $fld_id, $fld_name, in_array($optkey, $values) ? 'checked="checked"' : '', $optkey, $optlabel); $input = sprintf('
    ',$input, isset($class) ? (' '.$class) : ''); break; case 'keypairs': $keypairs = (array) $value; $i=0; $lines = array(); $spares = isset($args['spares']) ? $args['spares'] : 0; if (isset($legend)) $input .= sprintf('%1$s', $legend); foreach ($keypairs as $key => $value){ $i++; $lines[] = sprintf('
    ', $fld_id.'key'.$i, sprintf('%1$s[key][%2$s]', $fld_name, $i), $key, isset($size) ? (' size="'.$size.'"') : '', isset($maxlength) ? (' maxlength="'.$maxlength.'"') : '', isset($pattern) ? (' pattern="'.$pattern.'"') : '') . sprintf('
    ', $fld_id.'val'.$i, sprintf('%1$s[val][%2$s]', $fld_name, $i), isset($rows) ? (' rows="'.$rows.'"') : '', isset($cols) ? (' cols="'.$cols.'"') : '', isset($class) ? (' class="'.$class.'"') : '', stripslashes($value)); } for ($j = 1; $j <= $spares; $j++) { $r = $i+$j; $lines[] = sprintf('
    ', $fld_id.'key'.($r), sprintf('%1$s[key][%2$s]', $fld_name, $r), '', isset($size) ? (' size="'.$size.'"') : '', isset($maxlength) ? (' maxlength="'.$maxlength.'"') : '', isset($placeholder) ? (' placeholder="'.$placeholder.'"') : '', isset($pattern) ? (' pattern="'.$pattern.'"') : '') . sprintf('
    ', $fld_id.'value'.($r), sprintf('%1$s[val][%2$s]', $fld_name, $r), isset($rows) ? (' rows="'.$rows.'"') : '', isset($cols) ? (' cols="'.$cols.'"') : '', isset($class) ? (' class="'.$class.'"') : '',''); } $input = sprintf('
    %1$s
    ',implode('',$lines), isset($class) ? (' '.$class) : ''); break; case 'radio': if (is_array($options) && (count($options) > 0)) { if (isset($legend)) $input .= sprintf('%1$s', $legend); if (!isset($separator)) $separator = ''; foreach ($options as $optkey => $optlabel) $input .= sprintf('%6$s', $fld_id, $fld_name, str_replace('\'','"',checked($optkey, $value, false)), $optkey, $optlabel, $separator); $input = sprintf('
    %1$s
    ',$input); } break; case 'select': $input = $this->selector($fld_id, $fld_name, $value, $options, isset($multiple)); break; case 'page': $args = array( 'id' => $fld_name, 'name' => $fld_name, 'selected' => $value, 'echo' => false, 'depth' => 0, 'option_none_value' => 0); if (isset($show_option_none)) $args['show_option_none'] = $show_option_none; $input = wp_dropdown_pages($args); break; case 'hidden': return sprintf('', $fld_name, $value); default: $input = $value; } if (!$wrap) $wrap = 'div'; switch ($wrap) { case 'tr': $format = '%1$s%2$s'; break; case 'br': $format = 'checkbox'==$type ? '%2$s%1$s
    ' : '%1$s%2$s
    '; break; default: $format = strpos($input,'fieldset') !== FALSE ? '
    %1$s%2$s
    ' : ('<'.$wrap.' class="diy-row">%1$s%2$s'); } return sprintf($format, $label, $input); } function sanitize_post_keypairs($lexicon) { if (isset($_POST[$lexicon]['key']) && isset($_POST[$lexicon]['val']) ) { foreach ( $_POST[$lexicon]['key'] as $i => $key ){ if ( empty($key) ){ unset( $_POST[$lexicon]['val'][$i] ); } else { $_POST[$lexicon][$key] = $_POST[$lexicon]['val'][$i]; } } unset($_POST[$lexicon]['key']); unset($_POST[$lexicon]['val']); } } function get_image_sizes() { global $_wp_additional_image_sizes; $sizes = array(); foreach ( get_intermediate_image_sizes() as $_size ) { if ( in_array( $_size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) { $sizes[ $_size ]['width'] = get_option( "{$_size}_size_w" ); $sizes[ $_size ]['height'] = get_option( "{$_size}_size_h" ); $sizes[ $_size ]['crop'] = (bool) get_option( "{$_size}_crop" ); } elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) { $sizes[ $_size ] = array( 'width' => $_wp_additional_image_sizes[ $_size ]['width'], 'height' => $_wp_additional_image_sizes[ $_size ]['height'], 'crop' => $_wp_additional_image_sizes[ $_size ]['crop'], ); } } return $sizes; } function get_image_size( $size ) { $sizes = $this->get_image_sizes(); if ( isset( $sizes[ $size ] ) ) { return $sizes[ $size ]; } return false; } function log($result) { if ($this->is_error_log()) { $this->error_log($this->prepare_error($result)); } } function prepare_error($result) { if (is_object($result) && is_wp_error( $result )) { $error_message = $result->get_error_message(); $error_code = $result->get_error_code(); $error_data = $result->get_error_data(); $message = sprintf('%1$s; Code:%2$s; Data: %3$s', $error_message, $error_code, print_r($error_data, true)); } else if (is_array($result) || is_object($result)) { $message = print_r($result, true); } else { $message = $result; } return $message; } function is_error_log() { return defined('WP_DEBUG') && (WP_DEBUG === true); } function error_log($message) { error_log($message); } function is_gutenberg_page() { return function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ; } function truncate_last_space($text, $max_chars ) { if ( ! $max_chars ) return ''; $text = trim( $text ); if ( mb_strlen( $text ) > $max_chars ) { $text = mb_substr( $text, 0, $max_chars + 1 ); $text_trim = trim( mb_substr( $text, 0, mb_strrpos( $text, ' ' ) ) ); $text = empty( $text_trim ) ? $text : $text_trim; } return $text; } function maybe_enqueue_tooltip_styles() { if ( class_exists('FLBuilderModel') && is_callable(array('FLBuilderModel', 'is_builder_active')) && FLBuilderModel::is_builder_active() ) { add_action('wp_enqueue_scripts', array($this, 'register_tooltip_styles')); add_action('wp_enqueue_scripts', array($this, 'enqueue_tooltip_styles')); } } function print_styles($css){ if (empty($css)) return false; printf('', $css); } function late_inline_styles($css) { if (empty($css)) return; $wrap = '$("").appendTo("head");'; $this->print_script(sprintf($wrap, $css)); } function print_immediate_script($script) { $this->print_script($script, false); } function print_script($script, $ready = true) { $ready_begin = $ready ? 'jQuery(document).ready( function($) {' : ''; $ready_end = $ready ? '});' : ''; print <<< SCRIPT SCRIPT; } function print_late_stylesheet($url) { print <<< STYLES STYLES; } function print_async_script($url, $config='') { print <<< SCRIPT SCRIPT; } function print_async_script_with_callback($url, $callback='') { print <<< SCRIPT SCRIPT; } function print_final_async_script($url) { print <<< SCRIPT SCRIPT; } }