DependencyManagement
5 years ago
ProductAttributesLookup
5 years ago
WCCom
5 years ago
AssignDefaultCategory.php
5 years ago
DownloadPermissionsAdjuster.php
5 years ago
RestApiUtil.php
5 years ago
ThemeSupport.php
5 years ago
ThemeSupport.php
119 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ThemeSupport class file. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\ThemeManagement |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal; |
| 9 | |
| 10 | use Automattic\WooCommerce\Proxies\LegacyProxy; |
| 11 | use Automattic\WooCommerce\Utilities\ArrayUtil; |
| 12 | |
| 13 | /** |
| 14 | * Provides methods for theme support. |
| 15 | */ |
| 16 | class ThemeSupport { |
| 17 | |
| 18 | const DEFAULTS_KEY = '_defaults'; |
| 19 | |
| 20 | /** |
| 21 | * The instance of LegacyProxy to use. |
| 22 | * |
| 23 | * @var LegacyProxy |
| 24 | */ |
| 25 | private $legacy_proxy; |
| 26 | |
| 27 | /** |
| 28 | * ThemeSupport constructor. |
| 29 | * |
| 30 | * @internal |
| 31 | * @param LegacyProxy $legacy_proxy The instance of LegacyProxy to use. |
| 32 | */ |
| 33 | final public function init( LegacyProxy $legacy_proxy ) { |
| 34 | $this->legacy_proxy = $legacy_proxy; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Adds theme support options for the current theme. |
| 39 | * |
| 40 | * @param array $options The options to be added. |
| 41 | */ |
| 42 | public function add_options( $options ) { |
| 43 | $this->legacy_proxy->call_function( 'add_theme_support', 'woocommerce', $options ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Adds default theme support options for the current theme. |
| 48 | * |
| 49 | * @param array $options The options to be added. |
| 50 | */ |
| 51 | public function add_default_options( $options ) { |
| 52 | $default_options = $this->get_option( self::DEFAULTS_KEY, array() ); |
| 53 | $default_options = array_merge( $default_options, $options ); |
| 54 | $this->add_options( array( self::DEFAULTS_KEY => $default_options ) ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Gets "theme support" options from the current theme, if set. |
| 59 | * |
| 60 | * @param string $option_name Option name, possibly nested (key::subkey), to get specific value. Blank to get all the existing options as an array. |
| 61 | * @param mixed $default_value Value to return if the specified option doesn't exist. |
| 62 | * @return mixed The retrieved option or the default value. |
| 63 | */ |
| 64 | public function get_option( $option_name = '', $default_value = null ) { |
| 65 | $theme_support_options = $this->get_all_options(); |
| 66 | |
| 67 | if ( ! $theme_support_options ) { |
| 68 | return $default_value; |
| 69 | } |
| 70 | |
| 71 | if ( $option_name ) { |
| 72 | $value = ArrayUtil::get_nested_value( $theme_support_options, $option_name ); |
| 73 | if ( is_null( $value ) ) { |
| 74 | $value = ArrayUtil::get_nested_value( $theme_support_options, self::DEFAULTS_KEY . '::' . $option_name, $default_value ); |
| 75 | } |
| 76 | return $value; |
| 77 | } |
| 78 | |
| 79 | return $theme_support_options; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Checks whether a given theme support option has been defined. |
| 84 | * |
| 85 | * @param string $option_name The (possibly nested) name of the option to check. |
| 86 | * @param bool $include_defaults True to include the default values in the check, false otherwise. |
| 87 | * |
| 88 | * @return bool True if the specified theme support option has been defined, false otherwise. |
| 89 | */ |
| 90 | public function has_option( $option_name, $include_defaults = true ) { |
| 91 | $theme_support_options = $this->get_all_options(); |
| 92 | |
| 93 | if ( ! $theme_support_options ) { |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | $value = ArrayUtil::get_nested_value( $theme_support_options, $option_name ); |
| 98 | if ( ! is_null( $value ) ) { |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | if ( ! $include_defaults ) { |
| 103 | return false; |
| 104 | } |
| 105 | $value = ArrayUtil::get_nested_value( $theme_support_options, self::DEFAULTS_KEY . '::' . $option_name ); |
| 106 | return ! is_null( $value ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get all the defined theme support options for the 'woocommerce' feature. |
| 111 | * |
| 112 | * @return array An array with all the theme support options defined for the 'woocommerce' feature, or false if nothing has been defined for that feature. |
| 113 | */ |
| 114 | private function get_all_options() { |
| 115 | $theme_support = $this->legacy_proxy->call_function( 'get_theme_support', 'woocommerce' ); |
| 116 | return is_array( $theme_support ) ? $theme_support[0] : false; |
| 117 | } |
| 118 | } |
| 119 |