| @@ -4,412 +4,505 @@ | ||
| 4 | 4 | * |
| 5 | 5 | * @package ghostkit |
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | -if ( ! function_exists( 'is_plugin_active' ) ) { | |
| 9 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 10 | -} | |
| 8 | +if ( ! class_exists( 'GhostKit_Breakpoints' ) ) { | |
| 9 | + /** | |
| 10 | + * GhostKit_Breakpoints class | |
| 11 | + */ | |
| 12 | + // phpcs:ignore | |
| 13 | + class GhostKit_Breakpoints { | |
| 14 | + /** | |
| 15 | + * Extra Small Default Breakpoint. | |
| 16 | + * | |
| 17 | + * @var int | |
| 18 | + */ | |
| 19 | + protected static $default_xs = 576; | |
| 11 | 20 | |
| 12 | -// phpcs:ignore | |
| 13 | -$pro_version = false; | |
| 21 | + /** | |
| 22 | + * Mobile Default Breakpoint. | |
| 23 | + * | |
| 24 | + * @var int | |
| 25 | + */ | |
| 26 | + protected static $default_sm = 768; | |
| 14 | 27 | |
| 15 | -if ( is_plugin_active( 'ghostkit-pro/class-ghost-kit-pro.php' ) ) { | |
| 16 | - // phpcs:ignore | |
| 17 | - $pro_data = get_plugin_data( ABSPATH . 'wp-content/plugins/ghostkit-pro/class-ghost-kit-pro.php' ); | |
| 18 | - // phpcs:ignore | |
| 19 | - $pro_version = isset( $pro_data['Version'] ) ? $pro_data['Version'] : false; | |
| 20 | -} | |
| 28 | + /** | |
| 29 | + * Tablet Breakpoint. | |
| 30 | + * | |
| 31 | + * @var int | |
| 32 | + */ | |
| 33 | + protected static $default_md = 992; | |
| 21 | 34 | |
| 22 | -if ( $pro_version && version_compare( $pro_version, '1.6.2', '<=' ) ) { | |
| 23 | - require_once $this->plugin_path . 'classes/class-breakpoints-fallback.php'; | |
| 24 | -} | |
| 35 | + /** | |
| 36 | + * Desktop Breakpoint. | |
| 37 | + * | |
| 38 | + * @var int | |
| 39 | + */ | |
| 40 | + protected static $default_lg = 1200; | |
| 25 | 41 | |
| 26 | -if ( ! class_exists( 'GhostKit_Breakpoints' ) ) { | |
| 27 | - /** | |
| 28 | - * GhostKit_Breakpoints class | |
| 29 | - */ | |
| 30 | - // phpcs:ignore | |
| 31 | - class GhostKit_Breakpoints { | |
| 32 | - /** | |
| 33 | - * Extra Small Default Breakpoint. | |
| 34 | - * | |
| 35 | - * @var int | |
| 36 | - */ | |
| 37 | - protected static $default_xs = 576; | |
| 42 | + /** | |
| 43 | + * Plugin name. Also the directory under uploads that holds the generated CSS. | |
| 44 | + * | |
| 45 | + * @var string | |
| 46 | + */ | |
| 47 | + protected $plugin_name = 'ghostkit'; | |
| 38 | 48 | |
| 39 | - /** | |
| 40 | - * Mobile Default Breakpoint. | |
| 41 | - * | |
| 42 | - * @var int | |
| 43 | - */ | |
| 44 | - protected static $default_sm = 768; | |
| 49 | + /** | |
| 50 | + * Plugin version. | |
| 51 | + * | |
| 52 | + * @var string | |
| 53 | + */ | |
| 54 | + protected $plugin_version = GHOSTKIT_VERSION; | |
| 45 | 55 | |
| 46 | - /** | |
| 47 | - * Tablet Breakpoint. | |
| 48 | - * | |
| 49 | - * @var int | |
| 50 | - */ | |
| 51 | - protected static $default_md = 992; | |
| 56 | + /** | |
| 57 | + * GhostKit_Breakpoints constructor. | |
| 58 | + */ | |
| 59 | + public function __construct() { | |
| 60 | + add_filter( 'style_loader_src', array( $this, 'change_style_src_to_compile' ), 10, 1 ); | |
| 61 | + add_action( 'gkt_before_assets_register', array( $this, 'maybe_generate_css' ) ); | |
| 62 | + } | |
| 52 | 63 | |
| 53 | - /** | |
| 54 | - * Desktop Breakpoint. | |
| 55 | - * | |
| 56 | - * @var int | |
| 57 | - */ | |
| 58 | - protected static $default_lg = 1200; | |
| 64 | + /** | |
| 65 | + * Get plugin path. | |
| 66 | + * | |
| 67 | + * @return string | |
| 68 | + */ | |
| 69 | + public function get_plugin_path() { | |
| 70 | + return ghostkit()->plugin_path; | |
| 71 | + } | |
| 59 | 72 | |
| 60 | - /** | |
| 61 | - * Database saved hash option Name. | |
| 62 | - * | |
| 63 | - * @var string | |
| 64 | - */ | |
| 65 | - protected $database_saved_hash_option_name = 'ghostkit_saved_breakpoints_hash'; | |
| 73 | + /** | |
| 74 | + * Get plugin url. | |
| 75 | + * | |
| 76 | + * @return string | |
| 77 | + */ | |
| 78 | + public function get_plugin_url() { | |
| 79 | + return ghostkit()->plugin_url; | |
| 80 | + } | |
| 66 | 81 | |
| 67 | - /** | |
| 68 | - * Plugin name. | |
| 69 | - * | |
| 70 | - * @var string | |
| 71 | - */ | |
| 72 | - protected $plugin_name = 'ghostkit'; | |
| 82 | + /** | |
| 83 | + * Name of the option that holds the hash and the list of generated files. | |
| 84 | + * | |
| 85 | + * @return string | |
| 86 | + */ | |
| 87 | + protected function get_option_name() { | |
| 88 | + return str_replace( '-', '_', $this->plugin_name ) . '_breakpoints_css'; | |
| 89 | + } | |
| 73 | 90 | |
| 74 | - /** | |
| 75 | - * Plugin version. | |
| 76 | - * | |
| 77 | - * @var string | |
| 78 | - */ | |
| 79 | - protected $plugin_version = '2.25.0'; | |
| 91 | + /** | |
| 92 | + * Directory under uploads that holds the generated CSS. | |
| 93 | + * | |
| 94 | + * @return string | |
| 95 | + */ | |
| 96 | + protected function get_output_dir() { | |
| 97 | + $upload_dir = wp_get_upload_dir(); | |
| 80 | 98 | |
| 81 | - /** | |
| 82 | - * Scss Configurations. | |
| 83 | - * | |
| 84 | - * @var array | |
| 85 | - */ | |
| 86 | - protected $scss_configs = array( | |
| 87 | - 'gutenberg/style.scss', | |
| 88 | - 'gutenberg/editor.scss', | |
| 89 | - 'gutenberg/blocks/*/styles/style.scss', | |
| 90 | - ); | |
| 99 | + return $upload_dir['basedir'] . '/' . $this->plugin_name; | |
| 100 | + } | |
| 91 | 101 | |
| 92 | - /** | |
| 93 | - * Compile Scss Configurations. | |
| 94 | - * | |
| 95 | - * @var array | |
| 96 | - */ | |
| 97 | - protected $compile_scss_configs; | |
| 102 | + /** | |
| 103 | + * Stored hash and list of generated files. | |
| 104 | + * | |
| 105 | + * @return array|false | |
| 106 | + */ | |
| 107 | + protected function get_generated_css() { | |
| 108 | + $stored = get_option( $this->get_option_name() ); | |
| 98 | 109 | |
| 99 | - /** | |
| 100 | - * Background breakpoints processing. | |
| 101 | - * | |
| 102 | - * @var object | |
| 103 | - */ | |
| 104 | - protected $breakpoints_background_process; | |
| 110 | + if ( | |
| 111 | + ! is_array( $stored ) || | |
| 112 | + ! isset( $stored['hash'], $stored['files'] ) || | |
| 113 | + ! is_array( $stored['files'] ) | |
| 114 | + ) { | |
| 115 | + return false; | |
| 116 | + } | |
| 105 | 117 | |
| 106 | - /** | |
| 107 | - * GhostKit_Breakpoints constructor. | |
| 108 | - */ | |
| 109 | - public function __construct() { | |
| 110 | - if ( ! class_exists( 'GhostKit_Breakpoints_Background' ) ) { | |
| 111 | - // breakpoints background. | |
| 112 | - require_once __DIR__ . '/class-breakpoints-background.php'; | |
| 113 | - } | |
| 118 | + return $stored; | |
| 119 | + } | |
| 114 | 120 | |
| 115 | - $this->compile_scss_configs = $this->get_compile_scss_configs(); | |
| 116 | - $this->breakpoints_background_process = new GhostKit_Breakpoints_Background(); | |
| 121 | + /** | |
| 122 | + * Regenerate the CSS when the breakpoints or the plugin version changed. | |
| 123 | + * | |
| 124 | + * @return void | |
| 125 | + */ | |
| 126 | + public function maybe_generate_css() { | |
| 127 | + $breakpoints = self::get_breakpoints(); | |
| 128 | + $hash = $this->get_breakpoints_hash( $breakpoints ); | |
| 129 | + $stored = $this->get_generated_css(); | |
| 117 | 130 | |
| 118 | - add_filter( 'style_loader_src', array( $this, 'change_style_src_to_compile' ), 10, 1 ); | |
| 119 | - add_action( 'gkt_before_assets_register', array( $this, 'maybe_compile_scss_files' ) ); | |
| 120 | - } | |
| 131 | + if ( $hash === $this->get_default_breakpoints_hash() ) { | |
| 132 | + if ( $stored ) { | |
| 133 | + $this->remove_generated_css(); | |
| 134 | + } | |
| 135 | + return; | |
| 136 | + } | |
| 121 | 137 | |
| 122 | - /** | |
| 123 | - * Get plugin path. | |
| 124 | - * | |
| 125 | - * @return string | |
| 126 | - */ | |
| 127 | - public function get_plugin_path() { | |
| 128 | - return ghostkit()->plugin_path; | |
| 129 | - } | |
| 138 | + if ( $stored && $stored['hash'] === $hash ) { | |
| 139 | + return; | |
| 140 | + } | |
| 130 | 141 | |
| 131 | - /** | |
| 132 | - * Get plugin url. | |
| 133 | - * | |
| 134 | - * @return string | |
| 135 | - */ | |
| 136 | - public function get_plugin_url() { | |
| 137 | - return ghostkit()->plugin_url; | |
| 138 | - } | |
| 142 | + $this->remove_generated_css(); | |
| 139 | 143 | |
| 140 | - /** | |
| 141 | - * Get compile scss configurations. | |
| 142 | - * | |
| 143 | - * @return array | |
| 144 | - */ | |
| 145 | - protected function get_compile_scss_configs() { | |
| 146 | - $plugin_path = $this->get_plugin_path(); | |
| 147 | - $upload_dir = wp_upload_dir(); | |
| 148 | - $compile_scss_configs = array(); | |
| 149 | - $scss_paths = apply_filters( 'gkt_scss_paths', $plugin_path ); | |
| 150 | - $scss_configs = apply_filters( 'gkt_scss_configs', $this->scss_configs ); | |
| 144 | + update_option( | |
| 145 | + $this->get_option_name(), | |
| 146 | + array( | |
| 147 | + 'hash' => $hash, | |
| 148 | + 'files' => $this->generate_css( $breakpoints ), | |
| 149 | + ) | |
| 150 | + ); | |
| 151 | + } | |
| 151 | 152 | |
| 152 | - foreach ( $scss_configs as $scss_config ) { | |
| 153 | - if ( is_array( $scss_paths ) ) { | |
| 154 | - foreach ( $scss_paths as $path ) { | |
| 155 | - $compile_scss_configs = $this->get_parsed_scss_files( $compile_scss_configs, $path, $upload_dir, $scss_config ); | |
| 156 | - } | |
| 157 | - } else { | |
| 158 | - $compile_scss_configs = $this->get_parsed_scss_files( $compile_scss_configs, $scss_paths, $upload_dir, $scss_config ); | |
| 159 | - } | |
| 160 | - } | |
| 153 | + /** | |
| 154 | + * Write the built CSS files with the breakpoint values replaced into uploads. | |
| 155 | + * | |
| 156 | + * @param array $breakpoints - Breakpoints. | |
| 157 | + * @return array Paths of the written files, relative to the plugin directory. | |
| 158 | + */ | |
| 159 | + protected function generate_css( $breakpoints ) { | |
| 160 | + $plugin_path = wp_normalize_path( $this->get_plugin_path() ); | |
| 161 | + $build_dir = $plugin_path . 'build'; | |
| 162 | + $output_dir = $this->get_output_dir(); | |
| 163 | + $files = array(); | |
| 161 | 164 | |
| 162 | - return $compile_scss_configs; | |
| 163 | - } | |
| 165 | + if ( ! is_dir( $build_dir ) ) { | |
| 166 | + return $files; | |
| 167 | + } | |
| 164 | 168 | |
| 165 | - /** | |
| 166 | - * Get parsed array of scss files | |
| 167 | - * | |
| 168 | - * @param array $compile_scss_configs - Array of config width scss files. | |
| 169 | - * @param string $scss_path - Path to scss files. | |
| 170 | - * @param string $upload_dir - Uploas WP dir. | |
| 171 | - * @param string $scss_config - File search mask. | |
| 172 | - * @return array | |
| 173 | - */ | |
| 174 | - protected function get_parsed_scss_files( &$compile_scss_configs, $scss_path, $upload_dir, $scss_config ) { | |
| 175 | - foreach ( glob( $scss_path . $scss_config ) as $template ) { | |
| 176 | - $output_file = str_replace( $scss_path, $upload_dir['basedir'] . '/' . $this->plugin_name . '/', $template ); | |
| 177 | - $output_file = str_replace( '.scss', '.min.css', $output_file ); | |
| 169 | + try { | |
| 170 | + // CATCH_GET_CHILD skips a subdirectory PHP cannot open instead of ending the walk. | |
| 171 | + $iterator = new RecursiveIteratorIterator( | |
| 172 | + new RecursiveDirectoryIterator( $build_dir, FilesystemIterator::SKIP_DOTS ), | |
| 173 | + RecursiveIteratorIterator::LEAVES_ONLY, | |
| 174 | + RecursiveIteratorIterator::CATCH_GET_CHILD | |
| 175 | + ); | |
| 178 | 176 | |
| 179 | - $compile_scss_configs[] = array( | |
| 180 | - 'input_file' => $template, | |
| 181 | - 'output_file' => $output_file, | |
| 182 | - ); | |
| 183 | - } | |
| 184 | - return $compile_scss_configs; | |
| 185 | - } | |
| 177 | + foreach ( $iterator as $file ) { | |
| 178 | + if ( 'css' !== $file->getExtension() ) { | |
| 179 | + continue; | |
| 180 | + } | |
| 186 | 181 | |
| 187 | - /** | |
| 188 | - * Change style src to compile css files. | |
| 189 | - * | |
| 190 | - * @param string $src - Url to style. | |
| 191 | - * @return string | |
| 192 | - */ | |
| 193 | - public function change_style_src_to_compile( $src ) { | |
| 194 | - $plugin_url = $this->get_plugin_url(); | |
| 195 | - $breakpoints = self::get_breakpoints(); | |
| 196 | - $breakpoints_hash = $this->get_breakpoints_hash( $breakpoints ); | |
| 197 | - $default_breakpoints_hash = $this->get_default_breakpoints_hash(); | |
| 182 | + $path = wp_normalize_path( $file->getPathname() ); | |
| 183 | + // phpcs:ignore | |
| 184 | + $css = file_get_contents( $path ); | |
| 198 | 185 | |
| 199 | - if ( $breakpoints_hash !== $default_breakpoints_hash ) { | |
| 200 | - $is_plugin_file = strstr( $src, $plugin_url ); | |
| 186 | + if ( false === $css ) { | |
| 187 | + continue; | |
| 188 | + } | |
| 201 | 189 | |
| 202 | - if ( $is_plugin_file ) { | |
| 203 | - $configs = $this->compile_scss_configs; | |
| 204 | - $relative_uri = str_replace( $plugin_url, '', $is_plugin_file ); | |
| 205 | - $relative_path = str_replace( '?ver=' . $this->plugin_version, '', $relative_uri ); | |
| 206 | - $upload_dir = wp_upload_dir(); | |
| 207 | - $output_file = $upload_dir['basedir'] . '/' . $this->plugin_name . '/' . $relative_path; | |
| 190 | + $replaced = self::replace_breakpoints( $css, $breakpoints ); | |
| 208 | 191 | |
| 209 | - foreach ( $configs as $config ) { | |
| 210 | - if ( | |
| 211 | - $config['output_file'] === $output_file && | |
| 212 | - file_exists( $output_file ) | |
| 213 | - ) { | |
| 214 | - $src = $upload_dir['baseurl'] . '/' . $this->plugin_name . '/' . $relative_uri; | |
| 215 | - } | |
| 216 | - } | |
| 217 | - } | |
| 218 | - } | |
| 192 | + if ( $replaced === $css ) { | |
| 193 | + continue; | |
| 194 | + } | |
| 219 | 195 | |
| 220 | - return $src; | |
| 221 | - } | |
| 196 | + $relative = substr( $path, strlen( $plugin_path ) ); | |
| 197 | + $target = $output_dir . '/' . $relative; | |
| 222 | 198 | |
| 223 | - /** | |
| 224 | - * Get Breakpoints. | |
| 225 | - */ | |
| 226 | - public static function get_breakpoints() { | |
| 227 | - $xs = self::get_breakpoint_xs(); | |
| 228 | - $xs = ( ! empty( $xs ) && $xs ) ? $xs : self::$default_xs; | |
| 199 | + if ( ! wp_mkdir_p( dirname( $target ) ) ) { | |
| 200 | + continue; | |
| 201 | + } | |
| 229 | 202 | |
| 230 | - $sm = self::get_breakpoint_sm(); | |
| 231 | - $sm = ( ! empty( $sm ) && $sm ) ? $sm : self::$default_sm; | |
| 203 | + // phpcs:ignore | |
| 204 | + if ( false === file_put_contents( $target, $replaced ) ) { | |
| 205 | + continue; | |
| 206 | + } | |
| 232 | 207 | |
| 233 | - $md = self::get_breakpoint_md(); | |
| 234 | - $md = ( ! empty( $md ) && $md ) ? $md : self::$default_md; | |
| 208 | + $files[] = $relative; | |
| 209 | + } | |
| 210 | + } catch ( UnexpectedValueException $e ) { | |
| 211 | + // `build` itself could not be opened; nothing was written. | |
| 212 | + unset( $e ); | |
| 213 | + } | |
| 235 | 214 | |
| 236 | - $lg = self::get_breakpoint_lg(); | |
| 237 | - $lg = ( ! empty( $lg ) && $lg ) ? $lg : self::$default_lg; | |
| 215 | + // WordPress derives the `-rtl.css` URL from the served one, so a file whose RTL twin | |
| 216 | + // exists in the build but was not written would give RTL sites a 404. Serve neither. | |
| 217 | + foreach ( $files as $index => $relative ) { | |
| 218 | + $rtl_twin = preg_replace( '/\.css$/', '-rtl.css', $relative ); | |
| 238 | 219 | |
| 239 | - return array( | |
| 240 | - 'xs' => $xs, | |
| 241 | - 'sm' => $sm, | |
| 242 | - 'md' => $md, | |
| 243 | - 'lg' => $lg, | |
| 244 | - ); | |
| 245 | - } | |
| 220 | + if ( | |
| 221 | + $rtl_twin !== $relative && | |
| 222 | + ! in_array( $rtl_twin, $files, true ) && | |
| 223 | + file_exists( $plugin_path . $rtl_twin ) | |
| 224 | + ) { | |
| 225 | + wp_delete_file( $output_dir . '/' . $relative ); | |
| 226 | + unset( $files[ $index ] ); | |
| 227 | + } | |
| 228 | + } | |
| 246 | 229 | |
| 247 | - /** | |
| 248 | - * Get default breakpoints. | |
| 249 | - * | |
| 250 | - * @return array | |
| 251 | - */ | |
| 252 | - public static function get_default_breakpoints() { | |
| 253 | - return array( | |
| 254 | - 'xs' => self::get_default_breakpoint_xs(), | |
| 255 | - 'sm' => self::get_default_breakpoint_sm(), | |
| 256 | - 'md' => self::get_default_breakpoint_md(), | |
| 257 | - 'lg' => self::get_default_breakpoint_lg(), | |
| 258 | - ); | |
| 259 | - } | |
| 230 | + $files = array_values( $files ); | |
| 231 | + sort( $files ); | |
| 260 | 232 | |
| 261 | - /** | |
| 262 | - * Get breakpoints Hash | |
| 263 | - * | |
| 264 | - * @param array $breakpoints - Breakpoints. | |
| 265 | - * @return string | |
| 266 | - */ | |
| 267 | - protected function get_breakpoints_hash( $breakpoints ) { | |
| 268 | - return md5( | |
| 269 | - wp_json_encode( | |
| 270 | - array_merge( | |
| 271 | - $breakpoints, | |
| 272 | - array( | |
| 273 | - $this->plugin_version, | |
| 274 | - ) | |
| 275 | - ) | |
| 276 | - ) | |
| 277 | - ); | |
| 278 | - } | |
| 233 | + return $files; | |
| 234 | + } | |
| 279 | 235 | |
| 280 | - /** | |
| 281 | - * Get default breakpoints Hash. | |
| 282 | - * | |
| 283 | - * @return string | |
| 284 | - */ | |
| 285 | - protected function get_default_breakpoints_hash() { | |
| 286 | - return $this->get_breakpoints_hash( | |
| 287 | - array( | |
| 288 | - 'xs' => self::$default_xs, | |
| 289 | - 'sm' => self::$default_sm, | |
| 290 | - 'md' => self::$default_md, | |
| 291 | - 'lg' => self::$default_lg, | |
| 292 | - ) | |
| 293 | - ); | |
| 294 | - } | |
| 236 | + /** | |
| 237 | + * Remove the generated CSS and the option that lists it. | |
| 238 | + * | |
| 239 | + * @return void | |
| 240 | + */ | |
| 241 | + protected function remove_generated_css() { | |
| 242 | + self::remove_dir( $this->get_output_dir() . '/build' ); | |
| 243 | + delete_option( $this->get_option_name() ); | |
| 244 | + } | |
| 295 | 245 | |
| 296 | - /** | |
| 297 | - * Styles may need to be compiled if breakpoints have been changed. | |
| 298 | - * | |
| 299 | - * @return void | |
| 300 | - */ | |
| 301 | - public function maybe_compile_scss_files() { | |
| 302 | - $breakpoints = self::get_breakpoints(); | |
| 303 | - $breakpoints_hash = $this->get_breakpoints_hash( $breakpoints ); | |
| 304 | - $default_breakpoints_hash = $this->get_default_breakpoints_hash(); | |
| 305 | - $saved_breakpoints_hash = get_option( $this->database_saved_hash_option_name ); | |
| 246 | + /** | |
| 247 | + * Delete a directory with everything inside it. | |
| 248 | + * | |
| 249 | + * @param string $dir - Absolute path. | |
| 250 | + * @return void | |
| 251 | + */ | |
| 252 | + public static function remove_dir( $dir ) { | |
| 253 | + if ( ! is_dir( $dir ) ) { | |
| 254 | + return; | |
| 255 | + } | |
| 306 | 256 | |
| 307 | - if ( | |
| 308 | - $breakpoints_hash !== $saved_breakpoints_hash && | |
| 309 | - $breakpoints_hash !== $default_breakpoints_hash | |
| 310 | - ) { | |
| 311 | - $compile_scss_configs = $this->compile_scss_configs; | |
| 257 | + try { | |
| 258 | + $iterator = new RecursiveIteratorIterator( | |
| 259 | + new RecursiveDirectoryIterator( $dir, FilesystemIterator::SKIP_DOTS ), | |
| 260 | + RecursiveIteratorIterator::CHILD_FIRST | |
| 261 | + ); | |
| 312 | 262 | |
| 313 | - $breakpoints = self::get_breakpoints(); | |
| 263 | + foreach ( $iterator as $item ) { | |
| 264 | + if ( $item->isDir() ) { | |
| 265 | + // phpcs:ignore | |
| 266 | + rmdir( $item->getPathname() ); | |
| 267 | + } else { | |
| 268 | + wp_delete_file( $item->getPathname() ); | |
| 269 | + } | |
| 270 | + } | |
| 271 | + } catch ( UnexpectedValueException $e ) { | |
| 272 | + // A directory PHP cannot open stays in place, with the directories above it. | |
| 273 | + return; | |
| 274 | + } | |
| 314 | 275 | |
| 315 | - $variables = array( | |
| 316 | - 'media-xs' => $breakpoints['xs'] . 'px', | |
| 317 | - 'media-sm' => $breakpoints['sm'] . 'px', | |
| 318 | - 'media-md' => $breakpoints['md'] . 'px', | |
| 319 | - 'media-lg' => $breakpoints['lg'] . 'px', | |
| 320 | - ); | |
| 276 | + // phpcs:ignore | |
| 277 | + rmdir( $dir ); | |
| 278 | + } | |
| 321 | 279 | |
| 322 | - foreach ( $compile_scss_configs as $compile_scss_config ) { | |
| 323 | - $scss_config_arguments = array_merge( | |
| 324 | - $compile_scss_config, | |
| 325 | - array( | |
| 326 | - 'variables' => $variables, | |
| 327 | - ) | |
| 328 | - ); | |
| 329 | - if ( ! file_exists( $compile_scss_config['output_file'] ) ) { | |
| 330 | - new GhostKit_Scss_Compiler( $scss_config_arguments ); | |
| 331 | - } else { | |
| 332 | - $this->breakpoints_background_process->push_to_queue( $scss_config_arguments ); | |
| 333 | - } | |
| 334 | - } | |
| 280 | + /** | |
| 281 | + * Replace the default breakpoint values inside `@media` preludes with the given ones. | |
| 282 | + * | |
| 283 | + * Only `(min-width: Npx)` and `(max-width: Npx)` whose N is a default breakpoint change, | |
| 284 | + * in one pass, so a custom value equal to another default is never replaced twice. | |
| 285 | + * | |
| 286 | + * @param string $css - CSS to process. | |
| 287 | + * @param array $breakpoints - Breakpoints keyed xs, sm, md, lg. | |
| 288 | + * @return string | |
| 289 | + */ | |
| 290 | + public static function replace_breakpoints( $css, $breakpoints ) { | |
| 291 | + $defaults = array( | |
| 292 | + 'xs' => self::$default_xs, | |
| 293 | + 'sm' => self::$default_sm, | |
| 294 | + 'md' => self::$default_md, | |
| 295 | + 'lg' => self::$default_lg, | |
| 296 | + ); | |
| 297 | + $map = array(); | |
| 335 | 298 | |
| 336 | - $this->breakpoints_background_process->save()->dispatch(); | |
| 299 | + foreach ( $defaults as $name => $default ) { | |
| 300 | + if ( isset( $breakpoints[ $name ] ) && is_numeric( $breakpoints[ $name ] ) ) { | |
| 301 | + // Themes pass fractions such as 777.98 to keep a max-width query from | |
| 302 | + // overlapping the min-width one, so the value is kept as given. | |
| 303 | + $map[ $default ] = (string) ( 0 + $breakpoints[ $name ] ); | |
| 304 | + } | |
| 305 | + } | |
| 337 | 306 | |
| 338 | - update_option( $this->database_saved_hash_option_name, $breakpoints_hash ); | |
| 339 | - } | |
| 340 | - } | |
| 307 | + return preg_replace_callback( | |
| 308 | + '/@media[^{;]*\{/', | |
| 309 | + function ( $prelude ) use ( $map ) { | |
| 310 | + return preg_replace_callback( | |
| 311 | + '/\(\s*(min|max)-width\s*:\s*(\d+)px\s*\)/', | |
| 312 | + function ( $query ) use ( $map ) { | |
| 313 | + $value = (int) $query[2]; | |
| 341 | 314 | |
| 342 | - /** | |
| 343 | - * Get Default Extra Small Breakpoint. | |
| 344 | - * | |
| 345 | - * @return int | |
| 346 | - */ | |
| 347 | - public static function get_default_breakpoint_xs() { | |
| 348 | - return apply_filters( 'gkt_default_breakpoint_xs', self::$default_xs ); | |
| 349 | - } | |
| 315 | + if ( ! isset( $map[ $value ] ) ) { | |
| 316 | + return $query[0]; | |
| 317 | + } | |
| 350 | 318 | |
| 351 | - /** | |
| 352 | - * Get Extra Small Breakpoint. | |
| 353 | - * | |
| 354 | - * @return int | |
| 355 | - */ | |
| 356 | - public static function get_breakpoint_xs() { | |
| 357 | - return apply_filters( 'gkt_breakpoint_xs', self::get_default_breakpoint_xs() ); | |
| 358 | - } | |
| 319 | + return '(' . $query[1] . '-width:' . $map[ $value ] . 'px)'; | |
| 320 | + }, | |
| 321 | + $prelude[0] | |
| 322 | + ); | |
| 323 | + }, | |
| 324 | + $css | |
| 325 | + ); | |
| 326 | + } | |
| 359 | 327 | |
| 360 | - /** | |
| 361 | - * Get Default Mobile Breakpoint. | |
| 362 | - * | |
| 363 | - * @return int | |
| 364 | - */ | |
| 365 | - public static function get_default_breakpoint_sm() { | |
| 366 | - return apply_filters( 'gkt_default_breakpoint_sm', self::$default_sm ); | |
| 367 | - } | |
| 328 | + /** | |
| 329 | + * Serve the generated CSS instead of the plugin file when custom breakpoints are set. | |
| 330 | + * | |
| 331 | + * @param string $src - Url to style. | |
| 332 | + * @return string | |
| 333 | + */ | |
| 334 | + public function change_style_src_to_compile( $src ) { | |
| 335 | + $stored = $this->get_generated_css(); | |
| 368 | 336 | |
| 369 | - /** | |
| 370 | - * Get Mobile Breakpoint. | |
| 371 | - * | |
| 372 | - * @return int | |
| 373 | - */ | |
| 374 | - public static function get_breakpoint_sm() { | |
| 375 | - return apply_filters( 'gkt_breakpoint_sm', self::get_default_breakpoint_sm() ); | |
| 376 | - } | |
| 337 | + if ( ! $stored || empty( $stored['files'] ) ) { | |
| 338 | + return $src; | |
| 339 | + } | |
| 377 | 340 | |
| 378 | - /** | |
| 379 | - * Get Default Tablet Breakpoint. | |
| 380 | - * | |
| 381 | - * @return int | |
| 382 | - */ | |
| 383 | - public static function get_default_breakpoint_md() { | |
| 384 | - return apply_filters( 'gkt_default_breakpoint_md', self::$default_md ); | |
| 385 | - } | |
| 341 | + $plugin_url = $this->get_plugin_url(); | |
| 386 | 342 | |
| 387 | - /** | |
| 388 | - * Get Tablet Breakpoint. | |
| 389 | - * | |
| 390 | - * @return int | |
| 391 | - */ | |
| 392 | - public static function get_breakpoint_md() { | |
| 393 | - return apply_filters( 'gkt_breakpoint_md', self::get_default_breakpoint_md() ); | |
| 394 | - } | |
| 343 | + if ( 0 !== strpos( $src, $plugin_url ) ) { | |
| 344 | + return $src; | |
| 345 | + } | |
| 395 | 346 | |
| 396 | - /** | |
| 397 | - * Get Default Desktop Breakpoint. | |
| 398 | - * | |
| 399 | - * @return int | |
| 400 | - */ | |
| 401 | - public static function get_default_breakpoint_lg() { | |
| 402 | - return apply_filters( 'gkt_default_breakpoint_lg', self::$default_lg ); | |
| 403 | - } | |
| 347 | + $path = strtok( substr( $src, strlen( $plugin_url ) ), '?' ); | |
| 404 | 348 | |
| 405 | - /** | |
| 406 | - * Get Desktop Breakpoint. | |
| 407 | - * | |
| 408 | - * @return int | |
| 409 | - */ | |
| 410 | - public static function get_breakpoint_lg() { | |
| 411 | - return apply_filters( 'gkt_breakpoint_lg', self::get_default_breakpoint_lg() ); | |
| 412 | - } | |
| 413 | - } | |
| 414 | - new GhostKit_Breakpoints(); | |
| 349 | + if ( ! in_array( $path, $stored['files'], true ) ) { | |
| 350 | + return $src; | |
| 351 | + } | |
| 352 | + | |
| 353 | + if ( ! file_exists( $this->get_output_dir() . '/' . $path ) ) { | |
| 354 | + return $src; | |
| 355 | + } | |
| 356 | + | |
| 357 | + $upload_dir = wp_get_upload_dir(); | |
| 358 | + | |
| 359 | + return add_query_arg( 'ver', $stored['hash'], $upload_dir['baseurl'] . '/' . $this->plugin_name . '/' . $path ); | |
| 360 | + } | |
| 361 | + | |
| 362 | + /** | |
| 363 | + * Get Breakpoints. | |
| 364 | + */ | |
| 365 | + public static function get_breakpoints() { | |
| 366 | + $xs = self::get_breakpoint_xs(); | |
| 367 | + $xs = ( ! empty( $xs ) && $xs ) ? $xs : self::$default_xs; | |
| 368 | + | |
| 369 | + $sm = self::get_breakpoint_sm(); | |
| 370 | + $sm = ( ! empty( $sm ) && $sm ) ? $sm : self::$default_sm; | |
| 371 | + | |
| 372 | + $md = self::get_breakpoint_md(); | |
| 373 | + $md = ( ! empty( $md ) && $md ) ? $md : self::$default_md; | |
| 374 | + | |
| 375 | + $lg = self::get_breakpoint_lg(); | |
| 376 | + $lg = ( ! empty( $lg ) && $lg ) ? $lg : self::$default_lg; | |
| 377 | + | |
| 378 | + return array( | |
| 379 | + 'xs' => $xs, | |
| 380 | + 'sm' => $sm, | |
| 381 | + 'md' => $md, | |
| 382 | + 'lg' => $lg, | |
| 383 | + ); | |
| 384 | + } | |
| 385 | + | |
| 386 | + /** | |
| 387 | + * Get default breakpoints. | |
| 388 | + * | |
| 389 | + * @return array | |
| 390 | + */ | |
| 391 | + public static function get_default_breakpoints() { | |
| 392 | + return array( | |
| 393 | + 'xs' => self::get_default_breakpoint_xs(), | |
| 394 | + 'sm' => self::get_default_breakpoint_sm(), | |
| 395 | + 'md' => self::get_default_breakpoint_md(), | |
| 396 | + 'lg' => self::get_default_breakpoint_lg(), | |
| 397 | + ); | |
| 398 | + } | |
| 399 | + | |
| 400 | + /** | |
| 401 | + * Get breakpoints Hash | |
| 402 | + * | |
| 403 | + * @param array $breakpoints - Breakpoints. | |
| 404 | + * @return string | |
| 405 | + */ | |
| 406 | + protected function get_breakpoints_hash( $breakpoints ) { | |
| 407 | + return md5( | |
| 408 | + wp_json_encode( | |
| 409 | + array_merge( | |
| 410 | + $breakpoints, | |
| 411 | + array( | |
| 412 | + $this->plugin_version, | |
| 413 | + ) | |
| 414 | + ) | |
| 415 | + ) | |
| 416 | + ); | |
| 417 | + } | |
| 418 | + | |
| 419 | + /** | |
| 420 | + * Get default breakpoints Hash. | |
| 421 | + * | |
| 422 | + * @return string | |
| 423 | + */ | |
| 424 | + protected function get_default_breakpoints_hash() { | |
| 425 | + return $this->get_breakpoints_hash( | |
| 426 | + array( | |
| 427 | + 'xs' => self::$default_xs, | |
| 428 | + 'sm' => self::$default_sm, | |
| 429 | + 'md' => self::$default_md, | |
| 430 | + 'lg' => self::$default_lg, | |
| 431 | + ) | |
| 432 | + ); | |
| 433 | + } | |
| 434 | + | |
| 435 | + /** | |
| 436 | + * Get Default Extra Small Breakpoint. | |
| 437 | + * | |
| 438 | + * @return int | |
| 439 | + */ | |
| 440 | + public static function get_default_breakpoint_xs() { | |
| 441 | + return apply_filters( 'gkt_default_breakpoint_xs', self::$default_xs ); | |
| 442 | + } | |
| 443 | + | |
| 444 | + /** | |
| 445 | + * Get Extra Small Breakpoint. | |
| 446 | + * | |
| 447 | + * @return int | |
| 448 | + */ | |
| 449 | + public static function get_breakpoint_xs() { | |
| 450 | + return apply_filters( 'gkt_breakpoint_xs', self::get_default_breakpoint_xs() ); | |
| 451 | + } | |
| 452 | + | |
| 453 | + /** | |
| 454 | + * Get Default Mobile Breakpoint. | |
| 455 | + * | |
| 456 | + * @return int | |
| 457 | + */ | |
| 458 | + public static function get_default_breakpoint_sm() { | |
| 459 | + return apply_filters( 'gkt_default_breakpoint_sm', self::$default_sm ); | |
| 460 | + } | |
| 461 | + | |
| 462 | + /** | |
| 463 | + * Get Mobile Breakpoint. | |
| 464 | + * | |
| 465 | + * @return int | |
| 466 | + */ | |
| 467 | + public static function get_breakpoint_sm() { | |
| 468 | + return apply_filters( 'gkt_breakpoint_sm', self::get_default_breakpoint_sm() ); | |
| 469 | + } | |
| 470 | + | |
| 471 | + /** | |
| 472 | + * Get Default Tablet Breakpoint. | |
| 473 | + * | |
| 474 | + * @return int | |
| 475 | + */ | |
| 476 | + public static function get_default_breakpoint_md() { | |
| 477 | + return apply_filters( 'gkt_default_breakpoint_md', self::$default_md ); | |
| 478 | + } | |
| 479 | + | |
| 480 | + /** | |
| 481 | + * Get Tablet Breakpoint. | |
| 482 | + * | |
| 483 | + * @return int | |
| 484 | + */ | |
| 485 | + public static function get_breakpoint_md() { | |
| 486 | + return apply_filters( 'gkt_breakpoint_md', self::get_default_breakpoint_md() ); | |
| 487 | + } | |
| 488 | + | |
| 489 | + /** | |
| 490 | + * Get Default Desktop Breakpoint. | |
| 491 | + * | |
| 492 | + * @return int | |
| 493 | + */ | |
| 494 | + public static function get_default_breakpoint_lg() { | |
| 495 | + return apply_filters( 'gkt_default_breakpoint_lg', self::$default_lg ); | |
| 496 | + } | |
| 497 | + | |
| 498 | + /** | |
| 499 | + * Get Desktop Breakpoint. | |
| 500 | + * | |
| 501 | + * @return int | |
| 502 | + */ | |
| 503 | + public static function get_breakpoint_lg() { | |
| 504 | + return apply_filters( 'gkt_breakpoint_lg', self::get_default_breakpoint_lg() ); | |
| 505 | + } | |
| 506 | + } | |
| 507 | + new GhostKit_Breakpoints(); | |
| 415 | 508 | } |