• Resolved michaelrhmw

    (@michaelrhmw)


    Hi there,

    I’m using this plugin on a website, however I’ve noticed that the plugin created a few performance issues for us.

    I have modified the plugin slightly, just thought that you might be interested in the changes.

    The changes have been made to the resolve_image_id function in /wp-content/plugins/gallery-custom-links/mgcl_core.php

    Original:

    
    	function resolve_image_id( $url ) {
    		global $wpdb;
    		$pattern = '/[_-]\d+x\d+(?=\.[a-z]{3,4}$)/';
    		$url = preg_replace( $pattern, '', $url );
    		$url = $this->get_pathinfo_from_image_src( $url );
    		$urlLike = '%' . $url . '%';
    		$query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid LIKE '%s'", $urlLike );
    		$attachment = $wpdb->get_col( $query );
    
    		// Code proposed by @wizardcoder
    		// https://www.ads-software.com/support/topic/issue-with-page-links-on-images/
    		if ( empty( $attachment ) ) {
    			$query = $wpdb->prepare( 
    				"SELECT post_id FROM $wpdb->postmeta WHERE meta_value LIKE '%s' AND meta_key = '_wp_attached_file'", 
    				$urlLike 
    			);
    			$attachment = $wpdb->get_col( $query );
    		}
    
    		return empty( $attachment ) ? null : $attachment[0];
    	}
    

    Modified:
    I added ” Limit 1 ” to the SQL queries to make them stop searching if they find a result.
    I also added a “cache” array to stop multiple SQL queries from being executed for the same url on a single page load.

    
    function resolve_image_id( $url ) {
    		global $wpdb;
    		global $galleryCustomLinksCache;
    		
    		if(!is_array($galleryCustomLinksCache)){
    		    $galleryCustomLinksCache = [];
    		}
    		
    		$pattern = '/[_-]\d+x\d+(?=\.[a-z]{3,4}$)/';
    		$url = preg_replace( $pattern, '', $url );
    		$url = $this->get_pathinfo_from_image_src( $url );
    		$urlLike = '%' . $url . '%';
    		
    		if(array_key_exists($urlLike, $galleryCustomLinksCache)){
    		    $attachment = $galleryCustomLinksCache[$urlLike];   
    		}else{
        		$query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid LIKE '%s' LIMIT 1", $urlLike );
        		$attachment = $wpdb->get_col( $query );
        
        		// Code proposed by @wizardcoder
        		// https://www.ads-software.com/support/topic/issue-with-page-links-on-images/
        		if ( empty( $attachment ) ) {
        			$query = $wpdb->prepare( 
        				"SELECT post_id FROM $wpdb->postmeta WHERE meta_value LIKE '%s' AND meta_key = '_wp_attached_file' LIMIT 1", 
        				$urlLike 
        			);
        			$attachment = $wpdb->get_col( $query );
        		}
        		
        		$galleryCustomLinksCache[$urlLike] = $attachment;   
    		}
    
    		return empty( $attachment ) ? null : $attachment[0];
    	}
    
Viewing 1 replies (of 1 total)
  • Plugin Author Jordy Meow

    (@tigroumeow)

    Hi,

    Sorry for the late reply about this. I am reviewing a lot of parts of this plugin. I have tested your changes and I will also include them in the next release. So thanks a lot for this ?? I really appreciate it!

Viewing 1 replies (of 1 total)
  • The topic ‘Duplicate Queries, Slowness’ is closed to new replies.