PluginProbe
Powerkit – Supercharge your WordPress Site / trunk
Powerkit – Supercharge your WordPress Site vtrunk
3.1.1 3.1.0 3.0.9 3.0.8 trunk 2.2.6 2.2.7 2.2.8 2.2.9 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 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 All 87 releases
powerkit / modules / basic-elements / admin / class-powerkit-basic-elements-admin.php

class-powerkit-basic-elements-admin.php in Powerkit – Supercharge your WordPress Site trunk, at modules/basic-elements/admin/class-powerkit-basic-elements-admin.php

469 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The admin-specific functionality of the module.
4 *
5 * @link https://codesupply.co
6 * @since 1.0.0
7 *
8 * @package Powerkit
9 * @subpackage Modules/Admin
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * The admin-specific functionality of the module.
19 */
20 class Powerkit_Basic_Elements_Admin extends Powerkit_Module_Admin {
21
22 /**
23 * Initialize
24 */
25 public function initialize() {
26 add_action( 'wp_ajax_powerkit_basic_shortcodes_sections', array( $this, 'powerkit_basic_shortcodes_ajax_panel' ) );
27 add_filter( 'mce_buttons', array( $this, 'powerkit_basic_shortcodes_register_buttons' ) );
28 add_filter( 'mce_external_plugins', array( $this, 'powerkit_basic_shortcodes_register_plugin' ) );
29 }
30
31
32 /**
33 * Register shorcodes button for TinyMCE buttons (Visual tab).
34 *
35 * @since 1.0.0
36 * @param array $buttons First-row list of buttons.
37 * @return array Buttons.
38 */
39 public function powerkit_basic_shortcodes_register_buttons( $buttons ) {
40 if ( current_user_can( 'edit_posts' ) ) {
41 array_push( $buttons, 'powerkit_basic_shortcodes_button' );
42 }
43
44 return $buttons;
45 }
46
47 /**
48 * Register shortcodes plugin
49 *
50 * @since 1.0.0
51 * @param array $plugins An array of external TinyMCE plugins.
52 * @return array Plugins.
53 */
54 public function powerkit_basic_shortcodes_register_plugin( $plugins ) {
55 if ( current_user_can( 'edit_posts' ) ) {
56 $plugins['powerkit_basic_shortcodes'] = trailingslashit( plugin_dir_url( __FILE__ ) ) . 'js/admin-powerkit-basic-elements.js';
57 }
58
59 return $plugins;
60 }
61
62 /**
63 * Generate Section Fileds
64 *
65 * @since 1.0.0
66 * @param array $section Sections Fields.
67 * @param array $counter Unique field identifier.
68 */
69 public function generate_section_fields( $section, $counter = 0 ) {
70 if ( isset( $section['fields'] ) && is_array( $section['fields'] ) ) {
71
72 // Default Field Settings.
73 $default_field = array(
74 'type' => 'input',
75 'name' => '',
76 'label' => '',
77 'desc' => '',
78 'default' => '',
79 'attrs' => array(),
80 'style' => 'vertical', // For radio.
81 'suffix' => '', // For inputs.
82 'options' => array(), // For select, radio.
83 'fields' => array(), // For repeater.
84 );
85
86 foreach ( $section['fields'] as $field ) {
87
88 $counter++;
89
90 // Merge Settings.
91 $field = array_merge( $default_field, $field );
92
93 // Attrs.
94 $attrs = null;
95
96 if ( ! empty( $field['attrs'] ) ) {
97 foreach ( $field['attrs'] as $key => $value ) {
98 $attrs[] = sprintf( '%s="%s"', $key, $value );
99 }
100 $attrs = implode( ' ', $attrs );
101 }
102
103 // Output by type.
104 switch ( $field['type'] ) {
105 case 'section':
106 ?>
107 <tr>
108 <th><h3><?php echo wp_kses_post( $field['label'] ); ?></h3></th><td>&nbsp;</td>
109 </tr>
110 <?php
111 break;
112 case 'input':
113 ?>
114 <tr>
115 <th><?php echo wp_kses_post( $field['label'] ); ?>:<small><?php echo wp_kses_post( $field['desc'] ); ?></small></th>
116 <td>
117 <input name="<?php echo esc_attr( $field['name'] ); ?>" type="text" value="<?php echo esc_attr( $field['default'] ); ?>" <?php echo wp_kses_data( $attrs ); ?>><?php echo esc_attr( $field['suffix'] ); ?>
118 </td>
119 </tr>
120 <?php
121 break;
122 case 'textarea':
123 ?>
124 <tr>
125 <th><?php echo wp_kses_post( $field['label'] ); ?>:<small><?php echo wp_kses_post( $field['desc'] ); ?></small></th>
126 <td>
127 <textarea name="<?php echo esc_attr( $field['name'] ); ?>" <?php echo wp_kses_data( $attrs ); ?>><?php echo wp_kses_post( $field['default'] ); ?></textarea>
128 </td>
129 </tr>
130 <?php
131 break;
132 case 'select':
133 ?>
134 <tr>
135 <th><?php echo wp_kses_post( $field['label'] ); ?>:<small><?php echo wp_kses_post( $field['desc'] ); ?></small></th>
136 <td>
137 <select name="<?php echo esc_attr( $field['name'] ); ?>" <?php echo wp_kses_data( $attrs ); ?>>
138 <?php if ( isset( $field['options'] ) && $field['options'] ) : ?>
139 <?php foreach ( $field['options'] as $key => $option ) : ?>
140 <option value="<?php echo esc_attr( $key ); ?>" <?php selected( $field['default'], esc_attr( $key ) ); ?>><?php echo esc_attr( $option ); ?></option>
141 <?php endforeach; ?>
142 <?php endif; ?>
143 </select>
144 </td>
145 </tr>
146 <?php
147 break;
148
149 case 'checkbox':
150 ?>
151 <tr>
152 <th><?php echo wp_kses_post( $field['label'] ); ?>:<small><?php echo wp_kses_post( $field['desc'] ); ?></small></th>
153 <td>
154 <input name="<?php echo esc_attr( $field['name'] ); ?>" type="checkbox" value="true" <?php checked( (bool) $field['default'], true ); ?> <?php echo wp_kses_data( $attrs ); ?>>
155 </td>
156 </tr>
157 <?php
158 break;
159
160 case 'radio':
161 ?>
162 <tr>
163 <th><?php echo wp_kses_post( $field['label'] ); ?>:<small><?php echo wp_kses_post( $field['desc'] ); ?></small></th>
164 <td>
165 <?php if ( isset( $field['options'] ) && $field['options'] ) : ?>
166 <?php foreach ( $field['options'] as $key => $option ) : $counter++; ?>
167 <?php $id = sprintf( 'field-%s-%s-%s', $section['name'], $field['name'], $counter ); ?>
168 <input id="<?php echo esc_attr( $id ); ?>" name="<?php echo esc_attr( $field['name'] ); ?>" type="radio" value="<?php echo esc_attr( $key ); ?>" <?php checked( $field['default'], $key ); ?> <?php echo wp_kses_data( $attrs ); ?>>
169 <label for="<?php echo esc_attr( $id ); ?>"><?php echo esc_attr( $option ); ?></label>
170 <?php if ( 'vertical' === $field['style'] ) : ?>
171 <br>
172 <?php else : ?>
173 &nbsp;
174 <?php endif; ?>
175 <?php endforeach; ?>
176 <?php endif; ?>
177 </td>
178 </tr>
179 <?php
180 break;
181
182 case 'content':
183 ?>
184 <tr>
185 <th><?php echo wp_kses_post( $field['label'] ); ?>:<small><?php echo wp_kses_post( $field['desc'] ); ?></small></th>
186 <td>
187 <textarea name="<?php echo esc_attr( $field['name'] ); ?>" <?php echo wp_kses_data( $attrs ); ?>><?php echo wp_kses_post( $field['default'] ); ?></textarea>
188 </td>
189 </tr>
190 <?php
191 break;
192
193 case 'repeater':
194 ?>
195 <tr>
196 <th><?php echo wp_kses_post( $field['label'] ); ?>:</label><small><?php echo wp_kses_post( $field['desc'] ); ?></small></th>
197 <td class="basic-shortcodes-repeater-field">
198 <div data-repeater-list="<?php echo esc_attr( $field['base'] ); ?>" >
199 <table class="form-table" data-repeater-item>
200 <tbody>
201 <?php $this->generate_section_fields( $field, $counter ); ?>
202 </tbody>
203 <tfoot>
204 <tr>
205 <th></th>
206 <td>
207 <span class="button-secondary" data-repeater-delete><?php esc_html_e( 'Delete Item', 'powerkit' ); ?></span>
208 </td>
209 </tr>
210 </tfoot>
211 </table>
212 </div>
213 <span data-repeater-create class="button-primary"><?php esc_html_e( 'Add Item', 'powerkit' ); ?></span>
214 </td>
215 </tr>
216 <?php
217 break;
218 }
219 }
220 }
221 }
222
223 /**
224 * Ajax Load Admin Shortcodes Panel
225 *
226 * @since 1.0.0
227 */
228 public function powerkit_basic_shortcodes_ajax_panel() {
229
230 // Allow themes and plugins to enable/disable specific shortcodes UI panel.
231 $sections = apply_filters( 'powerkit_basic_shortcodes_ui_args', array() );
232
233 // Sort elements.
234 usort( $sections, function ( $a, $b ) {
235 return $a['priority'] - $b['priority'];
236 } );
237 ?>
238 <div class="wrap powerkit_basic_shortcodes_wrap">
239
240 <div class="powerkit_basic_shortcodes_tabs">
241
242 <ul>
243 <?php foreach ( $sections as $section ) : ?>
244 <li><a data-nav="<?php echo esc_attr( $section['name'] ); ?>" href="#"><?php echo esc_html( $section['title'] ); ?></a></li>
245 <?php endforeach; ?>
246 </ul>
247
248 </div>
249
250 <div class="powerkit_basic_shortcodes_tabs_sections">
251
252 <?php foreach ( $sections as $section ) : ?>
253 <div class="hidable wrap tabs-<?php echo esc_attr( $section['name'] ); ?>" style="display: none;">
254
255 <form class="powerkit_basic_shortcodes_tab" enctype="multipart/form-data">
256 <table class="form-table">
257 <tbody>
258 <?php $this->generate_section_fields( $section ); ?>
259
260 <tr>
261 <th><input type="submit" class="button-primary" value="<?php esc_html_e( 'Insert', 'powerkit' ); ?>"></th>
262 <td>&nbsp;</td>
263 </tr>
264 </tbody>
265 </table>
266 </form>
267 </div>
268
269 <script type="text/javascript">
270 /* <![CDATA[ */
271 (function($) {
272
273 // Insert Shortcode.
274 $('.tabs-<?php echo esc_attr( $section['name'] ); ?> form').submit( function( e ) {
275 e.preventDefault();
276
277 // Hide Popup.
278 tb_remove();
279
280 // Get input values from form.
281 var fieldsObj = $( this ).serializeObject();
282
283 // Vars.
284 var shortcodeContent = '',
285 shortcodeAttrs = [];
286
287 <?php foreach ( (array) $section['fields'] as $field ) : ?>
288
289 var fieldName = '<?php echo esc_attr( isset( $field['name'] ) ? $field['name'] : '' ); ?>';
290 <?php
291 switch ( $field['type'] ) {
292 case 'section':
293 break;
294 case 'repeater':
295 ?>
296 var subContentVars = [],
297 subCheckboxVars = [],
298 shortcodeBase = '<?php echo esc_attr( $field['base'] ); ?>';
299
300 <?php foreach ( (array) $field['fields'] as $sub_field ) : ?>
301
302 <?php if ( 'content' === $sub_field['type'] ) : ?>
303 subContentVars.push( '<?php echo esc_attr( $sub_field['name'] ); ?>' );
304 <?php endif; ?>
305
306 <?php if ( 'checkbox' === $sub_field['type'] ) : ?>
307 subCheckboxVars.push( '<?php echo esc_attr( $sub_field['name'] ); ?>' );
308 <?php endif; ?>
309
310 <?php endforeach; ?>
311
312 if( typeof fieldsObj[ shortcodeBase ] !== 'undefined' ) {
313
314 $.each( fieldsObj[ shortcodeBase ], function( key, obj ) {
315
316 var subShortcodeContent = '',
317 subShortcodeAttrs = [];
318
319 $.each( obj, function( s_key, s_obj ) {
320 if( typeof s_obj !== 'undefined' ) {
321
322 // Field "Content" fix.
323 if( ! $.inArray( s_key, subContentVars ) ) {
324 subShortcodeContent += s_obj;
325 } else {
326
327 // Other fields.
328 subShortcodeAttrs.push( s_key + '="' + s_obj + '"' );
329 }
330 }
331
332 } );
333
334 // Add Inner Shortcode.
335 if( subShortcodeContent ) {
336 shortcodeContent += '[' + shortcodeBase + ' ' + subShortcodeAttrs.join( ' ' ) + ']<br>' + subShortcodeContent + '<br>[/' + shortcodeBase + ']<br>';
337 } else {
338 shortcodeContent += '[' + shortcodeBase + ' ' + subShortcodeAttrs.join( ' ' ) + ']<br>';
339 }
340
341 } );
342 }
343 <?php
344 break;
345 case 'content':
346 ?>
347 if( typeof fieldsObj[ fieldName ] !== 'undefined' ) {
348 shortcodeContent += fieldsObj[ fieldName ];
349 }
350 <?php
351 break;
352 case 'checkbox':
353 ?>
354 if( typeof fieldsObj[ fieldName ] !== 'undefined' ) {
355 var fieldAttr = fieldName + '="' + fieldsObj[ fieldName ] + '"';
356 } else {
357 var fieldAttr = fieldName + '="false"';
358 }
359
360 shortcodeAttrs.push( fieldAttr );
361 <?php
362 break;
363 default:
364 ?>
365 if( typeof fieldsObj[ fieldName ] !== 'undefined' ) {
366 var fieldAttr = fieldName + '="' + fieldsObj[ fieldName ] + '"';
367 } else {
368 var fieldAttr = fieldName + '=""';
369 }
370
371 shortcodeAttrs.push( fieldAttr );
372 <?php
373 break;
374 }
375 ?>
376 <?php endforeach; ?>
377
378 // Add Shortcode.
379 var shortcodeName = '<?php echo esc_attr( $section['base'] ); ?>';
380
381 if( shortcodeContent ) {
382
383 // Trim Line Breaks.
384 shortcodeContent = shortcodeContent.replace( /^<br>|<br>$/gm, '' );
385
386 var content = '[' + shortcodeName + ' ' + shortcodeAttrs.join( ' ' ) + ']<br>' + shortcodeContent + '<br>[/' + shortcodeName + ']';
387 } else {
388 var content = '[' + shortcodeName + ' ' + shortcodeAttrs.join( ' ' ) + ']';
389 }
390
391 // Remove odd space.
392 content = content.replace(/\s\]/g, ']');
393
394 // Convert br.
395 content = content.replace(/([^>])\n/g, '$1<br/>');
396
397 powerkit_basic_shortcodes.setContent( content );
398 });
399
400 })(jQuery);
401 /* ]]> */
402 </script>
403 <?php endforeach; ?>
404
405 </div>
406
407 <script type="text/javascript">
408 /* <![CDATA[ */
409 ( function( $ ) {
410
411 // Tabs Navigation.
412 $( '.powerkit_basic_shortcodes_tabs a' ).click( function( e ) {
413 e.preventDefault();
414 powerkit_basic_shortcodes_tabs_switch( $( this ) );
415 } );
416
417 powerkit_basic_shortcodes_tabs_switch( $( '.powerkit_basic_shortcodes_tabs a' ).first() );
418
419 function powerkit_basic_shortcodes_tabs_switch( obj ) {
420 $( '.powerkit_basic_shortcodes_tabs_sections .hidable' ).hide();
421 $( '.powerkit_basic_shortcodes_tabs_sections .tabs-' + obj.attr( 'data-nav' ) ).show();
422 $( '.powerkit_basic_shortcodes_tabs li' ).removeClass( 'current' );
423 obj.parent().addClass( 'current' );
424 }
425
426 // Init Repeater.
427 $( document ).ready( function() {
428 $( '.basic-shortcodes-repeater-field' ).each( function() {
429 $( this ).repeater( {
430 show: function() {
431 $( this ).slideDown();
432 },
433 hide: function( deleteElement ) {
434 $( this ).fadeOut( 400 );
435 },
436 isFirstItemUndeletable: true,
437 } );
438 } );
439 } );
440
441 } )( jQuery );
442 /* ]]> */
443 </script>
444 </div>
445 <?php
446 die();
447 }
448
449 /**
450 * Register the stylesheets and JavaScript for the admin area.
451 *
452 * @param string $page Current page.
453 */
454 public function admin_enqueue_scripts( $page ) {
455
456 add_thickbox();
457
458 wp_enqueue_style( 'wp-color-picker' );
459
460 // Styles.
461 wp_enqueue_style( 'powerkit-basic-elements', powerkit_style( plugin_dir_url( __FILE__ ) . 'css/admin-powerkit-basic-elements.css' ), array(), powerkit_get_setting( 'version' ), 'all' );
462
463 wp_enqueue_script( 'wp-color-picker' );
464 // Scripts.
465 wp_enqueue_script( 'powerkit-jquery-serialize', plugin_dir_url( __FILE__ ) . 'js/jquery.serialize-to-json.min.js', array( 'jquery' ), powerkit_get_setting( 'version' ), false );
466 wp_enqueue_script( 'powerkit-jquery-repeater', plugin_dir_url( __FILE__ ) . 'js/jquery.repeater.min.js', array( 'jquery' ), powerkit_get_setting( 'version' ), false );
467 }
468 }
469