修复mp3j-player插件main.php中的PHP未定义索引与常量警告
removeRemoteTracks Function Let's break down your problem and walk through the best solutions here:
Why You're Seeing These Errors
Notice: Undefined index: url: This happens because the$trackarrays in your playlist (created by thenewTrack()function) don't have a'url'key. When you try to access$track['url'], PHP throws a notice because that index doesn't exist.Warning: Use of undefined constant 'url': This is almost certainly a typo somewhere in your code—maybe a missing quote, like writing$track[url]instead of$track['url']. PHP assumesurlis a constant when it's not quoted, and since no such constant exists, it falls back to treating it as the string"url"while warning you about the mistake.- The 480+衍生 errors from Query Monitor are just repeated instances of these two issues firing every time the
removeRemoteTracksloop runs over a track.
Your Current Fix: Switching to 'src' or 'filename'
Great news—this isn't just a temporary workaround, it's actually a correct fix!
Looking at your newTrack() function, the array returned includes 'src' and 'filename' (the actual fields storing track URLs/paths), but no 'url' key. The original removeRemoteTracks code was incorrectly referencing a non-existent key. By switching to 'src' or 'filename', you're aligning the code with the actual data structure of the tracks in your plugin.
Since you've confirmed the plugin works normally after this change, this is a solid, long-term solution.
Should You Add a 'url' Key to newTrack()?
This depends on whether other parts of the plugin expect a 'url' key to exist. Here are two scenarios to consider:
Scenario 1: No other code uses 'url'
Stick with your current fix. There's no need to add a redundant 'url' key if nothing else relies on it. This keeps your code clean and aligned with the plugin's existing structure.
Scenario 2: Other parts of the plugin reference $track['url']
If you find other functions in the plugin that expect 'url' to exist, you have two options:
Add
'url'tonewTrack()and sync it with'src':
Update thenewTrack()function to include the key, and make sure whenever a track's'src'is set,'url'is updated too (or vice versa):function newTrack () { return array( 'url' => '', // Add this line 'src' => '', 'filename' => '', 'formats' => array(), 'counterpart' => '', 'title' => '', 'caption' => '', 'image' => '', 'link' => '' ); }Then, wherever tracks are populated in the plugin, ensure
$track['url']is set to the same value as$track['src'](or whichever field holds the actual URL).Add a fallback check in
removeRemoteTracks:
If you don't want to modifynewTrack(), you can adjust the function to use'url'if it exists, otherwise fall back to'src':function removeRemoteTracks ( $playlist ) { $filtered = array(); foreach ( $playlist as $track ) { // Get the track URL safely $trackUrl = isset($track['url']) ? $track['url'] : $track['src']; if ( strpos($trackUrl, $this->Rooturl) !== false || ( strpos($trackUrl, 'http://') === false && strpos($trackUrl, 'https://') === false ) || ( strpos($this->theSettings['mp3_dir'], "http://") !== false && strpos($trackUrl, $this->theSettings['mp3_dir']) !== false ) ) { $filtered[] = $track; } } return $filtered; }This makes the function more resilient in case some tracks have
'url'and others don't.
Final Recommendation
Since your current fix (using 'src' or 'filename') works perfectly and aligns with the plugin's actual data structure, that's the best approach unless you find other code that depends on the 'url' key. This eliminates the errors now and prevents potential issues in future PHP versions where notice-level errors might be treated more strictly.
内容的提问来源于stack exchange,提问作者audioscapist

