PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / includes / Manager / PluginShortcode.php
kirki / includes / Manager Last commit date
PluginActiveEvents.php 3 weeks ago PluginDeactivateEvents.php 3 weeks ago PluginInitEvents.php 1 month ago PluginLoadedEvents.php 3 weeks ago PluginShortcode.php 2 months ago TemplateRedirection.php 1 month ago
PluginShortcode.php
213 lines
1 <?php
2
3 /**
4 * Plugin Shortcode handler
5 *
6 * @package kirki
7 */
8
9 /**
10 * So far supported shortcodes:
11 * [kirki data="current_year"] - Returns the current year.
12 * [kirki data="current_date"] - Returns the current date in "F j, Y" format.
13 * [kirki data="reading_time"] - Returns the estimated reading time for the current post.
14 * [kirki type="post_meta" data="meta_key"] - Returns the value of the specified post meta key for the current post.
15 *
16 * [kirki_cm slug="your-content-manager-slug" data="count"] - Returns the count of child posts under the specified content manager slug.
17 */
18
19 namespace Kirki\Manager;
20
21 use Kirki\API\ContentManager\ContentManagerHelper;
22
23 if ( ! defined( 'ABSPATH' ) ) {
24 exit; // Exit if accessed directly.
25 }
26
27 /**
28 * Do some task with shortcode
29 */
30
31 class PluginShortcode {
32
33 /**
34 * Initilize the class
35 *
36 * @return void
37 */
38 public function __construct() {
39 // Kirki shortcode
40 add_shortcode( 'kirki', array( $this, 'kirki_shortcode_handler' ) );
41
42 // Content manager shortcode
43 add_shortcode('kirki_cm', array( $this, 'kirki_cm_shortcode_handler' ) );
44 }
45
46 private function clean_shortcode_value( $val ) {
47 if ( ! is_string( $val ) ) {
48 return $val;
49 }
50
51 // Decode entities like &#8220; or &#8221;
52 $val = html_entity_decode( $val, ENT_QUOTES | ENT_HTML5, 'UTF-8' );
53 $val = trim( $val );
54
55 // Strip wrapping quotes repeatedly (any combination)
56 while (
57 preg_match(
58 '/^([\'"“”‘’]+)(.*?)([\'"“”‘’]+)$/u',
59 $val,
60 $matches
61 )
62 ) {
63 // Only strip if first and last char are quote-like
64 $first = mb_substr( $val, 0, 1 );
65 $last = mb_substr( $val, -1 );
66 if ( preg_match( '/[\'"“”‘’]/u', $first ) && preg_match( '/[\'"“”‘’]/u', $last ) ) {
67 $val = trim( $matches[2] );
68 } else {
69 break;
70 }
71 }
72
73 return $val;
74 }
75
76 private static function post_estimated_reading_time( $post ) {
77 // load the content
78 $thecontent = $post->post_content;
79 // count the number of words
80 $words = str_word_count( wp_strip_all_tags( $thecontent ) );
81 // rounding off and deviding per 200 words per minute
82 $m = floor( $words / 200 );
83 // rounding off to get the seconds
84 $s = floor( $words % 200 / ( 200 / 60 ) );
85 // calculate the amount of time needed to read
86 if ( $m == 0 ) {
87 return $s . 's';
88 } else {
89 return $m . 'm';
90 }
91 }
92
93 /**
94 * Render the shortcode
95 *
96 * @param array $atts Shortcode attributes.
97 * @return string
98 */
99 public function kirki_cm_shortcode_handler( $attributes ) {
100 $no_data_response = '<strong>[No Data found]</strong>';
101
102 try {
103 if ( ! is_array( $attributes ) ) {
104 $attributes = array();
105 }
106
107 $defaults_attrs = array(
108 'slug' => '',
109 'data' => '',
110 );
111
112 $atts = shortcode_atts( $defaults_attrs, $attributes );
113
114 $slug = $this->clean_shortcode_value( sanitize_text_field( $atts['slug'] ) );
115 $data = $this->clean_shortcode_value( sanitize_text_field( $atts['data'] ) );
116
117 if ( empty( $slug ) || empty( $data ) ) {
118 return '<strong>[Slug and Data attributes are required]</strong>';
119 }
120
121 $cm_post = get_page_by_path( $slug, OBJECT, KIRKI_CONTENT_MANAGER_PREFIX );
122
123 if ( ! $cm_post ) {
124 return $no_data_response;
125 }
126
127 $data = strtolower( trim( $data ) );
128
129 switch ( $data ) {
130 case 'count': {
131 return ContentManagerHelper::get_child_post_count( $cm_post->ID, array( 'publish' ) );
132 }
133
134 default:
135 return $no_data_response;
136 }
137
138 return $no_data_response;
139 } catch ( \Exception $e ) {
140 return $no_data_response;
141 }
142 }
143
144 /**
145 * Kirki shortcode handler
146 *
147 * @param array $atts Shortcode attributes.
148 * @return string
149 */
150 public function kirki_shortcode_handler( $attributes ) {
151 $no_data_response = '<strong>[No Data found]</strong>';
152
153 try {
154 if ( ! is_array( $attributes ) ) {
155 $attributes = array();
156 }
157
158 $defaults_attrs = array(
159 'type' => '',
160 'data' => '',
161 );
162
163 $atts = shortcode_atts( $defaults_attrs, $attributes );
164
165 $type = $this->clean_shortcode_value( sanitize_text_field( $atts['type'] ) );
166 $data = $this->clean_shortcode_value( sanitize_text_field( $atts['data'] ) );
167
168 $data = strtolower( trim( $data ) );
169
170 switch ( $type ) {
171 case 'post_meta': {
172 if ( is_singular() ) {
173 global $post;
174 $meta_value = get_post_meta( $post->ID, $data, true );
175 return $meta_value;
176 }
177 return '';
178 }
179
180 default: {
181 switch ( $data ) {
182 case 'current_year': {
183 return gmdate( 'Y' );
184 }
185
186 case 'current_date': {
187 return gmdate( 'F j, Y' );
188 }
189
190 case 'reading_time': {
191 if ( is_singular() ) {
192 global $post;
193 return $this->post_estimated_reading_time( $post );
194 }
195
196 return $no_data_response;
197 }
198
199 default:
200 return $no_data_response;
201 }
202
203 return $no_data_response;
204 }
205 }
206
207 return $no_data_response;
208 } catch ( \Exception $e ) {
209 return $no_data_response;
210 }
211 }
212 }
213