Last active
October 18, 2018 01:29
-
-
Save kgsi/e86123a9e5c6d841ad7c0e153d6358de to your computer and use it in GitHub Desktop.
Post data from WordPress to firebase RealtimeDatabase / Storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require '/xxx/autoload.php'; | |
require_once '/xxx/wp-load.php'; | |
// firebase-phpのnamespace設定 | |
use Kreait\Firebase; | |
use Kreait\Firebase\Factory; | |
use Kreait\Firebase\ServiceAccount; | |
/* | |
* Firebase Admin SDKのサービスアカウントを登録 | |
* https://firebase.google.com/docs/admin/setup?hl=ja | |
*/ | |
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/fireabase-admin-json/xxxxxxx-adminsdk-xxxxxxxxx.json'); | |
$partsQueryKey = array( | |
'post_status' => 'publish', | |
'order' => 'DESC', | |
'post_type' => 'xxxx', | |
'posts_per_page' => 999 | |
); | |
$query = new WP_Query($partsQueryKey); | |
$databaseArray = []; // Firebase Databaseへ登録する連想配列 | |
$storageArray = []; // Firebase Storageへ登録する画像データ用連想配列 | |
$num = 0; | |
/* | |
* WordPressのループ処理でデータ作成 | |
*/ | |
while($query->have_posts()){ | |
$query->the_post(); | |
$id = get_the_ID(); | |
// Database登録用データ | |
$databaseArray[$id]['id'] = $id; | |
$databaseArray[$id]['name'] = get_the_title(); | |
$databaseArray[$id]['mainImage'] = get_field('mainImage')['filename']; | |
// Storage登録用データ | |
$storageArray[$brandsData[$id]['mainImage']] = array( | |
'modified' => get_field('mainImage')['modified'], // 画像更新日付を取得 | |
'src' => get_field('mainImage')['url'] // 画像URLを取得 | |
); | |
$num++; | |
} | |
$firebase = (new Factory) | |
->withServiceAccount($serviceAccount) | |
->create(); // firebaseインスタンス作成 | |
$database = $firebase->getDatabase(); | |
$storage = $firebase->getStorage(); | |
$filesystem = $storage->getFilesystem(); | |
$firebaseImageList = $filesystem->listContents('img/'); //アップロードしたいfirebase storageのディレクトリ内画像一覧を取得 | |
/* | |
* アップロードする画像のみを抽出 | |
*/ | |
foreach ($storageArray as $key => $value) { | |
$fimename = $key; | |
$timestamp = strtotime($value['modified']); | |
foreach ($firebaseImageList as $value) { | |
if($fimename == $value['basename'] && $timestamp <= $value['timestamp']) { // 差分抽出 | |
unset($storageArray[$fimename]); | |
} | |
} | |
} | |
/* | |
* Storageへの画像アップロード処理(差分処理) | |
*/ | |
foreach ($storageArray as $key => $value) { | |
if($key != null && $key != '') { | |
$filename = $key; | |
$src = file_get_contents($value['src'], false, stream_context_create($options)); | |
$filesystem->put('img/' . $filename, $src); | |
} | |
} | |
/* | |
* Databaseへのアップロード処理 | |
*/ | |
$database->getReference('items')->set($databaseArray); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WordPressの投稿から配列データを作り、Firebase Realtime Database / StorageへPOSTするscript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment