| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package RGC\CMB2\Field_JS_Controls |
| 4 |
* @author Ruben Garcia (RubenGC) <[email protected]>, GamiPress <[email protected]> |
| 5 |
* @copyright Copyright (c) Tsunoa |
| 6 |
* |
| 7 |
* Plugin Name: CMB2 Field JS Controls |
| 8 |
* Plugin URI: https://github.com/rubengc/cmb2-field-js-controls |
| 9 |
* GitHub Plugin URI: https://github.com/rubengc/cmb2-field-js-controls |
| 10 |
* Description: Show any field similar to Wordpress publishing actions (Post/Page post_status, visibility and post_date submit box field). |
| 11 |
* Version: 1.0.0 |
| 12 |
* Author: Tsunoa |
| 13 |
* Author URI: https://tsunoa.com/ |
| 14 |
* License: GPLv2+ |
| 15 |
*/ |
| 16 |
// Exit if accessed directly |
| 17 |
if( !defined( 'ABSPATH' ) ) exit; |
| 18 |
|
| 19 |
// Prevent CMB2 autoload adding "RGC_" at start |
| 20 |
if( !class_exists( 'RGC_CMB2_Field_JS_Controls' ) ) { |
| 21 |
|
| 22 |
/** |
| 23 |
* Class RGC_CMB2_Field_JS_Controls |
| 24 |
*/ |
| 25 |
class RGC_CMB2_Field_JS_Controls { |
| 26 |
|
| 27 |
/** |
| 28 |
* Current version number |
| 29 |
*/ |
| 30 |
const VERSION = '1.0.0'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Initialize the plugin by hooking into CMB2 |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
add_action( 'admin_enqueue_scripts', array( $this, 'setup_admin_scripts' ) ); |
| 37 |
|
| 38 |
// TODO: Find a way to add this content if field has parameter 'js_controls' => true |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @param array $field_args Current field args |
| 43 |
* @param CMB2_Field $field Current field object |
| 44 |
*/ |
| 45 |
public function before_row( $field_args, $field ) { |
| 46 |
$field_args = $this->parse_field_args( $field_args ); |
| 47 |
|
| 48 |
if( ! empty( $field_args['js_controls']['icon'] ) ) { |
| 49 |
$icon = ( ( strpos( $field_args['js_controls']['icon'], 'dashicons-' ) !== false ) ? 'dashicons ' : '' ) . $field_args['js_controls']['icon']; |
| 50 |
} |
| 51 |
|
| 52 |
$id_attr_prefix = 'cmb-field-js-controls-' . $field_args['id']; |
| 53 |
|
| 54 |
?> |
| 55 |
<div id="<?php echo $id_attr_prefix; ?>-before" class="cmb-field-js-controls-before"> |
| 56 |
|
| 57 |
<?php if( isset( $icon ) ) : ?> |
| 58 |
<span id="<?php echo $id_attr_prefix; ?>-icon" class="cmb-field-js-controls-icon <?php echo $icon; ?>"></span> |
| 59 |
<?php endif; ?> |
| 60 |
|
| 61 |
<span id="<?php echo $id_attr_prefix; ?>-label" class="cmb-field-js-controls-label"><?php echo $field_args['name']; ?>:</span> |
| 62 |
|
| 63 |
<div id="<?php echo $id_attr_prefix; ?>-value" class="cmb-field-js-controls-value"><?php $field->render_column(); ?></div> |
| 64 |
|
| 65 |
<?php if( $field_args['js_controls']['edit_button'] ) : ?> |
| 66 |
<a href="#<?php echo $field_args['id']; ?>" id="<?php echo $id_attr_prefix; ?>-edit" class="cmb-field-js-controls-edit hide-if-no-js"><?php echo $field_args['js_controls']['edit_button']; ?></a> |
| 67 |
<?php endif; ?> |
| 68 |
</div> |
| 69 |
<?php |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* @param array $field_args Current field args |
| 74 |
* @param CMB2_Field $field Current field object |
| 75 |
*/ |
| 76 |
public function after_row( $field_args, $field ) { |
| 77 |
$field_args = $this->parse_field_args( $field_args ); |
| 78 |
|
| 79 |
$id_attr_prefix = 'cmb-field-js-controls-' . $field_args['id']; |
| 80 |
|
| 81 |
?> |
| 82 |
<div id="<?php echo $id_attr_prefix; ?>-after" class="cmb-field-js-controls-after hide-if-js"> |
| 83 |
<?php if( $field_args['js_controls']['save_button'] ) : ?> |
| 84 |
<a href="#<?php echo $field_args['id']; ?>" |
| 85 |
id="<?php echo $id_attr_prefix; ?>-save" |
| 86 |
class="cmb-field-js-controls-save <?php echo esc_attr( $field_args['js_controls']['save_button_classes'] ); ?>" |
| 87 |
><?php echo $field_args['js_controls']['save_button']; ?></a> |
| 88 |
<?php endif; ?> |
| 89 |
|
| 90 |
<?php if( $field_args['js_controls']['cancel_button'] ) : ?> |
| 91 |
<a href="#<?php echo $field_args['id']; ?>" |
| 92 |
id="<?php echo $id_attr_prefix; ?>-cancel" |
| 93 |
class="cmb-field-js-controls-cancel <?php echo esc_attr( $field_args['js_controls']['cancel_button_classes'] ); ?>" |
| 94 |
><?php echo $field_args['js_controls']['cancel_button']; ?></a> |
| 95 |
<?php endif; ?> |
| 96 |
</div> |
| 97 |
<?php |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @param array $field_args Current field args |
| 102 |
* @return array |
| 103 |
*/ |
| 104 |
private function parse_field_args( $field_args ) { |
| 105 |
$field_args['js_controls'] = array_merge( |
| 106 |
array( |
| 107 |
'icon' => '', |
| 108 |
'edit_button' => __( 'Edit' ), |
| 109 |
'save_button' => __( 'OK' ), |
| 110 |
'save_button_classes' => 'button', |
| 111 |
'cancel_button' => __( 'Cancel' ), |
| 112 |
'cancel_button_classes' => '', |
| 113 |
), |
| 114 |
( ( isset( $field_args['js_controls'] ) && is_array( $field_args['js_controls'] ) ) ? $field_args['js_controls'] : array() ) |
| 115 |
); |
| 116 |
|
| 117 |
return $field_args; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Enqueue scripts and styles |
| 122 |
*/ |
| 123 |
public function setup_admin_scripts() { |
| 124 |
wp_register_script( 'cmb-js-controls-event-manager', plugins_url( 'js/event-manager.min.js', __FILE__ ), array( 'jquery' ), self::VERSION, true ); |
| 125 |
wp_register_script( 'cmb-js-controls', plugins_url( 'js/js-controls.js', __FILE__ ), array( 'jquery', 'cmb-js-controls-event-manager' ), self::VERSION, true ); |
| 126 |
|
| 127 |
wp_enqueue_script( 'cmb-js-controls-event-manager' ); |
| 128 |
wp_enqueue_script( 'cmb-js-controls' ); |
| 129 |
|
| 130 |
wp_enqueue_style( 'cmb-js-controls', plugins_url( 'css/js-controls.css', __FILE__ ), array(), self::VERSION ); |
| 131 |
} |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
//$cmb2_field_js_controls = new RGC_CMB2_Field_JS_Controls(); |
| 136 |
|
| 137 |
// TODO: Temporal solution to output html content |
| 138 |
function js_controls_before( $field_args, $field ) { |
| 139 |
$cmb2_field_js_controls = new RGC_CMB2_Field_JS_Controls(); |
| 140 |
|
| 141 |
$cmb2_field_js_controls->before_row( $field_args, $field ); |
| 142 |
} |
| 143 |
|
| 144 |
function js_controls_after( $field_args, $field ) { |
| 145 |
$cmb2_field_js_controls = new RGC_CMB2_Field_JS_Controls(); |
| 146 |
|
| 147 |
$cmb2_field_js_controls->after_row( $field_args, $field ); |
| 148 |
} |
| 149 |
|
| 150 |
$cmb2_field_js_controls = new RGC_CMB2_Field_JS_Controls(); |
| 151 |
} |