Pull images from wordpress.org

Is it possible even via filter to have it copy/store the image for each plugin from wordpress.org/plugins. IE the Square image for each plugin available at https://ps.w.org/{{plugin_slug}}/assets/icon-128x128.png

Just a thought, would automate the images unless you choose to override them.

  • Rupok

    Hi danieliser

    Hope you had a wonderful day.

    I'm afraid, you can't copy/store the image for each plugin from wordpress.org/plugins but you can do this with custom development.

    Is it possible even via filter

    Our plugin developer can give you best idea regarding this. So I'm flagging our developer here as he can give you best idea regarding this.

    Please keep in mind, our developers work round the clock and they have to deal with lots of critical issues and other things. So it may take a little while for them to check this and provide a feedback.

    Have a nice day. Cheers!
    Rupok

  • danieliser

    Figured I would post this here for anyone else that wants it. First you need to add this filter around line 81 of pretty-plugins/includes/functions.php

    $screenshot_value = apply_filters( 'wmd_prettyplugins_screenshot_value', $screenshot_value, $plugin_path, $get_default );

    Then in your own plugin, themes functions.php, mu-plugin etc use this.

    <?php
    
    add_filter( 'wmd_prettyplugins_screenshot_value', function ( $screenshot_value, $plugin_path, $get_default ) {
    
    	static $cache = null;
    	if ( $cache === null ) {
    		$cache = get_site_option( 'wmd_prettyplugins_screenshot_list', array() );
    	}
    
    	$plugin_path_slug = str_replace('.php', '', str_replace('/', '-', $plugin_path));
    
    	$plugin_slug = explode( '/', $plugin_path )[0];
    
    	if ( isset( $cache[ $plugin_slug ] ) ) {
    		return $cache[ $plugin_slug ];
    	}
    
    	// Modify the upload dir.
    	add_filter( 'upload_dir', 'wmd_prettyplugins_set_upload_dir' );
    
    	$upload = wp_upload_dir();
    
    	$image_path = $upload['path'] . '/' . $plugin_path_slug . '.png';
    	$image_url = $upload['url'] . '/' . $plugin_path_slug . '.png';
    
    	if ( file_exists( $image_path ) ) {
    		$cache[ $plugin_slug ] = $image_url;
    	} else {
    
    		$image = "http://ps.w.org/$plugin_slug/assets/icon-256x256.png";
    
    		$get = wp_remote_get( $image );
    
    		if ( wp_remote_retrieve_response_code( $get ) == 200 ) {
    			// Mirror this image into the custom prettyplugins directory
    			$mirror = wp_upload_bits( $plugin_path_slug . '.png', '', wp_remote_retrieve_body( $get ) );
    
    			if ( ! $mirror['error'] ) {
    				$cache[ $plugin_slug ] = $image_url;
    			}
    		}
    	}
    
    	if ( ! isset( $cache[ $plugin_slug ] ) ) {
    		$cache[ $plugin_slug ] = $screenshot_value;
    	}
    
    	// Reset the upload dir.
    	remove_filter( 'upload_dir', 'wmd_prettyplugins_set_upload_dir' );
    
    	update_site_option( 'wmd_prettyplugins_screenshot_list', $cache );
    	return $cache[ $plugin_slug ];
    
    }, 10, 3 );
    
    function wmd_prettyplugins_set_upload_dir( $upload ) {
    	$upload['subdir'] = '/prettyplugins/screenshots';
    	$upload['path']   = $upload['basedir'] . $upload['subdir'];
    	$upload['url']    = $upload['baseurl'] . $upload['subdir'];
    	return $upload;
    }
  • danieliser

    Here are some screenshots showing it working.

    I will add currently the first time you load the plugins page will take quite a bit longer as it saves all of the images it finds. All non found images will be skipped. You could add a transient to recheck at a later time for those that came up empty, but since a lot of plugins won't be available via the wordpress.org repo, such as those from WPMUDev, it won't work for them all.

  • danieliser

    Here is a full on mu-plugin that will handle everything except adding the new filter. That would need to be added via WPMUDev team or manually yourself to be able to filter them correctly.

    <?php
    /**
     * Pretty Plugin Automatic Thumbnail Downloader
     *
     * Automatically downloads wordpress.org plugin images for each plugin if available.
     *
     * Plugin Name: Pretty Plugin Automatic Thumbnail Downloader
     * Plugin URI:
     * Description:  Automatically downloads wordpress.org plugin images for each plugin if available.
     * Author: Daniel Iser
     * Author URI: http://presspowered.com
     * Version: 1.0
     */
    
    add_filter( 'wmd_prettyplugins_screenshot_value', function ( $screenshot_value, $plugin_path, $get_default ) {
    
    	static $cache = null;
    	if ( $cache === null ) {
    		$cache = get_site_option( 'wmd_prettyplugins_screenshot_list', array() );
    	}
    
    	$plugin_path_slug = str_replace('.php', '', str_replace('/', '-', $plugin_path));
    
    	$plugin_slug = explode( '/', $plugin_path )[0];
    
    	if ( isset( $cache[ $plugin_slug ] ) ) {
    		return $cache[ $plugin_slug ];
    	}
    
    	// Modify the upload dir.
    	add_filter( 'upload_dir', 'wmd_prettyplugins_set_upload_dir' );
    
    	$upload = wp_upload_dir();
    
    	$image_path = $upload['path'] . '/' . $plugin_path_slug . '.png';
    	$image_url = $upload['url'] . '/' . $plugin_path_slug . '.png';
    
    	if ( file_exists( $image_path ) ) {
    		$cache[ $plugin_slug ] = $image_url;
    	} else {
    
    		$image = "http://ps.w.org/$plugin_slug/assets/icon-256x256.png";
    
    		$get = wp_remote_get( $image );
    
    		if ( wp_remote_retrieve_response_code( $get ) == 200 ) {
    			// Mirror this image into the custom prettyplugins directory
    			$mirror = wp_upload_bits( $plugin_path_slug . '.png', '', wp_remote_retrieve_body( $get ) );
    
    			if ( ! $mirror['error'] ) {
    				$cache[ $plugin_slug ] = $image_url;
    			}
    		}
    	}
    
    	if ( ! isset( $cache[ $plugin_slug ] ) ) {
    		$cache[ $plugin_slug ] = $screenshot_value;
    	}
    
    	// Reset the upload dir.
    	remove_filter( 'upload_dir', 'wmd_prettyplugins_set_upload_dir' );
    
    	update_site_option( 'wmd_prettyplugins_screenshot_list', $cache );
    	return $cache[ $plugin_slug ];
    
    }, 10, 3 );
    
    function wmd_prettyplugins_set_upload_dir( $upload ) {
    	$upload['subdir'] = '/prettyplugins/screenshots';
    	$upload['path']   = $upload['basedir'] . $upload['subdir'];
    	$upload['url']    = $upload['baseurl'] . $upload['subdir'];
    	return $upload;
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.