/** * When activating the theme, make sure the article bottom widget area has widgets assigned * * @since 0.5.3 */ function largo_populate_article_bottom_widget_area($theme) { // If the old article bottom settings are present, we'll skip populating the article bottom // area as those are handled during the migration/update. $old_largo_article_bottom_settings = array('show_tags', 'show_author_box', 'show_related_content', 'show_next_prev_nav_single'); $old_largo_article_bottom_settings_check = false; foreach ($old_largo_article_bottom_settings as $option_name) { $option = of_get_option($option_name); if (!empty($option)) { $old_largo_article_bottom_settings_check = true; break; } } if ($old_largo_article_bottom_settings_check) { return; } // Otherwise, if there's no largo_version or the 'article-bottom' area is empty, // we're on a clean install and should set the default widgets if (!of_get_option('largo_version') || !is_active_sidebar('article-bottom')) { largo_instantiate_widget('largo-author', array(), 'article-bottom'); largo_instantiate_widget('largo-related-posts', array(), 'article-bottom'); } }
/** * Puts new widgets into sidebars as appropriate based on old theme options */ function largo_update_widgets() { /* checks and adds if necessary: social_icons_display ('btm' or 'both') add series widget show_tags show_author_box show_related_content show_next_prev_nav_single */ $checks = array(); $checks['social_icons_display'] = array('values' => array('btm', 'both'), 'widget' => 'largo-follow', 'settings' => array('title' => '')); //this is a dummy check $checks['in_series'] = array('values' => NULL, 'widget' => 'largo-post-series-links', 'settings' => array('title' => __('Related Series', 'largo'))); $checks['show_tags'] = array('values' => array(1), 'widget' => 'largo-tag-list', 'settings' => array('title' => __('Filed Under:', 'largo'), 'tag_limit' => 20)); $checks['show_author_box'] = array('values' => array('1'), 'widget' => 'largo-author-bio', 'settings' => array('title' => __('Author', 'largo'))); $checks['show_related_content'] = array('values' => array('1'), 'widget' => 'largo-explore-related', 'settings' => array('title' => __('More About', 'largo'), 'topics' => 6, 'posts' => 3)); $checks['show_next_prev_nav_single'] = array('values' => array('1'), 'widget' => 'largo-prev-next-post-links', 'settings' => array()); //loop thru, see if value is present, then see if widget exists, if not, create one foreach ($checks as $option => $i) { $opt = of_get_option($option); if ($i['values'] === NULL || in_array($opt, $i['values'])) { //we found an option that suggests we need to add a widget. //if there's not aleady one present, add it if (!largo_widget_in_region($i['widget'])) { largo_instantiate_widget($i['widget'], $i['settings'], 'article-bottom'); } } } }
/** * Replace deprecated widgets with new widgets * * To add widgets to this list of widgets to be upgraded: * - Add the deprecated widget class and its replacement to $upgrades * * @uses largo_get_widget_basename * @uses largo_get_widget_number * @since 0.5.3 */ function largo_replace_deprecated_widgets() { // This defines the classes of widget that will be updated, the class that they will be // replaced with, and the default args on the replacement widget that must be set. $upgrades = array('largo-footer-featured' => array('class' => 'largo-featured', 'defaults' => array('term' => 'footer-featured', 'title' => __('In Case You Missed It', 'largo'))), 'largo-sidebar-featured' => array('class' => 'largo-featured', 'defaults' => array('term' => 'sidebar-featured', 'title' => __('We Recommend', 'largo')))); $all_widgets = get_option('sidebars_widgets'); /* * Find the widgets that need to be replaced, * Move their arguments to new widgets, * Name the new widgets appropriately * Place the new widgets into the widgets list and into the sidebars list */ foreach ($all_widgets as $region => $current_sidebar) { // Unlike largo_check_deprecated_widgets, this does not care if the // widget is inactive. This replaces *all* widgets. if ($region != 'array_version' && is_array($current_sidebar)) { foreach ($current_sidebar as $current_widget_slug) { foreach ($upgrades as $old_widget_name => $upgrade) { // Check if the current widget matches a widget in // $updates that needs to be replaced. if (strpos($current_widget_slug, $old_widget_name) === 0) { // Update all this here and now, in case the indexes are off because this // has been meddled with in a previous loop. $local_all_widgets = get_option('sidebars_widgets'); $local_current_sidebar = $local_all_widgets[$region]; $index = array_search($current_widget_slug, $local_current_sidebar); /* * So many variables ... * * $local_all_widgets: Associative array of $region a sidebar or widget area => $local_current_sidebar array of widget slugs in regione * $local_current_sidebar: Array of the widgets in the current sidebar/widget area/$region * $current_widget_slug: the old widget's ID: slug-widget-2 * $old_widget_name: The slug of the widget that needs to be updated, from $upgrades: slug * $region: the id of the current sidebar/widget area * $index: Where @current_widget_slug is located in $local_current_sidebar * $basename: the slug of the widget $current_widget_slug, when you remove the prefix widget_ and postfix -number * $all_instances_of_widget: All instance of $current_widget_slug in all sidebars. * $upgrade['class'] : The class of the replacement widget, which needs -widget appended to it. * $upgrade['defaults'] : Default instance arguments for the replacement widget. * $all_instances_of_upgrade: All instances of $$upgrade['class'] in all sidebars. * $upgrade_instance_args: The merged old args of the widget with the args from $upgrade['defaults'] * $liw_return: array returned by largo_instantiate_widget with the widget's slug info and place in the sidebar. */ // Let's steal some logic from INN/wp-scripts/inc/class-cmd-sidebars.php's dump() $basename = largo_get_widget_basename($current_widget_slug); $number = largo_get_widget_number($current_widget_slug); if (!empty($basename)) { // get all the widgets of this basename $all_instances_of_widget = get_option('widget_' . $basename, false); $upgrade_instance_args = array_replace($all_instances_of_widget[$number], $upgrade['defaults']); // create the new widget. $liw_return = largo_instantiate_widget($upgrade['class'], $upgrade_instance_args, $region); // remove the old widget unset($all_instances_of_widget[$number]); update_option('widget_' . $basename, $all_instances_of_widget); // @todo: if there are no widgets left in the array, why not just remove the option? // update $local_current_sidebar $local_all_widgets = get_option('sidebars_widgets'); $local_current_sidebar = $local_all_widgets[$region]; // Shuffle the new widget around // replace the old widget slug with the new widget slug $local_current_sidebar[$index] = $liw_return['id']; // remove the now-duplicate instance of the old widget // added by largo_instantiate_widget unset($local_current_sidebar[$liw_return['place']]); $local_all_widgets[$region] = $local_current_sidebar; update_option('sidebars_widgets', $local_all_widgets); } } } } } } }
function test_largo_replace_deprecated_widgets() { // First, create some deprecated widgets largo_instantiate_widget('largo-sidebar-featured', array('title' => 'Foo'), 'sidebar-single'); largo_instantiate_widget('largo-footer-featured', array('title' => 'Bar'), 'footer-1'); largo_instantiate_widget('largo-featured', array('title' => 'Baz'), 'sidebar-main'); largo_instantiate_widget('largo-follow', array('title' => 'Baz'), 'homepage-alert'); largo_instantiate_widget('largo-recent-posts', array('title' => 'Baz'), 'homepage-alert'); // chek that things were set up correctly $this->assertTrue(largo_widget_in_region('largo-sidebar-featured', 'sidebar-single'), "The Largo Sidebar Featured widget was left in the Sidebar Single widget area."); $this->assertTrue(largo_widget_in_region('largo-footer-featured', 'footer-1'), "The Largo Footer Featured widget was left in the Footer 1 widget area."); $this->assertTrue(largo_widget_in_region('largo-featured', 'sidebar-main'), "Setup: The old Largo Featured widget was not created in the Sidebar Main widget area."); $this->assertTrue(largo_widget_in_region('largo-follow', 'homepage-alert'), "Setup: The Largo Follow widget was not created in the Homepage Alert widget area."); $this->assertTrue(largo_widget_in_region('largo-recent-posts', 'homepage-alert'), "Setup: The Largo Recent Posts was not created in the Homepage Alert widget area."); // Run the actual updates largo_replace_deprecated_widgets(); // This array is currently unused. $updates = array('largo-sidebar-featured' => array('name' => 'largo-recent-posts', 'count' => 0), 'largo-sidebar-featured' => array('name' => 'largo-recent-posts', 'count' => 0), 'largo-featured' => array('name' => 'largo-recent-posts', 'count' => 0)); // You will want to check this later; $this->assertFalse(largo_widget_in_region('largo-sidebar-featured', 'sidebar-single'), "The Largo Sidebar Featured widget was left in the Sidebar Single widget area."); $this->assertFalse(largo_widget_in_region('largo-footer-featured', 'footer-1'), "The Largo Footer Featured widget was left in the Footer 1 widget area."); $this->assertTrue(largo_widget_in_region('largo-recent-posts', 'sidebar-single'), "The new Largo Featured widget was not found in the Sidebar Single widget area."); $this->assertTrue(largo_widget_in_region('largo-recent-posts', 'footer-1'), "The new Largo Featured widget was not found in the Footer 1 widget area."); $this->assertTrue(largo_widget_in_region('largo-recent-posts', 'sidebar-main'), "The old Largo Featured widget was not found in the Sidebar Main widget area."); $this->assertTrue(largo_widget_in_region('largo-follow', 'homepage-alert'), "The Largo Follow widget was not found in the Homepage Alert widget area."); $this->assertTrue(largo_widget_in_region('largo-recent-posts', 'homepage-alert'), "The old Largo Featured widget was not found in the Homepage Alert widget area."); }
function test_largo_instantiate_widget() { // uses wp_parse_args, available here // uses update_option, available here $widgets_backup = get_option('sidebars_widgets '); update_option('sidebars_widgets', array('article-bottom' => array())); // instantiate the widget largo_instantiate_widget('largo-follow', array('title' => ''), 'article-bottom'); // Check $widgets = get_option('sidebars_widgets'); $this->assertEquals('largo-follow-widget-2', $widgets['article-bottom'][0]); delete_option('sidebars_widgets'); update_option('sidebars_widgets', $widgets_backup); unset($widgets_backup); }