Layers_Customizer_Regsitrar

Customizer Registration File.

Defined (1)

The class is defined in the following location(s).

/core/customizer/registration.php  
  1. class Layers_Customizer_Regsitrar { 
  2.  
  3. public $customizer; 
  4.  
  5. public $config; 
  6.  
  7. public $prefix; 
  8.  
  9. private static $instance; // stores singleton class 
  10.  
  11. /** 
  12. * Get Instance creates a singleton class that's cached to stop duplicate instances 
  13. */ 
  14. public static function get_instance() { 
  15. if ( ! self::$instance ) { 
  16. self::$instance = new self(); 
  17. self::$instance->init(); 
  18. return self::$instance; 
  19.  
  20. /** 
  21. * Construct empty on purpose 
  22. */ 
  23.  
  24. private function __construct() {} 
  25.  
  26. /** 
  27. * Init behaves like, and replaces, construct 
  28. */ 
  29.  
  30. public function init() { 
  31.  
  32. // Register the customizer object 
  33. global $wp_customize; 
  34.  
  35. $this->customizer = $wp_customize; 
  36.  
  37. // Set Prefix 
  38. $this->prefix = LAYERS_THEME_SLUG . '-'; 
  39.  
  40. // Grab the customizer config 
  41. $this->config = Layers_Customizer_Config::get_instance(); 
  42.  
  43. //Register the panels and sections based on this instance's config 
  44.  
  45. // Start registration with the panels & sections 
  46. $this->register_panels( $this->config->panels ); 
  47. $this->register_sections ( $this->config->sections ); 
  48.  
  49. // Move default sections into Layers Panels 
  50. $this->move_default_panels( $this->config->default_panels ); 
  51. $this->move_default_sections( $this->config->default_sections ); 
  52. $this->move_default_controls( $this->config->default_controls ); 
  53.  
  54. // Change 'Widgets' panel title to 'Edit Layout' 
  55. if ( method_exists( $wp_customize, 'add_panel' ) ) { // `add_panel` only arrived in WP 4.0 
  56. $wp_customize->add_panel( 
  57. 'widgets', array( 
  58. 'priority' => 0,  
  59. 'title' => __( 'Edit Layout' , 'layerswp' ),  
  60. 'description' => Layers_Customizer::get_instance()->render_builder_page_dropdown() . __('Use this area to add widgets to your page, use the (Layers) widgets for the Body section.' , 'layerswp' ),  
  61. ); 
  62.  
  63. /** 
  64. * Check whether or not panels are supported by the customizer 
  65. * @return boolean true if panels are supported 
  66. */ 
  67.  
  68. function customizer_supports_panels() { 
  69. return ( class_exists( 'WP_Customize_Manager' ) && method_exists( 'WP_Customize_Manager', 'add_panel' ) ) || function_exists( 'wp_validate_boolean' ); 
  70.  
  71. /** 
  72. * Register Panels 
  73. * @panels array Array of panel config 
  74. */ 
  75.  
  76. function register_panels( $panels = array() ) { 
  77.  
  78. // If there are no panels, return 
  79. if( empty( $panels ) ) return; 
  80.  
  81. foreach( $panels as $panel_key => $panel_data ) { 
  82.  
  83. // If panels are supported, add this as a panel 
  84. if( $this->customizer_supports_panels() ) { 
  85.  
  86. // Add Panel. 
  87. if ( in_array( $panel_key, $this->customizer->panels() ) ) { 
  88. // Panel exists without 'layers-' prepended, so shouldn't be added. 
  89. continue; 
  90. else { 
  91. // Add Panel with 'layers-' prepended. 
  92. $this->customizer->add_panel( $this->prefix . $panel_key , $panel_data ); 
  93.  
  94. } // foreach panel 
  95.  
  96. /** 
  97. * Register Sections 
  98. * @panel_key string Unique key for which panel this section belongs to 
  99. * @sections array Array of sections config 
  100. */ 
  101. public function register_sections( $sections = array() ) { 
  102.  
  103. // If there are no sections, return 
  104. if( empty( $sections ) ) return; 
  105.  
  106. $section_priority = 150; 
  107.  
  108. foreach( $sections as $section_key => $section_data ) { 
  109.  
  110. if( $this->customizer_supports_panels() && isset( $section_data[ 'panel' ] ) ) { 
  111.  
  112. // Add Section. 
  113. if ( in_array( $section_data[ 'panel' ], $this->customizer->panels() ) ) { 
  114. // Panel exists without 'layers-' prepended, so add the section to that panel. 
  115. $section_data[ 'panel' ] = $section_data[ 'panel' ]; 
  116. else { 
  117. // Panel exists with 'layers-' prepended, so add the section to that panel. 
  118. $section_data[ 'panel' ] = $this->prefix . $section_data[ 'panel' ]; 
  119.  
  120. if( !isset( $section_data[ 'priority' ] ) ) { 
  121. $section_data[ 'priority' ] = $section_priority; 
  122.  
  123. $this->customizer->add_section( 
  124. $this->prefix . $section_key ,  
  125. $section_data 
  126. ); 
  127.  
  128. $section_priority++; 
  129.  
  130. // Register Sections for this Panel 
  131. $this->register_controls ( $section_key , $this->config->controls ); 
  132.  
  133.  
  134. /** 
  135. * Register Panels 
  136. * @panel_section_key string Unique key for which section this control belongs to 
  137. * @controls array Array of controls config 
  138. */ 
  139. public function register_controls( $panel_section_key = '' , $controls = array() ) { 
  140.  
  141. // If there are no sections, return 
  142. if( empty( $controls ) ) return; 
  143.  
  144. // Make sure that there is actually section config for this panel 
  145. if( !isset( $controls[ $panel_section_key ] ) ) return; 
  146.  
  147. $control_priority = 150; 
  148.  
  149. foreach( $controls[ $panel_section_key ] as $control_key => $control_data ) { 
  150.  
  151. $setting_key = $this->prefix . $control_key; 
  152.  
  153. // Add Control. 
  154. if ( $this->customizer->get_section( $panel_section_key ) ) { 
  155. // Section exists without 'layers-' prepended, so add control to it. 
  156. $control_data[ 'section' ] = $panel_section_key; 
  157. else { 
  158. // Section exists with 'layers-' prepended, so add control to it. 
  159. $control_data[ 'section' ] = $this->prefix . $panel_section_key; 
  160.  
  161. // Set control priority to obey order of setup 
  162. $control_data[ 'priority' ] = $control_priority; 
  163.  
  164. // Add the default into the control data so it can be accessed if needed. 
  165. $control_data[ 'default' ] = isset( $control_data['default'] ) ? $control_data['default'] : NULL ; 
  166.  
  167. // Add Setting 
  168. $this->customizer->add_setting( 
  169. $setting_key,  
  170. array( 
  171. 'default' => ( isset( $control_data['default'] ) ? $control_data['default'] : NULL ) ,  
  172. 'type' => 'theme_mod',  
  173. 'capability' => 'manage_options',  
  174. 'sanitize_callback' => $this->add_sanitize_callback( $control_data ) 
  175. ); 
  176.  
  177. if ( 'layers-select-images' == $control_data['type'] ) { 
  178.  
  179. // Add Control 
  180. $this->customizer->add_control( 
  181. new Layers_Customize_Select_Image_Control( 
  182. $this->customizer,  
  183. $setting_key,  
  184. $control_data 
  185. ); 
  186. } else if( 'layers-select-icons' == $control_data['type'] ) { 
  187.  
  188. // Add Control 
  189. $this->customizer->add_control( 
  190. new Layers_Customize_Select_Icon_Control( 
  191. $this->customizer,  
  192. $setting_key,  
  193. $control_data 
  194. ); 
  195. } else if( 'layers-seperator' == $control_data['type'] ) { 
  196.  
  197. // Add Control 
  198. $this->customizer->add_control( 
  199. new Layers_Customize_Seperator_Control( 
  200. $this->customizer,  
  201. $setting_key,  
  202. $control_data 
  203. ); 
  204. } else if( 'layers-heading' == $control_data['type'] ) { 
  205.  
  206. // Add Control 
  207. $this->customizer->add_control( 
  208. new Layers_Customize_Heading_Control( 
  209. $this->customizer,  
  210. $setting_key,  
  211. $control_data 
  212. ); 
  213. } else if( 'layers-color' == $control_data['type'] ) { 
  214.  
  215. // Add Control 
  216. $this->customizer->add_control( 
  217. new Layers_Customize_Color_Control( 
  218. $this->customizer,  
  219. $setting_key,  
  220. $control_data 
  221. ); 
  222. } else if( 'layers-checkbox' == $control_data['type'] ) { 
  223.  
  224. // Add Control 
  225. $this->customizer->add_control( 
  226. new Layers_Customize_Checkbox_Control( 
  227. $this->customizer,  
  228. $setting_key,  
  229. $control_data 
  230. ); 
  231. } else if( 'layers-select' == $control_data['type'] ) { 
  232.  
  233. // Add Control 
  234. $this->customizer->add_control( 
  235. new Layers_Customize_Select_Control( 
  236. $this->customizer,  
  237. $setting_key,  
  238. $control_data 
  239. ); 
  240. } else if( 'layers-textarea' == $control_data['type'] ) { 
  241.  
  242. // Add Control 
  243. $this->customizer->add_control( 
  244. new Layers_Customize_Textarea_Control( 
  245. $this->customizer,  
  246. $setting_key,  
  247. $control_data 
  248. ); 
  249. } else if( 'layers-rte' == $control_data['type'] ) { 
  250.  
  251. // Add Control 
  252. $this->customizer->add_control( 
  253. new Layers_Customize_RTE_Control( 
  254. $this->customizer,  
  255. $setting_key,  
  256. $control_data 
  257. ); 
  258.  
  259. } else if( 'layers-font' == $control_data['type'] ) { 
  260.  
  261. // Add Control 
  262. $this->customizer->add_control( 
  263. new Layers_Customize_Font_Control( 
  264. $this->customizer,  
  265. $setting_key,  
  266. $control_data 
  267. ); 
  268. } else if ( 'layers-button' == $control_data['type'] ) { 
  269.  
  270. // Add Control 
  271. $this->customizer->add_control( 
  272. new Layers_Customize_Button_Control( 
  273. $this->customizer,  
  274. $setting_key,  
  275. $control_data 
  276. ); 
  277. } else if( 'layers-code' == $control_data['type'] ) { 
  278.  
  279. // Add Control 
  280. $this->customizer->add_control( 
  281. new Layers_Customize_Code_Control( 
  282. $this->customizer,  
  283. $setting_key,  
  284. $control_data 
  285. ); 
  286. } else if( 'layers-text' == $control_data['type'] ) { 
  287.  
  288. // Add Control 
  289. $this->customizer->add_control( 
  290. new Layers_Customize_Text_Control( 
  291. $this->customizer,  
  292. $setting_key,  
  293. $control_data 
  294. ); 
  295. } else if( 'layers-number' == $control_data['type'] ) { 
  296.  
  297. // Add Control 
  298. $this->customizer->add_control( 
  299. new Layers_Customize_Number_Control( 
  300. $this->customizer,  
  301. $setting_key,  
  302. $control_data 
  303. ); 
  304. } else if( 'layers-range' == $control_data['type'] ) { 
  305.  
  306. // Add Control 
  307. $this->customizer->add_control( 
  308. new Layers_Customize_Range_Control( 
  309. $this->customizer,  
  310. $setting_key,  
  311. $control_data 
  312. ); 
  313. } else if( 'layers-trbl-fields' == $control_data['type'] ) { 
  314.  
  315. // Add extra settings fields for Top/Right/Bottom/Left 
  316. $this->customizer->add_setting( 
  317. $setting_key . '-top',  
  318. array( 
  319. 'default' => ( isset( $control_data['default'] ) ? $control_data['default'] : NULL ) ,  
  320. 'type' => 'theme_mod',  
  321. 'capability' => 'manage_options',  
  322. 'sanitize_callback' => $this->add_sanitize_callback( $control_data ) 
  323. ); 
  324. $this->customizer->add_setting( 
  325. $setting_key . '-right',  
  326. array( 
  327. 'default' => ( isset( $control_data['default'] ) ? $control_data['default'] : NULL ) ,  
  328. 'type' => 'theme_mod',  
  329. 'capability' => 'manage_options',  
  330. 'sanitize_callback' => $this->add_sanitize_callback( $control_data ) 
  331. ); 
  332. $this->customizer->add_setting( 
  333. $setting_key . '-bottom',  
  334. array( 
  335. 'default' => ( isset( $control_data['default'] ) ? $control_data['default'] : NULL ) ,  
  336. 'type' => 'theme_mod',  
  337. 'capability' => 'manage_options',  
  338. 'sanitize_callback' => $this->add_sanitize_callback( $control_data ) 
  339. ); 
  340. $this->customizer->add_setting( 
  341. $setting_key . '-left',  
  342. array( 
  343. 'default' => ( isset( $control_data['default'] ) ? $control_data['default'] : NULL ) ,  
  344. 'type' => 'theme_mod',  
  345. 'capability' => 'manage_options',  
  346. 'sanitize_callback' => $this->add_sanitize_callback( $control_data ) 
  347. ); 
  348.  
  349. // Add Control 
  350. $this->customizer->add_control( 
  351. new Layers_Customize_TRBL_Control( 
  352. $this->customizer,  
  353. $setting_key,  
  354. $control_data 
  355. ); 
  356. } else if( 'text' == $control_data['type'] ) { 
  357.  
  358. // Add Control 
  359. $this->customizer->add_control( 
  360. new WP_Customize_Control( 
  361. $this->customizer,  
  362. $setting_key,  
  363. $control_data 
  364. ); 
  365. } else if( 'color' == $control_data['type'] ) { 
  366.  
  367. // Add Control 
  368. $this->customizer->add_control( 
  369. new WP_Customize_Color_Control( 
  370. $this->customizer,  
  371. $setting_key,  
  372. $control_data 
  373. ); 
  374. } else if( 'upload' == $control_data['type'] ) { 
  375.  
  376. // Add Control 
  377. $this->customizer->add_control( 
  378. new WP_Customize_Upload_Control( 
  379. $this->customizer,  
  380. $setting_key,  
  381. $control_data 
  382. ); 
  383. } else if( 'image' == $control_data['type'] ) { 
  384.  
  385. // Add Control 
  386. $this->customizer->add_control( 
  387. new WP_Customize_Image_Control( 
  388. $this->customizer,  
  389. $setting_key,  
  390. $control_data 
  391. ); 
  392. } else if( 'background-image' == $control_data['type'] ) { 
  393.  
  394. // Add Control 
  395. $this->customizer->add_control( 
  396. new WP_Customize_Background_Image_Control( 
  397. $this->customizer,  
  398. $setting_key,  
  399. $control_data 
  400. ); 
  401. } else if( 'header-image' == $control_data['type'] ) { 
  402.  
  403. // Add Control 
  404. $this->customizer->add_control( 
  405. new WP_Customize_Header_Image_Control( 
  406. $this->customizer,  
  407. $setting_key,  
  408. $control_data 
  409. ); 
  410. } else { 
  411.  
  412. // Add Control 
  413. $this->customizer->add_control( 
  414. $setting_key,  
  415. $control_data 
  416. ); 
  417.  
  418. $control_priority++; 
  419.  
  420. } // foreach controls panel_section_key 
  421.  
  422. /** 
  423. * Move Default Panels 
  424. */ 
  425.  
  426. public function move_default_panels( $panels = array() ) { 
  427.  
  428. if ( ! method_exists( $this->customizer, 'get_panel' ) ) return; // `get_panel` only arrived in WP 4.0 
  429.  
  430. foreach( $panels as $panel_key => $panel_data ) { 
  431.  
  432. // Get the current panel 
  433. $panel = $this->customizer->get_panel( $panel_key ); 
  434.  
  435. // Panel Title 
  436. if( isset( $panel->title ) && isset( $panel_data[ 'title' ] ) ) { 
  437. $panel->title = $panel_data[ 'title' ]; 
  438.  
  439. // Panel Priority 
  440. if( isset( $panel->priority ) && isset( $panel_data[ 'priority' ] ) ) { 
  441. $panel->priority = $panel_data[ 'priority' ]; 
  442.  
  443.  
  444. // Remove the theme switcher Panel, Layers isn't ready for that 
  445. $this->customizer->remove_section( 'themes' ); 
  446.  
  447. /** 
  448. * Move Default Sections 
  449. */ 
  450.  
  451. public function move_default_sections( $sections = array() ) { 
  452.  
  453. foreach( $sections as $section_key => $section_data ) { 
  454.  
  455. // Get the current section 
  456. $section = $this->customizer->get_section( $section_key ); 
  457.  
  458. // Move this section to a specific panel 
  459. if( isset( $section->panel ) && isset( $section_data[ 'panel' ] ) ) { 
  460. $section->panel = $this->prefix . $section_data[ 'panel' ]; 
  461.  
  462. // Section Title 
  463. if( isset( $section->title ) && isset( $section_data[ 'title' ] ) ) { 
  464. $section->title = $section_data[ 'title' ]; 
  465.  
  466. // Section Priority 
  467. if( isset( $section->priority ) && isset( $section_data[ 'priority' ] ) ) { 
  468. $section->priority = $section_data[ 'priority' ]; 
  469. /** 
  470. * Move Default Controls 
  471. */ 
  472.  
  473. public function move_default_controls( $controls = array() ) { 
  474.  
  475. foreach( $controls as $control_key => $control_data ) { 
  476.  
  477. // Get the current control 
  478. $control = $this->customizer->get_control( $control_key ); 
  479.  
  480. // Move this control to a specific section 
  481. if( isset( $control->section ) && isset( $control_data[ 'section' ] ) ) { 
  482. $control->section = $this->prefix . $control_data[ 'section' ]; 
  483.  
  484. // Section Title 
  485. if( isset( $control->title ) && isset( $control_data[ 'title' ] ) ) { 
  486. $control->title = $control_data[ 'title' ]; 
  487.  
  488. // Section Priority 
  489. if( isset( $control->priority ) && isset( $control_data[ 'priority' ] ) ) { 
  490. $control->priority = $control_data[ 'priority' ]; 
  491.  
  492.  
  493. // Remove the header text color control, we don't support it yet 
  494. $this->customizer->remove_section( 'colors' ); 
  495.  
  496. /** 
  497. * Add Sanitization according to the control type (or use the explicit callback that has been set) 
  498. */ 
  499.  
  500. function add_sanitize_callback( $control_data = FALSE ) { 
  501.  
  502. // If there's an override, use the override rather than the automatic sanitization 
  503. if( isset( $control_data[ 'sanitize_callback' ] ) ) { 
  504. if( FALSE == $control_data[ 'sanitize_callback' ] ) { 
  505. return FALSE; 
  506. } else { 
  507. return $control_data[ 'sanitize_callback' ]; 
  508.  
  509. switch( $control_data[ 'type' ] ) { 
  510. case 'layers-color' : 
  511. $callback = 'sanitize_hex_color'; 
  512. break; 
  513. case 'layers-checkbox' : 
  514. $callback = 'layers_sanitize_checkbox'; 
  515. break; 
  516. case 'layers-textarea' : 
  517. $callback = 'esc_textarea'; 
  518. break; 
  519. case 'layers-code' : 
  520. $callback = false; 
  521. break; 
  522. case 'layers-rte' : 
  523. $callback = false; 
  524. break; 
  525. default : 
  526. $callback = 'sanitize_text_field'; 
  527.  
  528. return $callback; 
  529.