Example #1
0
function tesseract_import_packages($packages)
{
    if (empty($packages)) {
        return;
    }
    $current_package_slugs_to_versions = array();
    foreach ($packages as $package) {
        $slug = $package['details']['slug'];
        $version = $package['details']['version'];
        $current_package_slugs_to_versions[$slug] = $version;
    }
    $existing_packages = tesseract_get_previously_imported_packages();
    // Get packages that need to be totally deleted
    $packages_for_deletion = array();
    foreach ($existing_packages as $slug => $existing_package) {
        if (empty($current_package_slugs_to_versions[$slug])) {
            $packages_for_deletion[] = $slug;
        }
    }
    // Get packages that need to be added/updated
    $packages_for_importing = array();
    $packages_for_updating = array();
    foreach ($current_package_slugs_to_versions as $slug => $version) {
        // Check to see if this package slug & version have been imported.
        if (empty($existing_packages[$slug])) {
            // If not, do the import
            $packages_for_importing[] = $slug;
        } elseif ($existing_packages[$slug] != $version) {
            $packages_for_updating[] = $slug;
        }
    }
    foreach ($packages as $package) {
        $slug = $package['details']['slug'];
        if (in_array($slug, $packages_for_importing)) {
            tesseract_import_package($package);
        } elseif (in_array($slug, $packages_for_updating)) {
            tesseract_delete_package_with_slug($slug);
            tesseract_import_package($package);
        }
    }
    foreach ($existing_packages as $slug => $existing_package) {
        if (in_array($slug, $packages_for_deletion)) {
            tesseract_delete_package_with_slug($slug);
        }
    }
}
function tesseract_import_packages($packages)
{
    if (empty($packages)) {
        return;
    }
    $current_package_slugs_to_versions = array();
    foreach ($packages as $package) {
        $slug = $package['details']['slug'];
        $version = $package['details']['version'];
        $current_package_slugs_to_versions[$slug] = $version;
    }
    $existing_packages = get_option(Tesseract_Importer_Constants::$IMPORTED_PACKAGES_OPTION_NAME, array());
    // Get packages that need to be totally deleted
    $packages_for_deletion = array();
    foreach ($existing_packages as $slug => $existing_package) {
        if (empty($current_package_slugs_to_versions[$slug])) {
            $packages_for_deletion[] = $slug;
        }
    }
    // Get packages that need to be added/updated
    $packages_for_importing = array();
    $packages_for_updating = array();
    foreach ($current_package_slugs_to_versions as $slug => $version) {
        // Check to see if this package slug & version have been imported.
        if (empty($existing_packages[$slug])) {
            // If not, do the import
            $packages_for_importing[] = $slug;
        } elseif ($existing_packages[$slug] != $version) {
            $packages_for_updating[] = $slug;
        }
    }
    foreach ($packages as $package) {
        $slug = $package['details']['slug'];
        if (!empty($packages_for_importing) && in_array($slug, $packages_for_importing)) {
            tesseract_import_package($package);
        } elseif (!empty($packages_for_updating) && in_array($slug, $packages_for_updating)) {
            tesseract_delete_package_with_slug($slug);
            tesseract_import_package($package);
        }
    }
    foreach ($existing_packages as $slug => $existing_package) {
        if (!empty($packages_for_deletion) && in_array($slug, $packages_for_deletion)) {
            tesseract_delete_package_with_slug($slug);
        }
    }
    /* return some information about what we did with the packages */
    $result = array('deleted' => count($packages_for_deletion), 'updated' => count($packages_for_updating), 'imported' => count($packages_for_importing));
    return $result;
}