tag = $tag; return $this; } /** * Define accepted attributes for the shortcode */ public function attrs(array $attrs): self { $this->attrs = $attrs; return $this; } /** * Set the rendering method for the shortcode (callback function or view file path) */ public function render($render): self { $this->render = $render; add_shortcode($this->tag, [$this, 'handleShortcode']); return $this; } /** * Handle shortcode rendering */ public function handleShortcode($atts, $content = null) { $atts = shortcode_atts(array_fill_keys($this->attrs, ''), $atts, $this->tag); if (is_callable($this->render)) { return call_user_func($this->render, $atts, $content); } elseif (is_string($this->render) && file_exists($this->render)) { ob_start(); extract($atts); // Extract attributes to be used as variables $shortcode_content = $content; // Pass content to the view file include $this->render; return ob_get_clean(); } return ''; // Return empty string if no valid render function or file is found } }