PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.7.7
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.7.7
2.7.7 2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 All 28 releases
← All changes | includes/shortcodes/shortcodes.php +117 -36 2.6.12.7.7 View file →
@@ -1,7 +1,7 @@
1 1 <?php
2 2 /**
3 - * A base shortcode for all lockers
3 + * A base shortcode for all snippets
4 4 *
5 5 * @since 1.0.0
6 6 */
7 7
@@ -9,33 +9,103 @@
9 9 if ( ! defined( 'ABSPATH' ) ) {
10 10 exit;
11 11 }
12 12
13 -class WINP_SnippetShortcode extends Wbcr_FactoryShortcodes335_Shortcode {
13 +/**
14 + * Base shortcode class for all snippet shortcodes
15 + */
16 +class WINP_SnippetShortcode {
14 17
15 - public $manager;
18 + /**
19 + * Plugin instance
20 + *
21 + * @var WINP_Plugin
22 + */
23 + public $plugin;
16 24
25 + /**
26 + * Shortcode name(s)
27 + *
28 + * @var string|array<string>
29 + */
17 30 public $shortcode_name = 'wbcr_php_snippet';
18 31
19 32 /**
20 - * Includes assets
33 + * Includes assets in header
34 + *
21 35 * @var bool
22 36 */
23 37 public $assets_in_header = true;
24 38
39 + /**
40 + * Constructor
41 + *
42 + * @param WINP_Plugin $plugin Plugin instance.
43 + */
25 44 public function __construct( $plugin ) {
26 - parent::__construct( $plugin );
45 + $this->plugin = $plugin;
46 +
47 + // Ensure shortcode_name is an array.
48 + if ( ! is_array( $this->shortcode_name ) ) {
49 + $this->shortcode_name = [ $this->shortcode_name ];
50 + }
51 +
52 + // Register shortcode(s) with WordPress.
53 + foreach ( $this->shortcode_name as $name ) {
54 + if ( ! empty( $name ) ) {
55 + add_shortcode( $name, [ $this, 'render' ] );
56 + }
57 + }
58 +
59 + // Enqueue assets in header if needed.
60 + if ( $this->assets_in_header ) {
61 + add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] );
62 + }
27 63 }
28 64
29 65 /**
66 + * Enqueue assets if needed.
67 + *
68 + * @return void
69 + */
70 + public function enqueue_assets() {
71 + // Override in child classes if needed.
72 + }
73 +
74 + /**
75 + * Shortcode render callback.
76 + *
77 + * @param array<string, mixed> $attr Shortcode attributes.
78 + * @param string|null $content Shortcode content.
79 + * @param string $tag Shortcode tag.
80 + *
81 + * @return string
82 + */
83 + public function render( $attr, $content, $tag ) {
84 + if ( WINP_Helper::is_safe_mode() ) {
85 + return '';
86 + }
87 +
88 + ob_start();
89 + $result = $this->html( $attr, $content ?? '', $tag );
90 + $html = ob_get_clean();
91 +
92 + if ( is_string( $result ) ) {
93 + $html = ( false !== $html ? $html : '' ) . $result;
94 + }
95 +
96 + return false !== $html ? $html : '';
97 + }
98 +
99 + /**
30 100 * Filter attributes
31 101 *
32 - * @param $attr
33 - * @param $post_id
102 + * @param array<string, mixed> $attr Shortcode attributes.
103 + * @param int $post_id Post ID.
34 104 *
35 - * @return mixed
105 + * @return array<string, mixed>
36 106 */
37 - public function filterAttributes( $attr, $post_id ) {
107 + public function filter_attributes( $attr, $post_id ) {
38 108 if ( ! empty( $attr ) ) {
39 109 $available_tags = WINP_Helper::getMetaOption( $post_id, 'snippet_tags', null );
40 110
41 111 if ( ! empty( $available_tags ) ) {
@@ -43,9 +113,9 @@
43 113 $available_tags = array_map( 'trim', $available_tags );
44 114 }
45 115
46 116 foreach ( $attr as $name => $value ) {
47 - $is_allow_attr = in_array( $name, array( 'id', 'title' ) );
117 + $is_allow_attr = in_array( $name, [ 'id', 'title' ] );
48 118 $validate_name = preg_match( '/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $name );
49 119
50 120 if ( ! $is_allow_attr && ( ( ! empty( $available_tags ) && ! in_array( $name, $available_tags ) ) || ! $validate_name ) ) {
51 121 unset( $attr[ $name ] );
@@ -82,18 +152,28 @@
82 152
83 153 /**
84 154 * Get snippet id
85 155 *
86 - * @param $attr
87 - * @param $type
156 + * @param array<string, mixed> $attr Shortcode attributes.
157 + * @param string $type Snippet type.
88 158 *
89 159 * @return int|null
90 160 */
91 - public function getSnippetId( $attr, $type ) {
161 + public function get_snippet_id( $attr, $type ) {
92 162 $id = isset( $attr['id'] ) ? (int) $attr['id'] : null;
93 163
94 - if ( $id && $type != WINP_Helper::get_snippet_type( $id ) ) {
95 - $id = 0;
164 + $snippet_type = null;
165 +
166 + // Only resolve snippet type when a valid (truthy) ID is provided to avoid
167 + // unnecessary request parsing or database lookups for invalid IDs.
168 + if ( $id ) {
169 + $snippet_type = WINP_Helper::get_snippet_type( $id );
170 +
171 + // Security: Reject if get_snippet_type() returned false (invalid post type)
172 + // or if the snippet type doesn't match the expected type.
173 + if ( false === $snippet_type || $snippet_type !== $type ) {
174 + $id = 0;
175 + }
96 176 }
97 177
98 178 return $id;
99 179 }
@@ -100,16 +180,16 @@
100 180
101 181 /**
102 182 * Get snippet activate
103 183 *
104 - * @param $snippet_meta
184 + * @param array<string, mixed> $snippet_meta Snippet metadata.
105 185 *
106 186 * @return bool
107 187 */
108 - public function getSnippetActivate( $snippet_meta ) {
109 - // WPML Compatibility
188 + public function get_snippet_activate( $snippet_meta ) {
189 + // WPML Compatibility.
110 190 if ( defined( 'WPML_PLUGIN_FILE' ) ) {
111 - $wpml_langs = isset( $snippet_meta[ $this->plugin->getPrefix() . 'snippet_wpml_lang' ][0] ) ? $snippet_meta[ $this->plugin->getPrefix() . 'snippet_wpml_lang' ][0] : '';
191 + $wpml_langs = isset( $snippet_meta['wbcr_inp_snippet_wpml_lang'][0] ) ? $snippet_meta['wbcr_inp_snippet_wpml_lang'][0] : '';
112 192 if ( $wpml_langs !== '' && defined( 'ICL_LANGUAGE_CODE' ) ) {
113 193 if ( ! in_array( ICL_LANGUAGE_CODE, explode( ',', $wpml_langs ) ) ) {
114 194 return false;
115 195 }
@@ -115,48 +195,49 @@
115 195 }
116 196 }
117 197 }
118 198
119 - return isset( $snippet_meta[ $this->plugin->getPrefix() . 'snippet_activate' ] ) && $snippet_meta[ $this->plugin->getPrefix() . 'snippet_activate' ][0];
199 + return isset( $snippet_meta['wbcr_inp_snippet_activate'] ) && $snippet_meta['wbcr_inp_snippet_activate'][0];
120 200 }
121 201
122 202 /**
123 203 * Get snippet scope
124 204 *
125 - * @param $snippet_meta
205 + * @param array<string, mixed> $snippet_meta Snippet metadata.
126 206 *
127 - * @return null
207 + * @return string|null
128 208 */
129 - public function getSnippetScope( $snippet_meta ) {
130 - return isset( $snippet_meta[ $this->plugin->getPrefix() . 'snippet_scope' ] ) ? $snippet_meta[ $this->plugin->getPrefix() . 'snippet_scope' ][0] : null;
209 + public function get_snippet_scope( $snippet_meta ) {
210 + return isset( $snippet_meta['wbcr_inp_snippet_scope'] ) ? $snippet_meta['wbcr_inp_snippet_scope'][0] : null;
131 211 }
132 212
133 213 /**
134 214 * Get snippet content
135 215 *
136 - * @param WP_Post $snippet
137 - * @param array $snippet_meta
138 - * @param int $id
216 + * @param WP_Post $snippet Snippet post object.
217 + * @param array<string, mixed> $snippet_meta Snippet metadata.
218 + * @param int $id Snippet ID.
139 219 *
140 - * @return null|string
220 + * @return string|null
141 221 */
142 - public function getSnippetContent( $snippet, $snippet_meta, $id ) {
222 + public function get_snippet_content( $snippet, $snippet_meta, $id ) {
143 223 $snippet_code = WINP_Helper::get_snippet_code( $snippet );
144 224
145 - if ( WINP_Plugin::app()->getOption( 'execute_shortcode' ) ) {
225 + if ( get_option( 'wbcr_inp_execute_shortcode' ) ) {
146 226 $snippet_code = do_shortcode( $snippet_code );
147 227 }
148 228
149 - return WINP_Plugin::app()->getExecuteObject()->prepareCode( $snippet_code, $id );
229 + return WINP_Plugin::app()->get_execute_object()->prepareCode( $snippet_code, $id );
150 230 }
151 231
152 232 /**
153 233 * Content render
154 234 *
155 - * @param array $attr
156 - * @param string $content
157 - * @param string $tag
235 + * @param array<string, mixed> $attr Shortcode attributes.
236 + * @param string $content Shortcode content.
237 + * @param string $tag Shortcode tag.
238 + *
239 + * @return mixed Rendered content when returned directly, if any.
158 240 */
159 241 public function html( $attr, $content, $tag ) {
160 -
161 242 }
162 -}
243 +}