How do you do what the menu item "GameObject > Apply Changes To Prefab" does in an editor script?

I'm almost done with an editor script to simulate nested prefabs by replacing `GameObject`s matching the name of the prefab with a new instance of the prefab. Where I'm stuck at is needing to "Apply Changes To Prefab" to that object's parent if it's a `Prefab`.

I may be going about the whole thing the wrong way but I'm close, here's my editor script.

using UnityEngine;
using System.Collections;
using UnityEditor;

public class UpdateNestedPrefabsWizard : ScriptableWizard {

    public GameObject[] prefabsToUpdate;

    void OnWizardCreate () {
        foreach (GameObject prefabToUpdate in prefabsToUpdate) {
            UpdateNestedPrefab(prefabToUpdate);
        }
    }

    [MenuItem ("Custom/Update Nested Prefabs %#u")]
    static void Handler () {
        ScriptableWizard.DisplayWizard<UpdateNestedPrefabsWizard>("Update Nested Prefabs Wizard", "Apply");
    }

    static void UpdateNestedPrefab (GameObject prefab) {
        GameObject[] objects = Object.FindObjectsOfType(typeof(GameObject)) as GameObject[];
        for (int i = 0; i < objects.Length; ++i) {
            GameObject gameObject = objects*;*
 *string name = gameObject.name;*
 *if (name == prefab.name) {*
 *GameObject parent = gameObject.transform.parent.gameObject;*
 *Replace(gameObject, prefab);*
 *if (EditorUtility.GetPrefabType(parent) == PrefabType.PrefabInstance) {*
 *// Here is where I run into problems*
 *EditorUtility.ReplacePrefab(parent, EditorUtility.GetPrefabParent(parent));*
 _/* the previous line seems to sort of work, it actually replaced one_
 _* of `parent`'s children with an up-to-date copy of `prefab`, but_
 _* just the first one._
 _*/_ 
 *}*
 *}*
 *EditorUtility.DisplayProgressBar("Updating " + name, "", (float)i/(float)objects.Length);*
 *}*
 *EditorUtility.ClearProgressBar();*
 *}*
 *static void Replace (GameObject originalObject, GameObject newObject) {*
 *newObject = (GameObject)EditorUtility.InstantiatePrefab(newObject);*
 *newObject.transform.position = originalObject.transform.position;*
 *newObject.transform.rotation = originalObject.transform.rotation;*
 *newObject.transform.localScale = originalObject.transform.localScale;*
 *newObject.transform.parent = originalObject.transform.parent;*
 *DestroyImmediate(originalObject);*
 *}*
*}*
*```*

I found the problem, after replacing one child object the `parent` is no longer of the type `PrefabType.PrefabInstance` it becomes `PrefabType.DisconnectedPrefabInstance`. Reconnecting to the prefab after updating it fixes this.

Here's the complete script if anyone wants it:

UpdateNestedPrefabsWizard.cs

using UnityEngine;
using System.Collections;
using UnityEditor;

public class UpdateNestedPrefabsWizard : ScriptableWizard {

    public GameObject[] prefabsToUpdate;

    void OnWizardCreate () {
        Undo.RegisterSceneUndo("Update Nested Prefabs");
        foreach (GameObject prefabToUpdate in prefabsToUpdate) {
            UpdateNestedPrefab(prefabToUpdate);
        }
    }

    [MenuItem ("Custom/Update Nested Prefabs %#u")]
    static void Handler () {
        ScriptableWizard.DisplayWizard<UpdateNestedPrefabsWizard>("Update Nested Prefabs Wizard", "Apply");
    }

    static void UpdateNestedPrefab (GameObject prefab) {
        GameObject[] objects = Object.FindObjectsOfType(typeof(GameObject)) as GameObject[];
        for (int i = 0; i < objects.Length; ++i) {
            GameObject gameObject = objects*;*
 *string name = gameObject.name;*
 *if (name == prefab.name) {*
 *GameObject parent = gameObject.transform.parent.gameObject;*
 *Replace(gameObject, prefab);*
 *if (EditorUtility.GetPrefabType(parent) == PrefabType.PrefabInstance) {*
 *EditorUtility.ReplacePrefab(parent, EditorUtility.GetPrefabParent(parent));*
 *EditorUtility.ReconnectToLastPrefab(parent);*
 *}*
 *}*
 *EditorUtility.DisplayProgressBar("Updating " + prefab.name, "checking " + name, (float)i/(float)objects.Length);*
 *}*
 *EditorUtility.ClearProgressBar();*
 *}*
 *static void Replace (GameObject originalObject, GameObject newObject) {*
 *newObject = (GameObject)EditorUtility.InstantiatePrefab(newObject);*
 *newObject.transform.position = originalObject.transform.position;*
 *newObject.transform.rotation = originalObject.transform.rotation;*
 *newObject.transform.localScale = originalObject.transform.localScale;*
 *newObject.transform.parent = originalObject.transform.parent;*
 *DestroyImmediate(originalObject);*
 *}*
*}*
*```*