PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/fusion-core/shortcodes/class-button.php

https://gitlab.com/woxiprogrammers/infinia-wordpress
PHP | 377 lines | 268 code | 90 blank | 19 comment | 72 complexity | a1d10b35d9564ca0a73e55a10782f932 MD5 | raw file
  1. <?php
  2. class FusionSC_Button {
  3. private $button_counter = 1;
  4. public static $args;
  5. /**
  6. * Initiate the shortcode
  7. */
  8. public function __construct() {
  9. add_filter( 'fusion_attr_button-shortcode', array( $this, 'attr' ) );
  10. add_filter( 'fusion_attr_button-shortcode-icon-divder', array( $this, 'icon_divider_attr' ) );
  11. add_filter( 'fusion_attr_button-shortcode-icon', array( $this, 'icon_attr' ) );
  12. add_filter( 'fusion_attr_button-shortcode-button-text', array( $this, 'button_text_attr' ) );
  13. add_shortcode( 'button', array( $this, 'render' ) );
  14. }
  15. /**
  16. * Render the shortcode
  17. * @param array $args Shortcode paramters
  18. * @param string $content Content between shortcode
  19. * @return string HTML output
  20. */
  21. function render( $args, $content = '') {
  22. global $smof_data;
  23. $defaults = FusionCore_Plugin::set_shortcode_defaults(
  24. array(
  25. 'class' => '',
  26. 'id' => '',
  27. 'accent_color' => strtolower( $smof_data['button_accent_color'] ),
  28. 'accent_hover_color' => strtolower( $smof_data['button_accent_hover_color'] ),
  29. 'bevel_color' => strtolower( $smof_data['button_bevel_color'] ),
  30. 'border_width' => strtolower( $smof_data['button_border_width'] ),
  31. 'color' => 'default',
  32. 'gradient_colors' => strtolower( $smof_data['button_gradient_top_color'] ) . '|' . strtolower( $smof_data['button_gradient_bottom_color'] ),
  33. 'icon' => '',
  34. 'icon_divider' => 'no',
  35. 'icon_position' => 'left',
  36. 'link' => '',
  37. 'modal' => '',
  38. 'shape' => strtolower( $smof_data['button_shape'] ),
  39. 'size' => strtolower( $smof_data['button_size'] ),
  40. 'target' => '_self',
  41. 'title' => '',
  42. 'type' => strtolower( $smof_data['button_type'] ),
  43. 'alignment' => '',
  44. 'animation_type' => '',
  45. 'animation_direction' => 'down',
  46. 'animation_speed' => '',
  47. // Combined in accent_color
  48. 'border_color' => '',
  49. 'icon_color' => '',
  50. 'text_color' => '',
  51. // Combined in accent_hover_color
  52. 'border_hover_color' => '',
  53. 'icon__hover_color' => '',
  54. 'text_hover_color' => '',
  55. // Combined with gradient_colors
  56. 'gradient_hover_colors' => strtolower( $smof_data['button_gradient_top_color_hover'] ) . '|' . strtolower( $smof_data['button_gradient_bottom_color_hover'] ),
  57. ), $args
  58. );
  59. if( $defaults['color'] == 'default' ) {
  60. $defaults['accent_color'] = strtolower( $smof_data['button_accent_color'] );
  61. $defaults['accent_hover_color'] = strtolower( $smof_data['button_accent_hover_color'] );
  62. $defaults['bevel_color'] = strtolower( $smof_data['button_bevel_color'] );
  63. $defaults['gradient_colors'] = strtolower( $smof_data['button_gradient_top_color'] ) . '|' . strtolower( $smof_data['button_gradient_bottom_color'] );
  64. $defaults['gradient_hover_colors'] = strtolower( $smof_data['button_gradient_top_color_hover'] ) . '|' . strtolower( $smof_data['button_gradient_bottom_color_hover'] );
  65. }
  66. // Combined variable settings
  67. $old_border_color = $defaults['border_color'];
  68. $old_text_color = $defaults['text_color'];
  69. $defaults['border_color'] = $defaults['icon_color'] = $defaults['text_color'] = $defaults['accent_color'];
  70. $defaults['border_hover_color'] = $defaults['icon_hover_color'] = $defaults['text_hover_color'] = $defaults['accent_hover_color'];
  71. //$defaults['gradient_hover_colors'] = $defaults['gradient_hover_colors']; // See below for array reverting
  72. if( $old_border_color ) {
  73. $defaults['border_color'] = $old_border_color;
  74. }
  75. if( $old_text_color ) {
  76. $defaults['text_color'] = $old_border_color;
  77. }
  78. if( $defaults['modal'] ) {
  79. $defaults['link'] = '#';
  80. }
  81. $defaults['type'] = strtolower( $defaults['type'] );
  82. extract( $defaults );
  83. self::$args = $defaults;
  84. $styles = '';
  85. if( ( $color == 'custom' || $color == 'default' ) &&
  86. (
  87. $bevel_color ||
  88. $accent_color ||
  89. $accent_hover_color ||
  90. $border_width ||
  91. $gradient_colors
  92. )
  93. ) {
  94. $general_styles = $button_3d_styles = $hover_styles = $gradient_styles = $gradient_hover_styles = '';
  95. $styles = '<style type="text/css" scoped="scoped">';
  96. if( ( $type == '3d') &&
  97. $bevel_color
  98. ) {
  99. if( $size == 'small' ) {
  100. $button_3d_add = 0;
  101. } elseif( $size == 'medium' ) {
  102. $button_3d_add = 1;
  103. } elseif( $size == 'large' ) {
  104. $button_3d_add = 2;
  105. } elseif( $size == 'xlarge' ) {
  106. $button_3d_add = 3;
  107. }
  108. $button_3d_shadow_part_1 = 'inset 0px 1px 0px #fff,';
  109. $button_3d_shadow_part_2 = sprintf( '0px %spx 0px %s,', 2 + $button_3d_add, $bevel_color );
  110. $button_3d_shadow_part_3 = sprintf( '1px %spx %spx 3px rgba(0,0,0,0.3)', 4 + $button_3d_add, 4 + $button_3d_add );
  111. if( $size == 'small' ) {
  112. $button_3d_shadow_part_3 = str_replace( '3px', '2px', $button_3d_shadow_part_3 );
  113. }
  114. $button_3d_shadow = $button_3d_shadow_part_1 . $button_3d_shadow_part_2 . $button_3d_shadow_part_3;
  115. $button_3d_styles = sprintf( '-webkit-box-shadow: %s;-moz-box-shadow: %s;box-shadow: %s;', $button_3d_shadow, $button_3d_shadow, $button_3d_shadow );
  116. }
  117. if( $border_width ) {
  118. $general_styles .= sprintf( 'border-width:%s;', $border_width );
  119. $hover_styles .= sprintf( 'border-width:%s;', $border_width );
  120. }
  121. if( $accent_color ) {
  122. if( ! $old_text_color ) {
  123. $general_styles .= sprintf( 'color:%s;', $accent_color );
  124. }
  125. if( ! $old_border_color ) {
  126. $general_styles .= sprintf( 'border-color:%s;', $border_color );
  127. }
  128. }
  129. if( $old_border_color ) {
  130. $general_styles .= sprintf( 'border-color:%s;', $old_border_color );
  131. }
  132. if( $old_text_color ) {
  133. $general_styles .= sprintf( 'color:%s;', $old_text_color );
  134. }
  135. if( $accent_hover_color && ! $old_border_color ) {
  136. $hover_styles .= sprintf( 'border-color:%s;', $accent_hover_color );
  137. } elseif( $accent_color && ! $old_border_color ) {
  138. $hover_styles .= sprintf( 'border-color:%s;', $accent_color );
  139. }
  140. if( $accent_hover_color && ! $old_text_color ) {
  141. $hover_styles .= sprintf( 'color:%s;', $accent_hover_color );
  142. } elseif( $accent_color && ! $old_text_color ) {
  143. $hover_styles .= sprintf( 'color:%s;', $accent_color );
  144. }
  145. if( $old_border_color ) {
  146. $hover_styles .= sprintf( 'border-color:%s;', $old_border_color );
  147. }
  148. if( $old_text_color ) {
  149. $hover_styles .= sprintf( 'color:%s;', $old_text_color );
  150. }
  151. if( $general_styles ) {
  152. $styles .= sprintf( '.fusion-button.button-%s{%s}', $this->button_counter, $general_styles );
  153. }
  154. if( $button_3d_styles ) {
  155. $styles .= sprintf( '.fusion-button.button-%s.button-3d{%s}.button-%1$s.button-3d:active{%2$s}', $this->button_counter, $button_3d_styles );
  156. }
  157. if( $hover_styles ) {
  158. $styles .= sprintf( '.fusion-button.button-%s:hover,.fusion-button.button-%s:focus,.fusion-button.button-%s:active{%s}', $this->button_counter, $this->button_counter, $this->button_counter, $hover_styles );
  159. }
  160. if( $gradient_colors ) {
  161. // checking for deprecated separators: ;
  162. if( strpos( $gradient_colors, ';' ) ) {
  163. $grad_colors = explode( ';', $gradient_colors );
  164. } else {
  165. $grad_colors = explode( '|', $gradient_colors );
  166. }
  167. if( count($grad_colors) == 1 ||
  168. $grad_colors[1] == '' ||
  169. $grad_colors[0] == $grad_colors[1]
  170. ) {
  171. $gradient_styles = "background: {$grad_colors[0]};";
  172. } else {
  173. $gradient_styles =
  174. "background: {$grad_colors[0]};
  175. background-image: -webkit-gradient( linear, left bottom, left top, from( {$grad_colors[1]} ), to( {$grad_colors[0]} ) );
  176. background-image: -webkit-linear-gradient( bottom, {$grad_colors[1]}, {$grad_colors[0]} );
  177. background-image: -moz-linear-gradient( bottom, {$grad_colors[1]}, {$grad_colors[0]} );
  178. background-image: -o-linear-gradient( bottom, {$grad_colors[1]}, {$grad_colors[0]} );
  179. background-image: linear-gradient( to top, {$grad_colors[1]}, {$grad_colors[0]} );";
  180. }
  181. $styles .= sprintf( '.fusion-button.button-%s{%s}', $this->button_counter, $gradient_styles );
  182. }
  183. if( $gradient_hover_colors ) {
  184. // checking for deprecated separators: ;
  185. if( strpos( $gradient_hover_colors, ';' ) ) {
  186. $grad_hover_colors = explode( ';', $gradient_hover_colors );
  187. } else {
  188. $grad_hover_colors = explode( '|', $gradient_hover_colors );
  189. }
  190. // For combination of gradient_hover_colors and gradient_hover_colors
  191. //$grad_hover_colors = array_reverse( $grad_hover_colors );
  192. if( count($grad_hover_colors) == 1 ||
  193. $grad_hover_colors[1] == '' ||
  194. $grad_hover_colors[0] == $grad_hover_colors[1]
  195. ) {
  196. $gradient_hover_styles = "background: {$grad_hover_colors[0]};";
  197. } else {
  198. $gradient_hover_styles .=
  199. "background: {$grad_hover_colors[0]};
  200. background-image: -webkit-gradient( linear, left bottom, left top, from( {$grad_hover_colors[1]} ), to( {$grad_hover_colors[0]} ) );
  201. background-image: -webkit-linear-gradient( bottom, {$grad_hover_colors[1]}, {$grad_hover_colors[0]} );
  202. background-image: -moz-linear-gradient( bottom, {$grad_hover_colors[1]}, {$grad_hover_colors[0]} );
  203. background-image: -o-linear-gradient( bottom, {$grad_hover_colors[1]}, {$grad_hover_colors[0]} );
  204. background-image: linear-gradient( to top, {$grad_hover_colors[1]}, {$grad_hover_colors[0]} );";
  205. }
  206. $styles .= sprintf( '.fusion-button.button-%s:hover,.button-%s:focus,.fusion-button.button-%s:active{%s}', $this->button_counter, $this->button_counter, $this->button_counter, $gradient_hover_styles );
  207. }
  208. $styles .= '</style>';
  209. }
  210. $icon_html = '';
  211. if( $icon ) {
  212. $icon_html = sprintf( '<i %s></i>', FusionCore_Plugin::attributes( 'button-shortcode-icon' ) );
  213. if( $icon_divider == 'yes' ) {
  214. $icon_html = sprintf( '<span %s>%s</span>', FusionCore_Plugin::attributes( 'button-shortcode-icon-divder' ), $icon_html );
  215. }
  216. }
  217. $button_text = sprintf( '<span %s>%s</span>', FusionCore_Plugin::attributes( 'button-shortcode-button-text' ), do_shortcode( $content ) );
  218. if( $icon_position == 'left' ) {
  219. $inner_content = $icon_html . $button_text;
  220. } else {
  221. $inner_content = $button_text . $icon_html;
  222. }
  223. $html = sprintf( '%s<a %s>%s</a>', $styles, FusionCore_Plugin::attributes( 'button-shortcode' ), $inner_content );
  224. // Add wrapper to the button for alignment and scoped styling
  225. if ( $alignment ) {
  226. $alignment = ' fusion-align' . $alignment;
  227. }
  228. $html = sprintf( '<div class="fusion-button-wrapper%s">%s</div>', $alignment, $html );
  229. $this->button_counter++;
  230. return $html;
  231. }
  232. function attr() {
  233. $attr['class'] = sprintf( 'fusion-button button-%s button-%s button-%s button-%s button-%s', self::$args['type'], self::$args['shape'], self::$args['size'], self::$args['color'], $this->button_counter );
  234. if( self::$args['animation_type'] ) {
  235. $animations = FusionCore_Plugin::animations( array(
  236. 'type' => self::$args['animation_type'],
  237. 'direction' => self::$args['animation_direction'],
  238. 'speed' => self::$args['animation_speed'],
  239. ) );
  240. $attr = array_merge( $attr, $animations );
  241. $attr['class'] .= ' ' . $attr['animation_class'];
  242. unset($attr['animation_class']);
  243. }
  244. $attr['target'] = self::$args['target'];
  245. $attr['title'] = self::$args['title'];
  246. $attr['href'] = self::$args['link'];
  247. if( self::$args['modal'] ) {
  248. $attr['data-toggle'] = 'modal';
  249. $attr['data-target'] = '.' . self::$args['modal'];
  250. }
  251. if( self::$args['class'] ) {
  252. $attr['class'] .= ' ' . self::$args['class'];
  253. }
  254. if( self::$args['id'] ) {
  255. $attr['id'] = self::$args['id'];
  256. }
  257. return $attr;
  258. }
  259. function icon_divider_attr() {
  260. $attr = array();
  261. $attr['class'] = sprintf( 'button-icon-divider-%s', self::$args['icon_position'] );
  262. return $attr;
  263. }
  264. function icon_attr() {
  265. $attr = array();
  266. $attr['class'] = sprintf( 'fa %s', FusionCore_Plugin::font_awesome_name_handler( self::$args['icon'] ) );
  267. if( self::$args['icon_divider'] != 'yes' ) {
  268. $attr['class'] .= sprintf( ' button-icon-%s', self::$args['icon_position'] );
  269. }
  270. if( self::$args['icon_color'] != self::$args['accent_color'] ) {
  271. $attr['style'] = sprintf( 'color:%s;', self::$args['icon_color'] );
  272. }
  273. return $attr;
  274. }
  275. function button_text_attr() {
  276. $attr = array();
  277. if( self::$args['icon'] &&
  278. self::$args['icon_divider'] == 'yes'
  279. ) {
  280. $attr['class'] = sprintf( 'fusion-button-text-%s', self::$args['icon_position'] );
  281. } else {
  282. $attr['class'] = 'fusion-button-text';
  283. }
  284. return $attr;
  285. }
  286. }
  287. new FusionSC_Button();