PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.6
JetFormBuilder — Dynamic Blocks Form Builder v3.6.6
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 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 All 134 releases
jetformbuilder / modules / framework / blocks-style-manager / style-inserter.php

style-inserter.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.6, at modules/framework/blocks-style-manager/style-inserter.php

171 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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' ) && ! wp_doing_ajax() ) {
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 or this is an AJAX request,
101 // we will add the styles to the content.
102 $content = self::styles_with_tag( $styles ) . $content;
103 }
104 }
105 }
106
107 return $content;
108 }
109
110 /**
111 * Get styles collection by name.
112 *
113 * @param string|null $name
114 * @return string
115 */
116 public static function get_styles_collection( $name = null, $with_tag = true ) {
117
118 if ( ! $name ) {
119 return '';
120 }
121
122 $styles = isset( self::$collections[ $name ] ) ? self::$collections[ $name ] : '';
123
124 if ( ! $styles ) {
125 return '';
126 }
127
128 return $with_tag ? self::styles_with_tag( $styles ) : $styles;
129 }
130
131 /**
132 * Add class name to the content wrapper.
133 *
134 * @param string $content Content to add class name to.
135 * @return string
136 */
137 public function with_class_name( $content = '' ) {
138
139 if ( empty( $content ) || empty( $this->class_name ) ) {
140 return $content;
141 }
142
143 if ( ! class_exists( '\WP_HTML_Processor' ) ) {
144 require_once ABSPATH . WPINC . '/class-wp-html-processor.php';
145 }
146
147 $processor = \WP_HTML_Processor::create_fragment( $content );
148
149 while ( $processor->next_tag( [
150 'breadcrumbs' => [ 'BODY', '*' ],
151 'tag_closers' => 'skip',
152 ] ) ) {
153 $processor->add_class( $this->class_name );
154 }
155
156 $content = $processor->get_updated_html();
157
158 return $content;
159 }
160
161 /**
162 * Get styles wrapped into the <style> tag.
163 *
164 * @param string $css CSS styles to insert.
165 * @return string
166 */
167 public static function styles_with_tag( $css = '' ) {
168 return '<style>' . $css . '</style>';
169 }
170 }
171