Created
November 26, 2022 08:20
-
-
Save cvn/e2dad31c38be34cee59f3be25ca191d2 to your computer and use it in GitHub Desktop.
Page to view and fix legacy WP Offload Media images
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
<form method="post"> | |
<button type="submit"> | |
Fix WP Offload Media images | |
</button> | |
<?= $_SERVER['REQUEST_METHOD'] === 'POST' ? '✓' : ''; ?> | |
</form> | |
<?php | |
include('wp-config.php'); | |
use DeliciousBrains\WP_Offload_Media\Items\Media_Library_Item; | |
$servername = DB_HOST; | |
$username = DB_USER; | |
$password = DB_PASSWORD; | |
$dbname = DB_NAME; | |
// Create connection | |
$conn = new mysqli($servername, $username, $password, $dbname); | |
// Check connection | |
if ($conn->connect_error) { | |
die("Connection failed: " . $conn->connect_error); | |
} | |
// sql based on get_attachments_with_legacy_metadata | |
// from https://github.com/deliciousbrains/wp-amazon-s3-and-cloudfront/blob/87b04c4f7123e100ec05477a8c055116b01949f5/classes/upgrades/upgrade-items-table.php#L148 | |
$sql = " | |
SELECT a.post_id AS id, p.meta_id AS meta_id, a.meta_value AS source_path, p.meta_value AS provider_object | |
FROM wordpress.wp_postmeta AS a, wordpress.wp_postmeta AS p | |
WHERE a.meta_key = '_wp_attached_file' | |
AND p.meta_key = 'amazonS3_info' | |
AND a.post_id = p.post_id | |
"; | |
$result = $conn->query($sql); | |
if ($result->num_rows > 0) { | |
$output = []; | |
// output data of each row | |
while($row = $result->fetch_assoc()) { | |
$item = Media_Library_Item::get_by_source_id( $row['id'] ); | |
array_push($output, $item); | |
if ($_SERVER['REQUEST_METHOD'] === 'POST') { | |
$item->save(); | |
} | |
} | |
// add status info | |
$unmatched = $conn->query($sql . 'AND a.post_id NOT IN (SELECT source_id FROM wordpress.wp_as3cf_items)'); | |
print_r($result->num_rows . ' legacy <br> ' . $unmatched->num_rows . ' broken'); | |
?><pre><? print_r($output); ?></pre><? | |
} else { | |
echo "0 results"; | |
} | |
$conn->close(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment