| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Runners; |
| 4 |
|
| 5 |
use Templately\Core\Importer\Runners\BaseRunner; |
| 6 |
use Templately\Core\Importer\Utils\Utils; |
| 7 |
|
| 8 |
/** |
| 9 |
* Imports the pack's site-wide custom CSS (`custom.css` at the pack root) into |
| 10 |
* the `templately_custom_css` option, which Settings::add_custom_css() prints |
| 11 |
* inside a <style> tag on wp_head. |
| 12 |
* |
| 13 |
* The value is written through Utils::update_option(), so the previous value is |
| 14 |
* backed up to `__templately_templately_custom_css` and import_revert() restores |
| 15 |
* it — the same backup/revert path every other imported option uses. |
| 16 |
*/ |
| 17 |
class CustomCSS extends BaseRunner { |
| 18 |
|
| 19 |
const OPTION_KEY = 'templately_custom_css'; |
| 20 |
const FILE_NAME = 'custom.css'; |
| 21 |
|
| 22 |
public function get_name(): string { |
| 23 |
return 'custom-css'; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_label(): string { |
| 27 |
return __( 'Custom CSS', 'templately' ); |
| 28 |
} |
| 29 |
|
| 30 |
public function should_log(): bool { |
| 31 |
return true; |
| 32 |
} |
| 33 |
|
| 34 |
public function get_action(): string { |
| 35 |
return 'eventLog'; |
| 36 |
} |
| 37 |
|
| 38 |
public function log_message(): string { |
| 39 |
return __( 'Importing custom CSS.', 'templately' ); |
| 40 |
} |
| 41 |
|
| 42 |
public function should_run( $data, $imported_data = [] ): bool { |
| 43 |
$file = $this->dir_path . self::FILE_NAME; |
| 44 |
return is_readable( $file ) && filesize( $file ) > 0; |
| 45 |
} |
| 46 |
|
| 47 |
public function import( $data, $imported_data ): array { |
| 48 |
$this->log( 0 ); |
| 49 |
|
| 50 |
$file = $this->dir_path . self::FILE_NAME; |
| 51 |
$css = file_get_contents( $file ); |
| 52 |
|
| 53 |
if ( false === $css || '' === trim( $css ) ) { |
| 54 |
return []; |
| 55 |
} |
| 56 |
|
| 57 |
// Backup the current value then write the pack CSS. The backup lands in |
| 58 |
// `__templately_templately_custom_css`; import_revert() restores it. |
| 59 |
Utils::update_option( self::OPTION_KEY, $css ); |
| 60 |
|
| 61 |
return [ 'custom_css' => true ]; |
| 62 |
} |
| 63 |
} |
| 64 |
|