| @@ -48,9 +48,9 @@ | ||
| 48 | 48 | */ |
| 49 | 49 | public function get_attachment_by_name($attachment_name) |
| 50 | 50 | { |
| 51 | 51 | global $wpdb; |
| 52 | - $post = $wpdb->get_row( | |
| 52 | + $post = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- attachment lookup by guid/post_name — guid search has no WordPress API | |
| 53 | 53 | $wpdb->prepare( |
| 54 | 54 | "SELECT * FROM $wpdb->posts WHERE `post_name` = %s and `post_type` = 'attachment' LIMIT 1", |
| 55 | 55 | $attachment_name |
| 56 | 56 | ) |
| @@ -72,9 +72,10 @@ | ||
| 72 | 72 | */ |
| 73 | 73 | public function get_file_name_by_url($url) |
| 74 | 74 | { |
| 75 | 75 | if (stripos($url, "https://cdn.midjourney.com/") !== false) { |
| 76 | - return pathinfo(str_replace("/", "_", parse_url($url, PHP_URL_PATH) ?? ''), PATHINFO_FILENAME); | |
| 76 | + $path = wp_parse_url($url, PHP_URL_PATH); | |
| 77 | + return pathinfo(false === $path ? '' : str_replace('/', '_', $path), PATHINFO_FILENAME); | |
| 77 | 78 | } elseif (stripos($url, "https://drive.google.com/") !== false) { |
| 78 | 79 | $parse_url = wp_parse_url($url); |
| 79 | 80 | // Modern permalinks are /file/d/<ID>/view — the ID lives in the path, |
| 80 | 81 | // and there is no query component at all on those URLs. |
| @@ -84,9 +85,10 @@ | ||
| 84 | 85 | $args = []; |
| 85 | 86 | wp_parse_str($parse_url['query'] ?? '', $args); |
| 86 | 87 | return $args['id'] ?? null; |
| 87 | 88 | } else { |
| 88 | - return pathinfo(parse_url($url, PHP_URL_PATH) ?? '', PATHINFO_FILENAME); | |
| 89 | + $path = wp_parse_url($url, PHP_URL_PATH); | |
| 90 | + return pathinfo(false === $path ? '' : $path, PATHINFO_FILENAME); | |
| 89 | 91 | } |
| 90 | 92 | } |
| 91 | 93 | |
| 92 | 94 | public function allowedDownloadSources($url) |
| @@ -106,18 +108,17 @@ | ||
| 106 | 108 | public function get_media_id_from_url($url) { |
| 107 | 109 | global $wpdb; |
| 108 | 110 | |
| 109 | 111 | // Search for any attachment matching the URL |
| 110 | - $query = $wpdb->prepare(" | |
| 112 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- attachment lookup by guid/post_name — guid search has no WordPress API | |
| 113 | + $attachment_id = $wpdb->get_var($wpdb->prepare(" | |
| 111 | 114 | SELECT ID |
| 112 | - FROM $wpdb->posts | |
| 115 | + FROM {$wpdb->posts} | |
| 113 | 116 | WHERE post_type = 'attachment' |
| 114 | 117 | AND guid = %s |
| 115 | 118 | LIMIT 1 |
| 116 | - ", $url); | |
| 119 | + ", $url)); | |
| 117 | 120 | |
| 118 | - $attachment_id = $wpdb->get_var($query); | |
| 119 | - | |
| 120 | 121 | // Return the attachment ID if found, otherwise return false |
| 121 | 122 | return $attachment_id ? intval($attachment_id) : false; |
| 122 | 123 | } |
| 123 | 124 | /** |
| @@ -134,9 +135,8 @@ | ||
| 134 | 135 | |
| 135 | 136 | $tmp = download_url($url); |
| 136 | 137 | if (is_wp_error($tmp)){ |
| 137 | 138 | $attachment_id = $this->get_media_id_from_url($url); |
| 138 | - error_log($attachment_id); | |
| 139 | 139 | return $attachment_id; |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | $filename = $this->get_file_name_by_url($url); |
| @@ -174,9 +174,9 @@ | ||
| 174 | 174 | $extension = $mime_extensions[$mime]; |
| 175 | 175 | } else { |
| 176 | 176 | // Safely delete temporary file if it exists |
| 177 | 177 | if (file_exists($tmp)) { |
| 178 | - unlink($tmp); | |
| 178 | + wp_delete_file($tmp); | |
| 179 | 179 | } |
| 180 | 180 | return false; |
| 181 | 181 | } |
| 182 | 182 | } |
| @@ -200,9 +200,9 @@ | ||
| 200 | 200 | // before the result is used as a post ID anywhere below. |
| 201 | 201 | if (is_wp_error($attachment_id)) { |
| 202 | 202 | // Safely delete temporary file if it exists |
| 203 | 203 | if (file_exists($tmp)) { |
| 204 | - unlink($tmp); | |
| 204 | + wp_delete_file($tmp); | |
| 205 | 205 | } |
| 206 | 206 | return false; |
| 207 | 207 | } |
| 208 | 208 | update_post_meta($attachment_id, '_wp_attachment_image_alt', $alt); |
| @@ -222,9 +222,9 @@ | ||
| 222 | 222 | wp_update_attachment_metadata($attachment_id, $attach_data); |
| 223 | 223 | |
| 224 | 224 | // Safely delete temporary file if it exists |
| 225 | 225 | if (file_exists($tmp)) { |
| 226 | - unlink($tmp); | |
| 226 | + wp_delete_file($tmp); | |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | return $attachment_id; |
| 230 | 230 | } else { |
| @@ -251,9 +251,9 @@ | ||
| 251 | 251 | } |
| 252 | 252 | |
| 253 | 253 | // Delete the temporary file unless the sideload above moved it |
| 254 | 254 | if (file_exists($tmp)) { |
| 255 | - unlink($tmp); | |
| 255 | + wp_delete_file($tmp); | |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | // check if the title attribute is set on the image tag and then update the title |
| 259 | 259 | if($title_text !== ''){ |