WP_Customize_Widgets

Customize Widgets class.

Defined (1)

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

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