deprecated
1 month ago
Aliases.php
2 months ago
Config.php
5 months ago
Control.php
5 months ago
Deprecated.php
5 months ago
Field.php
2 months ago
Framework.php
5 months ago
Init.php
2 months ago
Kirki.php
2 months ago
Modules.php
2 months ago
Pro_Namespace_Compatibility.php
5 months ago
Sanitize_Values.php
5 months ago
Scripts.php
2 months ago
Settings.php
5 months ago
Values.php
5 months ago
Init.php
314 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Initializes Kirki |
| 4 | * |
| 5 | * @package Kirki |
| 6 | * @category Core |
| 7 | * @author Themeum |
| 8 | * @copyright Copyright (c) 2023, Themeum |
| 9 | * @license https://opensource.org/licenses/MIT |
| 10 | * @since 1.0 |
| 11 | */ |
| 12 | |
| 13 | namespace Kirki\Compatibility; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Initialize Kirki |
| 21 | */ |
| 22 | class Init { |
| 23 | |
| 24 | /** |
| 25 | * Control types. |
| 26 | * |
| 27 | * @access private |
| 28 | * @since 3.0.0 |
| 29 | * @var array |
| 30 | */ |
| 31 | private $control_types = []; |
| 32 | |
| 33 | /** |
| 34 | * Should we show a nag for the deprecated fontawesome field? |
| 35 | * |
| 36 | * @static |
| 37 | * @access private |
| 38 | * @since 3.0.42 |
| 39 | * @var bool |
| 40 | */ |
| 41 | private static $show_fa_nag = false; |
| 42 | |
| 43 | /** |
| 44 | * The class constructor. |
| 45 | */ |
| 46 | public function __construct() { |
| 47 | add_action( 'wp_loaded', [ $this, 'add_to_customizer' ], 1 ); |
| 48 | add_filter( 'kirki_control_types', [ $this, 'default_control_types' ] ); |
| 49 | |
| 50 | add_action( 'customize_register', [ $this, 'remove_controls' ], 99999 ); |
| 51 | |
| 52 | add_action( 'admin_notices', [ $this, 'admin_notices' ] ); |
| 53 | add_action( 'admin_init', [ $this, 'dismiss_nag' ] ); |
| 54 | |
| 55 | // ? Bagus: is this necessary? The Values class doesn't have constructor, so this does nothing. |
| 56 | new Values(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Add the default Kirki control types. |
| 61 | * |
| 62 | * @access public |
| 63 | * @since 3.0.0 |
| 64 | * @param array $control_types The control types array. |
| 65 | * @return array |
| 66 | */ |
| 67 | public function default_control_types( $control_types = [] ) { |
| 68 | $this->control_types = [ |
| 69 | 'kirki-composite' => '\Kirki\Control\Composite', |
| 70 | 'checkbox' => '\Kirki\Control\Checkbox', |
| 71 | 'kirki-color' => '\Kirki\Control\ReactColorful', |
| 72 | 'kirki-color-palette' => '\Kirki\Control\Color_Palette', |
| 73 | 'kirki-custom' => '\Kirki\Control\Custom', |
| 74 | 'kirki-date' => '\Kirki\Control\Date', |
| 75 | 'kirki-dashicons' => '\Kirki\Control\Dashicons', |
| 76 | 'kirki-dimension' => '\Kirki\Control\Dimension', |
| 77 | 'kirki-dimensions' => '\Kirki\Control\Dimensions', |
| 78 | 'kirki-editor' => '\Kirki\Control\Editor', |
| 79 | 'kirki-image' => '\Kirki\Control\Image', |
| 80 | 'kirki-multicolor' => '\Kirki\Control\Multicolor', |
| 81 | 'kirki-multicheck' => '\Kirki\Control\Multicheck', |
| 82 | 'kirki-number' => '\Kirki\Control\Number', |
| 83 | 'kirki-radio' => '\Kirki\Control\Radio', |
| 84 | 'kirki-radio-buttonset' => '\Kirki\Control\Radio_Buttonset', |
| 85 | 'kirki-radio-image' => '\Kirki\Control\Radio_Image', |
| 86 | 'repeater' => '\Kirki\Control\Repeater', |
| 87 | 'kirki-select' => '\Kirki\Control\Select', |
| 88 | 'kirki-slider' => '\Kirki\Control\Slider', |
| 89 | 'kirki-sortable' => '\Kirki\Control\Sortable', |
| 90 | 'kirki-spacing' => '\Kirki\Control\Dimensions', |
| 91 | 'kirki-switch' => '\Kirki\Control\Checkbox_Switch', |
| 92 | 'kirki-generic' => '\Kirki\Control\Generic', |
| 93 | 'kirki-toggle' => '\Kirki\Control\Checkbox_Toggle', |
| 94 | 'image' => '\Kirki\Control\Image', |
| 95 | 'cropped_image' => '\Kirki\Control\Cropped_Image', |
| 96 | 'upload' => '\Kirki\Control\Upload', |
| 97 | ]; |
| 98 | return array_merge( $this->control_types, $control_types ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Helper function that adds the fields to the customizer. |
| 103 | */ |
| 104 | public function add_to_customizer() { |
| 105 | $this->fields_from_filters(); |
| 106 | add_action( 'customize_register', [ $this, 'register_control_types' ] ); |
| 107 | add_action( 'customize_register', [ $this, 'add_fields' ], 99 ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Register control types |
| 112 | */ |
| 113 | public function register_control_types() { |
| 114 | global $wp_customize; |
| 115 | |
| 116 | $this->control_types = $this->default_control_types(); |
| 117 | if ( ! class_exists( 'WP_Customize_Code_Editor_Control' ) ) { |
| 118 | unset( $this->control_types['code_editor'] ); |
| 119 | } |
| 120 | foreach ( $this->control_types as $key => $classname ) { |
| 121 | if ( ! class_exists( $classname ) ) { |
| 122 | unset( $this->control_types[ $key ] ); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | $skip_control_types = apply_filters( |
| 127 | 'kirki_control_types_exclude', |
| 128 | [ |
| 129 | '\Kirki\Control\Repeater', |
| 130 | '\WP_Customize_Control', |
| 131 | ] |
| 132 | ); |
| 133 | |
| 134 | foreach ( $this->control_types as $control_type ) { |
| 135 | if ( ! in_array( $control_type, $skip_control_types, true ) && class_exists( $control_type ) ) { |
| 136 | $wp_customize->register_control_type( $control_type ); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Create the settings and controls from the $fields array and register them. |
| 143 | * |
| 144 | * @var object The WordPress Customizer object. |
| 145 | */ |
| 146 | public function add_fields() { |
| 147 | global $wp_customize; |
| 148 | |
| 149 | foreach ( Kirki::$fields as $args ) { |
| 150 | |
| 151 | // Create the settings. |
| 152 | new \Kirki\Compatibility\Settings( $args ); |
| 153 | |
| 154 | // Check if we're on the customizer. |
| 155 | // If we are, then we will create the controls, add the scripts needed for the customizer |
| 156 | // and any other tweaks that this field may require. |
| 157 | if ( $wp_customize ) { |
| 158 | |
| 159 | // Create the control. |
| 160 | new Control( $args ); |
| 161 | |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Process fields added using the 'kirki_fields' and 'kirki_controls' filter. |
| 168 | * These filters are no longer used, this is simply for backwards-compatibility. |
| 169 | * |
| 170 | * @access private |
| 171 | * @since 2.0.0 |
| 172 | */ |
| 173 | private function fields_from_filters() { |
| 174 | $fields = apply_filters( 'kirki_controls', [] ); |
| 175 | $fields = apply_filters( 'kirki_fields', $fields ); |
| 176 | |
| 177 | if ( ! empty( $fields ) ) { |
| 178 | foreach ( $fields as $field ) { |
| 179 | $field['kirki_config'] = 'global'; |
| 180 | Kirki::add_field( 'global', $field ); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Alias for the is_plugin static method in the Kirki\Util\Util class. |
| 187 | * This is here for backwards-compatibility purposes. |
| 188 | * |
| 189 | * @static |
| 190 | * @access public |
| 191 | * @since 3.0.0 |
| 192 | * @return bool |
| 193 | */ |
| 194 | public static function is_plugin() { |
| 195 | return Util::is_plugin(); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Alias for the get_variables static method in the Kirki\Util\Util class. |
| 200 | * This is here for backwards-compatibility purposes. |
| 201 | * |
| 202 | * @static |
| 203 | * @access public |
| 204 | * @since 2.0.0 |
| 205 | * @return array Formatted as array( 'variable-name' => value ). |
| 206 | */ |
| 207 | public static function get_variables() { |
| 208 | |
| 209 | // Log error for developers. |
| 210 | _doing_it_wrong( __METHOD__, esc_html__( 'We detected you\'re using Kirki\Compatibility\Init::get_variables(). Please use \Kirki\Util\Util::get_variables() instead.', 'kirki' ), '3.0.10' ); |
| 211 | |
| 212 | // ! This will be failed, because Util class is under Kirki\Util namespace. |
| 213 | return Util::get_variables(); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Remove controls. |
| 218 | * |
| 219 | * @since 3.0.17 |
| 220 | * @param object $wp_customize The customizer object. |
| 221 | * @return void |
| 222 | */ |
| 223 | public function remove_controls( $wp_customize ) { |
| 224 | foreach ( Kirki::$controls_to_remove as $control ) { |
| 225 | $wp_customize->remove_control( $control ); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Shows an admin notice. |
| 231 | * |
| 232 | * @access public |
| 233 | * @since 3.0.42 |
| 234 | * @return void |
| 235 | */ |
| 236 | public function admin_notices() { |
| 237 | |
| 238 | // No need for a nag if we don't need to recommend installing the FA plugin. |
| 239 | if ( ! self::$show_fa_nag ) { |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | // No need for a nag if FA plugin is already installed. |
| 244 | if ( defined( 'FONTAWESOME_DIR_PATH' ) ) { |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | // No need for a nag if current user can't install plugins. |
| 249 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | // No need for a nag if user has dismissed it. |
| 254 | $dismissed = get_user_meta( get_current_user_id(), 'kirki_fa_nag_dismissed', true ); |
| 255 | if ( true === $dismissed || 1 === $dismissed || '1' === $dismissed ) { |
| 256 | return; |
| 257 | } |
| 258 | ?> |
| 259 | <div class="notice notice-info is-dismissible"> |
| 260 | <p> |
| 261 | <?php esc_html_e( 'Your theme uses a Font Awesome field for icons. To avoid issues with missing icons on your frontend we recommend you install the official Font Awesome plugin.', 'kirki' ); ?> |
| 262 | </p> |
| 263 | <p> |
| 264 | <a class="button button-primary" href="<?php echo esc_url( admin_url( 'plugin-install.php?tab=plugin-information&plugin=font-awesome&TB_iframe=true&width=600&height=550' ) ); ?>"><?php esc_html_e( 'Install Plugin', 'kirki' ); ?></a> |
| 265 | <a class="button button-secondary" href="<?php echo esc_url( wp_nonce_url( admin_url( '?dismiss-nag=font-awesome-kirki' ), 'kirki-dismiss-nag', 'nonce' ) ); ?>"><?php esc_html_e( 'Don\'t show this again', 'kirki' ); ?></a> |
| 266 | </p> |
| 267 | </div> |
| 268 | <?php |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Dismisses the nag. |
| 273 | * |
| 274 | * @access public |
| 275 | * @since 3.0.42 |
| 276 | * @return void |
| 277 | */ |
| 278 | public function dismiss_nag() { |
| 279 | if ( isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], 'kirki-dismiss-nag' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 280 | if ( get_current_user_id() && isset( $_GET['dismiss-nag'] ) && 'font-awesome-kirki' === $_GET['dismiss-nag'] ) { |
| 281 | update_user_meta( get_current_user_id(), 'kirki_fa_nag_dismissed', true ); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Handles showing a nag if the theme is using the deprecated fontawesome field |
| 288 | * |
| 289 | * @static |
| 290 | * @access protected |
| 291 | * @since 3.0.42 |
| 292 | * @param array $args The field arguments. |
| 293 | * @return void |
| 294 | */ |
| 295 | protected static function maybe_show_fontawesome_nag( $args ) { |
| 296 | |
| 297 | // If we already know we want it, skip check. |
| 298 | if ( self::$show_fa_nag ) { |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | // Check if the field is fontawesome. |
| 303 | if ( isset( $args['type'] ) && in_array( $args['type'], [ 'fontawesome', 'kirki-fontawesome' ], true ) ) { |
| 304 | |
| 305 | // Skip check if theme has disabled FA enqueueing via a filter. |
| 306 | if ( ! apply_filters( 'kirki_load_fontawesome', true ) ) { |
| 307 | return; |
| 308 | } |
| 309 | |
| 310 | // If we got this far, we need to show the nag. |
| 311 | self::$show_fa_nag = true; |
| 312 | } |
| 313 | } |
| 314 | } |