Plugin Directory

Changeset 3441000


Ignore:
Timestamp:
01/16/2026 01:08:53 PM (2 months ago)
Author:
butterflymedia
Message:

Optimize w3p_get_excerpt() to avoid get_the_excerpt() overhead by using direct post field access

Location:
wp-perfect-plugin/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • wp-perfect-plugin/trunk/includes/functions.php

    r3440912 r3441000  
    261261
    262262function w3p_get_excerpt( $post_id ) {
     263    // Check for custom excerpt first (fastest - already stored)
    263264    $custom_excerpt = get_post_meta( $post_id, '_w3p_excerpt', true );
    264    
    265265    if ( (string) $custom_excerpt !== '' ) {
    266         return $custom_excerpt;
    267     }
    268 
    269     return esc_attr( wp_strip_all_tags( get_the_excerpt( $post_id ) ) );
     266        return esc_attr( $custom_excerpt );
     267    }
     268
     269    // Get post object to access fields directly (avoids get_the_excerpt() overhead)
     270    $post = get_post( $post_id );
     271    if ( ! $post ) {
     272        return '';
     273    }
     274
     275    // Use post_excerpt field directly if available (no processing needed)
     276    if ( ! empty( $post->post_excerpt ) ) {
     277        return esc_attr( wp_strip_all_tags( $post->post_excerpt ) );
     278    }
     279
     280    // Fallback: Generate excerpt from content (only if excerpt is empty)
     281    // Use wp_trim_words() directly on post_content for better performance
     282    $content = $post->post_content;
     283    $content = strip_shortcodes( $content );
     284    $content = wp_strip_all_tags( $content );
     285    $excerpt = wp_trim_words( $content, 55, '...' );
     286
     287    return esc_attr( $excerpt );
    270288}
    271289
     
    412430
    413431    // Get logo details from attachment metadata (database) instead of file system
     432    // Use transients to persist attachment ID lookup across requests (avoids expensive query)
    414433    $w3p_kg_logo_width  = 0;
    415434    $w3p_kg_logo_height = 0;
    416435    if ( $w3p_kg_logo ) {
    417         // Try to get attachment ID from URL
    418         $attachment_id = attachment_url_to_postid( $w3p_kg_logo );
    419         if ( $attachment_id ) {
    420             // Get dimensions from attachment metadata (stored in database)
    421             $metadata = wp_get_attachment_metadata( $attachment_id );
    422             if ( $metadata && isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
    423                 $w3p_kg_logo_width  = (int) $metadata['width'];
    424                 $w3p_kg_logo_height = (int) $metadata['height'];
     436        // Transient key based on logo URL hash
     437        $transient_key = 'w3p_kg_logo_dimensions_' . md5( $w3p_kg_logo );
     438        $cached_dimensions = get_transient( $transient_key );
     439       
     440        if ( false !== $cached_dimensions ) {
     441            // Use cached dimensions
     442            $w3p_kg_logo_width  = (int) $cached_dimensions['width'];
     443            $w3p_kg_logo_height = (int) $cached_dimensions['height'];
     444        } else {
     445            // Try to get attachment ID from URL (expensive query - cache the result)
     446            $attachment_id_transient_key = 'w3p_kg_logo_attachment_id_' . md5( $w3p_kg_logo );
     447            $attachment_id = get_transient( $attachment_id_transient_key );
     448           
     449            if ( false === $attachment_id ) {
     450                // Only call attachment_url_to_postid if not in transient (expensive query)
     451                $attachment_id = attachment_url_to_postid( $w3p_kg_logo );
     452                // Cache the attachment ID for 7 days (logo URL rarely changes)
     453                set_transient( $attachment_id_transient_key, $attachment_id, WEEK_IN_SECONDS );
    425454            }
     455           
     456            if ( $attachment_id ) {
     457                // Get dimensions from attachment metadata (stored in database)
     458                $metadata = wp_get_attachment_metadata( $attachment_id );
     459                if ( $metadata && isset( $metadata['width'] ) && isset( $metadata['height'] ) ) {
     460                    $w3p_kg_logo_width  = (int) $metadata['width'];
     461                    $w3p_kg_logo_height = (int) $metadata['height'];
     462                }
     463            }
     464           
     465            // Cache dimensions for 7 days
     466            set_transient( $transient_key, [
     467                'width'  => $w3p_kg_logo_width,
     468                'height' => $w3p_kg_logo_height,
     469            ], WEEK_IN_SECONDS );
    426470        }
    427471    }
  • wp-perfect-plugin/trunk/modules/w3p-search-console.php

    r3440912 r3441000  
    220220    // Facebook
    221221    if ( ! has_post_thumbnail( $post->ID ) ) {
    222         if ( ! empty( get_option( 'w3p_fb_default_image' ) ) ) {
    223             $out .= '<meta property="og:image" content="' . esc_url( get_option( 'w3p_fb_default_image' ) ) . '">';
     222        $fb_default_image = get_option( 'w3p_fb_default_image' );
     223        if ( ! empty( $fb_default_image ) ) {
     224            $out .= '<meta property="og:image" content="' . esc_url( $fb_default_image ) . '">';
    224225        }
    225226    } else {
    226         $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
    227         $thumbnail_alt = esc_attr( get_post_meta( get_post_thumbnail_id( $post->ID ), '_wp_attachment_image_alt', true ) );
    228 
    229         if ( $thumbnail_src ) {
    230             $out .= '<meta property="og:image" content="' . esc_url( $thumbnail_src[0] ) . '">
    231             <meta property="og:image:width" content="' . esc_attr( $thumbnail_src[1] ) . '">
    232             <meta property="og:image:height" content="' . esc_attr( $thumbnail_src[2] ) . '">
    233             <meta property="og:image:alt" content="' . $thumbnail_alt . '">
    234             <meta property="twitter:image" content="' . esc_url( $thumbnail_src[0] ) . '">';
     227        // Get thumbnail ID once and reuse it
     228        $thumbnail_id = get_post_thumbnail_id( $post->ID );
     229        if ( $thumbnail_id ) {
     230            $thumbnail_src = wp_get_attachment_image_src( $thumbnail_id, 'full' );
     231            if ( $thumbnail_src ) {
     232                // Get alt text using the same thumbnail ID (avoid duplicate query)
     233                $thumbnail_alt = esc_attr( get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true ) );
     234                $out .= '<meta property="og:image" content="' . esc_url( $thumbnail_src[0] ) . '">
     235                <meta property="og:image:width" content="' . esc_attr( $thumbnail_src[1] ) . '">
     236                <meta property="og:image:height" content="' . esc_attr( $thumbnail_src[2] ) . '">
     237                <meta property="og:image:alt" content="' . $thumbnail_alt . '">
     238                <meta property="twitter:image" content="' . esc_url( $thumbnail_src[0] ) . '">';
     239            }
    235240        }
    236241    }
  • wp-perfect-plugin/trunk/modules/w3p-settings.php

    r3440912 r3441000  
    434434                    update_option( 'w3p_kg_type', sanitize_text_field( $_POST['w3p_kg_type'] ) );
    435435                    update_option( 'w3p_kg_name', sanitize_text_field( $_POST['w3p_kg_name'] ) );
    436                     update_option( 'w3p_kg_logo', sanitize_url( $_POST['w3p_kg_logo'] ) );
     436                   
     437                    $old_logo = get_option( 'w3p_kg_logo' );
     438                    $new_logo = sanitize_url( $_POST['w3p_kg_logo'] );
     439                    update_option( 'w3p_kg_logo', $new_logo );
     440                   
     441                    // Clear transients if logo changed
     442                    if ( $old_logo !== $new_logo ) {
     443                        if ( $old_logo ) {
     444                            delete_transient( 'w3p_kg_logo_dimensions_' . md5( $old_logo ) );
     445                            delete_transient( 'w3p_kg_logo_attachment_id_' . md5( $old_logo ) );
     446                        }
     447                        if ( $new_logo ) {
     448                            delete_transient( 'w3p_kg_logo_dimensions_' . md5( $new_logo ) );
     449                            delete_transient( 'w3p_kg_logo_attachment_id_' . md5( $new_logo ) );
     450                        }
     451                    }
    437452
    438453                    $w3p_kg_same_as = isset( $_POST['w3p_kg_same_as'] ) ? sanitize_textarea_field( $_POST['w3p_kg_same_as'] ) : '';
  • wp-perfect-plugin/trunk/readme.txt

    r3440912 r3441000  
    55Requires PHP: 7.1
    66Tested up to: 6.9
    7 Stable tag: 2.1.0
     7Stable tag: 2.1.1
    88License: GPLv3 or later
    99License URI: https://www.gnu.org/licenses/gpl-3.0.html
     
    3333
    3434== Changelog =
     35
     36= 2.1.1 =
     37* PERFORMANCE: Optimize w3p_get_excerpt() to avoid get_the_excerpt() overhead by using direct post field access
     38* PERFORMANCE: Fix duplicate get_post_thumbnail_id() calls in w3p_head_og()
     39* PERFORMANCE: Add transient caching for expensive attachment_url_to_postid() query (reduces query time from 0.0065s to 0s on cached requests)
     40* PERFORMANCE: Replace object cache with transients for better compatibility across all WordPress installations
    3541
    3642= 2.1.0 =
  • wp-perfect-plugin/trunk/wp-perfect-plugin.php

    r3440912 r3441000  
    66 * Author: Ciprian Popescu
    77 * Author URI: https://getbutterfly.com/
    8  * Version: 2.1.0
     8 * Version: 2.1.1
    99 * License: GNU General Public License v3 or later
    1010 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
     
    3535define( 'W3P_URL', WP_PLUGIN_URL . '/' . dirname( plugin_basename( __FILE__ ) ) );
    3636define( 'W3P_PATH', plugin_dir_path( __FILE__ ) );
    37 define( 'W3P_VERSION', '2.1.0' );
     37define( 'W3P_VERSION', '2.1.1' );
    3838
    3939require 'includes/functions.php';
Note: See TracChangeset for help on using the changeset viewer.