| @@ -1,0 +1,113 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * CMB2 Button and Multi Buttons Field Type | |
| 4 | + */ | |
| 5 | + | |
| 6 | +if( ! function_exists( 'cmb2_render_button' ) ) : | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * Render a button | |
| 10 | + * | |
| 11 | + * @param array $field The field data array | |
| 12 | + * @param mixed $value The stored value for this field | |
| 13 | + * @param integer|string $object_id The object ID | |
| 14 | + * @param string $object_type The object type | |
| 15 | + * @param CMB2_Types $field_type The field type object | |
| 16 | + * | |
| 17 | + * @return string HTML markup for our field | |
| 18 | + */ | |
| 19 | + function cmb2_render_button( $field, $value, $object_id, $object_type, $field_type ) { | |
| 20 | + | |
| 21 | + // Parse args | |
| 22 | + $attrs = $field_type->parse_args( 'button', array( | |
| 23 | + 'type' => 'button', | |
| 24 | + 'name' => $field_type->_name(), | |
| 25 | + 'id' => $field_type->_id(), | |
| 26 | + 'class' => 'button ' . ( isset( $field->args['button'] ) && ! empty( $field->args['button'] ) ? 'button-' . $field->args['button'] : '' ), | |
| 27 | + ) ); | |
| 28 | + | |
| 29 | + $button_pattern = '%s<button %s>%s</button>'; | |
| 30 | + | |
| 31 | + if( isset( $field->args['action'] ) && ! empty( $field->args['action'] ) ) { | |
| 32 | + $attrs['name'] = 'gamipress-action'; | |
| 33 | + $attrs['value'] = $field->args['action']; | |
| 34 | + $attrs['type'] = 'submit'; | |
| 35 | + } | |
| 36 | + | |
| 37 | + $icon_html = ''; | |
| 38 | + | |
| 39 | + if( isset( $field->args['icon'] ) && ! empty( $field->args['icon'] ) ) { | |
| 40 | + $icon_html = '<i class="dashicons ' . $field->args['icon'] . '"></i>'; | |
| 41 | + } | |
| 42 | + | |
| 43 | + echo sprintf( $button_pattern, | |
| 44 | + $field_type->_desc( true ), | |
| 45 | + $field_type->concat_attrs( $attrs ), | |
| 46 | + $icon_html . ( isset( $field->args['label'] ) && ! empty( $field->args['label'] ) ? $field->args['label'] : $field->args( 'name' ) ) | |
| 47 | + ); | |
| 48 | + } | |
| 49 | + add_action( 'cmb2_render_button', 'cmb2_render_button', 10, 5 ); | |
| 50 | + | |
| 51 | +endif; | |
| 52 | + | |
| 53 | +if( ! function_exists( 'cmb2_render_multi_buttons' ) ) : | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Render a multi buttons | |
| 57 | + * | |
| 58 | + * @param array $field The field data array | |
| 59 | + * @param mixed $value The stored value for this field | |
| 60 | + * @param integer|string $object_id The object ID | |
| 61 | + * @param string $object_type The object type | |
| 62 | + * @param CMB2_Types $field_type The field type object | |
| 63 | + * | |
| 64 | + * @return string HTML markup for our field | |
| 65 | + */ | |
| 66 | + function cmb2_render_multi_buttons( $field, $value, $object_id, $object_type, $field_type ) { | |
| 67 | + | |
| 68 | + if( ! isset( $field->args['buttons'] ) ) { | |
| 69 | + $field->args['buttons'] = array(); | |
| 70 | + } | |
| 71 | + | |
| 72 | + echo $field_type->_desc( true ); | |
| 73 | + | |
| 74 | + foreach( $field->args['buttons'] as $button_id => $button ) { | |
| 75 | + | |
| 76 | + $attrs = $field_type->parse_args( 'multibutton', array( | |
| 77 | + 'class' => 'button ' . ( isset( $button['button'] ) && ! empty( $button['button'] ) ? 'button-' . $button['button'] : '' ), | |
| 78 | + 'id' => $button_id, | |
| 79 | + ) ); | |
| 80 | + | |
| 81 | + $button_pattern = '<button type="button" %s>%s</button>'; | |
| 82 | + | |
| 83 | + if( isset( $button['type'] ) && $button['type'] === 'link' ) { | |
| 84 | + | |
| 85 | + $href = 'javascript:void(0);'; | |
| 86 | + | |
| 87 | + if( isset( $button['link'] ) ) { | |
| 88 | + $href = $button['link']; | |
| 89 | + } | |
| 90 | + | |
| 91 | + if( isset( $button['target'] ) ) { | |
| 92 | + $attrs['target'] = $button['target']; | |
| 93 | + } | |
| 94 | + | |
| 95 | + $button_pattern = '<a href="' . $href . '" %s>%s</a>'; | |
| 96 | + } | |
| 97 | + | |
| 98 | + $icon_html = ''; | |
| 99 | + | |
| 100 | + if( isset( $button['icon'] ) && ! empty( $button['icon'] ) ) { | |
| 101 | + $icon_html = '<i class="dashicons ' . $button['icon'] . '"></i>'; | |
| 102 | + } | |
| 103 | + | |
| 104 | + echo sprintf( $button_pattern, | |
| 105 | + $field_type->concat_attrs( $attrs ), | |
| 106 | + $icon_html . ( isset( $button['label'] ) && ! empty( $button['label'] ) ? $button['label'] : '' ) | |
| 107 | + ); | |
| 108 | + } | |
| 109 | + | |
| 110 | + } | |
| 111 | + add_action( 'cmb2_render_multi_buttons', 'cmb2_render_multi_buttons', 10, 5 ); | |
| 112 | + | |
| 113 | +endif; | |