PluginProbe
StreamCast – bring live radio to your site with a sleek player / 2.1.4
StreamCast – bring live radio to your site with a sleek player v2.1.4
2.4.5 2.4.4 trunk 1.0 1.1 2.0.0 2.1.10 2.1.2 2.1.4 2.1.5 2.1.8 2.2.1 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.7 2.3.8 2.3.9 All 29 releases
streamcast / admin / codestar-framework / classes / shortcode-options.class.php

shortcode-options.class.php in StreamCast – bring live radio to your site with a sleek player 2.1.4, at admin/codestar-framework/classes/shortcode-options.class.php

336 lines 12.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if ( ! defined( 'ABSPATH' ) ) { die; } // Cannot access directly.
2 /**
3 *
4 * Shortcoder Class
5 *
6 * @since 1.0.0
7 * @version 1.0.0
8 *
9 */
10 if ( ! class_exists( 'CSF_Shortcoder' ) ) {
11 class CSF_Shortcoder extends CSF_Abstract{
12
13 // constans
14 public $unique = '';
15 public $abstract = 'shortcoder';
16 public $blocks = array();
17 public $sections = array();
18 public $pre_tabs = array();
19 public $pre_sections = array();
20 public $args = array(
21 'button_title' => 'Add Shortcode',
22 'select_title' => 'Select a shortcode',
23 'insert_title' => 'Insert Shortcode',
24 'show_in_editor' => true,
25 'show_in_custom' => false,
26 'defaults' => array(),
27 'class' => '',
28 'gutenberg' => array(
29 'title' => 'CSF Shortcodes',
30 'description' => 'CSF Shortcode Block',
31 'icon' => 'screenoptions',
32 'category' => 'widgets',
33 'keywords' => array( 'shortcode', 'csf', 'insert' ),
34 'placeholder' => 'Write shortcode here...',
35 ),
36 );
37
38 // run shortcode construct
39 public function __construct( $key, $params = array() ) {
40
41 $this->unique = $key;
42 $this->args = apply_filters( "csf_{$this->unique}_args", wp_parse_args( $params['args'], $this->args ), $this );
43 $this->sections = apply_filters( "csf_{$this->unique}_sections", $params['sections'], $this );
44 $this->pre_tabs = $this->pre_tabs( $this->sections );
45 $this->pre_sections = $this->pre_sections( $this->sections );
46
47 add_action( 'admin_footer', array( &$this, 'add_footer_modal_shortcode' ) );
48 add_action( 'customize_controls_print_footer_scripts', array( &$this, 'add_footer_modal_shortcode' ) );
49 add_action( 'wp_ajax_csf-get-shortcode-'. $this->unique, array( &$this, 'get_shortcode' ) );
50
51 if ( ! empty( $this->args['show_in_editor'] ) ) {
52
53 CSF::$shortcode_instances[$this->unique] = wp_parse_args( array( 'hash' => md5( $key ), 'modal_id' => $this->unique ), $this->args );
54
55 // elementor editor support
56 if ( CSF::is_active_plugin( 'elementor/elementor.php' ) ) {
57 add_action( 'elementor/editor/before_enqueue_scripts', array( 'CSF', 'add_admin_enqueue_scripts' ) );
58 add_action( 'elementor/editor/footer', array( 'CSF_Field_icon', 'add_footer_modal_icon' ) );
59 add_action( 'elementor/editor/footer', array( &$this, 'add_footer_modal_shortcode' ) );
60 }
61
62 }
63
64 }
65
66 // instance
67 public static function instance( $key, $params = array() ) {
68 return new self( $key, $params );
69 }
70
71 public function pre_tabs( $sections ) {
72
73 $result = array();
74 $parents = array();
75 $count = 100;
76
77 foreach ( $sections as $key => $section ) {
78 if ( ! empty( $section['parent'] ) ) {
79 $section['priority'] = ( isset( $section['priority'] ) ) ? $section['priority'] : $count;
80 $parents[$section['parent']][] = $section;
81 unset( $sections[$key] );
82 }
83 $count++;
84 }
85
86 foreach ( $sections as $key => $section ) {
87 $section['priority'] = ( isset( $section['priority'] ) ) ? $section['priority'] : $count;
88 if ( ! empty( $section['id'] ) && ! empty( $parents[$section['id']] ) ) {
89 $section['subs'] = wp_list_sort( $parents[$section['id']], array( 'priority' => 'ASC' ), 'ASC', true );
90 }
91 $result[] = $section;
92 $count++;
93 }
94
95 return wp_list_sort( $result, array( 'priority' => 'ASC' ), 'ASC', true );
96 }
97
98 public function pre_sections( $sections ) {
99
100 $result = array();
101
102 foreach ( $this->pre_tabs as $tab ) {
103 if ( ! empty( $tab['subs'] ) ) {
104 foreach ( $tab['subs'] as $sub ) {
105 $result[] = $sub;
106 }
107 }
108 if ( empty( $tab['subs'] ) ) {
109 $result[] = $tab;
110 }
111 }
112
113 return $result;
114 }
115
116 // get default value
117 public function get_default( $field ) {
118
119 $default = ( isset( $field['default'] ) ) ? $field['default'] : '';
120 $default = ( isset( $this->args['defaults'][$field['id']] ) ) ? $this->args['defaults'][$field['id']] : $default;
121
122 return $default;
123
124 }
125
126 public function add_footer_modal_shortcode() {
127
128 if( ! wp_script_is( 'csf' ) ) {
129 return;
130 }
131
132 $class = ( $this->args['class'] ) ? ' '. esc_attr( $this->args['class'] ) : '';
133 $has_select = ( count( $this->pre_tabs ) > 1 ) ? true : false;
134 $single_usage = ( ! $has_select ) ? ' csf-shortcode-single' : '';
135 $hide_header = ( ! $has_select ) ? ' hidden' : '';
136
137 ?>
138 <div id="csf-modal-<?php echo esc_attr( $this->unique ); ?>" class="wp-core-ui csf-modal csf-shortcode hidden<?php echo esc_attr( $single_usage . $class ); ?>" data-modal-id="<?php echo esc_attr( $this->unique ); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( 'csf_shortcode_nonce' ) ); ?>">
139 <div class="csf-modal-table">
140 <div class="csf-modal-table-cell">
141 <div class="csf-modal-overlay"></div>
142 <div class="csf-modal-inner">
143 <div class="csf-modal-title">
144 <?php echo $this->args['button_title']; ?>
145 <div class="csf-modal-close"></div>
146 </div>
147 <?php
148
149 echo '<div class="csf-modal-header'. esc_attr( $hide_header ) .'">';
150 echo '<select>';
151 echo ( $has_select ) ? '<option value="">'. esc_attr( $this->args['select_title'] ) .'</option>' : '';
152
153 $tab_key = 1;
154
155 foreach ( $this->pre_tabs as $tab ) {
156
157 if ( ! empty( $tab['subs'] ) ) {
158
159 echo '<optgroup label="'. esc_attr( $tab['title'] ) .'">';
160
161 foreach ( $tab['subs'] as $sub ) {
162
163 $view = ( ! empty( $sub['view'] ) ) ? ' data-view="'. esc_attr( $sub['view'] ) .'"' : '';
164 $shortcode = ( ! empty( $sub['shortcode'] ) ) ? ' data-shortcode="'. esc_attr( $sub['shortcode'] ) .'"' : '';
165 $group = ( ! empty( $sub['group_shortcode'] ) ) ? ' data-group="'. esc_attr( $sub['group_shortcode'] ) .'"' : '';
166
167 echo '<option value="'. esc_attr( $tab_key ) .'"'. $view . $shortcode . $group .'>'. esc_attr( $sub['title'] ) .'</option>';
168
169 $tab_key++;
170
171 }
172
173 echo '</optgroup>' ;
174
175 } else {
176
177 $view = ( ! empty( $tab['view'] ) ) ? ' data-view="'. esc_attr( $tab['view'] ) .'"' : '';
178 $shortcode = ( ! empty( $tab['shortcode'] ) ) ? ' data-shortcode="'. esc_attr( $tab['shortcode'] ) .'"' : '';
179 $group = ( ! empty( $tab['group_shortcode'] ) ) ? ' data-group="'. esc_attr( $tab['group_shortcode'] ) .'"' : '';
180
181 echo '<option value="'. esc_attr( $tab_key ) .'"'. $view . $shortcode . $group .'>'. esc_attr( $tab['title'] ) .'</option>';
182
183 $tab_key++;
184
185 }
186
187 }
188
189 echo '</select>';
190 echo '</div>';
191
192 ?>
193 <div class="csf-modal-content">
194 <div class="csf-modal-loading"><div class="csf-loading"></div></div>
195 <div class="csf-modal-load"></div>
196 </div>
197 <div class="csf-modal-insert-wrapper hidden"><a href="#" class="button button-primary csf-modal-insert"><?php echo $this->args['insert_title']; ?></a></div>
198 </div>
199 </div>
200 </div>
201 </div>
202 <?php
203 }
204
205 public function get_shortcode() {
206
207 ob_start();
208
209 $nonce = ( ! empty( $_POST[ 'nonce' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'nonce' ] ) ) : '';
210 $shortcode_key = ( ! empty( $_POST[ 'shortcode_key' ] ) ) ? sanitize_text_field( wp_unslash( $_POST[ 'shortcode_key' ] ) ) : '';
211
212 if ( ! empty( $shortcode_key ) && wp_verify_nonce( $nonce, 'csf_shortcode_nonce' ) ) {
213
214 $unallows = array( 'group', 'repeater', 'sorter' );
215 $section = $this->pre_sections[$shortcode_key-1];
216 $shortcode = ( ! empty( $section['shortcode'] ) ) ? $section['shortcode'] : '';
217 $view = ( ! empty( $section['view'] ) ) ? $section['view'] : 'normal';
218
219 if ( ! empty( $section ) ) {
220
221 //
222 // View: normal
223 if ( ! empty( $section['fields'] ) && $view !== 'repeater' ) {
224
225 echo '<div class="csf-fields">';
226
227 foreach ( $section['fields'] as $field ) {
228
229 if ( in_array( $field['type'], $unallows ) ) { $field['_notice'] = true; }
230
231 // Extra tag improves for spesific fields (border, spacing, dimensions etc...)
232 $field['tag_prefix'] = ( ! empty( $field['tag_prefix'] ) ) ? $field['tag_prefix'] .'_' : '';
233
234 $field_default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
235
236 CSF::field( $field, $field_default, $shortcode, 'shortcode' );
237
238 }
239
240 echo '</div>';
241
242 }
243
244 //
245 // View: group and repeater fields
246 $repeatable_fields = ( $view === 'repeater' && ! empty( $section['fields'] ) ) ? $section['fields'] : array();
247 $repeatable_fields = ( $view === 'group' && ! empty( $section['group_fields'] ) ) ? $section['group_fields'] : $repeatable_fields;
248
249 if ( ! empty( $repeatable_fields ) ) {
250
251 $button_title = ( ! empty( $section['button_title'] ) ) ? ' '. $section['button_title'] : esc_html__( 'Add New', 'csf' );
252 $inner_shortcode = ( ! empty( $section['group_shortcode'] ) ) ? $section['group_shortcode'] : $shortcode;
253
254 echo '<div class="csf--repeatable">';
255
256 echo '<div class="csf--repeat-shortcode">';
257
258 echo '<div class="csf-repeat-remove fas fa-times"></div>';
259
260 echo '<div class="csf-fields">';
261
262 foreach ( $repeatable_fields as $field ) {
263
264 if ( in_array( $field['type'], $unallows ) ) { $field['_notice'] = true; }
265
266 // Extra tag improves for spesific fields (border, spacing, dimensions etc...)
267 $field['tag_prefix'] = ( ! empty( $field['tag_prefix'] ) ) ? $field['tag_prefix'] .'_' : '';
268
269 $field_default = ( isset( $field['id'] ) ) ? $this->get_default( $field ) : '';
270
271 CSF::field( $field, $field_default, $inner_shortcode.'[0]', 'shortcode' );
272
273 }
274
275 echo '</div>';
276
277 echo '</div>';
278
279 echo '</div>';
280
281 echo '<div class="csf--repeat-button-block"><a class="button csf--repeat-button" href="#"><i class="fas fa-plus-circle"></i> '. $button_title .'</a></div>';
282
283 }
284
285 }
286
287 } else {
288 echo '<div class="csf-field csf-error-text">'. esc_html__( 'Error: Invalid nonce verification.', 'csf' ) .'</div>';
289 }
290
291 wp_send_json_success( array( 'content' => ob_get_clean() ) );
292
293 }
294
295 // Once editor setup for gutenberg and media buttons
296 public static function once_editor_setup() {
297
298 if ( function_exists( 'register_block_type' ) ) {
299 add_action( 'enqueue_block_editor_assets', array( 'CSF_Shortcoder', 'add_guteberg_blocks' ) );
300 }
301
302 if ( csf_wp_editor_api() ) {
303 add_action( 'media_buttons', array( 'CSF_Shortcoder', 'add_media_buttons' ) );
304 }
305
306 }
307
308 // Add gutenberg blocks.
309 public static function add_guteberg_blocks() {
310
311 wp_enqueue_script( 'csf-gutenberg-block', CSF::include_plugin_url( 'assets/js/gutenberg.js' ), array( 'wp-blocks', 'wp-editor', 'wp-element', 'wp-components' ) );
312
313 wp_localize_script( 'csf-gutenberg-block', 'csf_gutenberg_blocks', CSF::$shortcode_instances );
314
315 foreach ( CSF::$shortcode_instances as $value ) {
316
317 register_block_type( 'csf-gutenberg-block/block-'. $value['hash'], array(
318 'editor_script' => 'csf-gutenberg-block',
319 ) );
320
321 }
322
323 }
324
325 // Add media buttons
326 public static function add_media_buttons( $editor_id ) {
327
328 foreach ( CSF::$shortcode_instances as $value ) {
329 echo '<a href="#" class="button button-primary csf-shortcode-button" data-editor-id="'. esc_attr( $editor_id ) .'" data-modal-id="'. esc_attr( $value['modal_id'] ) .'">'. $value['button_title'] .'</a>';
330 }
331
332 }
333
334 }
335 }
336