global-css.php (4024B)
1 <?php 2 namespace Elementor\Core\Files\CSS; 3 4 use Elementor\Core\Kits\Manager; 5 use Elementor\Plugin; 6 use Elementor\Settings; 7 8 if ( ! defined( 'ABSPATH' ) ) { 9 exit; // Exit if accessed directly. 10 } 11 12 /** 13 * Elementor global CSS file. 14 * 15 * Elementor CSS file handler class is responsible for generating the global CSS 16 * file. 17 * 18 * @since 1.2.0 19 */ 20 class Global_CSS extends Base { 21 22 /** 23 * Elementor global CSS file handler ID. 24 */ 25 const FILE_HANDLER_ID = 'elementor-global'; 26 27 const META_KEY = '_elementor_global_css'; 28 29 /** 30 * Get CSS file name. 31 * 32 * Retrieve the CSS file name. 33 * 34 * @since 1.6.0 35 * @access public 36 * 37 * @return string CSS file name. 38 */ 39 public function get_name() { 40 return 'global'; 41 } 42 43 /** 44 * Get file handle ID. 45 * 46 * Retrieve the handle ID for the global post CSS file. 47 * 48 * @since 1.2.0 49 * @access protected 50 * 51 * @return string CSS file handle ID. 52 */ 53 protected function get_file_handle_id() { 54 return self::FILE_HANDLER_ID; 55 } 56 57 /** 58 * Render CSS. 59 * 60 * Parse the CSS for all the widgets and all the scheme controls. 61 * 62 * @since 1.2.0 63 * @access protected 64 */ 65 protected function render_css() { 66 $this->render_schemes_and_globals_css(); 67 } 68 69 /** 70 * Get inline dependency. 71 * 72 * Retrieve the name of the stylesheet used by `wp_add_inline_style()`. 73 * 74 * @since 1.2.0 75 * @access protected 76 * 77 * @return string Name of the stylesheet. 78 */ 79 protected function get_inline_dependency() { 80 return 'elementor-frontend'; 81 } 82 83 /** 84 * Is update required. 85 * 86 * Whether the CSS requires an update. When there are new schemes or settings 87 * updates. 88 * 89 * @since 1.2.0 90 * @access protected 91 * 92 * @return bool True if the CSS requires an update, False otherwise. 93 */ 94 protected function is_update_required() { 95 return $this->get_meta( 'time' ) < get_option( Settings::UPDATE_TIME_FIELD ); 96 } 97 98 /** 99 * Render schemes CSS. 100 * 101 * Parse the CSS for all the widgets and all the scheme controls. 102 * 103 * @since 1.2.0 104 * @access private 105 */ 106 private function render_schemes_and_globals_css() { 107 $elementor = Plugin::$instance; 108 109 /** @var Manager $module */ 110 $kits_manager = Plugin::$instance->kits_manager; 111 $custom_colors_enabled = $kits_manager->is_custom_colors_enabled(); 112 $custom_typography_enabled = $kits_manager->is_custom_typography_enabled(); 113 114 // If both default colors and typography are disabled, there is no need to render schemes and default global css. 115 if ( ! $custom_colors_enabled && ! $custom_typography_enabled ) { 116 return; 117 } 118 119 foreach ( $elementor->widgets_manager->get_widget_types() as $widget ) { 120 $controls = $widget->get_controls(); 121 122 $global_controls = []; 123 124 $global_values['__globals__'] = []; 125 126 foreach ( $controls as $control ) { 127 $is_color_control = 'color' === $control['type']; 128 $is_typography_control = isset( $control['groupType'] ) && 'typography' === $control['groupType']; 129 130 // If it is a color/typography control and default colors/typography are disabled, 131 // don't add the default CSS. 132 if ( ( $is_color_control && ! $custom_colors_enabled ) || ( $is_typography_control && ! $custom_typography_enabled ) ) { 133 continue; 134 } 135 136 $global_control = $control; 137 138 // Handle group controls that don't have a default global property. 139 if ( ! empty( $control['groupType'] ) ) { 140 $global_control = $controls[ $control['groupPrefix'] . $control['groupType'] ]; 141 } 142 143 // If the control has a default global defined, add it to the globals array 144 // that is used in add_control_rules. 145 if ( ! empty( $control['global']['default'] ) ) { 146 $global_values['__globals__'][ $control['name'] ] = $global_control['global']['default']; 147 } 148 149 if ( ! empty( $global_control['global']['default'] ) ) { 150 $global_controls[] = $control; 151 } 152 } 153 154 foreach ( $global_controls as $control ) { 155 $this->add_control_rules( $control, $controls, function( $control ) {}, [ '{{WRAPPER}}' ], [ '.elementor-widget-' . $widget->get_name() ], $global_values ); 156 } 157 } 158 } 159 }