PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.3.1
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.3.1
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
insert-php / libs / factory / shortcodes / shortcode.class.php

shortcode.class.php in Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts 2.3.1, at libs/factory/shortcodes/shortcode.class.php

320 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The file contains a base class for all shortcodes.
4 *
5 * @author Alex Kovalev <alex.kovalevv@gmail.com>
6 * @since 1.0.0
7 * @package core
8 * @copyright (c) 2018, Webcraftic Ltd
9 *
10 */
11
12 // Exit if accessed directly
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 if ( ! class_exists( 'Wbcr_FactoryShortcodes329_Shortcode' ) ) {
18
19 /**
20 * The base class for all shortcodes.
21 *
22 * @since 1.0.0
23 */
24 abstract class Wbcr_FactoryShortcodes329_Shortcode {
25
26 private static $meta_key_shorcode_assets_for_posts = 'factory_shortcodes_assets';
27
28 /**
29 * Shortcode name.
30 *
31 * @since 1.0.0
32 * @var string
33 */
34 public $shortcode_name = null;
35
36 /**
37 * If true, the assets methods will be called in header.
38 *
39 * @since 3.0.0
40 * @var boolean
41 */
42 public $assets_in_header = false;
43
44 /**
45 * A manager that created and track this shortcode.
46 *
47 * @since 1.0.0
48 * @var Wbcr_FactoryShortcodes329_ShortcodeManager
49 */
50 private $manager;
51
52 /**
53 * Scripts to include on the same page.
54 *
55 * @since 1.0.0
56 * @var Wbcr_Factory422_ScriptList
57 */
58 public $scripts;
59
60 /**
61 * Styles to include on the same page.
62 *
63 * @since 1.0.0
64 * @var Wbcr_Factory422_StyleList
65 */
66 public $styles;
67
68 /**
69 * If set true, this shortcode will be tracked on altering post content.
70 *
71 * When this shortcode will be found in a post content, the method onTrack will be fired.
72 *
73 * @since 1.0.0
74 * @var bool
75 */
76 public $track = false;
77
78 /**
79 * If true, it means that shortcodes assets have been conected already.
80 *
81 * @since 1.0.0
82 * @var bool
83 */
84 protected $connected = false;
85
86 /**
87 * /**
88 * Creates a new instance of a shortcode objects.
89 *
90 * @since 1.0.0
91 *
92 * @param Wbcr_Factory422_Plugin $plugin
93 */
94 public function __construct( $plugin ) {
95 $this->plugin = $plugin;
96
97 $this->scripts = $this->plugin->newScriptList();
98 $this->styles = $this->plugin->newStyleList();
99
100 if ( ! is_array( $this->shortcode_name ) ) {
101 $this->shortcode_name = [ $this->shortcode_name ];
102 }
103
104 if ( $this->assets_in_header ) {
105 add_action( 'wp_enqueue_scripts', [ $this, 'actionEnqueueScripts' ] );
106 }
107
108 if ( is_admin() ) {
109 add_action( 'save_post', [ $this, 'actionSavePost' ] );
110 }
111 }
112
113 /**
114 * Adds shortcode scripts and styles of it's nedded.
115 *
116 * Calls on the hook "wp_enqueue_scripts".
117 *
118 * @since 1.0.0
119 * @return void
120 * @global Wp_post $post
121 */
122 public function actionEnqueueScripts() {
123 global $post;
124 if ( empty( $post ) ) {
125 return;
126 }
127
128 foreach ( $this->shortcode_name as $shortcode_name ) {
129 if ( $this->connected ) {
130 return;
131 }
132
133 $metaValue = get_post_meta( $post->ID, self::$meta_key_shorcode_assets_for_posts, true );
134
135 if ( ! isset( $metaValue[ $shortcode_name ] ) ) {
136 continue;
137 }
138
139 $result = $this->assets( $metaValue[ $shortcode_name ], false, true );
140
141 if ( ! $result ) {
142 continue;
143 }
144
145 $this->scripts->connect();
146 $this->styles->connect();
147
148 $this->connected = true;
149 }
150 }
151
152 /**
153 * Adds shortcode scripts and styles of it's nedded.
154 *
155 * Calls on the hook "save_post".
156 *
157 * @return void|int
158 */
159 public function actionSavePost( $post_id ) {
160 if ( wp_is_post_revision( $post_id ) ) {
161 return $post_id;
162 }
163
164 $post = get_post( $post_id );
165
166 if ( empty( $post ) ) {
167 return;
168 }
169
170 $this->onPostSave( $post );
171
172 if ( $this->track ) {
173 $this->trackShortcode( $post );
174 }
175 }
176
177 /**
178 * Checks if a post contains a given shortcode and extract its atrributes and content on post saving.
179 *
180 * @since 1.0.0
181 *
182 * @param object $post A current post.
183 *
184 * @return void
185 */
186 private function trackShortcode( $post ) {
187 if ( empty( $this->shortcode_name ) ) {
188 return;
189 }
190
191 $matches = [];
192
193 $shortcodes = $this->shortcode_name;
194
195 if ( ! is_array( $shortcodes ) ) {
196 $shortcodes = [ $this->shortcode_name ];
197 }
198
199 $tagregexp = join( '|', $shortcodes );
200
201 $start = '(\[(' . $tagregexp . ')([^\[\]]*)\])';
202 $end = '\[\/\2\]';
203 $pattern = '/' . $start . '(.*?)' . $end . '/is';
204
205 $count = preg_match_all( $pattern, $post->post_content, $matches, PREG_SET_ORDER );
206
207 $found_shortcodes = get_post_meta( $post->ID, self::$meta_key_shorcode_assets_for_posts, true );
208
209 if ( ! is_array( $found_shortcodes ) ) {
210 $found_shortcodes = [];
211 }
212
213 foreach ( $shortcodes as $shortcode ) {
214 unset( $found_shortcodes[ $shortcode ] );
215 }
216
217 if ( ! $count ) {
218 update_post_meta( $post->ID, self::$meta_key_shorcode_assets_for_posts, $found_shortcodes );
219
220 return;
221 }
222
223 // clears info about previously existing shortcodes
224
225 foreach ( $matches as $order => $match ) {
226
227 $shortcode = $match[2];
228 $attrContent = str_replace( '\\', '', $match[3] );
229 $innerContent = str_replace( '\\', '', $match[4] );
230
231 $attrs = shortcode_parse_atts( $attrContent );
232
233 if ( ! isset( $found_shortcodes[ $shortcode ] ) ) {
234 $found_shortcodes[ $shortcode ] = [];
235 }
236 $found_shortcodes[ $shortcode ][] = $attrs;
237
238 // allows to perfome custom actions
239
240 do_action( 'factory_shortcode_found', $shortcode, $attrs, $innerContent );
241
242 $this->onTrack( $shortcode, $attrs, $innerContent, $post->ID );
243 }
244
245 // saves info about existing shortcodes for a given post
246
247 delete_post_meta( $post->ID, self::$meta_key_shorcode_assets_for_posts );
248 update_post_meta( $post->ID, self::$meta_key_shorcode_assets_for_posts, $found_shortcodes );
249 }
250
251 /**
252 * Returns a shortcode html markup.
253 *
254 * @since 1.0.0
255 * @return string
256 */
257 public function render( $attr, $content, $tag ) {
258
259 if ( ! $this->connected ) {
260 $this->assets( [ $attr ], true, false );
261 $this->scripts->connect( true );
262 $this->styles->connect( true );
263 }
264
265 // fix for compability
266
267 global $post;
268 if ( ! empty( $post ) && ! empty( $this->shortcode_name ) ) {
269 $found_shortcodes = get_post_meta( $post->ID, self::$meta_key_shorcode_assets_for_posts, true );
270
271 if ( $found_shortcodes && ! is_array( $found_shortcodes ) ) {
272
273 $shortcodes = $this->shortcode_name;
274 if ( ! is_array( $shortcodes ) ) {
275 $shortcodes = [ $this->shortcode_name ];
276 }
277
278 $found_shortcodes = [];
279 $found_shortcodes[ $shortcodes[0] ] = [];
280 $found_shortcodes[ $shortcodes[0] ][] = $attr;
281
282 update_post_meta( $post->ID, self::$meta_key_shorcode_assets_for_posts, $found_shortcodes );
283 }
284 }
285
286 ob_start();
287 $this->html( $attr, $content, $tag );
288 $html = ob_get_clean();
289
290 //return nl2br($html);
291 return $html;
292 }
293
294 /**
295 * Configures assets (js and css) for the shortcodes.
296 *
297 * The method should be overwritten in a deferred class.
298 *
299 * @since 1.0.0
300 * @return void
301 */
302 public function assets() {
303 }
304
305
306 public function onPostSave( $post ) {
307 }
308
309 public function onTrack( $shortcode, $attrs, $innerContent, $postId ) {
310 }
311
312 /**
313 * Renders shortcode html.
314 *
315 * @since 1.0.0
316 * @return void
317 */
318 public abstract function html( $attr, $content, $tag );
319 }
320 }