Section.php
232 lines
| 1 | <?php |
| 2 | // Don't load directly |
| 3 | defined( 'WPINC' ) or die; |
| 4 | |
| 5 | /** |
| 6 | * The Events Calendar Customizer Section Abstract |
| 7 | * Extend this when you are trying to create a new The Events Calendar Section |
| 8 | * on the Customize from WordPress |
| 9 | * |
| 10 | * @package Common |
| 11 | * @subpackage Customizer |
| 12 | * @since 4.0 |
| 13 | */ |
| 14 | abstract class Tribe__Customizer__Section { |
| 15 | |
| 16 | /** |
| 17 | * ID of the section |
| 18 | * |
| 19 | * @since 4.0 |
| 20 | * |
| 21 | * @access public |
| 22 | * @var string |
| 23 | */ |
| 24 | public $ID; |
| 25 | |
| 26 | /** |
| 27 | * Load this section by default |
| 28 | * |
| 29 | * @since 4.4 |
| 30 | * |
| 31 | * @access public |
| 32 | * @var string |
| 33 | */ |
| 34 | public $load = true; |
| 35 | |
| 36 | /** |
| 37 | * Default values for the settings on this class |
| 38 | * |
| 39 | * @since 4.0 |
| 40 | * |
| 41 | * @access private |
| 42 | * @var array |
| 43 | */ |
| 44 | public $defaults = []; |
| 45 | |
| 46 | /** |
| 47 | * Information to setup the Section |
| 48 | * |
| 49 | * @since 4.0 |
| 50 | * |
| 51 | * @access public |
| 52 | * @var array |
| 53 | */ |
| 54 | public $arguments = [ |
| 55 | 'priority' => 10, |
| 56 | 'capability' => 'edit_theme_options', |
| 57 | 'title' => null, |
| 58 | 'description' => null, |
| 59 | ]; |
| 60 | |
| 61 | /** |
| 62 | * Overwrite this method to create the Fields/Settings for this section |
| 63 | * |
| 64 | * @param WP_Customize_Section $section The WordPress section instance |
| 65 | * @param WP_Customize_Manager $manager The WordPress Customizer Manager |
| 66 | * |
| 67 | * @return void |
| 68 | */ |
| 69 | public function register_settings( WP_Customize_Section $section, WP_Customize_Manager $manager ) { |
| 70 | |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Overwrite this method to be able to implement the CSS template related to this section |
| 75 | * |
| 76 | * @return string |
| 77 | */ |
| 78 | public function get_css_template( $template ) { |
| 79 | return $template; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Overwrite this method to be able to creaty dynamic settings |
| 84 | * |
| 85 | * @param array $settings The actual options on the database |
| 86 | * @return array |
| 87 | */ |
| 88 | public function create_ghost_settings( $settings = [] ) { |
| 89 | return $settings; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * This method will be executed when the Class in Initialized |
| 94 | * Overwrite this method to be able to setup the arguments of your section |
| 95 | * |
| 96 | * @return void |
| 97 | */ |
| 98 | abstract public function setup(); |
| 99 | |
| 100 | /** |
| 101 | * Private variable holding the class Instance |
| 102 | * |
| 103 | * @since 4.0 |
| 104 | * |
| 105 | * @access private |
| 106 | * @var Tribe__Events__Pro__Customizer__Section |
| 107 | */ |
| 108 | private static $instances; |
| 109 | |
| 110 | /** |
| 111 | * Get the section slug based on the Class name |
| 112 | * |
| 113 | * @param string $class_name The name of this Class |
| 114 | * @return the slug for this class |
| 115 | */ |
| 116 | final public static function get_section_slug( $class_name ) { |
| 117 | $abstract_name = __CLASS__; |
| 118 | $reflection = new ReflectionClass( $class_name ); |
| 119 | |
| 120 | // Get the Slug without the Base name |
| 121 | $slug = str_replace( $abstract_name . '_', '', $reflection->getName() ); |
| 122 | |
| 123 | if ( false !== strpos( $slug, '__Customizer__' ) ) { |
| 124 | $slug = explode( '__Customizer__', $slug ); |
| 125 | $slug = end( $slug ); |
| 126 | } |
| 127 | |
| 128 | return strtolower( $slug ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Setup and Load hooks for this Section |
| 133 | * |
| 134 | * @since 4.0 |
| 135 | * |
| 136 | * @return Tribe__Customizer__Section |
| 137 | */ |
| 138 | final public function __construct() { |
| 139 | $slug = self::get_section_slug( get_class( $this ) ); |
| 140 | |
| 141 | // If for weird reason we don't have the Section name |
| 142 | if ( ! is_string( $this->ID ) ){ |
| 143 | $this->ID = $slug; |
| 144 | } |
| 145 | |
| 146 | // Allow child classes to setup the section |
| 147 | $this->setup(); |
| 148 | |
| 149 | // Hook the Register methods |
| 150 | add_action( "tribe_customizer_register_{$this->ID}_settings", [ $this, 'register_settings' ], 10, 2 ); |
| 151 | add_filter( 'tribe_customizer_pre_sections', [ $this, 'register' ], 10, 2 ); |
| 152 | |
| 153 | // Append this section CSS template |
| 154 | add_filter( 'tribe_customizer_css_template', [ $this, 'get_css_template' ], 15 ); |
| 155 | add_filter( "tribe_customizer_section_{$this->ID}_defaults", [ $this, 'get_defaults' ], 10 ); |
| 156 | |
| 157 | // Create the Ghost Options |
| 158 | add_filter( 'tribe_customizer_pre_get_option', [ $this, 'filter_settings' ], 10, 2 ); |
| 159 | |
| 160 | // By Default Invoking a new Section will load, unless `load` is set to false |
| 161 | if ( true === (bool) $this->load ) { |
| 162 | Tribe__Customizer::instance()->load_section( $this ); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * A way to apply filters when getting the Customizer options |
| 168 | * @return array |
| 169 | */ |
| 170 | public function get_defaults( $settings = [] ) { |
| 171 | // Create Ghost Options |
| 172 | return $this->create_ghost_settings( wp_parse_args( $settings, $this->defaults ) ); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Get the Default Value requested |
| 177 | * @return mixed |
| 178 | */ |
| 179 | public function get_default( $key ) { |
| 180 | $defaults = $this->get_defaults(); |
| 181 | |
| 182 | if ( ! isset( $defaults[ $key ] ) ) { |
| 183 | return null; |
| 184 | } |
| 185 | |
| 186 | return $defaults[ $key ]; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Hooks to the `tribe_customizer_pre_get_option`, this applies |
| 191 | * the `$this->create_ghost_settings()` method to the settings on the correct section |
| 192 | * |
| 193 | * @param array $settings Values from the Database from Customizer actions |
| 194 | * @param array $search Indexed search @see Tribe__Customizer::search_var() |
| 195 | * |
| 196 | * @return array |
| 197 | */ |
| 198 | public function filter_settings( $settings, $search ) { |
| 199 | // Exit early. |
| 200 | if ( null === $search ) { |
| 201 | return $settings; |
| 202 | } |
| 203 | |
| 204 | // Only Apply if getting the full options or Section |
| 205 | if ( is_array( $search ) && count( $search ) > 1 ) { |
| 206 | return $settings; |
| 207 | } |
| 208 | |
| 209 | if ( is_array( $search ) && count( $search ) === 1 ) { |
| 210 | $settings = $this->create_ghost_settings( $settings ); |
| 211 | } else { |
| 212 | $settings[ $this->ID ] = $this->create_ghost_settings( $settings[ $this->ID ] ); |
| 213 | } |
| 214 | |
| 215 | return $settings; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Register this Section |
| 220 | * |
| 221 | * @param array $sections Array of Sections |
| 222 | * @param Tribe__Customizer $customizer Our internal Cutomizer Class Instance |
| 223 | * |
| 224 | * @return array Return the modified version of the Section array |
| 225 | */ |
| 226 | public function register( $sections, Tribe__Customizer $customizer ) { |
| 227 | $sections[ $this->ID ] = $this->arguments; |
| 228 | |
| 229 | return $sections; |
| 230 | } |
| 231 | } |
| 232 |