privacy
5 years ago
promo
6 years ago
yit-ajax.php
7 years ago
yit-assets.php
5 years ago
yit-cpt-unlimited.php
6 years ago
yit-debug.php
7 years ago
yit-icons.php
7 years ago
yit-metabox.php
6 years ago
yit-plugin-common.php
8 years ago
yit-plugin-gradients.php
6 years ago
yit-plugin-licence.php
5 years ago
yit-plugin-panel-wc.php
5 years ago
yit-plugin-panel.php
5 years ago
yit-plugin-subpanel.php
6 years ago
yit-pointers.php
6 years ago
yit-theme-licence.php
6 years ago
yit-upgrade.php
6 years ago
yit-video.php
7 years ago
yith-dashboard.php
7 years ago
yith-gutenberg.php
7 years ago
yith-system-status.php
5 years ago
yit-plugin-gradients.php
536 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Your Inspiration Themes |
| 4 | * |
| 5 | * In this files there is a collection of a functions useful for the core |
| 6 | * of the framework. |
| 7 | * |
| 8 | * @package WordPress |
| 9 | * @subpackage Your Inspiration Themes |
| 10 | * @author Your Inspiration Themes Team <info@yithemes.com> |
| 11 | * |
| 12 | * This source file is subject to the GNU GENERAL PUBLIC LICENSE (GPL 3.0) |
| 13 | * that is bundled with this package in the file LICENSE.txt. |
| 14 | * It is also available through the world-wide-web at this URL: |
| 15 | * http://www.gnu.org/licenses/gpl-3.0.txt |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * Handles colors |
| 20 | * |
| 21 | * @since 1.0 |
| 22 | */ |
| 23 | /** |
| 24 | * Generates CSS 3 gradients for all browsers. |
| 25 | * |
| 26 | * @since 1.0 |
| 27 | */ |
| 28 | |
| 29 | if ( ! class_exists( 'YIT_Gradients' ) ) { |
| 30 | |
| 31 | class YIT_Gradients { |
| 32 | |
| 33 | /** |
| 34 | * An array of colors to use for a gradient. |
| 35 | * |
| 36 | * @var array |
| 37 | * @since 1.0 |
| 38 | */ |
| 39 | public $colors_gradient = array(); |
| 40 | |
| 41 | /** |
| 42 | * Set properties |
| 43 | * |
| 44 | * @param string $key |
| 45 | * @param $value |
| 46 | * |
| 47 | * @internal param array $colors_gradient |
| 48 | * @return void |
| 49 | * @since 1.0 |
| 50 | */ |
| 51 | public function set( $key, $value ) { |
| 52 | if ( property_exists( $this, $key ) ) { |
| 53 | $this->{$key} = $value; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get properties |
| 59 | * |
| 60 | * @param string $key |
| 61 | * |
| 62 | * @return mixed |
| 63 | * @since 1.0 |
| 64 | */ |
| 65 | public function get( $key ) { |
| 66 | if ( property_exists( $this, $key ) ) { |
| 67 | return $this->{$key}; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Add a color to use in a gradient. |
| 73 | * |
| 74 | * @param string $color |
| 75 | * @param int $position |
| 76 | * |
| 77 | * @return void |
| 78 | * @since 1.0 |
| 79 | */ |
| 80 | public function add_color_gradient( $color, $position ) { |
| 81 | $the_color['color'] = $color; |
| 82 | $the_color['position'] = $position; |
| 83 | |
| 84 | array_push( $this->colors_gradient, $the_color ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Generate the CSS code for a gradient. |
| 89 | * |
| 90 | * @param string $role |
| 91 | * @param string $direction |
| 92 | * |
| 93 | * @return string|bool |
| 94 | * @since 1.0 |
| 95 | */ |
| 96 | public function gradient( $role, $direction ) { |
| 97 | if ( ! empty( $this->colors_gradient ) ) { |
| 98 | |
| 99 | $css = array( |
| 100 | 'old' => $this->_make_old_gradient( $this->colors_gradient[0]['color'] ), //old browser |
| 101 | 'ff3' => $this->_make_modern_gradient( $this->colors_gradient, $direction, 'moz' ), //firefox 3.6+ |
| 102 | 'chr_saf4' => $this->_make_chr_saf4_gradient( $this->colors_gradient, $direction ), //chrome and safari4+ |
| 103 | 'chr10_saf5' => $this->_make_modern_gradient( $this->colors_gradient, $direction, 'webkit' ), //chrome10+ and safari5+ |
| 104 | 'opera' => $this->_make_modern_gradient( $this->colors_gradient, $direction, 'o' ), //opera11.10+ |
| 105 | 'ie10' => $this->_make_modern_gradient( $this->colors_gradient, $direction, 'ms' ), //internet explorer 10+ |
| 106 | 'w3c' => $this->_make_modern_gradient( $this->colors_gradient, $direction, 'w3c' ), //w3c |
| 107 | 'ie6_9' => $this->_make_ie6_gradient( $this->colors_gradient, $direction ) //ie6-9 |
| 108 | ); |
| 109 | |
| 110 | $css = $role . '{' . implode( ';', $css ) . '}'; |
| 111 | |
| 112 | $this->colors_gradient = array(); |
| 113 | |
| 114 | return $css; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Reverse a gradient. This method should be used only before calling ::make_gradient(). Otherwise it will not works. |
| 120 | * |
| 121 | * @return void |
| 122 | * @since 1.0 |
| 123 | */ |
| 124 | public function reverse_gradient() { |
| 125 | $colors_gradient = array_reverse( $this->get( 'colors_gradient' ) ); |
| 126 | $colors_gradient_count = count( $colors_gradient ); |
| 127 | for ( $i = 0; $i < $colors_gradient_count; $i ++ ) { |
| 128 | $colors_gradient[$i]['position'] = 100 - $colors_gradient[$i]['position']; |
| 129 | } |
| 130 | |
| 131 | $this->set( 'colors_gradient', $colors_gradient ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Generate the CSS code for a gradient. |
| 136 | * |
| 137 | * @param string $role |
| 138 | * @param string $direction |
| 139 | * |
| 140 | * @return string|bool |
| 141 | * @since 1.0 |
| 142 | */ |
| 143 | public function get_gradient( $role, $direction ) { |
| 144 | return $this->gradient( $role, $direction ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Generate the CSS code for a gradient. |
| 149 | * |
| 150 | * @param string $role |
| 151 | * @param string $direction |
| 152 | * |
| 153 | * @return void |
| 154 | * @since 1.0 |
| 155 | */ |
| 156 | public function the_gradient( $role, $direction ) { |
| 157 | echo $this->get_gradient( $role, $direction ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Generate the CSS code for a gradient. |
| 162 | * |
| 163 | * @param string $role |
| 164 | * @param string $from |
| 165 | * @param string $to |
| 166 | * @param string $direction |
| 167 | * |
| 168 | * @return string|bool |
| 169 | * @since 1.0 |
| 170 | */ |
| 171 | public function gradient_from_to( $role, $from, $to, $direction ) { |
| 172 | |
| 173 | $colors = array( |
| 174 | array( |
| 175 | 'color' => $from, |
| 176 | 'position' => 0 |
| 177 | ), |
| 178 | array( |
| 179 | 'color' => $to, |
| 180 | 'position' => 100 |
| 181 | ), |
| 182 | ); |
| 183 | |
| 184 | $this->set( 'colors_gradient', $colors ); |
| 185 | return $this->get_gradient( $role, $direction ); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Generate the CSS code for a gradient. |
| 190 | * |
| 191 | * @param string $role |
| 192 | * @param string $color |
| 193 | * @param string $direction |
| 194 | * @param int|string $factor |
| 195 | * |
| 196 | * @return string|bool |
| 197 | * @since 1.0 |
| 198 | */ |
| 199 | public function gradient_darker( $role, $color, $direction, $factor = 30 ) { |
| 200 | |
| 201 | $colors = array( |
| 202 | array( |
| 203 | 'color' => $color, |
| 204 | 'position' => 0 |
| 205 | ), |
| 206 | array( |
| 207 | 'color' => $this->hex_darker( $color, $factor ), |
| 208 | 'position' => 100 |
| 209 | ), |
| 210 | ); |
| 211 | |
| 212 | $this->set( 'colors_gradient', $colors ); |
| 213 | return $this->get_gradient( $role, $direction ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Generate the CSS code for a gradient. |
| 218 | * |
| 219 | * @param string $role |
| 220 | * @param string $color |
| 221 | * @param string $direction |
| 222 | * @param int|string $factor |
| 223 | * |
| 224 | * @return string|bool |
| 225 | * @since 1.0 |
| 226 | */ |
| 227 | public function gradient_lighter( $role, $color, $direction, $factor = 30 ) { |
| 228 | |
| 229 | $colors = array( |
| 230 | array( |
| 231 | 'color' => $color, |
| 232 | 'position' => 0 |
| 233 | ), |
| 234 | array( |
| 235 | 'color' => $this->hex_lighter( $color, $factor ), |
| 236 | 'position' => 100 |
| 237 | ), |
| 238 | ); |
| 239 | |
| 240 | $this->set( 'colors_gradient', $colors ); |
| 241 | return $this->get_gradient( $role, $direction ); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Generate the CSS code for a gradient that not supports gradients (add only a background color). |
| 246 | * |
| 247 | * @param $color |
| 248 | * |
| 249 | * @internal param string $role |
| 250 | * @return string|bool |
| 251 | * @access private |
| 252 | * @since 1.0 |
| 253 | */ |
| 254 | private function _make_old_gradient( $color ) { |
| 255 | return 'background:' . $color; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Generate the CSS code for a gradient in IE6-9. |
| 260 | * |
| 261 | * @param $colors |
| 262 | * @param $direction |
| 263 | * |
| 264 | * @internal param string $role |
| 265 | * @return string|bool |
| 266 | * @access private |
| 267 | * @since 1.0 |
| 268 | */ |
| 269 | private function _make_ie6_gradient( $colors, $direction ) { |
| 270 | $css = 'filter:progid:DXImageTransform.Microsoft.gradient('; |
| 271 | $css .= 'startColorstr=\'' . $colors[0]['color'] . '\','; |
| 272 | $css .= 'endColorstr=\'' . $colors[count( $colors ) - 1]['color'] . '\','; |
| 273 | |
| 274 | if ( $direction == 'horizontal' ) { |
| 275 | $css .= 'GradientType=1'; |
| 276 | } |
| 277 | else { |
| 278 | $css .= 'GradientType=0'; |
| 279 | } //vertical |
| 280 | |
| 281 | $css .= ')'; |
| 282 | |
| 283 | return $css; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Make the CSS 3 for a gradient in modern browsers( FF3.6+, Chrome, Safari5+, Opera11.10+, IE10+ ) |
| 288 | * |
| 289 | * @param array $colors |
| 290 | * @param string $direction |
| 291 | * @param $browser |
| 292 | * |
| 293 | * @return string |
| 294 | * @access private |
| 295 | * @since 1.0 |
| 296 | */ |
| 297 | private function _make_modern_gradient( $colors, $direction, $browser ) { |
| 298 | $css = 'background:'; |
| 299 | |
| 300 | //Add the browser suffix |
| 301 | if ( $browser != 'w3c' ) { |
| 302 | $browser = '-' . $browser . '-'; |
| 303 | } |
| 304 | else { |
| 305 | $browser = ''; |
| 306 | } |
| 307 | |
| 308 | switch ( $direction ) { |
| 309 | case 'vertical' : |
| 310 | $css .= $browser . 'linear-gradient(top,'; |
| 311 | break; |
| 312 | case 'horizontal' : |
| 313 | $css .= $browser . 'linear-gradient(left,'; |
| 314 | break; |
| 315 | case 'diagonal-bottom': |
| 316 | $css .= $browser . 'linear-gradient(-45deg,'; |
| 317 | break; |
| 318 | case 'diagonal-top' : |
| 319 | $css .= $browser . 'linear-gradient(45deg,'; |
| 320 | break; |
| 321 | case 'radial' : |
| 322 | $css .= $browser . 'radial-gradient(center, ellipse cover,'; |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | foreach ( $colors as $stop ) { |
| 327 | $css .= $stop['color'] . ' ' . $stop['position'] . '%, '; |
| 328 | } |
| 329 | |
| 330 | $css = rtrim( $css ); |
| 331 | $css = rtrim( $css, ',' ); |
| 332 | $css .= ')'; |
| 333 | |
| 334 | return $css; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Make the CSS 3 for a gradient in Chrome and Safari 4+ |
| 339 | * |
| 340 | * @param array $colors |
| 341 | * @param string $direction |
| 342 | * |
| 343 | * @return string |
| 344 | * @access private |
| 345 | * @since 1.0 |
| 346 | */ |
| 347 | private function _make_chr_saf4_gradient( $colors, $direction ) { |
| 348 | $css = 'background:'; |
| 349 | |
| 350 | switch ( $direction ) { |
| 351 | case 'vertical' : |
| 352 | $css .= '-webkit-gradient(linear,left top,left bottom,'; |
| 353 | break; |
| 354 | case 'horizontal' : |
| 355 | $css .= '-webkit-gradient(linear,left top,right top,'; |
| 356 | break; |
| 357 | case 'diagonal-bottom': |
| 358 | $css .= '-webkit-gradient(linear,left top,right bottom,'; |
| 359 | break; |
| 360 | case 'diagonal-top' : |
| 361 | $css .= '-webkit-gradient(linear,left bottom,right top,'; |
| 362 | break; |
| 363 | case 'radial' : |
| 364 | $css .= '-webkit-gradient(radial,center center, 0px, center center, 100%,'; |
| 365 | break; |
| 366 | } |
| 367 | |
| 368 | foreach ( $colors as $stop ) { |
| 369 | $css .= 'color-stop(' . $stop['position'] . '%, ' . $stop['color'] . '), '; |
| 370 | } |
| 371 | |
| 372 | $css = rtrim( $css ); |
| 373 | $css = rtrim( $css, ',' ); |
| 374 | $css .= ')'; |
| 375 | |
| 376 | return $css; |
| 377 | } |
| 378 | |
| 379 | |
| 380 | /** |
| 381 | * Return an instance of the model called |
| 382 | * |
| 383 | * @param string $class The name of class that I want the instance |
| 384 | * |
| 385 | * @since 2.0.0 |
| 386 | * @author Simone D'Amico <simone.damico@yithemes.com> |
| 387 | * @return mixed |
| 388 | */ |
| 389 | public function getModel( $class ) { |
| 390 | return YIT_Registry::get_instance()->$class; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | /** |
| 395 | * Return a color darker then $color. |
| 396 | * |
| 397 | * @param string $color |
| 398 | * @param int $factor |
| 399 | * |
| 400 | * @return string |
| 401 | * @since 1.0 |
| 402 | * @author Andrea Grillo <andrea.grillo@yithemes.com> |
| 403 | */ |
| 404 | public function hex_darker( $color, $factor = 30 ) { |
| 405 | $color = str_replace( '#', '', $color ); |
| 406 | |
| 407 | $base['R'] = hexdec( substr( $color, 0, 2 ) ); |
| 408 | $base['G'] = hexdec( substr( $color, 2, 2 ) ); |
| 409 | $base['B'] = hexdec( substr( $color, 4, 2 ) ); |
| 410 | |
| 411 | $color = '#'; |
| 412 | |
| 413 | foreach ( $base as $k => $v ) { |
| 414 | $amount = $v / 100; |
| 415 | $amount = round( $amount * $factor ); |
| 416 | $new_decimal = $v - $amount; |
| 417 | |
| 418 | $new_hex_component = dechex( $new_decimal ); |
| 419 | |
| 420 | if ( strlen( $new_hex_component ) < 2 ) { |
| 421 | $new_hex_component = "0" . $new_hex_component; |
| 422 | } |
| 423 | |
| 424 | $color .= $new_hex_component; |
| 425 | } |
| 426 | |
| 427 | return $color; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Return a color lighter then $color. |
| 432 | * |
| 433 | * @param string $color |
| 434 | * @param int $factor |
| 435 | * |
| 436 | * @return string |
| 437 | * @since 1.0 |
| 438 | * @author Andrea Grillo <andrea.grillo@yithemes.com> |
| 439 | */ |
| 440 | public function hex_lighter( $color, $factor = 30 ) { |
| 441 | $color = str_replace( '#', '', $color ); |
| 442 | |
| 443 | $base['R'] = hexdec( $color[0] . $color[1] ); |
| 444 | $base['G'] = hexdec( $color[2] . $color[3] ); |
| 445 | $base['B'] = hexdec( $color[4] . $color[5] ); |
| 446 | |
| 447 | $color = '#'; |
| 448 | |
| 449 | foreach ( $base as $k => $v ) { |
| 450 | $amount = 255 - $v; |
| 451 | $amount = $amount / 100; |
| 452 | $amount = round( $amount * $factor ); |
| 453 | $new_decimal = $v + $amount; |
| 454 | |
| 455 | $new_hex_component = dechex( $new_decimal ); |
| 456 | |
| 457 | if ( strlen( $new_hex_component ) < 2 ) { |
| 458 | $new_hex_component = "0" . $new_hex_component; |
| 459 | } |
| 460 | |
| 461 | $color .= $new_hex_component; |
| 462 | } |
| 463 | |
| 464 | return $color; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Detect if we must use a color darker or lighter then the background. |
| 469 | * |
| 470 | * @param string $color |
| 471 | * @param string $dark |
| 472 | * @param string $light |
| 473 | * |
| 474 | * @return string |
| 475 | * @since 1.0 |
| 476 | * @author Andrea Grillo <andrea.grillo@yithemes.com> |
| 477 | */ |
| 478 | public function light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) { |
| 479 | $hex = str_replace( '#', '', $color ); |
| 480 | |
| 481 | $c_r = hexdec( substr( $hex, 0, 2 ) ); |
| 482 | $c_g = hexdec( substr( $hex, 2, 2 ) ); |
| 483 | $c_b = hexdec( substr( $hex, 4, 2 ) ); |
| 484 | $brightness = ( ( $c_r * 299 ) + ( $c_g * 587 ) + ( $c_b * 114 ) ) / 1000; |
| 485 | |
| 486 | return ( $brightness > 155 ) ? $dark : $light; |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Detect if we must use a color darker or lighter then the background. |
| 491 | * |
| 492 | * @param $hex |
| 493 | * |
| 494 | * @internal param string $color |
| 495 | * @return string |
| 496 | * @since 1.0 |
| 497 | * @author Andrea Grillo <andrea.grillo@yithemes.com> |
| 498 | */ |
| 499 | public function hex2rgb( $hex ) { |
| 500 | $hex = str_replace( "#", "", $hex ); |
| 501 | |
| 502 | if ( strlen( $hex ) == 3 ) { |
| 503 | $r = hexdec( substr( $hex, 0, 1 ) . substr( $hex, 0, 1 ) ); |
| 504 | $g = hexdec( substr( $hex, 1, 1 ) . substr( $hex, 1, 1 ) ); |
| 505 | $b = hexdec( substr( $hex, 2, 1 ) . substr( $hex, 2, 1 ) ); |
| 506 | } |
| 507 | else { |
| 508 | $r = hexdec( substr( $hex, 0, 2 ) ); |
| 509 | $g = hexdec( substr( $hex, 2, 2 ) ); |
| 510 | $b = hexdec( substr( $hex, 4, 2 ) ); |
| 511 | } |
| 512 | $rgb = array( $r, $g, $b ); |
| 513 | //return implode(",", $rgb); // returns the rgb values separated by commas |
| 514 | return $rgb; // returns an array with the rgb values |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Magic method for this class |
| 519 | * |
| 520 | * @param $name string The name of magic property |
| 521 | * |
| 522 | * @since 2.0.0 |
| 523 | * @author Simone D'Amico <simone.damico@yithemes.com> |
| 524 | * @return mixed |
| 525 | */ |
| 526 | public function __get( $name ) { |
| 527 | if ( $name == 'request' ) { |
| 528 | if ( ! $this->_request instanceof YIT_Request ) { |
| 529 | $this->_request = YIT_Registry::get_instance()->request; |
| 530 | } |
| 531 | |
| 532 | return $this->_request; |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | } |