class-spec-icon.php
531 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Sureforms - Icons |
| 4 | * |
| 5 | * @package Sureforms |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly. |
| 10 | } |
| 11 | |
| 12 | if ( ! class_exists( 'Spec_Icon' ) ) { |
| 13 | |
| 14 | /** |
| 15 | * Class Spec_Icon. |
| 16 | */ |
| 17 | class Spec_Icon { |
| 18 | |
| 19 | /** |
| 20 | * Member Variable |
| 21 | * |
| 22 | * @var instance |
| 23 | */ |
| 24 | private static $instance; |
| 25 | |
| 26 | /** |
| 27 | * Initiator |
| 28 | */ |
| 29 | public static function get_instance() { |
| 30 | if ( ! isset( self::$instance ) ) { |
| 31 | self::$instance = new self(); |
| 32 | } |
| 33 | return self::$instance; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Constructor |
| 38 | */ |
| 39 | public function __construct() { |
| 40 | |
| 41 | // Activation hook. |
| 42 | add_action( 'init', [ $this, 'register_blocks' ] ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Registers the `core/latest-posts` block on server. |
| 47 | * |
| 48 | * @since 0.0.1 |
| 49 | */ |
| 50 | public function register_blocks() { |
| 51 | |
| 52 | // Check if the register function exists. |
| 53 | if ( ! function_exists( 'register_block_type' ) ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $attr = [ |
| 58 | 'icon' => |
| 59 | [ |
| 60 | 'type' => 'string', |
| 61 | 'default' => 'circle-check', |
| 62 | ], |
| 63 | 'iconSize' => |
| 64 | [ |
| 65 | 'type' => 'number', |
| 66 | 'default' => 40, |
| 67 | ], |
| 68 | 'iconSizeTablet' => |
| 69 | [ |
| 70 | 'type' => 'number', |
| 71 | ], |
| 72 | 'iconSizeMobile' => |
| 73 | [ |
| 74 | 'type' => 'number', |
| 75 | ], |
| 76 | 'iconSizeUnit' => |
| 77 | [ |
| 78 | 'type' => 'string', |
| 79 | 'default' => 'px', |
| 80 | ], |
| 81 | 'align' => |
| 82 | [ |
| 83 | 'type' => 'string', |
| 84 | 'default' => 'center', |
| 85 | ], |
| 86 | 'alignTablet' => |
| 87 | [ |
| 88 | 'type' => 'string', |
| 89 | 'default' => '', |
| 90 | ], |
| 91 | 'alignMobile' => |
| 92 | [ |
| 93 | 'type' => 'string', |
| 94 | 'default' => '', |
| 95 | ], |
| 96 | 'iconColor' => |
| 97 | [ |
| 98 | 'type' => 'string', |
| 99 | 'default' => '#333', |
| 100 | ], |
| 101 | 'iconBorderColor' => |
| 102 | [ |
| 103 | 'type' => 'string', |
| 104 | 'default' => '', |
| 105 | ], |
| 106 | 'iconBackgroundColorType' => |
| 107 | [ |
| 108 | 'type' => 'string', |
| 109 | 'default' => 'classic', |
| 110 | ], |
| 111 | 'iconBackgroundColor' => |
| 112 | [ |
| 113 | 'type' => 'string', |
| 114 | 'default' => '', |
| 115 | ], |
| 116 | 'iconBackgroundGradientColor' => |
| 117 | [ |
| 118 | 'type' => 'string', |
| 119 | 'default' => 'linear-gradient(90deg, rgb(155, 81, 224) 0%, rgb(6, 147, 227) 100%)', |
| 120 | ], |
| 121 | 'iconHoverColor' => |
| 122 | [ |
| 123 | 'type' => 'string', |
| 124 | 'default' => '', |
| 125 | ], |
| 126 | 'iconHoverBackgroundColorType' => |
| 127 | [ |
| 128 | 'type' => 'string', |
| 129 | 'default' => 'classic', |
| 130 | ], |
| 131 | 'iconHoverBackgroundColor' => |
| 132 | [ |
| 133 | 'type' => 'string', |
| 134 | ], |
| 135 | 'iconHoverBackgroundGradientColor' => |
| 136 | [ |
| 137 | 'type' => 'string', |
| 138 | 'default' => 'linear-gradient(90deg, rgb(155, 81, 224) 0%, rgb(6, 147, 227) 100%)', |
| 139 | ], |
| 140 | 'rotation' => |
| 141 | [ |
| 142 | 'type' => 'number', |
| 143 | 'default' => 0, |
| 144 | ], |
| 145 | 'rotationUnit' => |
| 146 | [ |
| 147 | 'type' => 'string', |
| 148 | 'default' => 'deg', |
| 149 | ], |
| 150 | 'block_id' => |
| 151 | [ |
| 152 | 'type' => 'string', |
| 153 | ], |
| 154 | 'link' => |
| 155 | [ |
| 156 | 'type' => 'string', |
| 157 | 'default' => '', |
| 158 | ], |
| 159 | 'target' => |
| 160 | [ |
| 161 | 'type' => 'boolean', |
| 162 | 'default' => false, |
| 163 | ], |
| 164 | 'disableLink' => |
| 165 | [ |
| 166 | 'type' => 'boolean', |
| 167 | 'default' => false, |
| 168 | ], |
| 169 | 'iconTopPadding' => |
| 170 | [ |
| 171 | 'type' => 'number', |
| 172 | 'default' => 5, |
| 173 | ], |
| 174 | 'iconRightPadding' => |
| 175 | [ |
| 176 | 'type' => 'number', |
| 177 | 'default' => 5, |
| 178 | ], |
| 179 | 'iconLeftPadding' => |
| 180 | [ |
| 181 | 'type' => 'number', |
| 182 | 'default' => 5, |
| 183 | ], |
| 184 | 'iconBottomPadding' => |
| 185 | [ |
| 186 | 'type' => 'number', |
| 187 | 'default' => 5, |
| 188 | ], |
| 189 | 'iconTopTabletPadding' => |
| 190 | [ |
| 191 | 'type' => 'number', |
| 192 | ], |
| 193 | 'iconRightTabletPadding' => |
| 194 | [ |
| 195 | 'type' => 'number', |
| 196 | ], |
| 197 | 'iconLeftTabletPadding' => |
| 198 | [ |
| 199 | 'type' => 'number', |
| 200 | ], |
| 201 | 'iconBottomTabletPadding' => |
| 202 | [ |
| 203 | 'type' => 'number', |
| 204 | ], |
| 205 | 'iconTopMobilePadding' => |
| 206 | [ |
| 207 | 'type' => 'number', |
| 208 | ], |
| 209 | 'iconRightMobilePadding' => |
| 210 | [ |
| 211 | 'type' => 'number', |
| 212 | ], |
| 213 | 'iconLeftMobilePadding' => |
| 214 | [ |
| 215 | 'type' => 'number', |
| 216 | ], |
| 217 | 'iconBottomMobilePadding' => |
| 218 | [ |
| 219 | 'type' => 'number', |
| 220 | ], |
| 221 | 'iconPaddingUnit' => |
| 222 | [ |
| 223 | 'type' => 'string', |
| 224 | 'default' => 'px', |
| 225 | ], |
| 226 | 'iconTabletPaddingUnit' => |
| 227 | [ |
| 228 | 'type' => 'string', |
| 229 | 'default' => 'px', |
| 230 | ], |
| 231 | 'iconMobilePaddingUnit' => |
| 232 | [ |
| 233 | 'type' => 'string', |
| 234 | 'default' => 'px', |
| 235 | ], |
| 236 | 'iconPaddingLink' => |
| 237 | [ |
| 238 | 'type' => 'boolean', |
| 239 | 'default' => false, |
| 240 | ], |
| 241 | 'iconTopMargin' => |
| 242 | [ |
| 243 | 'type' => 'number', |
| 244 | ], |
| 245 | 'iconRightMargin' => |
| 246 | [ |
| 247 | 'type' => 'number', |
| 248 | ], |
| 249 | 'iconLeftMargin' => |
| 250 | [ |
| 251 | 'type' => 'number', |
| 252 | ], |
| 253 | 'iconBottomMargin' => |
| 254 | [ |
| 255 | 'type' => 'number', |
| 256 | ], |
| 257 | 'iconTopTabletMargin' => |
| 258 | [ |
| 259 | 'type' => 'number', |
| 260 | ], |
| 261 | 'iconRightTabletMargin' => |
| 262 | [ |
| 263 | 'type' => 'number', |
| 264 | ], |
| 265 | 'iconLeftTabletMargin' => |
| 266 | [ |
| 267 | 'type' => 'number', |
| 268 | ], |
| 269 | 'iconBottomTabletMargin' => |
| 270 | [ |
| 271 | 'type' => 'number', |
| 272 | ], |
| 273 | 'iconTopMobileMargin' => |
| 274 | [ |
| 275 | 'type' => 'number', |
| 276 | ], |
| 277 | 'iconRightMobileMargin' => |
| 278 | [ |
| 279 | 'type' => 'number', |
| 280 | ], |
| 281 | 'iconLeftMobileMargin' => |
| 282 | [ |
| 283 | 'type' => 'number', |
| 284 | ], |
| 285 | 'iconBottomMobileMargin' => |
| 286 | [ |
| 287 | 'type' => 'number', |
| 288 | ], |
| 289 | 'iconMarginUnit' => |
| 290 | [ |
| 291 | 'type' => 'string', |
| 292 | 'default' => 'px', |
| 293 | ], |
| 294 | 'iconTabletMarginUnit' => |
| 295 | [ |
| 296 | 'type' => 'string', |
| 297 | 'default' => 'px', |
| 298 | ], |
| 299 | 'iconMobileMarginUnit' => |
| 300 | [ |
| 301 | 'type' => 'string', |
| 302 | 'default' => 'px', |
| 303 | ], |
| 304 | 'iconMarginLink' => |
| 305 | [ |
| 306 | 'type' => 'boolean', |
| 307 | 'default' => false, |
| 308 | ], |
| 309 | 'isPreview' => |
| 310 | [ |
| 311 | 'type' => 'boolean', |
| 312 | 'default' => false, |
| 313 | ], |
| 314 | 'iconBorderStyle' => |
| 315 | [ |
| 316 | 'type' => 'string', |
| 317 | 'default' => 'default', |
| 318 | ], |
| 319 | 'useSeparateBoxShadows' => |
| 320 | [ |
| 321 | 'type' => 'boolean', |
| 322 | 'default' => true, |
| 323 | ], |
| 324 | 'iconShadowColor' => |
| 325 | [ |
| 326 | 'type' => 'string', |
| 327 | 'default' => '#00000070', |
| 328 | ], |
| 329 | 'iconShadowHOffset' => |
| 330 | [ |
| 331 | 'type' => 'number', |
| 332 | 'default' => 0, |
| 333 | ], |
| 334 | 'iconShadowVOffset' => |
| 335 | [ |
| 336 | 'type' => 'number', |
| 337 | 'default' => 0, |
| 338 | ], |
| 339 | 'iconShadowBlur' => |
| 340 | [ |
| 341 | 'type' => 'number', |
| 342 | 'default' => 0, |
| 343 | ], |
| 344 | 'iconBoxShadowColor' => |
| 345 | [ |
| 346 | 'type' => 'string', |
| 347 | 'default' => '#00000070', |
| 348 | ], |
| 349 | 'iconBoxShadowHOffset' => |
| 350 | [ |
| 351 | 'type' => 'number', |
| 352 | 'default' => 0, |
| 353 | ], |
| 354 | 'iconBoxShadowVOffset' => |
| 355 | [ |
| 356 | 'type' => 'number', |
| 357 | 'default' => 0, |
| 358 | ], |
| 359 | 'iconBoxShadowBlur' => |
| 360 | [ |
| 361 | 'type' => 'number', |
| 362 | ], |
| 363 | 'iconBoxShadowSpread' => |
| 364 | [ |
| 365 | 'type' => 'number', |
| 366 | ], |
| 367 | 'iconBoxShadowPosition' => |
| 368 | [ |
| 369 | 'type' => 'string', |
| 370 | 'default' => 'outset', |
| 371 | ], |
| 372 | 'iconShadowColorHover' => |
| 373 | [ |
| 374 | 'type' => 'string', |
| 375 | 'default' => '#00000070', |
| 376 | ], |
| 377 | 'iconShadowHOffsetHover' => |
| 378 | [ |
| 379 | 'type' => 'number', |
| 380 | 'default' => 0, |
| 381 | ], |
| 382 | 'iconShadowVOffsetHover' => |
| 383 | [ |
| 384 | 'type' => 'number', |
| 385 | 'default' => 0, |
| 386 | ], |
| 387 | 'iconShadowBlurHover' => |
| 388 | [ |
| 389 | 'type' => 'number', |
| 390 | 'default' => 0, |
| 391 | ], |
| 392 | 'iconBoxShadowColorHover' => |
| 393 | [ |
| 394 | 'type' => 'string', |
| 395 | ], |
| 396 | 'iconBoxShadowHOffsetHover' => |
| 397 | [ |
| 398 | 'type' => 'number', |
| 399 | 'default' => 0, |
| 400 | ], |
| 401 | 'iconBoxShadowVOffsetHover' => |
| 402 | [ |
| 403 | 'type' => 'number', |
| 404 | 'default' => 0, |
| 405 | ], |
| 406 | 'iconBoxShadowBlurHover' => |
| 407 | [ |
| 408 | 'type' => 'number', |
| 409 | ], |
| 410 | 'iconBoxShadowSpreadHover' => |
| 411 | [ |
| 412 | 'type' => 'number', |
| 413 | ], |
| 414 | 'iconBoxShadowPositionHover' => |
| 415 | [ |
| 416 | 'type' => 'string', |
| 417 | 'default' => 'outset', |
| 418 | ], |
| 419 | 'iconAccessabilityMode' => |
| 420 | [ |
| 421 | 'type' => 'string', |
| 422 | 'default' => 'svg', |
| 423 | ], |
| 424 | 'iconAccessabilityDesc' => |
| 425 | [ |
| 426 | 'type' => 'string', |
| 427 | 'default' => '', |
| 428 | ], |
| 429 | ]; |
| 430 | |
| 431 | $icon_border_attr = Spec_Gb_Helper::get_instance()->generate_php_border_attribute( 'icon' ); |
| 432 | |
| 433 | $attr = array_merge( $icon_border_attr, $attr ); |
| 434 | |
| 435 | $attributes = apply_filters( 'srfm_gutenberg_icon_attributes_filters', $attr ); |
| 436 | |
| 437 | register_block_type( |
| 438 | 'srfm/icon', |
| 439 | [ |
| 440 | 'attributes' => $attributes, |
| 441 | 'render_callback' => [ $this, 'render_html' ], |
| 442 | ] |
| 443 | ); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Render CF HTML. |
| 448 | * |
| 449 | * @param array $attributes Array of block attributes. |
| 450 | * |
| 451 | * @since 0.0.1 |
| 452 | */ |
| 453 | public function render_html( $attributes ) { |
| 454 | |
| 455 | $block_id = ''; |
| 456 | |
| 457 | if ( isset( $attributes['block_id'] ) ) { |
| 458 | $block_id = $attributes['block_id']; |
| 459 | } |
| 460 | |
| 461 | $icon = isset( $attributes['icon'] ) && $attributes['icon'] ? $attributes['icon'] : 'circle-check'; |
| 462 | $link = isset( $attributes['link'] ) && $attributes['link'] ? $attributes['link'] : ''; |
| 463 | $target = isset( $attributes['target'] ) && $attributes['target'] ? $attributes['target'] : ''; |
| 464 | $disable_link = isset( $attributes['disableLink'] ) && $attributes['disableLink'] ? $attributes['disableLink'] : ''; |
| 465 | $hash = '#'; |
| 466 | ob_start(); |
| 467 | $icon_html = Spec_Gb_Helper::render_svg_html( $icon ? $icon : 'circle-check' ); |
| 468 | $icon_html = ob_get_clean(); |
| 469 | |
| 470 | if ( $icon_html ) { |
| 471 | $role_attr = ( 'image' === $attributes['iconAccessabilityMode'] ) ? ' role="img"' : ( ( 'svg' === $attributes['iconAccessabilityMode'] ) ? ' role="graphics-symbol"' : '' ); |
| 472 | $aria_hidden_attr = ( 'presentation' === $attributes['iconAccessabilityMode'] ) ? 'true' : 'false'; |
| 473 | $aria_label_attr = ( 'presentation' !== $attributes['iconAccessabilityMode'] ) ? ' aria-label="' . esc_attr( $attributes['iconAccessabilityDesc'] ) . '"' : ''; |
| 474 | |
| 475 | $icon_html = preg_replace( |
| 476 | '/<svg(.*?)>/', |
| 477 | '<svg$1' . $role_attr . ' aria-hidden="' . $aria_hidden_attr . '"' . $aria_label_attr . '>', |
| 478 | $icon_html |
| 479 | ); |
| 480 | } |
| 481 | |
| 482 | $target_val = $target ? '_blank' : '_self'; |
| 483 | $link_url = $disable_link ? $link : '#'; |
| 484 | |
| 485 | if ( '#' !== $link_url ) { // need to fix. |
| 486 | $link_url = $link_url ? $link_url : $link_url; |
| 487 | } |
| 488 | |
| 489 | $main_classes = [ |
| 490 | 'wp-block-uagb-icon', |
| 491 | 'uagb-block', |
| 492 | 'uagb-icon-wrapper', |
| 493 | 'uagb-block-' . $block_id, |
| 494 | ]; |
| 495 | |
| 496 | if ( isset( $attributes['className'] ) ) { |
| 497 | $main_classes[] = $attributes['className']; |
| 498 | } |
| 499 | |
| 500 | ob_start(); |
| 501 | ?> |
| 502 | <div data-block-id="<?php echo esc_attr( $block_id ); ?>" class="<?php echo esc_attr( implode( ' ', $main_classes ) ); ?>"> |
| 503 | <span class="uagb-svg-wrapper"> |
| 504 | <?php |
| 505 | $link_condition = $disable_link && $link_url; |
| 506 | |
| 507 | if ( $link_condition ) { |
| 508 | $href = $link_url ? $link_url : $hash; |
| 509 | ?> |
| 510 | <a rel="noopener noreferrer" href="<?php echo esc_url( $href ); ?>" target="<?php echo esc_attr( $target_val ); ?>" > |
| 511 | <?php } ?> |
| 512 | <?php |
| 513 | echo $icon_html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 514 | ?> |
| 515 | <?php if ( $link_condition ) { ?> |
| 516 | </a> |
| 517 | <?php } ?> |
| 518 | </span> |
| 519 | </div> |
| 520 | <?php |
| 521 | return ob_get_clean(); |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Prepare if class 'Spec_Icon' exist. |
| 527 | * Kicking this off by calling 'get_instance()' method |
| 528 | */ |
| 529 | Spec_Icon::get_instance(); |
| 530 | } |
| 531 |