button.php (2421B)
1 <?php 2 namespace Elementor\Core\Admin\UI\Components; 3 4 use Elementor\Core\Base\Base_Object; 5 use Elementor\Utils; 6 7 if ( ! defined( 'ABSPATH' ) ) { 8 exit; // Exit if accessed directly. 9 } 10 11 class Button extends Base_Object { 12 13 private $options; 14 /** 15 * @inheritDoc 16 */ 17 public function get_name() { 18 return 'admin-button'; 19 } 20 21 public function print_button() { 22 $options = $this->get_options(); 23 24 if ( empty( $options['text'] ) ) { 25 return; 26 } 27 28 $html_tag = ! empty( $options['url'] ) ? 'a' : 'button'; 29 $before = ''; 30 $icon = ''; 31 $attributes = []; 32 33 if ( ! empty( $options['icon'] ) ) { 34 $icon = '<i class="' . esc_attr( $options['icon'] ) . '"></i>'; 35 } 36 37 $classes = $options['classes']; 38 39 $default_classes = $this->get_default_options( 'classes' ); 40 41 $classes = array_merge( $classes, $default_classes ); 42 43 if ( ! empty( $options['type'] ) ) { 44 $classes[] = 'e-button--' . $options['type']; 45 } 46 47 if ( ! empty( $options['variant'] ) ) { 48 $classes[] = 'e-button--' . $options['variant']; 49 } 50 51 if ( ! empty( $options['before'] ) ) { 52 $before = '<span>' . wp_kses_post( $options['before'] ) . '</span>'; 53 } 54 55 if ( ! empty( $options['url'] ) ) { 56 $attributes['href'] = $options['url']; 57 if ( $options['new_tab'] ) { 58 $attributes['target'] = '_blank'; 59 } 60 } 61 62 $attributes['class'] = $classes; 63 64 $html = $before . '<' . $html_tag . ' ' . Utils::render_html_attributes( $attributes ) . '>'; 65 $html .= $icon; 66 $html .= '<span>' . sanitize_text_field( $options['text'] ) . '</span>'; 67 $html .= '</' . $html_tag . '>'; 68 69 echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped 70 } 71 72 /** 73 * @param string $option Optional default is null 74 * @return array|mixed 75 */ 76 private function get_options( $option = null ) { 77 return $this->get_items( $this->options, $option ); 78 } 79 80 /** 81 * @param null $option 82 * @return array 83 */ 84 private function get_default_options( $option = null ) { 85 $default_options = [ 86 'classes' => [ 'e-button' ], 87 'icon' => '', 88 'new_tab' => false, 89 'text' => '', 90 'type' => '', 91 'url' => '', 92 'variant' => '', 93 'before' => '', 94 ]; 95 96 if ( null !== $option && -1 !== in_array( $option, $default_options ) ) { 97 return $default_options[ $option ]; 98 } 99 100 return $default_options; 101 } 102 103 public function __construct( array $options ) { 104 $this->options = $this->merge_properties( $this->get_default_options(), $options ); 105 } 106 }