-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathclass-coblocks-generated-styles.php
More file actions
176 lines (146 loc) · 4.89 KB
/
class-coblocks-generated-styles.php
File metadata and controls
176 lines (146 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/**
* Load generated styles for our blocks.
*
* @package CoBlocks
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Load footer assets for our blocks.
*
* @since 1.6.0
*/
class CoBlocks_Generated_Styles {
/**
* This plugin's instance.
*
* @var CoBlocks_Generated_Styles
*/
private static $instance;
/**
* Registers the plugin.
*
* @return CoBlocks_Generated_Styles
*/
public static function register() {
if ( null === self::$instance ) {
self::$instance = new CoBlocks_Generated_Styles();
}
return self::$instance;
}
/**
* The Constructor.
*/
public function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
}
/**
* Footer Styling
*
* @access public
*/
public function enqueue_styles() {
$styles = $this->styles();
if ( is_null( $styles ) ) {
return;
}
wp_add_inline_style( 'coblocks-frontend-inline', $styles );
}
/**
* Footer Styling
*
* @access public
*/
public function styles() {
global $post;
if ( $post && isset( $post->ID ) ) {
$meta = get_post_meta( $post->ID, '_coblocks_dimensions', true );
$desktop = array();
$tablet = array();
$mobile = array();
$output = '';
if ( $meta ) {
$meta = json_decode( $meta );
if ( ! empty( $meta ) ) {
$important = '';
if ( is_admin() ) {
$important = ' !important';
}
foreach ( $meta as $id => $block ) {
$output .= sprintf( '.%1$s > div {', esc_attr( $id ) );
if ( ! empty( $block ) ) {
foreach ( $block as $key => $style ) {
if ( ! empty( $style ) ) {
foreach ( $style as $ky => $value ) {
if ( ! empty( $value ) && ! preg_match( '/^(NaN)?(px|em|vw|vh|%)$/', $value ) ) {
if ( strpos( $ky, 'Mobile' ) !== false ) {
$mobile[] = strtolower( preg_replace( '/([a-zA-Z])(?=[A-Z])/', '$1-', str_replace( 'Mobile', '', $ky ) ) ) . ':' . esc_attr( $value ) . $important . ';';
} elseif ( strpos( $ky, 'Tablet' ) !== false ) {
$tablet[] = strtolower( preg_replace( '/([a-zA-Z])(?=[A-Z])/', '$1-', str_replace( 'Tablet', '', $ky ) ) ) . ':' . esc_attr( $value ) . $important . ';';
} else {
$output .= strtolower( preg_replace( '/([a-zA-Z])(?=[A-Z])/', '$1-', $ky ) ) . ':' . esc_attr( $value ) . ';';
}
}
}
}
}
}
$output .= '}';
if ( ! empty( $tablet ) ) {
$output .= '@media only screen and (max-width: ' . apply_filters( 'coblocks_tablet_breakpoint', '768px' ) . ') {';
$output .= sprintf( '.%1$s > div {', esc_attr( $id ) );
foreach ( $tablet as $tablet_setting ) {
$output .= $tablet_setting;
}
$output .= '}';
$output .= '}';
}
if ( ! empty( $mobile ) ) {
$output .= '@media only screen and (max-width: ' . apply_filters( 'coblocks_desktop_breakpoint', '514px' ) . ') {';
$output .= sprintf( '.%1$s > div {', esc_attr( $id ) );
foreach ( $mobile as $mobile_setting ) {
$output .= $mobile_setting;
}
$output .= '}';
$output .= '}';
}
// Reset media queries.
$tablet = array();
$mobile = array();
}
}
}
// Add media query for responsive height controls on the front-end.
$responsive_height = get_post_meta( $post->ID, '_coblocks_responsive_height', true );
$responsive_height = json_decode( $responsive_height );
if ( $responsive_height && ! is_admin() ) {
foreach ( $responsive_height as $divider_key => $divider_obj ) {
if ( ! empty( $divider_obj ) ) {
foreach ( $divider_obj as $divider_element => $divider_el_obj ) {
$output .= '@media only screen and (max-width: ' . apply_filters( 'coblocks_tablet_breakpoint', '768px' ) . ') {';
if ( 'height' === $divider_element && isset( $divider_el_obj->heightTablet ) ) { // @codingStandardsIgnoreLine
$output .= sprintf( '.%1$s > [class*="__inner"]:not(.is-fullscreen) {', esc_attr( $divider_key ) );
$output .= 'min-height:' . $divider_el_obj->heightTablet . 'px !important'; // @codingStandardsIgnoreLine
$output .= '}';
}
$output .= '}';
$output .= '@media only screen and (max-width: ' . apply_filters( 'coblocks_desktop_breakpoint', '514px' ) . ') {';
if ( 'height' === $divider_element && isset( $divider_el_obj->heightMobile ) ) { // @codingStandardsIgnoreLine
$output .= sprintf( '.%1$s > [class*="__inner"]:not(.is-fullscreen) {', esc_attr( $divider_key ) );
$output .= 'min-height:' . $divider_el_obj->heightMobile . 'px !important'; // @codingStandardsIgnoreLine
$output .= '}';
}
$output .= '}';
}
}
}
}
return wp_strip_all_tags( $output );
}
}
}
CoBlocks_Generated_Styles::register();