/** * Test WP_Customize_Partial::id_data(). * * @see WP_Customize_Partial::id_data() */ function test_id_data() { $partial = new WP_Customize_Partial($this->selective_refresh, 'foo'); $id_data = $partial->id_data(); $this->assertEquals('foo', $id_data['base']); $this->assertEquals(array(), $id_data['keys']); $partial = new WP_Customize_Partial($this->selective_refresh, 'bar[baz][quux]'); $id_data = $partial->id_data(); $this->assertEquals('bar', $id_data['base']); $this->assertEquals(array('baz', 'quux'), $id_data['keys']); }
/** * Renders a specific widget using the supplied sidebar arguments. * * @since 4.5.0 * @access public * * @see dynamic_sidebar() * * @param WP_Customize_Partial $partial Partial. * @param array $context { * Sidebar args supplied as container context. * * @type string $sidebar_id ID for sidebar for widget to render into. * @type int $sidebar_instance_number Disambiguating instance number. * } * @return string|false */ public function render_widget_partial($partial, $context) { $id_data = $partial->id_data(); $widget_id = array_shift($id_data['keys']); if (!is_array($context) || empty($context['sidebar_id']) || !is_registered_sidebar($context['sidebar_id'])) { return false; } $this->rendering_sidebar_id = $context['sidebar_id']; if (isset($context['sidebar_instance_number'])) { $this->context_sidebar_instance_number = intval($context['sidebar_instance_number']); } // Filter sidebars_widgets so that only the queried widget is in the sidebar. $this->rendering_widget_id = $widget_id; $filter_callback = array($this, 'filter_sidebars_widgets_for_rendering_widget'); add_filter('sidebars_widgets', $filter_callback, 1000); // Render the widget. ob_start(); dynamic_sidebar($this->rendering_sidebar_id = $context['sidebar_id']); $container = ob_get_clean(); // Reset variables for next partial render. remove_filter('sidebars_widgets', $filter_callback, 1000); $this->context_sidebar_instance_number = null; $this->rendering_sidebar_id = null; $this->rendering_widget_id = null; return $container; }
/** * Render a specific widget using the supplied sidebar arguments. * * @since 4.5.0 * @access public * * @see dynamic_sidebar() * * @param WP_Customize_Partial $partial Partial. * @param array $context { * Sidebar args supplied as container context. * * @type string [$sidebar_id] ID for sidebar for widget to render into. * @type array [$sidebar_args] Sidebar args supplied as container context. * @type string [$sidebar_args_hmac] HMAC for sidebar args. * } * @return string|false */ public function render_widget_partial($partial, $context) { $id_data = $partial->id_data(); $widget_id = array_shift($id_data['keys']); $sidebar_id = null; $sidebar_args = array(); if (!is_array($context)) { return false; } if (!empty($context['sidebar_args'])) { if (empty($context['sidebar_args_hmac'])) { return false; } $context['sidebar_args'] = $this->ksort_recursive($context['sidebar_args']); if (!hash_equals($this->hash_sidebar_args($context['sidebar_args']), $context['sidebar_args_hmac'])) { return false; } $sidebar_args = $context['sidebar_args']; } /* * Note that when a widget is first added to a sidebar, it only has a sidebar_id as context. * For existing widgets, and subsequent updates to the newly-added widget, the sidebar_args * will be available because they will be included in the partial's rendered response here. */ if (!empty($context['sidebar_id'])) { $sidebar_id = $context['sidebar_id']; } else { if (!empty($sidebar_args['id'])) { $sidebar_id = $sidebar_args['id']; } else { return false; } } if (!is_registered_sidebar($sidebar_id)) { return false; } if (isset($context['sidebar_instance_number'])) { $this->context_sidebar_instance_number = $context['sidebar_instance_number']; } // Filter sidebars_widgets so that only the queried widget is in the sidebar. $this->sidebar_args_overrides = $sidebar_args; $this->rendering_widget_id = $widget_id; $this->rendering_sidebar_id = $sidebar_id; $filter_callback = array($this, 'filter_sidebars_widgets_for_rendering_widget'); add_filter('sidebars_widgets', $filter_callback, 1000); // Render the widget. ob_start(); dynamic_sidebar($sidebar_id); $container = ob_get_clean(); // Reset variables for next partial render. remove_filter('sidebars_widgets', $filter_callback, 1000); $this->context_sidebar_instance_number = null; $this->sidebar_args_overrides = array(); $this->rendering_sidebar_id = null; $this->rendering_widget_id = null; return $container; }