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