PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 3.6.1
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v3.6.1
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Core / Shortcode.php

Shortcode.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 3.6.1, at includes/Core/Shortcode.php

150 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPDeveloper\BetterDocs\Core;
4
5 use WPDeveloper\BetterDocs\Utils\Base;
6 use WPDeveloper\BetterDocs\Utils\Helper;
7 use WPDeveloper\BetterDocs\Traits\EditorHelper;
8 use WPDeveloper\BetterDocs\Admin\Customizer\Defaults;
9
10 abstract class Shortcode extends Base {
11 use EditorHelper;
12
13 /**
14 * Global Settings Reference.
15 * @var Settings
16 */
17 protected $settings;
18 /**
19 * Global Query Reference.
20 * @var Query
21 */
22 protected $query;
23 /**
24 * Utility Helper
25 * @var Helper
26 */
27 protected $helper;
28 /**
29 * Customizer Defaults.
30 * @var Defaults
31 */
32 protected $customizer;
33
34 public function __construct( Settings $settings, Query $query, Helper $helper, Defaults $defaults ) {
35 $this->settings = $settings;
36 $this->query = $query;
37 $this->helper = $helper;
38 $this->customizer = $defaults;
39
40 // add_action( 'betterdocs_before_shortcode_load', [$this, 'enqueue_scripts'], 11, 1 );
41 }
42
43 /**
44 * Shortcode Tag Name.
45 * @return string
46 */
47 abstract public function get_name();
48
49 /**
50 * A list of default attributes.
51 * @return array
52 */
53 abstract public function default_attributes();
54
55 /**
56 * Individual shortcode render callback.
57 * @param mixed $atts
58 * @param mixed $content
59 * @return mixed
60 */
61 abstract public function render( $atts, $content = null );
62
63 public function get_style_depends() {
64 return [];
65 }
66
67 public function get_script_depends() {
68 return [];
69 }
70
71 public function enqueue_scripts() {
72 foreach ( $this->get_style_depends() as $handle ) {
73 wp_enqueue_style( $handle );
74 }
75
76 foreach ( $this->get_script_depends() as $handle ) {
77 wp_enqueue_script( $handle );
78 }
79 }
80
81 /**
82 * Shortcode render callback, which is hooked with add_shortcode function.
83 *
84 * @param mixed $atts
85 * @param mixed $content
86 *
87 * @return bool|string
88 */
89 final public function render_with_hooks( $atts, $content = null ) {
90 $this->enqueue_scripts();
91
92 $atts = $this->remove_deprecated_attributes( $atts );
93 $this->transform_attribute_types( $atts );
94
95 $this->client_attributes = empty( $atts ) ? [] : $atts;
96 do_action( 'betterdocs_before_shortcode_load', $this );
97 $this->set_attributes( $atts );
98
99 // reset attributes;
100 $this->reset_attributes();
101
102 do_action_ref_array( 'betterdocs_before_render', [ & $this, 'shortcode'] );
103 ob_start();
104 $this->render( $atts, $content = null );
105 $content = ob_get_clean();
106
107 do_action_ref_array( 'betterdocs_after_render', [ & $this, 'shortcode'] );
108
109 return $content;
110 }
111
112 protected function transform_attribute_types( &$atts ) {
113 if ( is_array( $atts ) ) {
114 $_default_attributes = $this->default_attributes();
115
116 foreach ( $atts as $key => $value ) {
117 if ( isset( $_default_attributes[$key] ) ) {
118 $type = gettype( $_default_attributes[$key] );
119
120 if ( $type === 'boolean' ) {
121 $atts[$key] = $atts[$key] == 'false' || $atts[$key] == '' ? false : true;
122 }
123 }
124 }
125 }
126 }
127
128 /**
129 * This method is responsive for set shortcode attributes in a property.
130 * @param mixed $atts
131 * @return array
132 */
133 private function set_attributes( $atts ) {
134 $this->attributes = shortcode_atts(
135 apply_filters( "betterdocs_shortcodes_default_atts", $this->default_attributes(), $this->get_name() ),
136 $atts
137 );
138
139 return $this->attributes;
140 }
141
142 public function isset( $key, $value = null ) {
143 if ( null === $value ) {
144 return isset( $this->attributes[$key] ) && ! empty( $this->attributes[$key] );
145 }
146
147 return isset( $this->attributes[$key] ) && $this->attributes[$key] == $value;
148 }
149 }
150