Editor_Styles.php
234 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Gutenberg integration for Kirki. |
| 4 | * |
| 5 | * This class contains methods for integrating Kirki with |
| 6 | * the new WordPress core editor, Gutenberg. It provides |
| 7 | * fonts and styles to be output by the theme. |
| 8 | * |
| 9 | * @package Kirki |
| 10 | * @category Core |
| 11 | * @author Tim Elsass |
| 12 | * @copyright Copyright (c) 2023, Themeum |
| 13 | * @license https://opensource.org/licenses/MIT |
| 14 | * @since 3.0.35 |
| 15 | */ |
| 16 | |
| 17 | namespace Kirki\Module; |
| 18 | |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit; |
| 21 | } |
| 22 | |
| 23 | use Kirki\Compatibility\Kirki; |
| 24 | |
| 25 | /** |
| 26 | * Wrapper class for static methods. |
| 27 | * |
| 28 | * @since 3.0.35 |
| 29 | */ |
| 30 | class Editor_Styles { |
| 31 | |
| 32 | /** |
| 33 | * The class instance. |
| 34 | * |
| 35 | * @static |
| 36 | * @access private |
| 37 | * @since 3.0.35 |
| 38 | * @var object |
| 39 | */ |
| 40 | private static $instance; |
| 41 | |
| 42 | /** |
| 43 | * Configuration reference. |
| 44 | * |
| 45 | * @access public |
| 46 | * @since 3.0.35 |
| 47 | * @var object $configs |
| 48 | */ |
| 49 | private $configs; |
| 50 | |
| 51 | /** |
| 52 | * Whether feature is enabled. |
| 53 | * |
| 54 | * @access public |
| 55 | * @since 3.0.35 |
| 56 | * @var bool $enabled |
| 57 | */ |
| 58 | public $enabled; |
| 59 | |
| 60 | /** |
| 61 | * CSS Module reference. |
| 62 | * |
| 63 | * @access public |
| 64 | * @since 3.0.35 |
| 65 | * @var object $modules_css |
| 66 | */ |
| 67 | public $modules_css; |
| 68 | |
| 69 | /** |
| 70 | * Webfonts Module reference. |
| 71 | * |
| 72 | * @access public |
| 73 | * @since 3.0.35 |
| 74 | * @var object $modules_webfonts |
| 75 | */ |
| 76 | private $modules_webfonts; |
| 77 | |
| 78 | /** |
| 79 | * Google Fonts reference. |
| 80 | * |
| 81 | * @access public |
| 82 | * @since 3.0.35 |
| 83 | * @var object $google_fonts |
| 84 | */ |
| 85 | private $google_fonts; |
| 86 | |
| 87 | /** |
| 88 | * Get the one, true instance of this class. |
| 89 | * |
| 90 | * @static |
| 91 | * @access public |
| 92 | * @since 3.0.35 |
| 93 | * @return object |
| 94 | */ |
| 95 | public static function get_instance() { |
| 96 | if ( null === self::$instance ) { |
| 97 | self::$instance = new self(); |
| 98 | } |
| 99 | return self::$instance; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Constructor. |
| 104 | * |
| 105 | * @access public |
| 106 | * @since 3.0.0 |
| 107 | */ |
| 108 | public function __construct() { |
| 109 | add_action( 'admin_init', [ $this, 'init' ] ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Initialize Module. |
| 114 | * |
| 115 | * Sets class properties and add necessary hooks. |
| 116 | * |
| 117 | * @since 3.0.35 |
| 118 | */ |
| 119 | public function init() { |
| 120 | $this->set_configs(); |
| 121 | $this->set_enabled(); |
| 122 | $this->set_modules_css(); |
| 123 | $this->set_google_fonts(); |
| 124 | $this->set_modules_webfonts(); |
| 125 | $this->add_hooks(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Add hooks for Gutenberg editor integration. |
| 130 | * |
| 131 | * @access protected |
| 132 | * @since 3.0.35 |
| 133 | */ |
| 134 | protected function add_hooks() { |
| 135 | if ( ! $this->is_disabled() ) { |
| 136 | add_action( 'enqueue_block_editor_assets', [ $this->modules_css, 'enqueue_styles' ], 999 ); |
| 137 | add_action( 'after_setup_theme', [ $this, 'add_theme_support' ], 999 ); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Add theme support for editor styles. |
| 143 | * |
| 144 | * This checks if theme has declared editor-styles support |
| 145 | * already, and if not present, declares it. Hooked late. |
| 146 | * |
| 147 | * @access public |
| 148 | * @since 3.0.35 |
| 149 | */ |
| 150 | public function add_theme_support() { |
| 151 | if ( true !== get_theme_support( 'editor-styles' ) ) { |
| 152 | add_theme_support( 'editor-styles' ); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Helper method to check if feature is disabled. |
| 158 | * |
| 159 | * Feature can be disabled by KIRKI_NO_OUTPUT constant, |
| 160 | * gutenbeg_support argument, and disabled output argument. |
| 161 | * |
| 162 | * @access public |
| 163 | * @param array $args An array of arguments. |
| 164 | * @since 3.0.35 |
| 165 | * |
| 166 | * @return bool $disabled Is gutenberg integration feature disabled? |
| 167 | */ |
| 168 | private function is_disabled( $args = [] ) { |
| 169 | if ( defined( 'KIRKI_NO_OUTPUT' ) && true === KIRKI_NO_OUTPUT ) { |
| 170 | return true; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * We would prefer to use "KIRKI_GUTENBERG_OUTPUT" instead. |
| 175 | * For consistency however, we will use "KIRKI_NO_GUTENBERG_OUTPUT". |
| 176 | */ |
| 177 | if ( defined( 'KIRKI_NO_GUTENBERG_OUTPUT' ) && true === KIRKI_NO_GUTENBERG_OUTPUT ) { |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Set class property for $configs. |
| 186 | * |
| 187 | * @access private |
| 188 | * @since 3.0.35 |
| 189 | */ |
| 190 | private function set_configs() { |
| 191 | $this->configs = Kirki::$config; |
| 192 | return $this->configs; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Set class property for $enabled. |
| 197 | * |
| 198 | * @access private |
| 199 | * @since 3.0.35 |
| 200 | */ |
| 201 | private function set_enabled() { |
| 202 | $this->enabled = ! $this->is_disabled(); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Set class property for $modules_css. |
| 207 | * |
| 208 | * @access private |
| 209 | * @since 3.0.35 |
| 210 | */ |
| 211 | private function set_modules_css() { |
| 212 | $this->modules_css = new \Kirki\Module\CSS(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Set class property for $google_fonts. |
| 217 | * |
| 218 | * @access private |
| 219 | * @since 3.0.35 |
| 220 | */ |
| 221 | private function set_google_fonts() { |
| 222 | $this->google_fonts = \Kirki\Module\Webfonts\Google::get_instance(); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Set class property for $modules_webfonts. |
| 227 | * |
| 228 | * @access private |
| 229 | * @since 3.0.35 |
| 230 | */ |
| 231 | private function set_modules_webfonts() { |
| 232 | $this->modules_webfonts = new \Kirki\Module\Webfonts(); |
| 233 | } |
| 234 | } |