PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.4.2
JetFormBuilder — Dynamic Blocks Form Builder v3.6.4.2
3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / modules / framework / blocks-style-manager / style-inserter.php
jetformbuilder / modules / framework / blocks-style-manager Last commit date
assets 3 months ago field-handlers 3 months ago migrator 3 months ago block.php 3 months ago editor.php 3 months ago path-url-trait.php 3 months ago proxy.php 3 months ago registry.php 3 months ago style-cache.php 3 months ago style-engine.php 3 months ago style-inserter.php 3 months ago style-manager.php 3 months ago webpack.config.js 3 months ago
style-inserter.php
170 lines
1 <?php
2 /**
3 * CSS parser engine
4 */
5 namespace Crocoblock\Blocks_Style;
6
7
8 class Style_Inserter {
9
10 protected $class_name = '';
11
12 protected $data = array();
13
14 /**
15 * Current styles collection.
16 *
17 * @var string|null
18 */
19 public static $current_collection = null;
20
21 /**
22 * All styles grouped into collections.
23 *
24 * @var array
25 */
26 public static $collections = array();
27
28 public function __construct( $class_name = '', $data = array() ) {
29 $this->class_name = $class_name;
30 $this->data = $data;
31 }
32
33 /**
34 * Set current styles collection.
35 *
36 * @param string|null $name
37 * @return void
38 */
39 public static function set_collection( $name = null ) {
40 self::$current_collection = $name;
41 }
42
43 /**
44 * Insert styles into the given content.
45 *
46 * 1. If the content is empty, it returns an empty string.
47 * 2. If $this->data contains not empty 'styles' key,
48 * it returns the styles wrapped in a <style> tag and adds to the begining
49 * of the content.
50 * 3. Also it adds $this->class_name to the content wrapper class.
51 * 4. To do this we need to check - if first tag of the content is contains
52 * class attribute, we will add our class to it.
53 * 5. If the first tag does not contain class attribute, we create a new class attribute with
54 * our class and add it to the first tag.
55 * 6. Now we need to check if $this->data contains not empty 'variables' key,
56 * if so, we need to add these variables to the style attribute of first tag in the content.
57 * 7. If the first tag does not contain style attribute, we create a new style attribute
58 * with our variables and add it to the first tag if already contain - we will append our
59 * variables to it.
60 *
61 * @param string $content Content to insert styles into.
62 * @return string
63 */
64 public function insert_styles( $content = '' ) {
65
66 $variables = '';
67
68 if ( ! empty( $this->data['variables'] ) ) {
69 $variables = trim( ' ' . $this->data['variables'] );
70 $variables = sprintf( '.%1$s { %2$s }', $this->class_name, $variables );
71 }
72
73 $styles = trim( $variables . ' ' . $this->data['styles'] );
74
75 if ( ! empty( $styles ) ) {
76
77 if ( ! empty( $content ) && ! empty( $this->class_name ) ) {
78 $content = $this->with_class_name( $content );
79 }
80
81 if ( ! empty( self::$current_collection ) ) {
82
83 if ( ! isset( self::$collections[ self::$current_collection ] ) ) {
84 self::$collections[ self::$current_collection ] = '';
85 }
86
87 self::$collections[ self::$current_collection ] .= $styles;
88
89 // If we stored styles into the collection - there is nothing more to do.
90 return $content;
91 }
92
93 if ( ! Style_Cache::get_instance()->is_printed( $this->class_name ) ) {
94 if ( ! did_action( 'wp_head' ) ) {
95 // If the wp_head action isn't called yet, we will add the styles to the head.
96 add_action( 'wp_head', function() use ( $styles ) {
97 echo self::styles_with_tag( $styles );
98 } );
99 } else {
100 // If the wp_head action is already called, we will add the styles to the content.
101 $content = self::styles_with_tag( $styles ) . $content;
102 }
103 }
104 }
105
106 return $content;
107 }
108
109 /**
110 * Get styles collection by name.
111 *
112 * @param string|null $name
113 * @return string
114 */
115 public static function get_styles_collection( $name = null, $with_tag = true ) {
116
117 if ( ! $name ) {
118 return '';
119 }
120
121 $styles = isset( self::$collections[ $name ] ) ? self::$collections[ $name ] : '';
122
123 if ( ! $styles ) {
124 return '';
125 }
126
127 return $with_tag ? self::styles_with_tag( $styles ) : $styles;
128 }
129
130 /**
131 * Add class name to the content wrapper.
132 *
133 * @param string $content Content to add class name to.
134 * @return string
135 */
136 public function with_class_name( $content = '' ) {
137
138 if ( empty( $content ) || empty( $this->class_name ) ) {
139 return $content;
140 }
141
142 if ( ! class_exists( '\WP_HTML_Processor' ) ) {
143 require_once ABSPATH . WPINC . '/class-wp-html-processor.php';
144 }
145
146 $processor = \WP_HTML_Processor::create_fragment( $content );
147
148 while ( $processor->next_tag( [
149 'breadcrumbs' => [ 'BODY', '*' ],
150 'tag_closers' => 'skip',
151 ] ) ) {
152 $processor->add_class( $this->class_name );
153 }
154
155 $content = $processor->get_updated_html();
156
157 return $content;
158 }
159
160 /**
161 * Get styles wrapped into the <style> tag.
162 *
163 * @param string $css CSS styles to insert.
164 * @return string
165 */
166 public static function styles_with_tag( $css = '' ) {
167 return '<style>' . $css . '</style>';
168 }
169 }
170