/wp-includes/class-wp-customize-widgets.php

  1. <?php 
  2. /** 
  3. * WordPress Customize Widgets classes 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Customize 
  7. * @since 3.9.0 
  8. */ 
  9.  
  10. /** 
  11. * Customize Widgets class. 
  12. * 
  13. * Implements widget management in the Customizer. 
  14. * 
  15. * @since 3.9.0 
  16. * 
  17. * @see WP_Customize_Manager 
  18. */ 
  19. final class WP_Customize_Widgets { 
  20.  
  21. /** 
  22. * WP_Customize_Manager instance. 
  23. * 
  24. * @since 3.9.0 
  25. * @access public 
  26. * @var WP_Customize_Manager 
  27. */ 
  28. public $manager; 
  29.  
  30. /** 
  31. * All id_bases for widgets defined in core. 
  32. * 
  33. * @since 3.9.0 
  34. * @access protected 
  35. * @var array 
  36. */ 
  37. protected $core_widget_id_bases = array( 
  38. 'archives', 'calendar', 'categories', 'links', 'meta',  
  39. 'nav_menu', 'pages', 'recent-comments', 'recent-posts',  
  40. 'rss', 'search', 'tag_cloud', 'text',  
  41. ); 
  42.  
  43. /** 
  44. * @since 3.9.0 
  45. * @access protected 
  46. * @var array 
  47. */ 
  48. protected $rendered_sidebars = array(); 
  49.  
  50. /** 
  51. * @since 3.9.0 
  52. * @access protected 
  53. * @var array 
  54. */ 
  55. protected $rendered_widgets = array(); 
  56.  
  57. /** 
  58. * @since 3.9.0 
  59. * @access protected 
  60. * @var array 
  61. */ 
  62. protected $old_sidebars_widgets = array(); 
  63.  
  64. /** 
  65. * Mapping of widget ID base to whether it supports selective refresh. 
  66. * 
  67. * @since 4.5.0 
  68. * @access protected 
  69. * @var array 
  70. */ 
  71. protected $selective_refreshable_widgets; 
  72.  
  73. /** 
  74. * Mapping of setting type to setting ID pattern. 
  75. * 
  76. * @since 4.2.0 
  77. * @access protected 
  78. * @var array 
  79. */ 
  80. protected $setting_id_patterns = array( 
  81. 'widget_instance' => '/^widget_(?P<id_base>.+?)(?:\[(?P<widget_number>\d+)\])?$/',  
  82. 'sidebar_widgets' => '/^sidebars_widgets\[(?P<sidebar_id>.+?)\]$/',  
  83. ); 
  84.  
  85. /** 
  86. * Initial loader. 
  87. * 
  88. * @since 3.9.0 
  89. * @access public 
  90. * 
  91. * @param WP_Customize_Manager $manager Customize manager bootstrap instance. 
  92. */ 
  93. public function __construct( $manager ) { 
  94. $this->manager = $manager; 
  95.  
  96. // See https://github.com/xwp/wp-customize-snapshots/blob/962586659688a5b1fd9ae93618b7ce2d4e7a421c/php/class-customize-snapshot-manager.php#L420-L449 
  97. add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_customize_dynamic_setting_args' ), 10, 2 ); 
  98. add_action( 'widgets_init', array( $this, 'register_settings' ), 95 ); 
  99. add_action( 'customize_register', array( $this, 'schedule_customize_register' ), 1 ); 
  100.  
  101. // Skip remaining hooks when the user can't manage widgets anyway. 
  102. if ( ! current_user_can( 'edit_theme_options' ) ) { 
  103. return; 
  104.  
  105. add_action( 'wp_loaded', array( $this, 'override_sidebars_widgets_for_theme_switch' ) ); 
  106. add_action( 'customize_controls_init', array( $this, 'customize_controls_init' ) ); 
  107. add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 
  108. add_action( 'customize_controls_print_styles', array( $this, 'print_styles' ) ); 
  109. add_action( 'customize_controls_print_scripts', array( $this, 'print_scripts' ) ); 
  110. add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_footer_scripts' ) ); 
  111. add_action( 'customize_controls_print_footer_scripts', array( $this, 'output_widget_control_templates' ) ); 
  112. add_action( 'customize_preview_init', array( $this, 'customize_preview_init' ) ); 
  113. add_filter( 'customize_refresh_nonces', array( $this, 'refresh_nonces' ) ); 
  114.  
  115. add_action( 'dynamic_sidebar', array( $this, 'tally_rendered_widgets' ) ); 
  116. add_filter( 'is_active_sidebar', array( $this, 'tally_sidebars_via_is_active_sidebar_calls' ), 10, 2 ); 
  117. add_filter( 'dynamic_sidebar_has_widgets', array( $this, 'tally_sidebars_via_dynamic_sidebar_calls' ), 10, 2 ); 
  118.  
  119. // Selective Refresh. 
  120. add_filter( 'customize_dynamic_partial_args', array( $this, 'customize_dynamic_partial_args' ), 10, 2 ); 
  121. add_action( 'customize_preview_init', array( $this, 'selective_refresh_init' ) ); 
  122.  
  123. /** 
  124. * List whether each registered widget can be use selective refresh. 
  125. * 
  126. * If the theme does not support the customize-selective-refresh-widgets feature,  
  127. * then this will always return an empty array. 
  128. * 
  129. * @since 4.5.0 
  130. * @access public 
  131. * 
  132. * @return array Mapping of id_base to support. If theme doesn't support 
  133. * selective refresh, an empty array is returned. 
  134. */ 
  135. public function get_selective_refreshable_widgets() { 
  136. global $wp_widget_factory; 
  137. if ( ! current_theme_supports( 'customize-selective-refresh-widgets' ) ) { 
  138. return array(); 
  139. if ( ! isset( $this->selective_refreshable_widgets ) ) { 
  140. $this->selective_refreshable_widgets = array(); 
  141. foreach ( $wp_widget_factory->widgets as $wp_widget ) { 
  142. $this->selective_refreshable_widgets[ $wp_widget->id_base ] = ! empty( $wp_widget->widget_options['customize_selective_refresh'] ); 
  143. return $this->selective_refreshable_widgets; 
  144.  
  145. /** 
  146. * Determines if a widget supports selective refresh. 
  147. * 
  148. * @since 4.5.0 
  149. * @access public 
  150. * 
  151. * @param string $id_base Widget ID Base. 
  152. * @return bool Whether the widget can be selective refreshed. 
  153. */ 
  154. public function is_widget_selective_refreshable( $id_base ) { 
  155. $selective_refreshable_widgets = $this->get_selective_refreshable_widgets(); 
  156. return ! empty( $selective_refreshable_widgets[ $id_base ] ); 
  157.  
  158. /** 
  159. * Retrieves the widget setting type given a setting ID. 
  160. * 
  161. * @since 4.2.0 
  162. * @access protected 
  163. * 
  164. * @staticvar array $cache 
  165. * 
  166. * @param string $setting_id Setting ID. 
  167. * @return string|void Setting type. 
  168. */ 
  169. protected function get_setting_type( $setting_id ) { 
  170. static $cache = array(); 
  171. if ( isset( $cache[ $setting_id ] ) ) { 
  172. return $cache[ $setting_id ]; 
  173. foreach ( $this->setting_id_patterns as $type => $pattern ) { 
  174. if ( preg_match( $pattern, $setting_id ) ) { 
  175. $cache[ $setting_id ] = $type; 
  176. return $type; 
  177.  
  178. /** 
  179. * Inspects the incoming customized data for any widget settings, and dynamically adds 
  180. * them up-front so widgets will be initialized properly. 
  181. * 
  182. * @since 4.2.0 
  183. * @access public 
  184. */ 
  185. public function register_settings() { 
  186. $widget_setting_ids = array(); 
  187. $incoming_setting_ids = array_keys( $this->manager->unsanitized_post_values() ); 
  188. foreach ( $incoming_setting_ids as $setting_id ) { 
  189. if ( ! is_null( $this->get_setting_type( $setting_id ) ) ) { 
  190. $widget_setting_ids[] = $setting_id; 
  191. if ( $this->manager->doing_ajax( 'update-widget' ) && isset( $_REQUEST['widget-id'] ) ) { 
  192. $widget_setting_ids[] = $this->get_setting_id( wp_unslash( $_REQUEST['widget-id'] ) ); 
  193.  
  194. $settings = $this->manager->add_dynamic_settings( array_unique( $widget_setting_ids ) ); 
  195.  
  196. /** 
  197. * Preview settings right away so that widgets and sidebars will get registered properly. 
  198. * But don't do this if a customize_save because this will cause WP to think there is nothing 
  199. * changed that needs to be saved. 
  200. */ 
  201. if ( ! $this->manager->doing_ajax( 'customize_save' ) ) { 
  202. foreach ( $settings as $setting ) { 
  203. $setting->preview(); 
  204.  
  205. /** 
  206. * Determines the arguments for a dynamically-created setting. 
  207. * 
  208. * @since 4.2.0 
  209. * @access public 
  210. * 
  211. * @param false|array $args The arguments to the WP_Customize_Setting constructor. 
  212. * @param string $setting_id ID for dynamic setting, usually coming from `$_POST['customized']`. 
  213. * @return false|array Setting arguments, false otherwise. 
  214. */ 
  215. public function filter_customize_dynamic_setting_args( $args, $setting_id ) { 
  216. if ( $this->get_setting_type( $setting_id ) ) { 
  217. $args = $this->get_setting_args( $setting_id ); 
  218. return $args; 
  219.  
  220. /** 
  221. * Retrieves an unslashed post value or return a default. 
  222. * 
  223. * @since 3.9.0 
  224. * @access protected 
  225. * 
  226. * @param string $name Post value. 
  227. * @param mixed $default Default post value. 
  228. * @return mixed Unslashed post value or default value. 
  229. */ 
  230. protected function get_post_value( $name, $default = null ) { 
  231. if ( ! isset( $_POST[ $name ] ) ) { 
  232. return $default; 
  233.  
  234. return wp_unslash( $_POST[ $name ] ); 
  235.  
  236. /** 
  237. * Override sidebars_widgets for theme switch. 
  238. * 
  239. * When switching a theme via the Customizer, supply any previously-configured 
  240. * sidebars_widgets from the target theme as the initial sidebars_widgets 
  241. * setting. Also store the old theme's existing settings so that they can 
  242. * be passed along for storing in the sidebars_widgets theme_mod when the 
  243. * theme gets switched. 
  244. * 
  245. * @since 3.9.0 
  246. * @access public 
  247. * 
  248. * @global array $sidebars_widgets 
  249. * @global array $_wp_sidebars_widgets 
  250. */ 
  251. public function override_sidebars_widgets_for_theme_switch() { 
  252. global $sidebars_widgets; 
  253.  
  254. if ( $this->manager->doing_ajax() || $this->manager->is_theme_active() ) { 
  255. return; 
  256.  
  257. $this->old_sidebars_widgets = wp_get_sidebars_widgets(); 
  258. add_filter( 'customize_value_old_sidebars_widgets_data', array( $this, 'filter_customize_value_old_sidebars_widgets_data' ) ); 
  259. $this->manager->set_post_value( 'old_sidebars_widgets_data', $this->old_sidebars_widgets ); // Override any value cached in changeset. 
  260.  
  261. // retrieve_widgets() looks at the global $sidebars_widgets 
  262. $sidebars_widgets = $this->old_sidebars_widgets; 
  263. $sidebars_widgets = retrieve_widgets( 'customize' ); 
  264. add_filter( 'option_sidebars_widgets', array( $this, 'filter_option_sidebars_widgets_for_theme_switch' ), 1 ); 
  265. // reset global cache var used by wp_get_sidebars_widgets() 
  266. unset( $GLOBALS['_wp_sidebars_widgets'] ); 
  267.  
  268. /** 
  269. * Filters old_sidebars_widgets_data Customizer setting. 
  270. * 
  271. * When switching themes, filter the Customizer setting old_sidebars_widgets_data 
  272. * to supply initial $sidebars_widgets before they were overridden by retrieve_widgets(). 
  273. * The value for old_sidebars_widgets_data gets set in the old theme's sidebars_widgets 
  274. * theme_mod. 
  275. * 
  276. * @since 3.9.0 
  277. * @access public 
  278. * 
  279. * @see WP_Customize_Widgets::handle_theme_switch() 
  280. * 
  281. * @param array $old_sidebars_widgets 
  282. * @return array 
  283. */ 
  284. public function filter_customize_value_old_sidebars_widgets_data( $old_sidebars_widgets ) { 
  285. return $this->old_sidebars_widgets; 
  286.  
  287. /** 
  288. * Filters sidebars_widgets option for theme switch. 
  289. * 
  290. * When switching themes, the retrieve_widgets() function is run when the Customizer initializes,  
  291. * and then the new sidebars_widgets here get supplied as the default value for the sidebars_widgets 
  292. * option. 
  293. * 
  294. * @since 3.9.0 
  295. * @access public 
  296. * 
  297. * @see WP_Customize_Widgets::handle_theme_switch() 
  298. * @global array $sidebars_widgets 
  299. * 
  300. * @param array $sidebars_widgets 
  301. * @return array 
  302. */ 
  303. public function filter_option_sidebars_widgets_for_theme_switch( $sidebars_widgets ) { 
  304. $sidebars_widgets = $GLOBALS['sidebars_widgets']; 
  305. $sidebars_widgets['array_version'] = 3; 
  306. return $sidebars_widgets; 
  307.  
  308. /** 
  309. * Ensures all widgets get loaded into the Customizer. 
  310. * 
  311. * Note: these actions are also fired in wp_ajax_update_widget(). 
  312. * 
  313. * @since 3.9.0 
  314. * @access public 
  315. */ 
  316. public function customize_controls_init() { 
  317. /** This action is documented in wp-admin/includes/ajax-actions.php */ 
  318. do_action( 'load-widgets.php' ); 
  319.  
  320. /** This action is documented in wp-admin/includes/ajax-actions.php */ 
  321. do_action( 'widgets.php' ); 
  322.  
  323. /** This action is documented in wp-admin/widgets.php */ 
  324. do_action( 'sidebar_admin_setup' ); 
  325.  
  326. /** 
  327. * Ensures widgets are available for all types of previews. 
  328. * 
  329. * When in preview, hook to {@see 'customize_register'} for settings after WordPress is loaded 
  330. * so that all filters have been initialized (e.g. Widget Visibility). 
  331. * 
  332. * @since 3.9.0 
  333. * @access public 
  334. */ 
  335. public function schedule_customize_register() { 
  336. if ( is_admin() ) { 
  337. $this->customize_register(); 
  338. } else { 
  339. add_action( 'wp', array( $this, 'customize_register' ) ); 
  340.  
  341. /** 
  342. * Registers Customizer settings and controls for all sidebars and widgets. 
  343. * 
  344. * @since 3.9.0 
  345. * @access public 
  346. * 
  347. * @global array $wp_registered_widgets 
  348. * @global array $wp_registered_widget_controls 
  349. * @global array $wp_registered_sidebars 
  350. */ 
  351. public function customize_register() { 
  352. global $wp_registered_widgets, $wp_registered_widget_controls, $wp_registered_sidebars; 
  353.  
  354. add_filter( 'sidebars_widgets', array( $this, 'preview_sidebars_widgets' ), 1 ); 
  355.  
  356. $sidebars_widgets = array_merge( 
  357. array( 'wp_inactive_widgets' => array() ),  
  358. array_fill_keys( array_keys( $wp_registered_sidebars ), array() ),  
  359. wp_get_sidebars_widgets() 
  360. ); 
  361.  
  362. $new_setting_ids = array(); 
  363.  
  364. /** 
  365. * Register a setting for all widgets, including those which are active,  
  366. * inactive, and orphaned since a widget may get suppressed from a sidebar 
  367. * via a plugin (like Widget Visibility). 
  368. */ 
  369. foreach ( array_keys( $wp_registered_widgets ) as $widget_id ) { 
  370. $setting_id = $this->get_setting_id( $widget_id ); 
  371. $setting_args = $this->get_setting_args( $setting_id ); 
  372. if ( ! $this->manager->get_setting( $setting_id ) ) { 
  373. $this->manager->add_setting( $setting_id, $setting_args ); 
  374. $new_setting_ids[] = $setting_id; 
  375.  
  376. /** 
  377. * Add a setting which will be supplied for the theme's sidebars_widgets 
  378. * theme_mod when the theme is switched. 
  379. */ 
  380. if ( ! $this->manager->is_theme_active() ) { 
  381. $setting_id = 'old_sidebars_widgets_data'; 
  382. $setting_args = $this->get_setting_args( $setting_id, array( 
  383. 'type' => 'global_variable',  
  384. 'dirty' => true,  
  385. ) ); 
  386. $this->manager->add_setting( $setting_id, $setting_args ); 
  387.  
  388. $this->manager->add_panel( 'widgets', array( 
  389. 'type' => 'widgets',  
  390. 'title' => __( 'Widgets' ),  
  391. 'description' => __( 'Widgets are independent sections of content that can be placed into widgetized areas provided by your theme (commonly called sidebars).' ),  
  392. 'priority' => 110,  
  393. 'active_callback' => array( $this, 'is_panel_active' ),  
  394. 'auto_expand_sole_section' => true,  
  395. ) ); 
  396.  
  397. foreach ( $sidebars_widgets as $sidebar_id => $sidebar_widget_ids ) { 
  398. if ( empty( $sidebar_widget_ids ) ) { 
  399. $sidebar_widget_ids = array(); 
  400.  
  401. $is_registered_sidebar = is_registered_sidebar( $sidebar_id ); 
  402. $is_inactive_widgets = ( 'wp_inactive_widgets' === $sidebar_id ); 
  403. $is_active_sidebar = ( $is_registered_sidebar && ! $is_inactive_widgets ); 
  404.  
  405. // Add setting for managing the sidebar's widgets. 
  406. if ( $is_registered_sidebar || $is_inactive_widgets ) { 
  407. $setting_id = sprintf( 'sidebars_widgets[%s]', $sidebar_id ); 
  408. $setting_args = $this->get_setting_args( $setting_id ); 
  409. if ( ! $this->manager->get_setting( $setting_id ) ) { 
  410. if ( ! $this->manager->is_theme_active() ) { 
  411. $setting_args['dirty'] = true; 
  412. $this->manager->add_setting( $setting_id, $setting_args ); 
  413. $new_setting_ids[] = $setting_id; 
  414.  
  415. // Add section to contain controls. 
  416. $section_id = sprintf( 'sidebar-widgets-%s', $sidebar_id ); 
  417. if ( $is_active_sidebar ) { 
  418.  
  419. $section_args = array( 
  420. 'title' => $wp_registered_sidebars[ $sidebar_id ]['name'],  
  421. 'description' => $wp_registered_sidebars[ $sidebar_id ]['description'],  
  422. 'priority' => array_search( $sidebar_id, array_keys( $wp_registered_sidebars ) ),  
  423. 'panel' => 'widgets',  
  424. 'sidebar_id' => $sidebar_id,  
  425. ); 
  426.  
  427. /** 
  428. * Filters Customizer widget section arguments for a given sidebar. 
  429. * 
  430. * @since 3.9.0 
  431. * 
  432. * @param array $section_args Array of Customizer widget section arguments. 
  433. * @param string $section_id Customizer section ID. 
  434. * @param int|string $sidebar_id Sidebar ID. 
  435. */ 
  436. $section_args = apply_filters( 'customizer_widgets_section_args', $section_args, $section_id, $sidebar_id ); 
  437.  
  438. $section = new WP_Customize_Sidebar_Section( $this->manager, $section_id, $section_args ); 
  439. $this->manager->add_section( $section ); 
  440.  
  441. $control = new WP_Widget_Area_Customize_Control( $this->manager, $setting_id, array( 
  442. 'section' => $section_id,  
  443. 'sidebar_id' => $sidebar_id,  
  444. 'priority' => count( $sidebar_widget_ids ), // place 'Add Widget' and 'Reorder' buttons at end. 
  445. ) ); 
  446. $new_setting_ids[] = $setting_id; 
  447.  
  448. $this->manager->add_control( $control ); 
  449.  
  450. // Add a control for each active widget (located in a sidebar). 
  451. foreach ( $sidebar_widget_ids as $i => $widget_id ) { 
  452.  
  453. // Skip widgets that may have gone away due to a plugin being deactivated. 
  454. if ( ! $is_active_sidebar || ! isset( $wp_registered_widgets[$widget_id] ) ) { 
  455. continue; 
  456.  
  457. $registered_widget = $wp_registered_widgets[$widget_id]; 
  458. $setting_id = $this->get_setting_id( $widget_id ); 
  459. $id_base = $wp_registered_widget_controls[$widget_id]['id_base']; 
  460.  
  461. $control = new WP_Widget_Form_Customize_Control( $this->manager, $setting_id, array( 
  462. 'label' => $registered_widget['name'],  
  463. 'section' => $section_id,  
  464. 'sidebar_id' => $sidebar_id,  
  465. 'widget_id' => $widget_id,  
  466. 'widget_id_base' => $id_base,  
  467. 'priority' => $i,  
  468. 'width' => $wp_registered_widget_controls[$widget_id]['width'],  
  469. 'height' => $wp_registered_widget_controls[$widget_id]['height'],  
  470. 'is_wide' => $this->is_wide_widget( $widget_id ),  
  471. ) ); 
  472. $this->manager->add_control( $control ); 
  473.  
  474. if ( ! $this->manager->doing_ajax( 'customize_save' ) ) { 
  475. foreach ( $new_setting_ids as $new_setting_id ) { 
  476. $this->manager->get_setting( $new_setting_id )->preview(); 
  477.  
  478. /** 
  479. * Determines whether the widgets panel is active, based on whether there are sidebars registered. 
  480. * 
  481. * @since 4.4.0 
  482. * @access public 
  483. * 
  484. * @see WP_Customize_Panel::$active_callback 
  485. * 
  486. * @global array $wp_registered_sidebars 
  487. * @return bool Active. 
  488. */ 
  489. public function is_panel_active() { 
  490. global $wp_registered_sidebars; 
  491. return ! empty( $wp_registered_sidebars ); 
  492.  
  493. /** 
  494. * Converts a widget_id into its corresponding Customizer setting ID (option name). 
  495. * 
  496. * @since 3.9.0 
  497. * @access public 
  498. * 
  499. * @param string $widget_id Widget ID. 
  500. * @return string Maybe-parsed widget ID. 
  501. */ 
  502. public function get_setting_id( $widget_id ) { 
  503. $parsed_widget_id = $this->parse_widget_id( $widget_id ); 
  504. $setting_id = sprintf( 'widget_%s', $parsed_widget_id['id_base'] ); 
  505.  
  506. if ( ! is_null( $parsed_widget_id['number'] ) ) { 
  507. $setting_id .= sprintf( '[%d]', $parsed_widget_id['number'] ); 
  508. return $setting_id; 
  509.  
  510. /** 
  511. * Determines whether the widget is considered "wide". 
  512. * 
  513. * Core widgets which may have controls wider than 250, but can still be shown 
  514. * in the narrow Customizer panel. The RSS and Text widgets in Core, for example,  
  515. * have widths of 400 and yet they still render fine in the Customizer panel. 
  516. * 
  517. * This method will return all Core widgets as being not wide, but this can be 
  518. * overridden with the {@see 'is_wide_widget_in_customizer'} filter. 
  519. * 
  520. * @since 3.9.0 
  521. * @access public 
  522. * 
  523. * @global $wp_registered_widget_controls 
  524. * 
  525. * @param string $widget_id Widget ID. 
  526. * @return bool Whether or not the widget is a "wide" widget. 
  527. */ 
  528. public function is_wide_widget( $widget_id ) { 
  529. global $wp_registered_widget_controls; 
  530.  
  531. $parsed_widget_id = $this->parse_widget_id( $widget_id ); 
  532. $width = $wp_registered_widget_controls[$widget_id]['width']; 
  533. $is_core = in_array( $parsed_widget_id['id_base'], $this->core_widget_id_bases ); 
  534. $is_wide = ( $width > 250 && ! $is_core ); 
  535.  
  536. /** 
  537. * Filters whether the given widget is considered "wide". 
  538. * 
  539. * @since 3.9.0 
  540. * 
  541. * @param bool $is_wide Whether the widget is wide, Default false. 
  542. * @param string $widget_id Widget ID. 
  543. */ 
  544. return apply_filters( 'is_wide_widget_in_customizer', $is_wide, $widget_id ); 
  545.  
  546. /** 
  547. * Converts a widget ID into its id_base and number components. 
  548. * 
  549. * @since 3.9.0 
  550. * @access public 
  551. * 
  552. * @param string $widget_id Widget ID. 
  553. * @return array Array containing a widget's id_base and number components. 
  554. */ 
  555. public function parse_widget_id( $widget_id ) { 
  556. $parsed = array( 
  557. 'number' => null,  
  558. 'id_base' => null,  
  559. ); 
  560.  
  561. if ( preg_match( '/^(.+)-(\d+)$/', $widget_id, $matches ) ) { 
  562. $parsed['id_base'] = $matches[1]; 
  563. $parsed['number'] = intval( $matches[2] ); 
  564. } else { 
  565. // likely an old single widget 
  566. $parsed['id_base'] = $widget_id; 
  567. return $parsed; 
  568.  
  569. /** 
  570. * Converts a widget setting ID (option path) to its id_base and number components. 
  571. * 
  572. * @since 3.9.0 
  573. * @access public 
  574. * 
  575. * @param string $setting_id Widget setting ID. 
  576. * @return WP_Error|array Array containing a widget's id_base and number components,  
  577. * or a WP_Error object. 
  578. */ 
  579. public function parse_widget_setting_id( $setting_id ) { 
  580. if ( ! preg_match( '/^(widget_(.+?))(?:\[(\d+)\])?$/', $setting_id, $matches ) ) { 
  581. return new WP_Error( 'widget_setting_invalid_id' ); 
  582.  
  583. $id_base = $matches[2]; 
  584. $number = isset( $matches[3] ) ? intval( $matches[3] ) : null; 
  585.  
  586. return compact( 'id_base', 'number' ); 
  587.  
  588. /** 
  589. * Calls admin_print_styles-widgets.php and admin_print_styles hooks to 
  590. * allow custom styles from plugins. 
  591. * 
  592. * @since 3.9.0 
  593. * @access public 
  594. */ 
  595. public function print_styles() { 
  596. /** This action is documented in wp-admin/admin-header.php */ 
  597. do_action( 'admin_print_styles-widgets.php' ); 
  598.  
  599. /** This action is documented in wp-admin/admin-header.php */ 
  600. do_action( 'admin_print_styles' ); 
  601.  
  602. /** 
  603. * Calls admin_print_scripts-widgets.php and admin_print_scripts hooks to 
  604. * allow custom scripts from plugins. 
  605. * 
  606. * @since 3.9.0 
  607. * @access public 
  608. */ 
  609. public function print_scripts() { 
  610. /** This action is documented in wp-admin/admin-header.php */ 
  611. do_action( 'admin_print_scripts-widgets.php' ); 
  612.  
  613. /** This action is documented in wp-admin/admin-header.php */ 
  614. do_action( 'admin_print_scripts' ); 
  615.  
  616. /** 
  617. * Enqueues scripts and styles for Customizer panel and export data to JavaScript. 
  618. * 
  619. * @since 3.9.0 
  620. * @access public 
  621. * 
  622. * @global WP_Scripts $wp_scripts 
  623. * @global array $wp_registered_sidebars 
  624. * @global array $wp_registered_widgets 
  625. */ 
  626. public function enqueue_scripts() { 
  627. global $wp_scripts, $wp_registered_sidebars, $wp_registered_widgets; 
  628.  
  629. wp_enqueue_style( 'customize-widgets' ); 
  630. wp_enqueue_script( 'customize-widgets' ); 
  631.  
  632. /** This action is documented in wp-admin/admin-header.php */ 
  633. do_action( 'admin_enqueue_scripts', 'widgets.php' ); 
  634.  
  635. /** 
  636. * Export available widgets with control_tpl removed from model 
  637. * since plugins need templates to be in the DOM. 
  638. */ 
  639. $available_widgets = array(); 
  640.  
  641. foreach ( $this->get_available_widgets() as $available_widget ) { 
  642. unset( $available_widget['control_tpl'] ); 
  643. $available_widgets[] = $available_widget; 
  644.  
  645. $widget_reorder_nav_tpl = sprintf( 
  646. '<div class="widget-reorder-nav"><span class="move-widget" tabindex="0">%1$s</span><span class="move-widget-down" tabindex="0">%2$s</span><span class="move-widget-up" tabindex="0">%3$s</span></div>',  
  647. __( 'Move to another area…' ),  
  648. __( 'Move down' ),  
  649. __( 'Move up' ) 
  650. ); 
  651.  
  652. $move_widget_area_tpl = str_replace( 
  653. array( '{description}', '{btn}' ),  
  654. array( 
  655. __( 'Select an area to move this widget into:' ),  
  656. _x( 'Move', 'Move widget' ),  
  657. ),  
  658. '<div class="move-widget-area"> 
  659. <p class="description">{description}</p> 
  660. <ul class="widget-area-select"> 
  661. <% _.each( sidebars, function ( sidebar ) { %> 
  662. <li class="" data-id="<%- sidebar.id %>" title="<%- sidebar.description %>" tabindex="0"><%- sidebar.name %></li> 
  663. <% }); %> 
  664. </ul> 
  665. <div class="move-widget-actions"> 
  666. <button class="move-widget-btn button" type="button">{btn}</button> 
  667. </div> 
  668. </div>' 
  669. ); 
  670.  
  671. $settings = array( 
  672. 'registeredSidebars' => array_values( $wp_registered_sidebars ),  
  673. 'registeredWidgets' => $wp_registered_widgets,  
  674. 'availableWidgets' => $available_widgets, // @todo Merge this with registered_widgets 
  675. 'l10n' => array( 
  676. 'saveBtnLabel' => __( 'Apply' ),  
  677. 'saveBtnTooltip' => __( 'Save and preview changes before publishing them.' ),  
  678. 'removeBtnLabel' => __( 'Remove' ),  
  679. 'removeBtnTooltip' => __( 'Trash widget by moving it to the inactive widgets sidebar.' ),  
  680. 'error' => __( 'An error has occurred. Please reload the page and try again.' ),  
  681. 'widgetMovedUp' => __( 'Widget moved up' ),  
  682. 'widgetMovedDown' => __( 'Widget moved down' ),  
  683. 'noAreasRendered' => __( 'There are no widget areas on the page shown, however other pages in this theme do have them.' ),  
  684. 'reorderModeOn' => __( 'Reorder mode enabled' ),  
  685. 'reorderModeOff' => __( 'Reorder mode closed' ),  
  686. 'reorderLabelOn' => esc_attr__( 'Reorder widgets' ),  
  687. 'widgetsFound' => __( 'Number of widgets found: %d' ),  
  688. 'noWidgetsFound' => __( 'No widgets found.' ),  
  689. ),  
  690. 'tpl' => array( 
  691. 'widgetReorderNav' => $widget_reorder_nav_tpl,  
  692. 'moveWidgetArea' => $move_widget_area_tpl,  
  693. ),  
  694. 'selectiveRefreshableWidgets' => $this->get_selective_refreshable_widgets(),  
  695. ); 
  696.  
  697. foreach ( $settings['registeredWidgets'] as &$registered_widget ) { 
  698. unset( $registered_widget['callback'] ); // may not be JSON-serializeable 
  699.  
  700. $wp_scripts->add_data( 
  701. 'customize-widgets',  
  702. 'data',  
  703. sprintf( 'var _wpCustomizeWidgetsSettings = %s;', wp_json_encode( $settings ) ) 
  704. ); 
  705.  
  706. /** 
  707. * Renders the widget form control templates into the DOM. 
  708. * 
  709. * @since 3.9.0 
  710. * @access public 
  711. */ 
  712. public function output_widget_control_templates() { 
  713. ?> 
  714. <div id="widgets-left"><!-- compatibility with JS which looks for widget templates here --> 
  715. <div id="available-widgets"> 
  716. <div class="customize-section-title"> 
  717. <button class="customize-section-back" tabindex="-1"> 
  718. <span class="screen-reader-text"><?php _e( 'Back' ); ?></span> 
  719. </button> 
  720. <h3> 
  721. <span class="customize-action"><?php 
  722. /** translators: ▸ is the unicode right-pointing triangle, and %s is the section title in the Customizer */ 
  723. echo sprintf( __( 'Customizing ▸ %s' ), esc_html( $this->manager->get_panel( 'widgets' )->title ) ); 
  724. ?></span> 
  725. <?php _e( 'Add a Widget' ); ?> 
  726. </h3> 
  727. </div> 
  728. <div id="available-widgets-filter"> 
  729. <label class="screen-reader-text" for="widgets-search"><?php _e( 'Search Widgets' ); ?></label> 
  730. <input type="text" id="widgets-search" placeholder="<?php esc_attr_e( 'Search widgets…' ) ?>" aria-describedby="widgets-search-desc" /> 
  731. <div class="search-icon" aria-hidden="true"></div> 
  732. <button type="button" class="clear-results"><span class="screen-reader-text"><?php _e( 'Clear Results' ); ?></span></button> 
  733. <p class="screen-reader-text" id="widgets-search-desc"><?php _e( 'The search results will be updated as you type.' ); ?></p> 
  734. </div> 
  735. <div id="available-widgets-list"> 
  736. <?php foreach ( $this->get_available_widgets() as $available_widget ): ?> 
  737. <div id="widget-tpl-<?php echo esc_attr( $available_widget['id'] ) ?>" data-widget-id="<?php echo esc_attr( $available_widget['id'] ) ?>" class="widget-tpl <?php echo esc_attr( $available_widget['id'] ) ?>" tabindex="0"> 
  738. <?php echo $available_widget['control_tpl']; ?> 
  739. </div> 
  740. <?php endforeach; ?> 
  741. <p class="no-widgets-found-message"><?php _e( 'No widgets found.' ); ?></p> 
  742. </div><!-- #available-widgets-list --> 
  743. </div><!-- #available-widgets --> 
  744. </div><!-- #widgets-left --> 
  745. <?php 
  746.  
  747. /** 
  748. * Calls admin_print_footer_scripts and admin_print_scripts hooks to 
  749. * allow custom scripts from plugins. 
  750. * 
  751. * @since 3.9.0 
  752. * @access public 
  753. */ 
  754. public function print_footer_scripts() { 
  755. /** This action is documented in wp-admin/admin-footer.php */ 
  756. do_action( 'admin_print_footer_scripts-widgets.php' ); 
  757.  
  758. /** This action is documented in wp-admin/admin-footer.php */ 
  759. do_action( 'admin_print_footer_scripts' ); 
  760.  
  761. /** This action is documented in wp-admin/admin-footer.php */ 
  762. do_action( 'admin_footer-widgets.php' ); 
  763.  
  764. /** 
  765. * Retrieves common arguments to supply when constructing a Customizer setting. 
  766. * 
  767. * @since 3.9.0 
  768. * @access public 
  769. * 
  770. * @param string $id Widget setting ID. 
  771. * @param array $overrides Array of setting overrides. 
  772. * @return array Possibly modified setting arguments. 
  773. */ 
  774. public function get_setting_args( $id, $overrides = array() ) { 
  775. $args = array( 
  776. 'type' => 'option',  
  777. 'capability' => 'edit_theme_options',  
  778. 'default' => array(),  
  779. ); 
  780.  
  781. if ( preg_match( $this->setting_id_patterns['sidebar_widgets'], $id, $matches ) ) { 
  782. $args['sanitize_callback'] = array( $this, 'sanitize_sidebar_widgets' ); 
  783. $args['sanitize_js_callback'] = array( $this, 'sanitize_sidebar_widgets_js_instance' ); 
  784. $args['transport'] = current_theme_supports( 'customize-selective-refresh-widgets' ) ? 'postMessage' : 'refresh'; 
  785. } elseif ( preg_match( $this->setting_id_patterns['widget_instance'], $id, $matches ) ) { 
  786. $args['sanitize_callback'] = array( $this, 'sanitize_widget_instance' ); 
  787. $args['sanitize_js_callback'] = array( $this, 'sanitize_widget_js_instance' ); 
  788. $args['transport'] = $this->is_widget_selective_refreshable( $matches['id_base'] ) ? 'postMessage' : 'refresh'; 
  789.  
  790. $args = array_merge( $args, $overrides ); 
  791.  
  792. /** 
  793. * Filters the common arguments supplied when constructing a Customizer setting. 
  794. * 
  795. * @since 3.9.0 
  796. * 
  797. * @see WP_Customize_Setting 
  798. * 
  799. * @param array $args Array of Customizer setting arguments. 
  800. * @param string $id Widget setting ID. 
  801. */ 
  802. return apply_filters( 'widget_customizer_setting_args', $args, $id ); 
  803.  
  804. /** 
  805. * Ensures sidebar widget arrays only ever contain widget IDS. 
  806. * 
  807. * Used as the 'sanitize_callback' for each $sidebars_widgets setting. 
  808. * 
  809. * @since 3.9.0 
  810. * @access public 
  811. * 
  812. * @param array $widget_ids Array of widget IDs. 
  813. * @return array Array of sanitized widget IDs. 
  814. */ 
  815. public function sanitize_sidebar_widgets( $widget_ids ) { 
  816. $widget_ids = array_map( 'strval', (array) $widget_ids ); 
  817. $sanitized_widget_ids = array(); 
  818. foreach ( $widget_ids as $widget_id ) { 
  819. $sanitized_widget_ids[] = preg_replace( '/[^a-z0-9_\-]/', '', $widget_id ); 
  820. return $sanitized_widget_ids; 
  821.  
  822. /** 
  823. * Builds up an index of all available widgets for use in Backbone models. 
  824. * 
  825. * @since 3.9.0 
  826. * @access public 
  827. * 
  828. * @global array $wp_registered_widgets 
  829. * @global array $wp_registered_widget_controls 
  830. * @staticvar array $available_widgets 
  831. * 
  832. * @see wp_list_widgets() 
  833. * 
  834. * @return array List of available widgets. 
  835. */ 
  836. public function get_available_widgets() { 
  837. static $available_widgets = array(); 
  838. if ( ! empty( $available_widgets ) ) { 
  839. return $available_widgets; 
  840.  
  841. global $wp_registered_widgets, $wp_registered_widget_controls; 
  842. require_once ABSPATH . '/wp-admin/includes/widgets.php'; // for next_widget_id_number() 
  843.  
  844. $sort = $wp_registered_widgets; 
  845. usort( $sort, array( $this, '_sort_name_callback' ) ); 
  846. $done = array(); 
  847.  
  848. foreach ( $sort as $widget ) { 
  849. if ( in_array( $widget['callback'], $done, true ) ) { // We already showed this multi-widget 
  850. continue; 
  851.  
  852. $sidebar = is_active_widget( $widget['callback'], $widget['id'], false, false ); 
  853. $done[] = $widget['callback']; 
  854.  
  855. if ( ! isset( $widget['params'][0] ) ) { 
  856. $widget['params'][0] = array(); 
  857.  
  858. $available_widget = $widget; 
  859. unset( $available_widget['callback'] ); // not serializable to JSON 
  860.  
  861. $args = array( 
  862. 'widget_id' => $widget['id'],  
  863. 'widget_name' => $widget['name'],  
  864. '_display' => 'template',  
  865. ); 
  866.  
  867. $is_disabled = false; 
  868. $is_multi_widget = ( isset( $wp_registered_widget_controls[$widget['id']]['id_base'] ) && isset( $widget['params'][0]['number'] ) ); 
  869. if ( $is_multi_widget ) { 
  870. $id_base = $wp_registered_widget_controls[$widget['id']]['id_base']; 
  871. $args['_temp_id'] = "$id_base-__i__"; 
  872. $args['_multi_num'] = next_widget_id_number( $id_base ); 
  873. $args['_add'] = 'multi'; 
  874. } else { 
  875. $args['_add'] = 'single'; 
  876.  
  877. if ( $sidebar && 'wp_inactive_widgets' !== $sidebar ) { 
  878. $is_disabled = true; 
  879. $id_base = $widget['id']; 
  880.  
  881. $list_widget_controls_args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) ); 
  882. $control_tpl = $this->get_widget_control( $list_widget_controls_args ); 
  883.  
  884. // The properties here are mapped to the Backbone Widget model. 
  885. $available_widget = array_merge( $available_widget, array( 
  886. 'temp_id' => isset( $args['_temp_id'] ) ? $args['_temp_id'] : null,  
  887. 'is_multi' => $is_multi_widget,  
  888. 'control_tpl' => $control_tpl,  
  889. 'multi_number' => ( $args['_add'] === 'multi' ) ? $args['_multi_num'] : false,  
  890. 'is_disabled' => $is_disabled,  
  891. 'id_base' => $id_base,  
  892. 'transport' => $this->is_widget_selective_refreshable( $id_base ) ? 'postMessage' : 'refresh',  
  893. 'width' => $wp_registered_widget_controls[$widget['id']]['width'],  
  894. 'height' => $wp_registered_widget_controls[$widget['id']]['height'],  
  895. 'is_wide' => $this->is_wide_widget( $widget['id'] ),  
  896. ) ); 
  897.  
  898. $available_widgets[] = $available_widget; 
  899.  
  900. return $available_widgets; 
  901.  
  902. /** 
  903. * Naturally orders available widgets by name. 
  904. * 
  905. * @since 3.9.0 
  906. * @access protected 
  907. * 
  908. * @param array $widget_a The first widget to compare. 
  909. * @param array $widget_b The second widget to compare. 
  910. * @return int Reorder position for the current widget comparison. 
  911. */ 
  912. protected function _sort_name_callback( $widget_a, $widget_b ) { 
  913. return strnatcasecmp( $widget_a['name'], $widget_b['name'] ); 
  914.  
  915. /** 
  916. * Retrieves the widget control markup. 
  917. * 
  918. * @since 3.9.0 
  919. * @access public 
  920. * 
  921. * @param array $args Widget control arguments. 
  922. * @return string Widget control form HTML markup. 
  923. */ 
  924. public function get_widget_control( $args ) { 
  925. $args[0]['before_form'] = '<div class="form">'; 
  926. $args[0]['after_form'] = '</div><!-- .form -->'; 
  927. $args[0]['before_widget_content'] = '<div class="widget-content">'; 
  928. $args[0]['after_widget_content'] = '</div><!-- .widget-content -->'; 
  929. ob_start(); 
  930. call_user_func_array( 'wp_widget_control', $args ); 
  931. $control_tpl = ob_get_clean(); 
  932. return $control_tpl; 
  933.  
  934. /** 
  935. * Retrieves the widget control markup parts. 
  936. * 
  937. * @since 4.4.0 
  938. * @access public 
  939. * 
  940. * @param array $args Widget control arguments. 
  941. * @return array { 
  942. * @type string $control Markup for widget control wrapping form. 
  943. * @type string $content The contents of the widget form itself. 
  944. * } 
  945. */ 
  946. public function get_widget_control_parts( $args ) { 
  947. $args[0]['before_widget_content'] = '<div class="widget-content">'; 
  948. $args[0]['after_widget_content'] = '</div><!-- .widget-content -->'; 
  949. $control_markup = $this->get_widget_control( $args ); 
  950.  
  951. $content_start_pos = strpos( $control_markup, $args[0]['before_widget_content'] ); 
  952. $content_end_pos = strrpos( $control_markup, $args[0]['after_widget_content'] ); 
  953.  
  954. $control = substr( $control_markup, 0, $content_start_pos + strlen( $args[0]['before_widget_content'] ) ); 
  955. $control .= substr( $control_markup, $content_end_pos ); 
  956. $content = trim( substr( 
  957. $control_markup,  
  958. $content_start_pos + strlen( $args[0]['before_widget_content'] ),  
  959. $content_end_pos - $content_start_pos - strlen( $args[0]['before_widget_content'] ) 
  960. ) ); 
  961.  
  962. return compact( 'control', 'content' ); 
  963.  
  964. /** 
  965. * Adds hooks for the Customizer preview. 
  966. * 
  967. * @since 3.9.0 
  968. * @access public 
  969. */ 
  970. public function customize_preview_init() { 
  971. add_action( 'wp_enqueue_scripts', array( $this, 'customize_preview_enqueue' ) ); 
  972. add_action( 'wp_print_styles', array( $this, 'print_preview_css' ), 1 ); 
  973. add_action( 'wp_footer', array( $this, 'export_preview_data' ), 20 ); 
  974.  
  975. /** 
  976. * Refreshes the nonce for widget updates. 
  977. * 
  978. * @since 4.2.0 
  979. * @access public 
  980. * 
  981. * @param array $nonces Array of nonces. 
  982. * @return array $nonces Array of nonces. 
  983. */ 
  984. public function refresh_nonces( $nonces ) { 
  985. $nonces['update-widget'] = wp_create_nonce( 'update-widget' ); 
  986. return $nonces; 
  987.  
  988. /** 
  989. * When previewing, ensures the proper previewing widgets are used. 
  990. * 
  991. * Because wp_get_sidebars_widgets() gets called early at {@see 'init' } (via 
  992. * wp_convert_widget_settings()) and can set global variable `$_wp_sidebars_widgets` 
  993. * to the value of `get_option( 'sidebars_widgets' )` before the Customizer preview 
  994. * filter is added, it has to be reset after the filter has been added. 
  995. * 
  996. * @since 3.9.0 
  997. * @access public 
  998. * 
  999. * @param array $sidebars_widgets List of widgets for the current sidebar. 
  1000. * @return array 
  1001. */ 
  1002. public function preview_sidebars_widgets( $sidebars_widgets ) { 
  1003. $sidebars_widgets = get_option( 'sidebars_widgets', array() ); 
  1004.  
  1005. unset( $sidebars_widgets['array_version'] ); 
  1006. return $sidebars_widgets; 
  1007.  
  1008. /** 
  1009. * Enqueues scripts for the Customizer preview. 
  1010. * 
  1011. * @since 3.9.0 
  1012. * @access public 
  1013. */ 
  1014. public function customize_preview_enqueue() { 
  1015. wp_enqueue_script( 'customize-preview-widgets' ); 
  1016.  
  1017. /** 
  1018. * Inserts default style for highlighted widget at early point so theme 
  1019. * stylesheet can override. 
  1020. * 
  1021. * @since 3.9.0 
  1022. * @access public 
  1023. */ 
  1024. public function print_preview_css() { 
  1025. ?> 
  1026. <style> 
  1027. .widget-customizer-highlighted-widget { 
  1028. outline: none; 
  1029. -webkit-box-shadow: 0 0 2px rgba(30, 140, 190, 0.8); 
  1030. box-shadow: 0 0 2px rgba(30, 140, 190, 0.8); 
  1031. position: relative; 
  1032. z-index: 1; 
  1033. </style> 
  1034. <?php 
  1035.  
  1036. /** 
  1037. * Communicates the sidebars that appeared on the page at the very end of the page,  
  1038. * and at the very end of the wp_footer,  
  1039. * 
  1040. * @since 3.9.0 
  1041. * @access public 
  1042. * 
  1043. * @global array $wp_registered_sidebars 
  1044. * @global array $wp_registered_widgets 
  1045. */ 
  1046. public function export_preview_data() { 
  1047. global $wp_registered_sidebars, $wp_registered_widgets; 
  1048.  
  1049. $switched_locale = switch_to_locale( get_user_locale() ); 
  1050. $l10n = array( 
  1051. 'widgetTooltip' => __( 'Shift-click to edit this widget.' ),  
  1052. ); 
  1053. if ( $switched_locale ) { 
  1054. restore_previous_locale(); 
  1055.  
  1056. // Prepare Customizer settings to pass to JavaScript. 
  1057. $settings = array( 
  1058. 'renderedSidebars' => array_fill_keys( array_unique( $this->rendered_sidebars ), true ),  
  1059. 'renderedWidgets' => array_fill_keys( array_keys( $this->rendered_widgets ), true ),  
  1060. 'registeredSidebars' => array_values( $wp_registered_sidebars ),  
  1061. 'registeredWidgets' => $wp_registered_widgets,  
  1062. 'l10n' => $l10n,  
  1063. 'selectiveRefreshableWidgets' => $this->get_selective_refreshable_widgets(),  
  1064. ); 
  1065. foreach ( $settings['registeredWidgets'] as &$registered_widget ) { 
  1066. unset( $registered_widget['callback'] ); // may not be JSON-serializeable 
  1067.  
  1068. ?> 
  1069. <script type="text/javascript"> 
  1070. var _wpWidgetCustomizerPreviewSettings = <?php echo wp_json_encode( $settings ); ?>; 
  1071. </script> 
  1072. <?php 
  1073.  
  1074. /** 
  1075. * Tracks the widgets that were rendered. 
  1076. * 
  1077. * @since 3.9.0 
  1078. * @access public 
  1079. * 
  1080. * @param array $widget Rendered widget to tally. 
  1081. */ 
  1082. public function tally_rendered_widgets( $widget ) { 
  1083. $this->rendered_widgets[ $widget['id'] ] = true; 
  1084.  
  1085. /** 
  1086. * Determine if a widget is rendered on the page. 
  1087. * 
  1088. * @since 4.0.0 
  1089. * @access public 
  1090. * 
  1091. * @param string $widget_id Widget ID to check. 
  1092. * @return bool Whether the widget is rendered. 
  1093. */ 
  1094. public function is_widget_rendered( $widget_id ) { 
  1095. return in_array( $widget_id, $this->rendered_widgets ); 
  1096.  
  1097. /** 
  1098. * Determines if a sidebar is rendered on the page. 
  1099. * 
  1100. * @since 4.0.0 
  1101. * @access public 
  1102. * 
  1103. * @param string $sidebar_id Sidebar ID to check. 
  1104. * @return bool Whether the sidebar is rendered. 
  1105. */ 
  1106. public function is_sidebar_rendered( $sidebar_id ) { 
  1107. return in_array( $sidebar_id, $this->rendered_sidebars ); 
  1108.  
  1109. /** 
  1110. * Tallies the sidebars rendered via is_active_sidebar(). 
  1111. * 
  1112. * Keep track of the times that is_active_sidebar() is called in the template,  
  1113. * and assume that this means that the sidebar would be rendered on the template 
  1114. * if there were widgets populating it. 
  1115. * 
  1116. * @since 3.9.0 
  1117. * @access public 
  1118. * 
  1119. * @param bool $is_active Whether the sidebar is active. 
  1120. * @param string $sidebar_id Sidebar ID. 
  1121. * @return bool Whether the sidebar is active. 
  1122. */ 
  1123. public function tally_sidebars_via_is_active_sidebar_calls( $is_active, $sidebar_id ) { 
  1124. if ( is_registered_sidebar( $sidebar_id ) ) { 
  1125. $this->rendered_sidebars[] = $sidebar_id; 
  1126. /** 
  1127. * We may need to force this to true, and also force-true the value 
  1128. * for 'dynamic_sidebar_has_widgets' if we want to ensure that there 
  1129. * is an area to drop widgets into, if the sidebar is empty. 
  1130. */ 
  1131. return $is_active; 
  1132.  
  1133. /** 
  1134. * Tallies the sidebars rendered via dynamic_sidebar(). 
  1135. * 
  1136. * Keep track of the times that dynamic_sidebar() is called in the template,  
  1137. * and assume this means the sidebar would be rendered on the template if 
  1138. * there were widgets populating it. 
  1139. * 
  1140. * @since 3.9.0 
  1141. * @access public 
  1142. * 
  1143. * @param bool $has_widgets Whether the current sidebar has widgets. 
  1144. * @param string $sidebar_id Sidebar ID. 
  1145. * @return bool Whether the current sidebar has widgets. 
  1146. */ 
  1147. public function tally_sidebars_via_dynamic_sidebar_calls( $has_widgets, $sidebar_id ) { 
  1148. if ( is_registered_sidebar( $sidebar_id ) ) { 
  1149. $this->rendered_sidebars[] = $sidebar_id; 
  1150.  
  1151. /** 
  1152. * We may need to force this to true, and also force-true the value 
  1153. * for 'is_active_sidebar' if we want to ensure there is an area to 
  1154. * drop widgets into, if the sidebar is empty. 
  1155. */ 
  1156. return $has_widgets; 
  1157.  
  1158. /** 
  1159. * Retrieves MAC for a serialized widget instance string. 
  1160. * 
  1161. * Allows values posted back from JS to be rejected if any tampering of the 
  1162. * data has occurred. 
  1163. * 
  1164. * @since 3.9.0 
  1165. * @access protected 
  1166. * 
  1167. * @param string $serialized_instance Widget instance. 
  1168. * @return string MAC for serialized widget instance. 
  1169. */ 
  1170. protected function get_instance_hash_key( $serialized_instance ) { 
  1171. return wp_hash( $serialized_instance ); 
  1172.  
  1173. /** 
  1174. * Sanitizes a widget instance. 
  1175. * 
  1176. * Unserialize the JS-instance for storing in the options. It's important that this filter 
  1177. * only get applied to an instance *once*. 
  1178. * 
  1179. * @since 3.9.0 
  1180. * @access public 
  1181. * 
  1182. * @param array $value Widget instance to sanitize. 
  1183. * @return array|void Sanitized widget instance. 
  1184. */ 
  1185. public function sanitize_widget_instance( $value ) { 
  1186. if ( $value === array() ) { 
  1187. return $value; 
  1188.  
  1189. if ( empty( $value['is_widget_customizer_js_value'] ) 
  1190. || empty( $value['instance_hash_key'] ) 
  1191. || empty( $value['encoded_serialized_instance'] ) ) 
  1192. return; 
  1193.  
  1194. $decoded = base64_decode( $value['encoded_serialized_instance'], true ); 
  1195. if ( false === $decoded ) { 
  1196. return; 
  1197.  
  1198. if ( ! hash_equals( $this->get_instance_hash_key( $decoded ), $value['instance_hash_key'] ) ) { 
  1199. return; 
  1200.  
  1201. $instance = unserialize( $decoded ); 
  1202. if ( false === $instance ) { 
  1203. return; 
  1204.  
  1205. return $instance; 
  1206.  
  1207. /** 
  1208. * Converts a widget instance into JSON-representable format. 
  1209. * 
  1210. * @since 3.9.0 
  1211. * @access public 
  1212. * 
  1213. * @param array $value Widget instance to convert to JSON. 
  1214. * @return array JSON-converted widget instance. 
  1215. */ 
  1216. public function sanitize_widget_js_instance( $value ) { 
  1217. if ( empty( $value['is_widget_customizer_js_value'] ) ) { 
  1218. $serialized = serialize( $value ); 
  1219.  
  1220. $value = array( 
  1221. 'encoded_serialized_instance' => base64_encode( $serialized ),  
  1222. 'title' => empty( $value['title'] ) ? '' : $value['title'],  
  1223. 'is_widget_customizer_js_value' => true,  
  1224. 'instance_hash_key' => $this->get_instance_hash_key( $serialized ),  
  1225. ); 
  1226. return $value; 
  1227.  
  1228. /** 
  1229. * Strips out widget IDs for widgets which are no longer registered. 
  1230. * 
  1231. * One example where this might happen is when a plugin orphans a widget 
  1232. * in a sidebar upon deactivation. 
  1233. * 
  1234. * @since 3.9.0 
  1235. * @access public 
  1236. * 
  1237. * @global array $wp_registered_widgets 
  1238. * 
  1239. * @param array $widget_ids List of widget IDs. 
  1240. * @return array Parsed list of widget IDs. 
  1241. */ 
  1242. public function sanitize_sidebar_widgets_js_instance( $widget_ids ) { 
  1243. global $wp_registered_widgets; 
  1244. $widget_ids = array_values( array_intersect( $widget_ids, array_keys( $wp_registered_widgets ) ) ); 
  1245. return $widget_ids; 
  1246.  
  1247. /** 
  1248. * Finds and invokes the widget update and control callbacks. 
  1249. * 
  1250. * Requires that `$_POST` be populated with the instance data. 
  1251. * 
  1252. * @since 3.9.0 
  1253. * @access public 
  1254. * 
  1255. * @global array $wp_registered_widget_updates 
  1256. * @global array $wp_registered_widget_controls 
  1257. * 
  1258. * @param string $widget_id Widget ID. 
  1259. * @return WP_Error|array Array containing the updated widget information. 
  1260. * A WP_Error object, otherwise. 
  1261. */ 
  1262. public function call_widget_update( $widget_id ) { 
  1263. global $wp_registered_widget_updates, $wp_registered_widget_controls; 
  1264.  
  1265. $setting_id = $this->get_setting_id( $widget_id ); 
  1266.  
  1267. /** 
  1268. * Make sure that other setting changes have previewed since this widget 
  1269. * may depend on them (e.g. Menus being present for Custom Menu widget). 
  1270. */ 
  1271. if ( ! did_action( 'customize_preview_init' ) ) { 
  1272. foreach ( $this->manager->settings() as $setting ) { 
  1273. if ( $setting->id !== $setting_id ) { 
  1274. $setting->preview(); 
  1275.  
  1276. $this->start_capturing_option_updates(); 
  1277. $parsed_id = $this->parse_widget_id( $widget_id ); 
  1278. $option_name = 'widget_' . $parsed_id['id_base']; 
  1279.  
  1280. /** 
  1281. * If a previously-sanitized instance is provided, populate the input vars 
  1282. * with its values so that the widget update callback will read this instance 
  1283. */ 
  1284. $added_input_vars = array(); 
  1285. if ( ! empty( $_POST['sanitized_widget_setting'] ) ) { 
  1286. $sanitized_widget_setting = json_decode( $this->get_post_value( 'sanitized_widget_setting' ), true ); 
  1287. if ( false === $sanitized_widget_setting ) { 
  1288. $this->stop_capturing_option_updates(); 
  1289. return new WP_Error( 'widget_setting_malformed' ); 
  1290.  
  1291. $instance = $this->sanitize_widget_instance( $sanitized_widget_setting ); 
  1292. if ( is_null( $instance ) ) { 
  1293. $this->stop_capturing_option_updates(); 
  1294. return new WP_Error( 'widget_setting_unsanitized' ); 
  1295.  
  1296. if ( ! is_null( $parsed_id['number'] ) ) { 
  1297. $value = array(); 
  1298. $value[$parsed_id['number']] = $instance; 
  1299. $key = 'widget-' . $parsed_id['id_base']; 
  1300. $_REQUEST[$key] = $_POST[$key] = wp_slash( $value ); 
  1301. $added_input_vars[] = $key; 
  1302. } else { 
  1303. foreach ( $instance as $key => $value ) { 
  1304. $_REQUEST[$key] = $_POST[$key] = wp_slash( $value ); 
  1305. $added_input_vars[] = $key; 
  1306.  
  1307. // Invoke the widget update callback. 
  1308. foreach ( (array) $wp_registered_widget_updates as $name => $control ) { 
  1309. if ( $name === $parsed_id['id_base'] && is_callable( $control['callback'] ) ) { 
  1310. ob_start(); 
  1311. call_user_func_array( $control['callback'], $control['params'] ); 
  1312. ob_end_clean(); 
  1313. break; 
  1314.  
  1315. // Clean up any input vars that were manually added 
  1316. foreach ( $added_input_vars as $key ) { 
  1317. unset( $_POST[ $key ] ); 
  1318. unset( $_REQUEST[ $key ] ); 
  1319.  
  1320. // Make sure the expected option was updated. 
  1321. if ( 0 !== $this->count_captured_options() ) { 
  1322. if ( $this->count_captured_options() > 1 ) { 
  1323. $this->stop_capturing_option_updates(); 
  1324. return new WP_Error( 'widget_setting_too_many_options' ); 
  1325.  
  1326. $updated_option_name = key( $this->get_captured_options() ); 
  1327. if ( $updated_option_name !== $option_name ) { 
  1328. $this->stop_capturing_option_updates(); 
  1329. return new WP_Error( 'widget_setting_unexpected_option' ); 
  1330.  
  1331. // Obtain the widget instance. 
  1332. $option = $this->get_captured_option( $option_name ); 
  1333. if ( null !== $parsed_id['number'] ) { 
  1334. $instance = $option[ $parsed_id['number'] ]; 
  1335. } else { 
  1336. $instance = $option; 
  1337.  
  1338. /** 
  1339. * Override the incoming $_POST['customized'] for a newly-created widget's 
  1340. * setting with the new $instance so that the preview filter currently 
  1341. * in place from WP_Customize_Setting::preview() will use this value 
  1342. * instead of the default widget instance value (an empty array). 
  1343. */ 
  1344. $this->manager->set_post_value( $setting_id, $this->sanitize_widget_js_instance( $instance ) ); 
  1345.  
  1346. // Obtain the widget control with the updated instance in place. 
  1347. ob_start(); 
  1348. $form = $wp_registered_widget_controls[ $widget_id ]; 
  1349. if ( $form ) { 
  1350. call_user_func_array( $form['callback'], $form['params'] ); 
  1351. $form = ob_get_clean(); 
  1352.  
  1353. $this->stop_capturing_option_updates(); 
  1354.  
  1355. return compact( 'instance', 'form' ); 
  1356.  
  1357. /** 
  1358. * Updates widget settings asynchronously. 
  1359. * 
  1360. * Allows the Customizer to update a widget using its form, but return the new 
  1361. * instance info via Ajax instead of saving it to the options table. 
  1362. * 
  1363. * Most code here copied from wp_ajax_save_widget(). 
  1364. * 
  1365. * @since 3.9.0 
  1366. * @access public 
  1367. * 
  1368. * @see wp_ajax_save_widget() 
  1369. */ 
  1370. public function wp_ajax_update_widget() { 
  1371.  
  1372. if ( ! is_user_logged_in() ) { 
  1373. wp_die( 0 ); 
  1374.  
  1375. check_ajax_referer( 'update-widget', 'nonce' ); 
  1376.  
  1377. if ( ! current_user_can( 'edit_theme_options' ) ) { 
  1378. wp_die( -1 ); 
  1379.  
  1380. if ( empty( $_POST['widget-id'] ) ) { 
  1381. wp_send_json_error( 'missing_widget-id' ); 
  1382.  
  1383. /** This action is documented in wp-admin/includes/ajax-actions.php */ 
  1384. do_action( 'load-widgets.php' ); 
  1385.  
  1386. /** This action is documented in wp-admin/includes/ajax-actions.php */ 
  1387. do_action( 'widgets.php' ); 
  1388.  
  1389. /** This action is documented in wp-admin/widgets.php */ 
  1390. do_action( 'sidebar_admin_setup' ); 
  1391.  
  1392. $widget_id = $this->get_post_value( 'widget-id' ); 
  1393. $parsed_id = $this->parse_widget_id( $widget_id ); 
  1394. $id_base = $parsed_id['id_base']; 
  1395.  
  1396. $is_updating_widget_template = ( 
  1397. isset( $_POST[ 'widget-' . $id_base ] ) 
  1398. && 
  1399. is_array( $_POST[ 'widget-' . $id_base ] ) 
  1400. && 
  1401. preg_match( '/__i__|%i%/', key( $_POST[ 'widget-' . $id_base ] ) ) 
  1402. ); 
  1403. if ( $is_updating_widget_template ) { 
  1404. wp_send_json_error( 'template_widget_not_updatable' ); 
  1405.  
  1406. $updated_widget = $this->call_widget_update( $widget_id ); // => {instance, form} 
  1407. if ( is_wp_error( $updated_widget ) ) { 
  1408. wp_send_json_error( $updated_widget->get_error_code() ); 
  1409.  
  1410. $form = $updated_widget['form']; 
  1411. $instance = $this->sanitize_widget_js_instance( $updated_widget['instance'] ); 
  1412.  
  1413. wp_send_json_success( compact( 'form', 'instance' ) ); 
  1414.  
  1415. /** 
  1416. * Selective Refresh Methods 
  1417. */ 
  1418.  
  1419. /** 
  1420. * Filters arguments for dynamic widget partials. 
  1421. * 
  1422. * @since 4.5.0 
  1423. * @access public 
  1424. * 
  1425. * @param array|false $partial_args Partial arguments. 
  1426. * @param string $partial_id Partial ID. 
  1427. * @return array (Maybe) modified partial arguments. 
  1428. */ 
  1429. public function customize_dynamic_partial_args( $partial_args, $partial_id ) { 
  1430. if ( ! current_theme_supports( 'customize-selective-refresh-widgets' ) ) { 
  1431. return $partial_args; 
  1432.  
  1433. if ( preg_match( '/^widget\[(?P<widget_id>.+)\]$/', $partial_id, $matches ) ) { 
  1434. if ( false === $partial_args ) { 
  1435. $partial_args = array(); 
  1436. $partial_args = array_merge( 
  1437. $partial_args,  
  1438. array( 
  1439. 'type' => 'widget',  
  1440. 'render_callback' => array( $this, 'render_widget_partial' ),  
  1441. 'container_inclusive' => true,  
  1442. 'settings' => array( $this->get_setting_id( $matches['widget_id'] ) ),  
  1443. 'capability' => 'edit_theme_options',  
  1444. ); 
  1445.  
  1446. return $partial_args; 
  1447.  
  1448. /** 
  1449. * Adds hooks for selective refresh. 
  1450. * 
  1451. * @since 4.5.0 
  1452. * @access public 
  1453. */ 
  1454. public function selective_refresh_init() { 
  1455. if ( ! current_theme_supports( 'customize-selective-refresh-widgets' ) ) { 
  1456. return; 
  1457. add_filter( 'dynamic_sidebar_params', array( $this, 'filter_dynamic_sidebar_params' ) ); 
  1458. add_filter( 'wp_kses_allowed_html', array( $this, 'filter_wp_kses_allowed_data_attributes' ) ); 
  1459. add_action( 'dynamic_sidebar_before', array( $this, 'start_dynamic_sidebar' ) ); 
  1460. add_action( 'dynamic_sidebar_after', array( $this, 'end_dynamic_sidebar' ) ); 
  1461.  
  1462. /** 
  1463. * Inject selective refresh data attributes into widget container elements. 
  1464. * 
  1465. * @param array $params { 
  1466. * Dynamic sidebar params. 
  1467. * 
  1468. * @type array $args Sidebar args. 
  1469. * @type array $widget_args Widget args. 
  1470. * } 
  1471. * @see WP_Customize_Nav_Menus_Partial_Refresh::filter_wp_nav_menu_args() 
  1472. * 
  1473. * @return array Params. 
  1474. */ 
  1475. public function filter_dynamic_sidebar_params( $params ) { 
  1476. $sidebar_args = array_merge( 
  1477. array( 
  1478. 'before_widget' => '',  
  1479. 'after_widget' => '',  
  1480. ),  
  1481. $params[0] 
  1482. ); 
  1483.  
  1484. // Skip widgets not in a registered sidebar or ones which lack a proper wrapper element to attach the data-* attributes to. 
  1485. $matches = array(); 
  1486. $is_valid = ( 
  1487. isset( $sidebar_args['id'] ) 
  1488. && 
  1489. is_registered_sidebar( $sidebar_args['id'] ) 
  1490. && 
  1491. ( isset( $this->current_dynamic_sidebar_id_stack[0] ) && $this->current_dynamic_sidebar_id_stack[0] === $sidebar_args['id'] ) 
  1492. && 
  1493. preg_match( '#^<(?P<tag_name>\w+)#', $sidebar_args['before_widget'], $matches ) 
  1494. ); 
  1495. if ( ! $is_valid ) { 
  1496. return $params; 
  1497. $this->before_widget_tags_seen[ $matches['tag_name'] ] = true; 
  1498.  
  1499. $context = array( 
  1500. 'sidebar_id' => $sidebar_args['id'],  
  1501. ); 
  1502. if ( isset( $this->context_sidebar_instance_number ) ) { 
  1503. $context['sidebar_instance_number'] = $this->context_sidebar_instance_number; 
  1504. } else if ( isset( $sidebar_args['id'] ) && isset( $this->sidebar_instance_count[ $sidebar_args['id'] ] ) ) { 
  1505. $context['sidebar_instance_number'] = $this->sidebar_instance_count[ $sidebar_args['id'] ]; 
  1506.  
  1507. $attributes = sprintf( ' data-customize-partial-id="%s"', esc_attr( 'widget[' . $sidebar_args['widget_id'] . ']' ) ); 
  1508. $attributes .= ' data-customize-partial-type="widget"'; 
  1509. $attributes .= sprintf( ' data-customize-partial-placement-context="%s"', esc_attr( wp_json_encode( $context ) ) ); 
  1510. $attributes .= sprintf( ' data-customize-widget-id="%s"', esc_attr( $sidebar_args['widget_id'] ) ); 
  1511. $sidebar_args['before_widget'] = preg_replace( '#^(<\w+)#', '$1 ' . $attributes, $sidebar_args['before_widget'] ); 
  1512.  
  1513. $params[0] = $sidebar_args; 
  1514. return $params; 
  1515.  
  1516. /** 
  1517. * List of the tag names seen for before_widget strings. 
  1518. * 
  1519. * This is used in the {@see 'filter_wp_kses_allowed_html'} filter to ensure that the 
  1520. * data-* attributes can be whitelisted. 
  1521. * 
  1522. * @since 4.5.0 
  1523. * @access protected 
  1524. * @var array 
  1525. */ 
  1526. protected $before_widget_tags_seen = array(); 
  1527.  
  1528. /** 
  1529. * Ensures the HTML data-* attributes for selective refresh are allowed by kses. 
  1530. * 
  1531. * This is needed in case the `$before_widget` is run through wp_kses() when printed. 
  1532. * 
  1533. * @since 4.5.0 
  1534. * @access public 
  1535. * 
  1536. * @param array $allowed_html Allowed HTML. 
  1537. * @return array (Maybe) modified allowed HTML. 
  1538. */ 
  1539. public function filter_wp_kses_allowed_data_attributes( $allowed_html ) { 
  1540. foreach ( array_keys( $this->before_widget_tags_seen ) as $tag_name ) { 
  1541. if ( ! isset( $allowed_html[ $tag_name ] ) ) { 
  1542. $allowed_html[ $tag_name ] = array(); 
  1543. $allowed_html[ $tag_name ] = array_merge( 
  1544. $allowed_html[ $tag_name ],  
  1545. array_fill_keys( array( 
  1546. 'data-customize-partial-id',  
  1547. 'data-customize-partial-type',  
  1548. 'data-customize-partial-placement-context',  
  1549. 'data-customize-partial-widget-id',  
  1550. 'data-customize-partial-options',  
  1551. ), true ) 
  1552. ); 
  1553. return $allowed_html; 
  1554.  
  1555. /** 
  1556. * Keep track of the number of times that dynamic_sidebar() was called for a given sidebar index. 
  1557. * 
  1558. * This helps facilitate the uncommon scenario where a single sidebar is rendered multiple times on a template. 
  1559. * 
  1560. * @since 4.5.0 
  1561. * @access protected 
  1562. * @var array 
  1563. */ 
  1564. protected $sidebar_instance_count = array(); 
  1565.  
  1566. /** 
  1567. * The current request's sidebar_instance_number context. 
  1568. * 
  1569. * @since 4.5.0 
  1570. * @access protected 
  1571. * @var int 
  1572. */ 
  1573. protected $context_sidebar_instance_number; 
  1574.  
  1575. /** 
  1576. * Current sidebar ID being rendered. 
  1577. * 
  1578. * @since 4.5.0 
  1579. * @access protected 
  1580. * @var array 
  1581. */ 
  1582. protected $current_dynamic_sidebar_id_stack = array(); 
  1583.  
  1584. /** 
  1585. * Begins keeping track of the current sidebar being rendered. 
  1586. * 
  1587. * Insert marker before widgets are rendered in a dynamic sidebar. 
  1588. * 
  1589. * @since 4.5.0 
  1590. * @access public 
  1591. * 
  1592. * @param int|string $index Index, name, or ID of the dynamic sidebar. 
  1593. */ 
  1594. public function start_dynamic_sidebar( $index ) { 
  1595. array_unshift( $this->current_dynamic_sidebar_id_stack, $index ); 
  1596. if ( ! isset( $this->sidebar_instance_count[ $index ] ) ) { 
  1597. $this->sidebar_instance_count[ $index ] = 0; 
  1598. $this->sidebar_instance_count[ $index ] += 1; 
  1599. if ( ! $this->manager->selective_refresh->is_render_partials_request() ) { 
  1600. printf( "\n<!--dynamic_sidebar_before:%s:%d-->\n", esc_html( $index ), intval( $this->sidebar_instance_count[ $index ] ) ); 
  1601.  
  1602. /** 
  1603. * Finishes keeping track of the current sidebar being rendered. 
  1604. * 
  1605. * Inserts a marker after widgets are rendered in a dynamic sidebar. 
  1606. * 
  1607. * @since 4.5.0 
  1608. * @access public 
  1609. * 
  1610. * @param int|string $index Index, name, or ID of the dynamic sidebar. 
  1611. */ 
  1612. public function end_dynamic_sidebar( $index ) { 
  1613. array_shift( $this->current_dynamic_sidebar_id_stack ); 
  1614. if ( ! $this->manager->selective_refresh->is_render_partials_request() ) { 
  1615. printf( "\n<!--dynamic_sidebar_after:%s:%d-->\n", esc_html( $index ), intval( $this->sidebar_instance_count[ $index ] ) ); 
  1616.  
  1617. /** 
  1618. * Current sidebar being rendered. 
  1619. * 
  1620. * @since 4.5.0 
  1621. * @access protected 
  1622. * @var string 
  1623. */ 
  1624. protected $rendering_widget_id; 
  1625.  
  1626. /** 
  1627. * Current widget being rendered. 
  1628. * 
  1629. * @since 4.5.0 
  1630. * @access protected 
  1631. * @var string 
  1632. */ 
  1633. protected $rendering_sidebar_id; 
  1634.  
  1635. /** 
  1636. * Filters sidebars_widgets to ensure the currently-rendered widget is the only widget in the current sidebar. 
  1637. * 
  1638. * @since 4.5.0 
  1639. * @access protected 
  1640. * 
  1641. * @param array $sidebars_widgets Sidebars widgets. 
  1642. * @return array Filtered sidebars widgets. 
  1643. */ 
  1644. public function filter_sidebars_widgets_for_rendering_widget( $sidebars_widgets ) { 
  1645. $sidebars_widgets[ $this->rendering_sidebar_id ] = array( $this->rendering_widget_id ); 
  1646. return $sidebars_widgets; 
  1647.  
  1648. /** 
  1649. * Renders a specific widget using the supplied sidebar arguments. 
  1650. * 
  1651. * @since 4.5.0 
  1652. * @access public 
  1653. * 
  1654. * @see dynamic_sidebar() 
  1655. * 
  1656. * @param WP_Customize_Partial $partial Partial. 
  1657. * @param array $context { 
  1658. * Sidebar args supplied as container context. 
  1659. * 
  1660. * @type string $sidebar_id ID for sidebar for widget to render into. 
  1661. * @type int $sidebar_instance_number Disambiguating instance number. 
  1662. * } 
  1663. * @return string|false 
  1664. */ 
  1665. public function render_widget_partial( $partial, $context ) { 
  1666. $id_data = $partial->id_data(); 
  1667. $widget_id = array_shift( $id_data['keys'] ); 
  1668.  
  1669. if ( ! is_array( $context ) 
  1670. || empty( $context['sidebar_id'] ) 
  1671. || ! is_registered_sidebar( $context['sidebar_id'] ) 
  1672. ) { 
  1673. return false; 
  1674.  
  1675. $this->rendering_sidebar_id = $context['sidebar_id']; 
  1676.  
  1677. if ( isset( $context['sidebar_instance_number'] ) ) { 
  1678. $this->context_sidebar_instance_number = intval( $context['sidebar_instance_number'] ); 
  1679.  
  1680. // Filter sidebars_widgets so that only the queried widget is in the sidebar. 
  1681. $this->rendering_widget_id = $widget_id; 
  1682.  
  1683. $filter_callback = array( $this, 'filter_sidebars_widgets_for_rendering_widget' ); 
  1684. add_filter( 'sidebars_widgets', $filter_callback, 1000 ); 
  1685.  
  1686. // Render the widget. 
  1687. ob_start(); 
  1688. dynamic_sidebar( $this->rendering_sidebar_id = $context['sidebar_id'] ); 
  1689. $container = ob_get_clean(); 
  1690.  
  1691. // Reset variables for next partial render. 
  1692. remove_filter( 'sidebars_widgets', $filter_callback, 1000 ); 
  1693.  
  1694. $this->context_sidebar_instance_number = null; 
  1695. $this->rendering_sidebar_id = null; 
  1696. $this->rendering_widget_id = null; 
  1697.  
  1698. return $container; 
  1699.  
  1700. // 
  1701. // Option Update Capturing 
  1702. // 
  1703.   
  1704. /** 
  1705. * List of captured widget option updates. 
  1706. * 
  1707. * @since 3.9.0 
  1708. * @access protected 
  1709. * @var array $_captured_options Values updated while option capture is happening. 
  1710. */ 
  1711. protected $_captured_options = array(); 
  1712.  
  1713. /** 
  1714. * Whether option capture is currently happening. 
  1715. * 
  1716. * @since 3.9.0 
  1717. * @access protected 
  1718. * @var bool $_is_current Whether option capture is currently happening or not. 
  1719. */ 
  1720. protected $_is_capturing_option_updates = false; 
  1721.  
  1722. /** 
  1723. * Determines whether the captured option update should be ignored. 
  1724. * 
  1725. * @since 3.9.0 
  1726. * @access protected 
  1727. * 
  1728. * @param string $option_name Option name. 
  1729. * @return bool Whether the option capture is ignored. 
  1730. */ 
  1731. protected function is_option_capture_ignored( $option_name ) { 
  1732. return ( 0 === strpos( $option_name, '_transient_' ) ); 
  1733.  
  1734. /** 
  1735. * Retrieves captured widget option updates. 
  1736. * 
  1737. * @since 3.9.0 
  1738. * @access protected 
  1739. * 
  1740. * @return array Array of captured options. 
  1741. */ 
  1742. protected function get_captured_options() { 
  1743. return $this->_captured_options; 
  1744.  
  1745. /** 
  1746. * Retrieves the option that was captured from being saved. 
  1747. * 
  1748. * @since 4.2.0 
  1749. * @access protected 
  1750. * 
  1751. * @param string $option_name Option name. 
  1752. * @param mixed $default Optional. Default value to return if the option does not exist. Default false. 
  1753. * @return mixed Value set for the option. 
  1754. */ 
  1755. protected function get_captured_option( $option_name, $default = false ) { 
  1756. if ( array_key_exists( $option_name, $this->_captured_options ) ) { 
  1757. $value = $this->_captured_options[ $option_name ]; 
  1758. } else { 
  1759. $value = $default; 
  1760. return $value; 
  1761.  
  1762. /** 
  1763. * Retrieves the number of captured widget option updates. 
  1764. * 
  1765. * @since 3.9.0 
  1766. * @access protected 
  1767. * 
  1768. * @return int Number of updated options. 
  1769. */ 
  1770. protected function count_captured_options() { 
  1771. return count( $this->_captured_options ); 
  1772.  
  1773. /** 
  1774. * Begins keeping track of changes to widget options, caching new values. 
  1775. * 
  1776. * @since 3.9.0 
  1777. * @access protected 
  1778. */ 
  1779. protected function start_capturing_option_updates() { 
  1780. if ( $this->_is_capturing_option_updates ) { 
  1781. return; 
  1782.  
  1783. $this->_is_capturing_option_updates = true; 
  1784.  
  1785. add_filter( 'pre_update_option', array( $this, 'capture_filter_pre_update_option' ), 10, 3 ); 
  1786.  
  1787. /** 
  1788. * Pre-filters captured option values before updating. 
  1789. * 
  1790. * @since 3.9.0 
  1791. * @access public 
  1792. * 
  1793. * @param mixed $new_value The new option value. 
  1794. * @param string $option_name Name of the option. 
  1795. * @param mixed $old_value The old option value. 
  1796. * @return mixed Filtered option value. 
  1797. */ 
  1798. public function capture_filter_pre_update_option( $new_value, $option_name, $old_value ) { 
  1799. if ( $this->is_option_capture_ignored( $option_name ) ) { 
  1800. return; 
  1801.  
  1802. if ( ! isset( $this->_captured_options[ $option_name ] ) ) { 
  1803. add_filter( "pre_option_{$option_name}", array( $this, 'capture_filter_pre_get_option' ) ); 
  1804.  
  1805. $this->_captured_options[ $option_name ] = $new_value; 
  1806.  
  1807. return $old_value; 
  1808.  
  1809. /** 
  1810. * Pre-filters captured option values before retrieving. 
  1811. * 
  1812. * @since 3.9.0 
  1813. * @access public 
  1814. * 
  1815. * @param mixed $value Value to return instead of the option value. 
  1816. * @return mixed Filtered option value. 
  1817. */ 
  1818. public function capture_filter_pre_get_option( $value ) { 
  1819. $option_name = preg_replace( '/^pre_option_/', '', current_filter() ); 
  1820.  
  1821. if ( isset( $this->_captured_options[ $option_name ] ) ) { 
  1822. $value = $this->_captured_options[ $option_name ]; 
  1823.  
  1824. /** This filter is documented in wp-includes/option.php */ 
  1825. $value = apply_filters( 'option_' . $option_name, $value ); 
  1826.  
  1827. return $value; 
  1828.  
  1829. /** 
  1830. * Undoes any changes to the options since options capture began. 
  1831. * 
  1832. * @since 3.9.0 
  1833. * @access protected 
  1834. */ 
  1835. protected function stop_capturing_option_updates() { 
  1836. if ( ! $this->_is_capturing_option_updates ) { 
  1837. return; 
  1838.  
  1839. remove_filter( 'pre_update_option', array( $this, 'capture_filter_pre_update_option' ), 10 ); 
  1840.  
  1841. foreach ( array_keys( $this->_captured_options ) as $option_name ) { 
  1842. remove_filter( "pre_option_{$option_name}", array( $this, 'capture_filter_pre_get_option' ) ); 
  1843.  
  1844. $this->_captured_options = array(); 
  1845. $this->_is_capturing_option_updates = false; 
  1846.  
  1847. /** 
  1848. * {@internal Missing Summary} 
  1849. * 
  1850. * See the {@see 'customize_dynamic_setting_args'} filter. 
  1851. * 
  1852. * @since 3.9.0 
  1853. * @deprecated 4.2.0 Deprecated in favor of the {@see 'customize_dynamic_setting_args'} filter. 
  1854. */ 
  1855. public function setup_widget_addition_previews() { 
  1856. _deprecated_function( __METHOD__, '4.2.0' ); 
  1857.  
  1858. /** 
  1859. * {@internal Missing Summary} 
  1860. * 
  1861. * See the {@see 'customize_dynamic_setting_args'} filter. 
  1862. * 
  1863. * @since 3.9.0 
  1864. * @deprecated 4.2.0 Deprecated in favor of the {@see 'customize_dynamic_setting_args'} filter. 
  1865. */ 
  1866. public function prepreview_added_sidebars_widgets() { 
  1867. _deprecated_function( __METHOD__, '4.2.0' ); 
  1868.  
  1869. /** 
  1870. * {@internal Missing Summary} 
  1871. * 
  1872. * See the {@see 'customize_dynamic_setting_args'} filter. 
  1873. * 
  1874. * @since 3.9.0 
  1875. * @deprecated 4.2.0 Deprecated in favor of the {@see 'customize_dynamic_setting_args'} filter. 
  1876. */ 
  1877. public function prepreview_added_widget_instance() { 
  1878. _deprecated_function( __METHOD__, '4.2.0' ); 
  1879.  
  1880. /** 
  1881. * {@internal Missing Summary} 
  1882. * 
  1883. * See the {@see 'customize_dynamic_setting_args'} filter. 
  1884. * 
  1885. * @since 3.9.0 
  1886. * @deprecated 4.2.0 Deprecated in favor of the {@see 'customize_dynamic_setting_args'} filter. 
  1887. */ 
  1888. public function remove_prepreview_filters() { 
  1889. _deprecated_function( __METHOD__, '4.2.0' ); 
.