non-sshare-styles
1 month ago
class-hustle-decorator-non-sshare.php
3 months ago
class-hustle-decorator-sshare.php
3 months ago
class-hustle-decorator_abstract.php
3 years ago
class-hustle-module-preview.php
1 year ago
hustle-module-front-ajax.php
1 month ago
hustle-module-front.php
1 month ago
hustle-module-inline-style-queue.php
3 months ago
hustle-module-renderer.php
1 month ago
hustle-renderer-abstract.php
3 months ago
hustle-renderer-sshare.php
5 months ago
hustle-module-inline-style-queue.php
114 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Hustle_Module_Inline_Style_Queue |
| 4 | * |
| 5 | * @package Hustle |
| 6 | */ |
| 7 | class Hustle_Module_Inline_Style_Queue { |
| 8 | |
| 9 | /** |
| 10 | * A queue of inline styles to be printed. |
| 11 | * |
| 12 | * @var array |
| 13 | */ |
| 14 | private static $inline_styles = array(); |
| 15 | |
| 16 | /** |
| 17 | * Initialize the class |
| 18 | */ |
| 19 | public static function init() { |
| 20 | add_action( 'wp_enqueue_scripts', array( __CLASS__, 'late_enqueue_bundle_inline_styles' ), 100 ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Enqueue inline style |
| 25 | * |
| 26 | * @param string $style_id Style ID. |
| 27 | * @param string $style Style. |
| 28 | */ |
| 29 | public static function enqueue_inline_style( $style_id, $style ) { |
| 30 | $style = str_replace( array( "\r", "\n" ), '', $style ); |
| 31 | |
| 32 | if ( ! did_action( 'wp_enqueue_scripts' ) ) { |
| 33 | // Not yet enqueued, add to the bundle. |
| 34 | self::$inline_styles[ $style_id ] = $style; |
| 35 | } else { |
| 36 | self::$inline_styles[ $style_id ] = true; |
| 37 | // Already enqueued, add it immediately. |
| 38 | self::late_enqueue_single_style( $style_id, $style ); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Late enqueue single inline style |
| 44 | * |
| 45 | * @param string $style_id Style ID. |
| 46 | * @param string $style Style. |
| 47 | */ |
| 48 | public static function late_enqueue_single_style( $style_id, $style ) { |
| 49 | wp_register_style( |
| 50 | $style_id, |
| 51 | false, |
| 52 | array(), |
| 53 | '1.0.0' |
| 54 | ); |
| 55 | wp_enqueue_style( $style_id ); |
| 56 | |
| 57 | wp_add_inline_style( |
| 58 | $style_id, |
| 59 | $style |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Has inline style |
| 65 | * |
| 66 | * @param string $style_id Style ID. |
| 67 | * @return bool |
| 68 | */ |
| 69 | public static function has_inline_style( $style_id ) { |
| 70 | return isset( self::$inline_styles[ $style_id ] ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Late enqueue bundle inline styles |
| 75 | * |
| 76 | * @return void |
| 77 | */ |
| 78 | public static function late_enqueue_bundle_inline_styles() { |
| 79 | $all_styles = ''; |
| 80 | |
| 81 | foreach ( self::$inline_styles as $style ) { |
| 82 | if ( ! is_string( $style ) ) { |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | $all_styles .= $style; |
| 87 | } |
| 88 | |
| 89 | if ( $all_styles ) { |
| 90 | |
| 91 | if ( ! wp_style_is( 'hustle_inline_styles_front', 'enqueued' ) ) { |
| 92 | // Enqueue the bundle if not already enqueued. |
| 93 | wp_register_style( |
| 94 | 'hustle_inline_styles_front', |
| 95 | false, |
| 96 | array(), |
| 97 | '1.0.0' |
| 98 | ); |
| 99 | wp_enqueue_style( 'hustle_inline_styles_front' ); |
| 100 | } |
| 101 | |
| 102 | wp_add_inline_style( |
| 103 | 'hustle_inline_styles_front', |
| 104 | $all_styles |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Fires after enqueueing inline styles. |
| 110 | */ |
| 111 | do_action( 'hustle_after_enqueue_inline_styles' ); |
| 112 | } |
| 113 | } |
| 114 |