| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Inline Enqueue class |
| 5 |
* |
| 6 |
* @package Timetics |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Timetics\Core\EnqueueInline; |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
use Timetics\Utils\Singleton; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class Enqueue_Inline |
| 17 |
*/ |
| 18 |
class Enqueue_Inline |
| 19 |
{ |
| 20 |
use Singleton; |
| 21 |
|
| 22 |
/** |
| 23 |
* Template path for dynamic color CSS. |
| 24 |
*/ |
| 25 |
const COLOR_TEMPLATE = __DIR__ . '/templates/dynamic-colors.php'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Initialize the shortcode class |
| 29 |
* |
| 30 |
* @return void |
| 31 |
*/ |
| 32 |
public function init() |
| 33 |
{ |
| 34 |
add_action( 'wp_enqueue_scripts', array( $this, 'custom_inline_css' ), 20 ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Build and attach the dynamic color CSS to the frontend stylesheet. |
| 39 |
*/ |
| 40 |
public function custom_inline_css() |
| 41 |
{ |
| 42 |
$primary_color = timetics_get_option( 'primary_color' ); |
| 43 |
$secondary_color = timetics_get_option( 'secondary_color' ); |
| 44 |
|
| 45 |
if ( empty( $primary_color ) && empty( $secondary_color ) ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
$custom_css = $this->render_color_css( $primary_color, $secondary_color ); |
| 50 |
|
| 51 |
if ( '' === trim( $custom_css ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
wp_register_style( 'timetics-custom-css', false, array(), TIMETICS_VERSION ); |
| 56 |
wp_enqueue_style( 'timetics-custom-css' ); |
| 57 |
wp_add_inline_style( 'timetics-frontend', $custom_css ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Render the CSS template with the given colors. |
| 62 |
* |
| 63 |
* @param string $primary_color |
| 64 |
* @param string $secondary_color |
| 65 |
* |
| 66 |
* @return string |
| 67 |
*/ |
| 68 |
protected function render_color_css( $primary_color, $secondary_color ) |
| 69 |
{ |
| 70 |
if ( ! file_exists( self::COLOR_TEMPLATE ) ) { |
| 71 |
return ''; |
| 72 |
} |
| 73 |
|
| 74 |
$primary_color = sanitize_hex_color( $primary_color ) ?: $primary_color; |
| 75 |
$secondary_color = sanitize_hex_color( $secondary_color ) ?: $secondary_color; |
| 76 |
|
| 77 |
ob_start(); |
| 78 |
include self::COLOR_TEMPLATE; |
| 79 |
return ob_get_clean(); |
| 80 |
} |
| 81 |
} |
| 82 |
|