/** * Logic for the ubc_template shortcode. Looks for a specific template file * that is requested in the shortcode atts, if it exists, fetches it. * * @since 1.0.0 * * @param (array) $attr - Attributes passed to the shortcode * @return (string) markup for this shortcode */ public function add_shortcode__ubc_template($attr) { $attr = shortcode_atts(array('department' => '', 'template' => ''), $attr, 'ubc_template'); // Need both, bail early if not set if (empty($attr['department']) || empty($attr['template'])) { return __('ubc_template shortcode requires both a department and template', $this->get_text_domain()); } // The root URL $path = self::$plugin_path . 'templates/'; $department = sanitize_title_with_dashes($attr['department']); $template = sanitize_title_with_dashes($attr['template']); // Form the full path and then check if that template exists $path .= trailingslashit($department) . $template . '.php'; $path = apply_filters('ubc_use_template_path', $path, $department, $template); if (!file_exists($path)) { return __('Specified template file does not exist', $this->get_text_domain()); } $data = apply_filters('ubc_use_template_data', array(), $department, $template); // This allows us to use odd/even counts in templates global $usage_id; if (!isset($usage_id)) { $usage_id = 0; } // Fetch the content of this template $content = \UBC\Helpers::fetch_template_part($path, $data); // Run it through a filter so we can modify outside should someone wish to add $content = apply_filters('ubc_use_template_content', $content, $data, $department, $template); do_action('ubc_use_template_end', $content, $data, $department, $template); return wp_kses_post($content); }
/** * Logic for the ubc_template shortcode. Looks for a specific template file * that is requested in the shortcode atts, if it exists, fetches it. * * @since 1.0.0 * * @param (array) $attr - Attributes passed to the shortcode * @return (string) markup for this shortcode */ public function add_shortcode__ubc_template($attr) { $attr = shortcode_atts(array('department' => '', 'template' => ''), $attr, 'ubc_template'); // Need both, bail early if not set if (empty($attr['department']) || empty($attr['template'])) { return esc_html__('ubc_template shortcode requires both a department and template', $this->get_text_domain()); } $department = sanitize_title_with_dashes($attr['department']); $template = sanitize_title_with_dashes($attr['template']); $path = $this->generate_path($department, $template); if (!file_exists($path)) { return esc_html__('Specified template file does not exist', $this->get_text_domain()); } $data = apply_filters('ubc_use_template_data', array(), $department, $template); // This allows us to use odd/even counts in templates global $usage_id; $usage_id = !isset($usage_id) ? 0 : $usage_id; // Fetch the content of this template $content = \UBC\Helpers::fetch_template_part($path, $data); // Run it through a filter so we can modify outside should someone wish to add $content = apply_filters('ubc_use_template_content', $content, $data, $department, $template); do_action('ubc_use_template_end', $content, $data, $department, $template); return wp_kses_post($content); }