Make WordPress Core

Opened 8 years ago

Closed 7 years ago

Last modified 7 years ago

#39771 closed enhancement (fixed)

Twenty Seventeen: Unused and duplicate variables in content-front-page.php

Reported by: dayun123's profile dayun123 Owned by: ocean90's profile ocean90
Milestone: 4.9 Priority: normal
Severity: normal Version: 4.7.2
Component: Bundled Theme Keywords: has-patch
Focuses: Cc:

Description

<?php if ( has_post_thumbnail() ) :
        $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'twentyseventeen-featured-image' );

        $post_thumbnail_id = get_post_thumbnail_id( $post->ID );

        $thumbnail_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'twentyseventeen-featured-image' );

        // Calculate aspect ratio: h / w * 100%.
        $ratio = $thumbnail_attributes[2] / $thumbnail_attributes[1] * 100;
        ?>

        <div class="panel-image" style="background-image: url(<?php echo esc_url( $thumbnail[0] ); ?>);">
            <div class="panel-image-prop" style="padding-top: <?php echo esc_attr( $ratio ); ?>%"></div>
        </div><!-- .panel-image -->

<?php endif; ?>

I'm still a novice programmer, so maybe I'm missing something obvious, but I don't understand why the following two things happen:

  1. Why is the $post_thumbnail_id variable created but never used? Why not create it right away instead of constantly calling get_post_thumbnail_id( $post->ID )?
  2. $thumbnail and $thumbnail_attributes are the same, right? So why create both?

Wouldn't this make more sense:

<?php if ( has_post_thumbnail() ) :

        $post_thumbnail_id = get_post_thumbnail_id( $post->ID );

        $thumbnail = wp_get_attachment_image_src( $post_thumbnail_id, 'twentyseventeen-featured-image' );

        // Calculate aspect ratio: h / w * 100%.
        $ratio = $thumbnail[2] / $thumbnail[1] * 100;
        ?>

        <div class="panel-image" style="background-image: url(<?php echo esc_url( $thumbnail[0] ); ?>);">
            <div class="panel-image-prop" style="padding-top: <?php echo esc_attr( $ratio ); ?>%"></div>
        </div><!-- .panel-image -->

<?php endif; ?>

Attachments (2)

39771.patch (1.8 KB) - added by dingo_bastard 8 years ago.
Replaced post thumbnail code in content-front-page.php with the one from content-front-page-panels.php, and escaped $twentyseventeencounter in content-front-page-panels.php
39771.2.patch (999 bytes) - added by mariusvetrici 7 years ago.
Remove changes from content-front-page-panels.php and only keep changes for content-front-page.php

Download all attachments as: .zip

Change History (12)

#1 @dingo_bastard
8 years ago

  • Component changed from Themes to Bundled Theme

The odd thing is that the content-front-page-panels.php uses

<?php if ( has_post_thumbnail() ) :
        $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'twentyseventeen-featured-image' );

        // Calculate aspect ratio: h / w * 100%.
        $ratio = $thumbnail[2] / $thumbnail[1] * 100;
        ?>

        <div class="panel-image" style="background-image: url(<?php echo esc_url( $thumbnail[0] ); ?>);">
                <div class="panel-image-prop" style="padding-top: <?php echo esc_attr( $ratio ); ?>%"></div>
        </div><!-- .panel-image -->

<?php endif; ?>

Maybe somebody missed that.

@dingo_bastard
8 years ago

Replaced post thumbnail code in content-front-page.php with the one from content-front-page-panels.php, and escaped $twentyseventeencounter in content-front-page-panels.php

#2 @SergeyBiryukov
8 years ago

  • Milestone changed from Awaiting Review to 4.8
  • Summary changed from Suggestion for edit to content-front-page.php in TwentySeventeen Theme to Twenty Seventeen: Unused and duplicate variables in content-front-page.php

#3 @jbpaul17
7 years ago

  • Keywords has-patch needs-testing added

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


7 years ago

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


7 years ago

#6 @jbpaul17
7 years ago

  • Milestone changed from 4.8 to 4.8.1

Punting to 4.8.1 per today's bug scrub.

@mariusvetrici
7 years ago

Remove changes from content-front-page-panels.php and only keep changes for content-front-page.php

#7 @mariusvetrici
7 years ago

  • Keywords needs-testing removed

The old patch contained changes for 2 files. Only 1 of the files (content-front-page-panels.php) is related to the current track issue. Therefore, the patch only contains the changes for this ticket.

Tested it with @grapplerulrich and @ocean90

#8 @ocean90
7 years ago

  • Owner set to ocean90
  • Resolution set to fixed
  • Status changed from new to closed

In 40904:

Twenty Seventeen: Remove/merge unused variables in content-front-page.php.

#wceu2017

Props dingo_bastard, mariusvetrici.
Fixes #39771.

This ticket was mentioned in Slack in #core by westonruter. View the logs.


7 years ago

#10 @pento
7 years ago

  • Milestone changed from 4.8.1 to 4.9

Moving this to 4.9, it doesn't need to to be backported to the 4.8 branch.

Note: See TracTickets for help on using tickets.