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