Advertisement
madebyvital

WP-CLI: Eval file - Bulk import list of media

Sep 23rd, 2021
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.45 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Bulk sideload list of media via CSV file.
  4.  *
  5.  * Assumes two columns: title, url
  6.  */
  7.  
  8. if (($data = fopen('file-list.csv', 'r')) !== false) {
  9.     $pdfs = [];
  10.  
  11.     while (($row = fgetcsv($data, 1000, ',')) !== false) {
  12.  
  13.         $pdf_url = parse_url($row[1]);
  14.         $slug = basename($pdf_url['path']);
  15.         $slug = preg_replace('/\\.[^.\\s]{3,4}$/', '', $slug);
  16.  
  17.         $title = $row[0];
  18.  
  19.         if ($title === '') {
  20.             $title = str_replace('_', ' ', $slug);
  21.             $title = str_replace('-', ' ', $title);
  22.             $title = str_replace('%20', ' ', $title);
  23.             $title = str_replace('  ', ' ', $title);
  24.             $title = ucwords($title);
  25.             $title = str_replace('Aci', 'ACI', $title);
  26.         }
  27.  
  28.         $pdfs[] = [
  29.             'post_title'   => $title,
  30.             'post_name'    => $slug,
  31.             'external_url' => $row[1],
  32.         ];
  33.     }
  34.  
  35.     fclose($data);
  36.  
  37. }
  38.  
  39. // WP_CLI::line(print_r($pdfs));
  40.  
  41. if (!empty($pdfs)) {
  42.     $download_errors = [];
  43.     $sideload_errors = [];
  44.     $count = 0;
  45.  
  46.     foreach ($pdfs as $pdf) {
  47.  
  48.         $tmp = download_url($pdf['external_url']);
  49.  
  50.         $file_array = [
  51.             'name'     => basename($pdf['external_url']),
  52.             'tmp_name' => $tmp,
  53.         ];
  54.  
  55.         // Log error if download fails.
  56.         if (is_wp_error($tmp)) {
  57.             @unlink($file_array['tmp_name']); // phpcs:ignore
  58.  
  59.             $download_errors[] = [
  60.                 'file'  => $pdf['external_url'],
  61.                 'error' => $tmp->get_error_message(),
  62.             ];
  63.  
  64.             WP_CLI::line(WP_CLI::colorize('%RDownload error:%n ' . $pdf['external_url']));
  65.  
  66.             continue;
  67.         }
  68.  
  69.         // Do the sideload.
  70.         $attachment_id = media_handle_sideload($file_array, null, null, );
  71.  
  72.         // Log error if sideload fails.
  73.         if (is_wp_error($attachment_id)) {
  74.             @unlink($file_array['tmp_name']); // phpcs:ignore
  75.  
  76.             $sideload_errors[] = [
  77.                 'file'  => $pdf['external_url'],
  78.                 'error' => $attachment_id->get_error_message(),
  79.             ];
  80.  
  81.             WP_CLI::line(WP_CLI::colorize('%RMedia sideload error:%n ' . $pdf['post_name']));
  82.  
  83.             continue;
  84.         }
  85.  
  86.         wp_update_post([
  87.             'ID'          => $attachment_id,
  88.             'post_title'  => $pdf['post_title'],
  89.             'post_name'   => $pdf['post_name'],
  90.             'post_author' => 1,
  91.         ]);
  92.  
  93.         $count++;
  94.  
  95.         WP_CLI::success('Added: ' . $pdf['post_title']);
  96.     }
  97.  
  98.     WP_CLI::success("Total media added: $count");
  99.  
  100.     if (!empty($download_errors)) {
  101.         WP_CLI::line(WP_CLI::colorize('%RDownload errors:%n '));
  102.         WP_CLI::line(print_r($download_errors));
  103.     }
  104.  
  105.     if (!empty($sideload_errors)) {
  106.         WP_CLI::line(WP_CLI::colorize('%RSideload errors:%n '));
  107.         WP_CLI::line(print_r($sideload_errors));
  108.     }
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement