function tbFileScanDirectory($dir, $mask, $options = array(), $depth = 0) { $options += array('nomask' => '/(\\.\\.?|CSV)$/', 'callback' => 0, 'recurse' => TRUE, 'key' => 'uri', 'min_depth' => 0); $options['key'] = in_array($options['key'], array('uri', 'filename', 'name')) ? $options['key'] : 'uri'; $files = array(); if (is_dir($dir) && ($handle = opendir($dir))) { while (FALSE !== ($filename = readdir($handle))) { if (!preg_match($options['nomask'], $filename) && $filename[0] != '.') { $uri = "{$dir}/{$filename}"; if (is_dir($uri) && $options['recurse']) { // Give priority to files in this folder by merging them in after any subdirectory files. $files = array_merge(tbFileScanDirectory($uri, $mask, $options, $depth + 1), $files); } elseif ($depth >= $options['min_depth'] && preg_match($mask, $filename)) { // Always use this match over anything already set in $files with the // same $$options['key']. $file = new stdClass(); $file->uri = $uri; $file->filename = $filename; $file->name = pathinfo($filename, PATHINFO_FILENAME); $files[$filename] = $file; } } } closedir($handle); } return $files; }
/** * Add Shortcode Params * * @return none */ function tbShortcodeAddParams() { $extra_params_folder = get_template_directory() . '/vc_params'; $files = tbFileScanDirectory($extra_params_folder, '/^tb_.*\\.php/'); if (!empty($files)) { foreach ($files as $file) { if (WPBMap::exists($file->name)) { include $file->uri; if (isset($params) && is_array($params)) { foreach ($params as $param) { if (is_array($param)) { $param['group'] = __('Template', TB_NAME); $param['edit_field_class'] = isset($param['edit_field_class']) ? $param['edit_field_class'] . ' tb_custom_param vc_col-sm-12 vc_column' : 'tb_custom_param vc_col-sm-12 vc_column'; $param['class'] = 'tb-extra-param'; if (isset($param['template']) && !empty($param['template'])) { if (!is_array($param['template'])) { $param['template'] = array($param['template']); } $param['dependency'] = array("element" => "tb_template", "value" => $param['template']); } vc_add_param($file->name, $param); } } } } } } }
function tb_shortcode_template($settings, $value) { $shortcode = $settings['shortcode']; $theme_dir = get_template_directory() . '/vc_templates'; $reg = "/^({$shortcode}\\.php|{$shortcode}--.*\\.php)/"; $files = tbFileScanDirectory($theme_dir, $reg); $files = array_merge(tbFileScanDirectory(TB_TEMPLATES, $reg), $files); $output = ""; $output .= "<select name=\"" . esc_attr($settings['param_name']) . "\" class=\"wpb_vc_param_value\">"; foreach ($files as $key => $file) { if ($key == esc_attr($value)) { $output .= "<option value=\"{$key}\" selected>{$key}</option>"; } else { $output .= "<option value=\"{$key}\">{$key}</option>"; } } $output .= "</select>"; $script = <<<SCRIPT <script type="text/javascript"> jQuery('button.vc_panel-btn-save[data-save=true]').click(function(){ jQuery('.tb_custom_param.vc_dependent-hidden').remove(); }); </script> SCRIPT; return $output . $script; }
/** * * @return Array(): array of all avaiable templates */ protected function findShortcodeTemplates() { $theme_dir = get_template_directory() . '/vc_templates'; $reg = "/^({$this->shortcode}\\.php|{$this->shortcode}--.*\\.php)/"; $files = tbFileScanDirectory($theme_dir, $reg); $files = array_merge(tbFileScanDirectory(TB_TEMPLATES, $reg), $files); return $files; }
function tb_shortcode_template_img($settings, $value) { $shortcode = $settings['shortcode']; $theme_dir = get_template_directory() . '/vc_templates'; $reg = "/^({$shortcode}\\.php|{$shortcode}--.*\\.php)/"; $files = tbFileScanDirectory($theme_dir, $reg); $files = array_merge(tbFileScanDirectory(TB_TEMPLATES, $reg), $files); $output = ""; $output .= "<select style=\"display:none;\" id=\"" . $shortcode . "-select-param\" name=\"" . esc_attr($settings['param_name']) . "\" class=\"wpb_vc_param_value\">"; foreach ($files as $key => $file) { if ($key == esc_attr($value)) { $output .= "<option value=\"{$key}\" selected>{$key}</option>"; } else { $output .= "<option value=\"{$key}\">{$key}</option>"; } } $output .= "</select>"; $output .= "<div id=\"" . $shortcode . "-tb-img-select\">"; foreach ($files as $key => $file) { $img = get_template_directory_uri() . '/vc_params/' . $shortcode . '/' . basename($key, '.php') . '.jpg'; if ($key == esc_attr($value)) { $output .= "<img src=\"" . $img . "\" data-value=\"" . $key . "\" class=\"tb-img-select selected\" />"; } else { $output .= "<img src=\"" . $img . "\" data-value=\"" . $key . "\" class=\"tb-img-select\" />"; } } $output .= "</div>"; $script = ' <script type="text/javascript"> jQuery(\'button.vc_panel-btn-save[data-save=true]\').click(function(){ jQuery(\'.tb_custom_param.vc_dependent-hidden\').remove(); }); jQuery(document).ready(function($){ $("#' . $shortcode . '-tb-img-select").find("img.tb-img-select").click(function(){ var $this = $(this); $("#' . $shortcode . '-tb-img-select").find("img.tb-img-select").removeClass("selected"); $this.addClass("selected");console.log($(":hidden#' . $shortcode . '-select-param")); $(":hidden#' . $shortcode . '-select-param").val($this.data("value")).change(); }); }); </script>'; return $output . $script; }