/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param string $campaignId the ID of the campaign to add the sitelinks to
 */
function AddSitelinksUsingFeedsExample(AdWordsUser $user, $campaignId)
{
    $sitelinksData = CreateSiteLinksFeed($user);
    $sitelinksData = CreateSiteLinksFeedItems($user, $sitelinksData);
    CreateSiteLinksFeedMapping($user, $sitelinksData);
    CreateSiteLinksCampaignFeed($user, $sitelinksData, $campaignId);
}
/**
 * Runs the example.
 * @param AdWordsUser $user the user to run the example with
 * @param array $campaignIds the IDs of the campaign to add the sitelinks to
 */
function UpgradeLegacySitelinksExample(AdWordsUser $user, $campaignIds)
{
    $campaignAdExtensionService = $user->getService('CampaignAdExtensionService', ADWORDS_VERSION);
    $feedService = $user->getService('FeedService', ADWORDS_VERSION);
    $feedItemService = $user->getService('FeedItemService', ADWORDS_VERSION);
    $feedMappingService = $user->getService('FeedMappingService', ADWORDS_VERSION);
    $campaignFeedService = $user->getService('CampaignFeedService', ADWORDS_VERSION);
    // Try to retrieve an existing feed that has been mapped for use with
    // sitelinks. if multiple such feeds exist, the first matching feed is
    // retrieved. You could modify this code example to retrieve all the feeds
    // and pick the appropriate feed based on user input.
    $siteLinksFeed = GetExistingFeed($feedMappingService);
    if (empty($siteLinksFeed)) {
        // Create a feed for storing sitelinks.
        $siteLinksFeed = CreateSiteLinksFeed($feedService);
        // Map the feed for using with sitelinks.
        CreateSiteLinksFeedMapping($feedMappingService, $siteLinksFeed);
    }
    foreach ($campaignIds as $campaignId) {
        // Get legacy sitelinks for the campaign.
        $extension = GetLegacySitelinksForCampaign($campaignAdExtensionService, $campaignId);
        if (!empty($extension)) {
            // Get the sitelinks.
            $legacySiteLinks = $extension->adExtension->sitelinks;
            // Add the sitelinks to the feed.
            $siteLinkFeedItemIds = CreateSiteLinkFeedItems($feedItemService, $siteLinksFeed, $legacySiteLinks);
            // Associate feeditems to the campaign.
            AssociateSitelinkFeedItemsWithCampaign($campaignFeedService, $siteLinksFeed, $siteLinkFeedItemIds, $campaignId);
            // Once the upgraded sitelinks are added to a campaign, the legacy
            // sitelinks will stop serving. You can delete the legacy sitelinks
            // once you have verified that the migration went fine. In case the
            // migration didn't succeed, you can roll back the migration by deleting
            // the CampaignFeed you created in the previous step.
            DeleteLegacySitelinks($campaignAdExtensionService, $extension);
        }
    }
}