button.php 13.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
<?php

su_add_shortcode(
	array(
		'id'       => 'button',
		'callback' => 'su_shortcode_button',
		'image'    => su_get_plugin_url() . 'admin/images/shortcodes/button.svg',
		'name'     => __( 'Button', 'shortcodes-ultimate' ),
		'type'     => 'wrap',
		'group'    => 'content',
		'atts'     => array(
			'url'         => array(
				'values'  => array(),
				'default' => get_option( 'home' ),
				'name'    => __( 'Link', 'shortcodes-ultimate' ),
				'desc'    => __( 'Button link', 'shortcodes-ultimate' ),
			),
			'target'      => array(
				'type'    => 'select',
				'values'  => array(
					'self'  => __( 'Open in same tab', 'shortcodes-ultimate' ),
					'blank' => __( 'Open in new tab', 'shortcodes-ultimate' ),
				),
				'default' => 'self',
				'name'    => __( 'Target', 'shortcodes-ultimate' ),
				'desc'    => __( 'Button link target', 'shortcodes-ultimate' ),
			),
			'style'       => array(
				'type'    => 'select',
				'values'  => array(
					'default' => __( 'Default', 'shortcodes-ultimate' ),
					'flat'    => __( 'Flat', 'shortcodes-ultimate' ),
					'ghost'   => __( 'Ghost', 'shortcodes-ultimate' ),
					'soft'    => __( 'Soft', 'shortcodes-ultimate' ),
					'glass'   => __( 'Glass', 'shortcodes-ultimate' ),
					'bubbles' => __( 'Bubbles', 'shortcodes-ultimate' ),
					'noise'   => __( 'Noise', 'shortcodes-ultimate' ),
					'stroked' => __( 'Stroked', 'shortcodes-ultimate' ),
					'3d'      => __( '3D', 'shortcodes-ultimate' ),
				),
				'default' => 'default',
				'name'    => __( 'Style', 'shortcodes-ultimate' ),
				'desc'    => __( 'Button background style preset', 'shortcodes-ultimate' ),
			),
			'background'  => array(
				'type'    => 'color',
				'values'  => array(),
				'default' => '#2D89EF',
				'name'    => __( 'Background', 'shortcodes-ultimate' ),
				'desc'    => __( 'Button background color', 'shortcodes-ultimate' ),
			),
			'color'       => array(
				'type'    => 'color',
				'values'  => array(),
				'default' => '#FFFFFF',
				'name'    => __( 'Text color', 'shortcodes-ultimate' ),
				'desc'    => __( 'Button text color', 'shortcodes-ultimate' ),
			),
			'size'        => array(
				'type'    => 'slider',
				'min'     => 1,
				'max'     => 20,
				'step'    => 1,
				'default' => 3,
				'name'    => __( 'Size', 'shortcodes-ultimate' ),
				'desc'    => __( 'Button size', 'shortcodes-ultimate' ),
			),
			'wide'        => array(
				'type'    => 'bool',
				'default' => 'no',
				'name'    => __( 'Fluid', 'shortcodes-ultimate' ),
				'desc'    => __( 'Fluid buttons has 100% width', 'shortcodes-ultimate' ),
			),
			'center'      => array(
				'type'    => 'bool',
				'default' => 'no',
				'name'    => __( 'Centered', 'shortcodes-ultimate' ),
				'desc'    => __( 'Is button centered on the page', 'shortcodes-ultimate' ),
			),
			'radius'      => array(
				'type'    => 'select',
				'values'  => array(
					'auto'  => __( 'Auto', 'shortcodes-ultimate' ),
					'round' => __( 'Round', 'shortcodes-ultimate' ),
					'0'     => __( 'Square', 'shortcodes-ultimate' ),
					'5'     => '5px',
					'10'    => '10px',
					'20'    => '20px',
				),
				'default' => 'auto',
				'name'    => __( 'Radius', 'shortcodes-ultimate' ),
				'desc'    => __( 'Radius of button corners. Auto-radius calculation based on button size', 'shortcodes-ultimate' ),
			),
			'icon'        => array(
				'type'    => 'icon',
				'default' => '',
				'name'    => __( 'Icon', 'shortcodes-ultimate' ),
				'desc'    => __( 'You can upload custom icon for this button or pick a built-in icon', 'shortcodes-ultimate' ),
			),
			'icon_color'  => array(
				'type'    => 'color',
				'default' => '#FFFFFF',
				'name'    => __( 'Icon color', 'shortcodes-ultimate' ),
				'desc'    => __( 'This color will be applied to the selected icon. Does not works with uploaded icons', 'shortcodes-ultimate' ),
			),
			'text_shadow' => array(
				'type'    => 'shadow',
				'default' => 'none',
				'name'    => __( 'Text shadow', 'shortcodes-ultimate' ),
				'desc'    => __( 'Button text shadow', 'shortcodes-ultimate' ),
			),
			'desc'        => array(
				'default' => '',
				'name'    => __( 'Description', 'shortcodes-ultimate' ),
				'desc'    => __( 'Small description under button text. This option is incompatible with icon.', 'shortcodes-ultimate' ),
			),
			'download'    => array(
				'default' => '',
				'name'    => __( 'Download', 'shortcodes-ultimate' ),
				'desc'    => __( 'The download attribute specifies that the button URL will be downloaded when a user clicks on the button. The value of the attribute will be the name of the downloaded file.', 'shortcodes-ultimate' ),
			),
			'onclick'     => array(
				'default' => '',
				'name'    => __( 'onClick', 'shortcodes-ultimate' ),
				'desc'    => __( 'Advanced JavaScript code for onClick action', 'shortcodes-ultimate' ),
			),
			'rel'         => array(
				'default' => '',
				'name'    => __( 'Rel attribute', 'shortcodes-ultimate' ),
				'desc'    => __( 'Here you can add value for the rel attribute.<br>Example values: <b%value>nofollow</b>, <b%value>lightbox</b>', 'shortcodes-ultimate' ),
			),
			'title'       => array(
				'default' => '',
				'name'    => __( 'Title attribute', 'shortcodes-ultimate' ),
				'desc'    => __( 'Here you can add value for the title attribute', 'shortcodes-ultimate' ),
			),
			'id'          => array(
				'default' => '',
				'name'    => __( 'Button ID', 'shortcodes-ultimate' ),
				'desc'    => __( 'Custom value for the ID attribute', 'shortcodes-ultimate' ),
			),
			'class'       => array(
				'type'    => 'extra_css_class',
				'name'    => __( 'Extra CSS class', 'shortcodes-ultimate' ),
				'desc'    => __( 'Additional CSS class name(s) separated by space(s)', 'shortcodes-ultimate' ),
				'default' => '',
			),
		),
		'content'  => __( 'Button text', 'shortcodes-ultimate' ),
		'desc'     => __( 'Styled button', 'shortcodes-ultimate' ),
		'example'  => 'buttons',
		'icon'     => 'heart',
	)
);

function su_shortcode_button( $atts = null, $content = null ) {

	$atts = shortcode_atts(
		array(
			'url'         => get_option( 'home' ),
			'link'        => null, // 3.x
			'target'      => 'self',
			'style'       => 'default',
			'background'  => '#2D89EF',
			'color'       => '#FFFFFF',
			'dark'        => null, // 3.x
			'size'        => 3,
			'wide'        => 'no',
			'center'      => 'no',
			'radius'      => 'auto',
			'icon'        => false,
			'icon_color'  => '#FFFFFF',
			'ts_color'    => null, // Dep. 4.3.2
			'ts_pos'      => null, // Dep. 4.3.2
			'text_shadow' => 'none',
			'desc'        => '',
			'download'    => '',
			'onclick'     => '',
			'rel'         => '',
			'title'       => '',
			'id'          => '',
			'class'       => '',
		),
		$atts,
		'button'
	);

	if ( $atts['link'] !== null ) {
		$atts['url'] = $atts['link'];
	}

	if ( $atts['dark'] !== null ) {
		$atts['background'] = $atts['color'];
		$atts['color']      = ( $atts['dark'] ) ? '#000' : '#fff';
	}

	// 3.x
	if ( is_numeric( $atts['style'] ) ) {
		$atts['style'] = str_replace( array( '1', '2', '3', '4', '5' ), array( 'default', 'glass', 'bubbles', 'noise', 'stroked' ), $atts['style'] );
	}

	// Prepare vars
	$a_css     = array();
	$span_css  = array();
	$img_css   = array();
	$small_css = array();
	$radius    = '0px';
	$before    = $after = '';

	// Text shadow values
	$shadows = array(
		'none'         => '0 0',
		'top'          => '0 -1px',
		'right'        => '1px 0',
		'bottom'       => '0 1px',
		'left'         => '-1px 0',
		'top-right'    => '1px -1px',
		'top-left'     => '-1px -1px',
		'bottom-right' => '1px 1px',
		'bottom-left'  => '-1px 1px',
	);

	// Sanitize the size value
	$atts['size'] = intval( $atts['size'] );

	// Common styles for button
	$styles = array(
		'size'     => round( ( $atts['size'] + 7 ) * 1.3 ),
		'ts_color' => ( $atts['ts_color'] === 'light' ) ? su_adjust_lightness( $atts['background'], 50 ) : su_adjust_lightness( $atts['background'], -40 ),
		'ts_pos'   => ( $atts['ts_pos'] !== null ) ? $shadows[ $atts['ts_pos'] ] : $shadows['none'],
	);

	// Calculate border-radius
	if ( $atts['radius'] == 'auto' ) {
		$radius = round( $atts['size'] + 2 ) . 'px';
	} elseif ( $atts['radius'] == 'round' ) {
		$radius = round( ( ( $atts['size'] * 2 ) + 2 ) * 2 + $styles['size'] ) . 'px';
	} elseif ( is_numeric( $atts['radius'] ) ) {
		$radius = intval( $atts['radius'] ) . 'px';
	}

	// CSS rules for <a> tag
	$a_rules = array(
		'color'                 => $atts['color'],
		'background-color'      => $atts['background'],
		'border-color'          => su_adjust_lightness( $atts['background'], -20 ),
		'border-radius'         => $radius,
		'-moz-border-radius'    => $radius,
		'-webkit-border-radius' => $radius,
	);

	// CSS rules for <span> tag
	$span_rules = array(
		'color'                 => $atts['color'],
		'padding'               => ( $atts['icon'] ) ? round( ( $atts['size'] ) / 2 + 4 ) . 'px ' . round( $atts['size'] * 2 + 10 ) . 'px' : '0px ' . round( $atts['size'] * 2 + 10 ) . 'px',
		'font-size'             => $styles['size'] . 'px',
		'line-height'           => ( $atts['icon'] ) ? round( $styles['size'] * 1.5 ) . 'px' : round( $styles['size'] * 2 ) . 'px',
		'border-color'          => su_adjust_lightness( $atts['background'], 30 ),
		'border-radius'         => $radius,
		'-moz-border-radius'    => $radius,
		'-webkit-border-radius' => $radius,
		'text-shadow'           => $styles['ts_pos'] . ' 1px ' . $styles['ts_color'],
		'-moz-text-shadow'      => $styles['ts_pos'] . ' 1px ' . $styles['ts_color'],
		'-webkit-text-shadow'   => $styles['ts_pos'] . ' 1px ' . $styles['ts_color'],
	);

	// Apply new text-shadow value
	if ( $atts['ts_color'] === null && $atts['ts_pos'] === null ) {
		$span_rules['text-shadow']         = $atts['text_shadow'];
		$span_rules['-moz-text-shadow']    = $atts['text_shadow'];
		$span_rules['-webkit-text-shadow'] = $atts['text_shadow'];
	}

	// CSS rules for <img> tag
	$img_rules = array(
		'width'  => round( $styles['size'] * 1.5 ) . 'px',
		'height' => round( $styles['size'] * 1.5 ) . 'px',
	);

	// CSS rules for <small> tag
	$small_rules = array(
		'padding-bottom' => round( ( $atts['size'] ) / 2 + 4 ) . 'px',
		'color'          => $atts['color'],
	);

	// Create style attr value for <a> tag
	foreach ( $a_rules as $a_rule => $a_value ) {
		$a_css[] = $a_rule . ':' . $a_value;
	}

	// Create style attr value for <span> tag
	foreach ( $span_rules as $span_rule => $span_value ) {
		$span_css[] = $span_rule . ':' . $span_value;
	}

	// Create style attr value for <img> tag
	foreach ( $img_rules as $img_rule => $img_value ) {
		$img_css[] = $img_rule . ':' . $img_value;
	}

	// Create style attr value for <img> tag
	foreach ( $small_rules as $small_rule => $small_value ) {
		$small_css[] = $small_rule . ':' . $small_value;
	}

	// Prepare button classes
	$classes = array( 'su-button', 'su-button-style-' . $atts['style'] );

	// Additional classes
	if ( $atts['class'] ) {
		$classes[] = $atts['class'];
	}

	// Wide class
	if ( $atts['wide'] === 'yes' ) {
		$classes[] = 'su-button-wide';
	}

	// Prepare icon
	if ( $atts['icon'] ) {

		if ( strpos( $atts['icon'], 'icon:' ) !== false ) {

			$icon = '<i class="sui sui-' . esc_attr( trim( str_replace( 'icon:', '', $atts['icon'] ) ) ) . '" style="font-size:' . $styles['size'] . 'px;color:' . esc_attr( $atts['icon_color'] ) . '"></i>';

			su_query_asset( 'css', 'su-icons' );

		} else {
			$icon = '<img src="' . esc_attr( $atts['icon'] ) . '" alt="' . esc_attr( $content ) . '" style="' . implode( ';', $img_css ) . '" />';
		}

	} else {
		$icon = '';
	}

	// Prepare <small> with description
	$desc = $atts['desc']
		? '<small style="' . esc_attr( implode( ';', $small_css ) ) . '">' . su_do_attribute( $atts['desc'] ) . '</small>'
		: '';

	// Wrap with div if button centered
	if ( $atts['center'] === 'yes' ) {
		$before .= '<div class="su-button-center">';
		$after  .= '</div>';
	}

	// Replace icon marker in content,
	// add float-icon class to rearrange margins
	if ( strpos( $content, '%icon%' ) !== false ) {
		$content   = str_replace( '%icon%', $icon, $content );
		$classes[] = 'su-button-float-icon';
	}

	// Button text has no icon marker, append icon to begin of the text
	else {
		$content = $icon . ' ' . $content;
	}

	// Prepare onclick action
	if ( $atts['onclick'] && ! su_is_unsafe_features_enabled() ) {

		return su_error_message(
			'Button',
			sprintf(
				'%s.<br><a href="https://getshortcodes.com/docs/unsafe-features/" target="_blank">%s</a>',
				// translators: do not translate the <b>onclick</b> part, the <b>Unsafe features</b> must be translated
				__( 'The <b>onclick</b> attribute cannot be used while <b>Unsafe features</b> option is turned off', 'shortcodes-ultimate' ),
				__( 'Learn more', 'shortcodes-ultimate' )
			)
		);

	}

	if ( $atts['onclick'] ) {
		$atts['onclick'] = ' onClick="' . $atts['onclick'] . '"';
	}

	// Set rel attribute to `noopener noreferrer` if it's empty and target=blank
	if ( 'blank' === $atts['target'] && '' === $atts['rel'] ) {
		$atts['rel'] = 'noopener noreferrer';
	}

	// Prepare download attribute
	$atts['download'] = $atts['download']
		? ' download="' . esc_attr( $atts['download'] ) . '"'
		: '';

	// Prepare rel attribute
	$atts['rel'] = $atts['rel']
		? ' rel="' . esc_attr( $atts['rel'] ) . '"'
		: '';

	// Prepare title attribute
	$atts['title'] = $atts['title']
		? ' title="' . esc_attr( su_do_attribute( $atts['title'] ) ) . '"'
		: '';

	// Add ID attribute
	$atts['id'] = ! empty( $atts['id'] )
		? sprintf( ' id="%s"', esc_attr( $atts['id'] ) )
		: '';

	su_query_asset( 'css', 'su-shortcodes' );

	return $before . '<a href="' . esc_attr( su_do_attribute( $atts['url'] ) ) . '" class="' . esc_attr( implode( ' ', $classes ) ) . '" style="' . esc_attr( implode( ';', $a_css ) ) . '" target="_' . esc_attr( $atts['target'] ) . '"' . $atts['onclick'] . $atts['rel'] . $atts['title'] . $atts['id'] . $atts['download'] . '><span style="' . esc_attr( implode( ';', $span_css ) ) . '">' . do_shortcode( stripcslashes( $content ) ) . $desc . '</span></a>' . $after;

}