GithubHelp home page GithubHelp logo

Comments (1)

twDuke avatar twDuke commented on July 17, 2024

This works, I take no responsibility for broken databases etc etc. I'm a php noob as well, so code might be so so.

It goes through all attachment posts and add the azure storage meta data.
Probably simple to re-add some of the code i deleted from the plugin if you want to transfer the files from local to azure if you need that.

<?php

if (!function_exists('windows_azure_storage_wp_update_attachment_metadata')) {
    echo "Cant find Windows Azure Storage plugin functions</br>";
    die();
}

$posts = get_posts(array('post_type' => "attachment", "posts_per_page" => 10));

foreach ($posts as $post) {
    $name = $post->post_title . " " . $post->ID;
    echo "Migrating " . $name . "</br>";

    $existingAzureMeta = get_post_meta($post->ID, "windows_azure_storage_info", true);
    if (isset($existingAzureMeta) && empty($existingAzureMeta) == false) {
        echo $name . " already migrated";
        echo "</br></br></br></br>";
        continue;
    }

    $file = get_post_meta($post->ID, "_wp_attached_file", true);

    $data = array(
        "data" =>
            array(
                "file" => $file
            ));
    $result = migrateMedia($data, $post->ID);
    echo json_encode($result);
    echo "</br></br></br></br>";
}

function migrateMedia($data, $post_id)
{
    $default_azure_storage_account_container_name = \Windows_Azure_Helper::get_default_container();
    $upload_file_name = get_attached_file($post_id, true);

    // Get upload directory.
    $upload_dir = wp_upload_dir();
    $upload_dir['subdir'] = ltrim($upload_dir['subdir'], '/');

    // Prepare blob name.
    $relative_file_name = ('' === $upload_dir['subdir']) ?
        basename($upload_file_name) :
        str_replace($upload_dir['basedir'] . '/', '', $upload_file_name);

    $data['file'] = $upload_file_name;

    $url = sprintf('%1$s/%2$s',
        untrailingslashit(WindowsAzureStorageUtil::get_storage_url_base()),
        $relative_file_name
    );

    $data['url'] = $url;

    $thumbnails = array();

    delete_post_meta($post_id, 'windows_azure_storage_info');

    add_post_meta(
        $post_id, 'windows_azure_storage_info',
        array(
            'container' => $default_azure_storage_account_container_name,
            'blob' => $relative_file_name,
            'url' => $url,
            'thumbnails' => $thumbnails
        )
    );

    return $data;
}

from windows-azure-storage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.