WordPress.org

Make WordPress Core

Opened 20 months ago

Last modified 13 months ago

#26530 new enhancement

Unnecessary database requests and untidy code in do_enclose

Reported by: joakimf Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version: 3.8
Component: Feeds Keywords: has-patch needs-refresh
Focuses: Cc:

Description

The function do_enclose in wp_includes/functions.php is 62 lines long with up to 8 levels of intendation, and begins with the comment

//TODO: Tidy this ghetto code up and make the debug code optional

The single biggest problem of this code might be that it unnecessarily asks the database if links are referenced as enclosures or not for the current post, once for every new link.

        foreach ( (array) $post_links as $url ) {
                if ( $url != '' && !$wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE post_id = %d AND meta_key = 'enclosure' AND meta_value LIKE (%s)", $post_ID, like_escape( $url ) . '%' ) ) ) {

This information is already available through $pung,

        $pung = get_enclosed( $post_ID );

do_enclose also always uses extension guessing to determine the mime type of a link, even when there is header information available.

Bad naming of variables and bad separation of tasks makes the workings this function difficult to grasp.

Attachments (1)

fix_do_enclose.diff (5.5 KB) - added by joakimf 20 months ago.
Suggested patch for problems described in ticket

Download all attachments as: .zip

Change History (4)

comment:1 @joakimf20 months ago

  • Cc frostegard@… added
  • Keywords has-patch added

Regarding the recently uploaded fix_do_enclose.diff: (I'm not sure that I'm posting this the right way but i couldn't manage to find out how to do it otherwise.)

Fixes the redundant database calls. Adds the option to avoid suppression of error messages through setting WP_DEBUG.

Now actually uses header information for links to attempt to determine mime types. Will not add links pointing to non-multimedia targets, even though the file extension is mp3, mp4 etc. I think this is better behaviour than in the patched version, which would only do guessing based on file names, and thus add such links.

Cleans up the code a lot. Adds three independent functions to wp-includes/functions.php; I'm not quite sure about the Wordpress policy for such changes.

I have tested the changes by adding and removing post links to 1) actual mp3 files 2) 404 pages with an mp3 file extension 3) non-reachable pages. The code in this patch behaves as it should in all these cases. The unit tests pass, though with one test incomplete:

.............................................................   61 / 1903 (  3%)
...........SS...SSSSSS.............S...SS............SS......  122 / 1903 (  6%)
..........S............S...........................S.........  183 / 1903 (  9%)
.......SSS.......SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS..S........  244 / 1903 ( 12%)
................S............................S.............S.  305 / 1903 ( 16%)
..........................SSSSSSSSSS.........................  366 / 1903 ( 19%)
.........S..................S.......I........................  427 / 1903 ( 22%)
................................SSSS.S..SSSSS................  488 / 1903 ( 25%)
.............................................................  549 / 1903 ( 28%)
.............................................................  610 / 1903 ( 32%)
.............................................................  671 / 1903 ( 35%)
.............................................................  732 / 1903 ( 38%)
.............................................................  793 / 1903 ( 41%)
..........S..............................SSSSSSSSSSSSSSSSSSSS  854 / 1903 ( 44%)
.............................................................  915 / 1903 ( 48%)
..................................SSSSSSSSSSSS...............  976 / 1903 ( 51%)
..............SSSSSS.........S.....S......................... 1037 / 1903 ( 54%)
...............................S............................. 1098 / 1903 ( 57%)
....................S................SSS.......S....SSSS..... 1159 / 1903 ( 60%)
SS........................................................... 1220 / 1903 ( 64%)
............................................................. 1281 / 1903 ( 67%)
............................................................. 1342 / 1903 ( 70%)
....S........................................................ 1403 / 1903 ( 73%)
...........S.........................SS...................... 1464 / 1903 ( 76%)
...........S..S.............................................S 1525 / 1903 ( 80%)
...................SSS....................................... 1586 / 1903 ( 83%)
............................................................. 1647 / 1903 ( 86%)
............................................................. 1708 / 1903 ( 89%)
............................................................. 1769 / 1903 ( 92%)
............................................................. 1830 / 1903 ( 96%)
...SS...............SSSSSSSS

Time: 4.49 minutes, Memory: 202.00Mb

OK, but incomplete or skipped tests!
Tests: 1857, Assertions: 8564, Incomplete: 1, Skipped: 151.

@joakimf20 months ago

Suggested patch for problems described in ticket

comment:2 @SergeyBiryukov20 months ago

  • Component changed from General to Feeds

Related: #14493, #20417

comment:3 @wonderboymusic13 months ago

  • Keywords needs-refresh added
  • Milestone changed from Awaiting Review to Future Release

Patch blows up.

Note: See TracTickets for help on using tickets.