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