Controls
2 weeks ago
Tabs
2 weeks ago
CFF_Builder_Customizer.php
2 weeks ago
CFF_Db.php
2 weeks ago
CFF_Feed_Builder.php
2 weeks ago
CFF_Feed_Saver.php
2 weeks ago
CFF_Feed_Saver_Manager.php
2 weeks ago
CFF_Post_Set.php
2 weeks ago
CFF_Source.php
2 weeks ago
CFF_Theme_CSS.php
2 weeks ago
CFF_Tooltip_Wizard.php
2 weeks ago
index.php
2 weeks ago
CFF_Theme_CSS.php
270 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * class CFF_Theme_CSS |
| 5 | */ |
| 6 | |
| 7 | namespace CustomFacebookFeed\Builder; |
| 8 | |
| 9 | if (!defined('ABSPATH')) { |
| 10 | exit; // Exit if accessed directly |
| 11 | } |
| 12 | |
| 13 | class CFF_Theme_CSS |
| 14 | { |
| 15 | const WRAP_SELECTOR = '.cff-preview-ctn'; |
| 16 | |
| 17 | /** |
| 18 | * @var string |
| 19 | */ |
| 20 | public $file; |
| 21 | |
| 22 | /** |
| 23 | * @var string |
| 24 | */ |
| 25 | public $css; |
| 26 | |
| 27 | /** |
| 28 | * @var array |
| 29 | */ |
| 30 | public $parsed; |
| 31 | |
| 32 | /** |
| 33 | * @var array |
| 34 | */ |
| 35 | public $styles; |
| 36 | |
| 37 | public function __construct($file) |
| 38 | { |
| 39 | $this->file = $file; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Whether or not a cache exists for this stylesheet. Updates daily or when the theme's stylesheet changes |
| 44 | * |
| 45 | * @return bool |
| 46 | * |
| 47 | * @since 4.0 |
| 48 | */ |
| 49 | public function is_cached() |
| 50 | { |
| 51 | $stored_styles = get_option('cff_theme_styles', array( 'file' => '', 'last_checked' => 0, 'styles' => array() )); |
| 52 | |
| 53 | if ( |
| 54 | empty($stored_styles['file']) |
| 55 | || $stored_styles['file'] !== $this->file |
| 56 | ) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | if ( |
| 61 | empty($stored_styles['last_checked']) |
| 62 | || $stored_styles['last_checked'] < (time() - DAY_IN_SECONDS) |
| 63 | ) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | $this->styles = $stored_styles['styles']; |
| 68 | |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Stores the styles in a wp_option |
| 74 | * |
| 75 | * @return bool |
| 76 | * |
| 77 | * @since 4.0 |
| 78 | */ |
| 79 | public function cache() |
| 80 | { |
| 81 | $stored_styles = get_option('cff_theme_styles', array( 'file' => '', 'last_checked' => 0, 'styles' => array() )); |
| 82 | |
| 83 | $stored_styles['file'] = $this->file; |
| 84 | $stored_styles['styles'] = $this->styles; |
| 85 | $stored_styles['last_checked'] = time(); |
| 86 | |
| 87 | return update_option('cff_theme_styles', $stored_styles, false); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @return array |
| 92 | * |
| 93 | * @since 4.0 |
| 94 | */ |
| 95 | public function get_styles() |
| 96 | { |
| 97 | return $this->styles; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Makes an HTTP request to get the contents of the stylesheet |
| 102 | * |
| 103 | * @since 4.0 |
| 104 | */ |
| 105 | public function load_css() |
| 106 | { |
| 107 | $url = $this->file; |
| 108 | $args = array( |
| 109 | 'timeout' => 60, |
| 110 | ); |
| 111 | $response = wp_safe_remote_get(esc_url_raw($url), $args); |
| 112 | |
| 113 | if (! is_wp_error($response)) { |
| 114 | // certain ways of representing the html for double quotes causes errors so replaced here. |
| 115 | $this->css = $response['body']; |
| 116 | } else { |
| 117 | $this->css = false; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Uses a regex to detect selectors and styles and coverts them to key => value pairs |
| 123 | * |
| 124 | * @return bool |
| 125 | * |
| 126 | * @since 4.0 |
| 127 | */ |
| 128 | public function parse() |
| 129 | { |
| 130 | if (empty($this->css)) { |
| 131 | return false; |
| 132 | } |
| 133 | $css = $this->css; |
| 134 | preg_match_all('/(?ims)([a-z0-9\s\.\:#_\-@,]+)\{([^\}]*)\}/', $css, $arr); |
| 135 | $result = array(); |
| 136 | foreach ($arr[0] as $i => $x) { |
| 137 | $selector = trim($arr[1][ $i ]); |
| 138 | $rules = explode(';', trim($arr[2][ $i ])); |
| 139 | $rules_arr = array(); |
| 140 | foreach ($rules as $strRule) { |
| 141 | if (!empty($strRule)) { |
| 142 | $rule = explode(":", $strRule); |
| 143 | $rule_0 = isset($rule[0]) ? $rule[0] : 'null'; |
| 144 | $rule_1 = isset($rule[1]) ? $rule[1] : ''; |
| 145 | $rules_arr[ trim($rule_0) ] = trim($rule_1); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | $selectors = explode(',', trim($selector)); |
| 150 | foreach ($selectors as $strSel) { |
| 151 | if (! isset($result[ $strSel ])) { |
| 152 | $result[ $strSel ] = $rules_arr; |
| 153 | } else { |
| 154 | $result[ $strSel . '_2' ] = $rules_arr; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | $this->parsed = $result; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Looks for styles based on specified selectors that are used |
| 164 | * in generating the style HTML |
| 165 | * |
| 166 | * @return array |
| 167 | * |
| 168 | * @since 4.0 |
| 169 | */ |
| 170 | public function find_styles() |
| 171 | { |
| 172 | if (empty($this->css)) { |
| 173 | return array(); |
| 174 | } |
| 175 | foreach ($this->parsed as $selector => $property_array) { |
| 176 | foreach ($property_array as $property => $style) { |
| 177 | $this->process($selector, $property, $style); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Loop through all selectors and see if they can be used in our generated |
| 184 | * style HTML |
| 185 | * |
| 186 | * @param string $selector |
| 187 | * @param string $property |
| 188 | * @param string $style |
| 189 | * |
| 190 | * @since 4.0 |
| 191 | */ |
| 192 | public function process($selector, $property, $style) |
| 193 | { |
| 194 | $selector = trim($selector); |
| 195 | if ($selector === 'body') { |
| 196 | if (in_array($property, array( 'color', 'background-color', 'background', 'font-size' ), true)) { |
| 197 | if (! isset($this->styles[ $selector ]['properties'][ $property ]['style'])) { |
| 198 | $this->styles[ $selector ]['properties'][ $property ]['style'] = $style; |
| 199 | } |
| 200 | } |
| 201 | } elseif ($selector === 'a') { |
| 202 | if (in_array($property, array( 'color', 'font-weight', 'text-decoration' ), true)) { |
| 203 | if (! isset($this->styles[ $selector ]['properties'][ $property ]['style'])) { |
| 204 | $this->styles[ $selector ]['properties'][ $property ]['style'] = $style; |
| 205 | } |
| 206 | } |
| 207 | } elseif ($selector === 'a:hover') { |
| 208 | if (in_array($property, array( 'color', 'font-weight', 'text-decoration' ), true)) { |
| 209 | if (! isset($this->styles[ $selector ]['properties'][ $property ]['style'])) { |
| 210 | $this->styles[ $selector ]['properties'][ $property ]['style'] = $style; |
| 211 | } |
| 212 | } |
| 213 | } elseif ($selector === 'p') { |
| 214 | if (in_array($property, array( 'color', 'font-weight', 'font-size' ), true)) { |
| 215 | if (! isset($this->styles[ $selector ]['properties'][ $property ]['style'])) { |
| 216 | $this->styles[ $selector ]['properties'][ $property ]['style'] = $style; |
| 217 | } |
| 218 | } |
| 219 | } elseif ($selector === 'h3') { |
| 220 | if (in_array($property, array( 'color', 'font-weight', 'font-size' ), true)) { |
| 221 | if (! isset($this->styles[ $selector ]['properties'][ $property ]['style'])) { |
| 222 | $this->styles[ $selector ]['properties'][ $property ]['style'] = $style; |
| 223 | } |
| 224 | } |
| 225 | } elseif ($selector === '.entry-content') { |
| 226 | if (in_array($property, array( 'color', 'font-size' ), true)) { |
| 227 | if (! isset($this->styles[ $selector ]['properties'][ $property ]['style'])) { |
| 228 | $this->styles[ $selector ]['properties'][ $property ]['style'] = $style; |
| 229 | } |
| 230 | } |
| 231 | } elseif ($selector === '.entry-content a') { |
| 232 | if (in_array($property, array( 'color', 'font-weight', 'text-decoration' ), true)) { |
| 233 | if (! isset($this->styles[ $selector ]['properties'][ $property ]['style'])) { |
| 234 | $this->styles[ $selector ]['properties'][ $property ]['style'] = $style; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Creates the actual style HTML as a string |
| 242 | * |
| 243 | * @return string |
| 244 | * |
| 245 | * @since 4.0 |
| 246 | */ |
| 247 | public function generate_style_html() |
| 248 | { |
| 249 | if (empty($this->styles)) { |
| 250 | return ''; |
| 251 | } |
| 252 | |
| 253 | $wrap_selector = self::WRAP_SELECTOR; |
| 254 | $html = '<style id="sb-customizer-preview-styles">' . "\n"; |
| 255 | foreach ($this->styles as $selector => $data) { |
| 256 | $selector = $selector === 'body' ? $wrap_selector : $wrap_selector . ' ' . $selector; |
| 257 | $html .= $selector . '{' . "\n"; |
| 258 | foreach ($data as $property_data => $values) { |
| 259 | foreach ($values as $property_key => $property_values) { |
| 260 | $html .= ' ' . $property_key . ': ' . $property_values['style'] . ';' . "\n"; |
| 261 | } |
| 262 | } |
| 263 | $html .= '}' . "\n"; |
| 264 | } |
| 265 | $html .= '</style>'; |
| 266 | |
| 267 | return $html; |
| 268 | } |
| 269 | } |
| 270 |