Plugin Directory

Changeset 3390203


Ignore:
Timestamp:
11/05/2025 07:30:46 AM (4 months ago)
Author:
aristath
Message:

v1.1.3

Location:
local-gravatars
Files:
10 added
2 edited

Legend:

Unmodified
Added
Removed
  • local-gravatars/trunk/local-gravatars.php

    r3337085 r3390203  
    33 * Plugin Name: Local Gravatars
    44 * Plugin URI: https://github.com/aristath/local-gravatars
    5  * Description: Locally host gravatars - for the privacy concious
     5 * Description: Locally host gravatars - for the privacy conscious
    66 * Requires at least: 5.3
    77 * Requires PHP: 5.6
    8  * Version: 1.1.2
     8 * Version: 1.1.3
    99 * Author: Ari Stathopoulos
    1010 * Text Domain: local-gravatars
     
    1414 */
    1515
    16 namespace Aristath\LocalGravatars;
     16require_once __DIR__ . '/includes/class-local-gravatars.php';
     17
     18use Aristath\LocalGravatars\LocalGravatars;
     19
     20// Initialize the plugin hooks and schedules.
     21LocalGravatars::init();
     22
     23/**
     24 * Process a URL and replace it with the local gravatar URL.
     25 *
     26 * @param string $url The URL to process.
     27 * @return string The local gravatar URL, or the filtered fallback URL if processing failed.
     28 */
     29function local_gravatars_process_url( $url ) {
     30    $local_gravatars = new LocalGravatars( $url );
     31    return $local_gravatars->get_gravatar();
     32}
    1733
    1834add_filter(
     
    2541     */
    2642    function( $avatar ) {
     43        // Process srcset attribute.
    2744        preg_match_all( '/srcset=["\']?((?:.(?!["\']?\s+(?:\S+)=|\s*\/?[>"\']))+.)["\']?/', $avatar, $srcset );
    2845        if ( isset( $srcset[1] ) && isset( $srcset[1][0] ) ) {
    29             $url             = explode( ' ', $srcset[1][0] )[0];
    30             $local_gravatars = new LocalGravatars( $url );
    31             $avatar          = str_replace( $url, $local_gravatars->get_gravatar(), $avatar );
     46            $url    = explode( ' ', $srcset[1][0] )[0];
     47            $avatar = str_replace( $url, local_gravatars_process_url( $url ), $avatar );
    3248        }
    3349
     50        // Process src attribute.
    3451        preg_match_all( '/src=["\']?((?:.(?!["\']?\s+(?:\S+)=|\s*\/?[>"\']))+.)["\']?/', $avatar, $src );
    3552        if ( isset( $src[1] ) && isset( $src[1][0] ) ) {
    36             $url             = explode( ' ', $src[1][0] )[0];
    37             $local_gravatars = new LocalGravatars( $url );
    38             $avatar          = str_replace( $url, $local_gravatars->get_gravatar(), $avatar );
     53            $url    = explode( ' ', $src[1][0] )[0];
     54            $avatar = str_replace( $url, local_gravatars_process_url( $url ), $avatar );
    3955        }
    4056        return $avatar;
     
    4258    \PHP_INT_MAX
    4359);
    44 
    45 /**
    46  * Download gravatars locally.
    47  */
    48 class LocalGravatars {
    49 
    50     /**
    51      * The remote URL.
    52      *
    53      * @access protected
    54      * @since 1.1.0
    55      * @var string
    56      */
    57     protected $remote_url;
    58 
    59     /**
    60      * Base path.
    61      *
    62      * @access protected
    63      * @since 1.1.0
    64      * @var string
    65      */
    66     protected $base_path;
    67 
    68     /**
    69      * Subfolder name.
    70      *
    71      * @access protected
    72      * @since 1.1.0
    73      * @var string
    74      */
    75     protected $subfolder_name;
    76 
    77     /**
    78      * Base URL.
    79      *
    80      * @access protected
    81      * @since 1.1.0
    82      * @var string
    83      */
    84     protected $base_url;
    85 
    86     /**
    87      * The gravatars folder.
    88      *
    89      * @access protected
    90      * @since 1.1.0
    91      * @var string
    92      */
    93     protected $gravatars_folder;
    94 
    95     /**
    96      * Cleanup routine frequency.
    97      */
    98     const CLEANUP_FREQUENCY = 'weekly';
    99 
    100     /**
    101      * Maxiumum process seconds.
    102      *
    103      * @since 1.0
    104      */
    105     const MAX_PROCESS_TIME = 5;
    106 
    107     /**
    108      * Start tim of all processes.
    109      *
    110      * @static
    111      *
    112      * @access private
    113      *
    114      * @since 1.0.1
    115      *
    116      * @var int
    117      */
    118     private static $start_time;
    119 
    120     /**
    121      * Set to true if we want to stop processing.
    122      *
    123      * @static
    124      *
    125      * @access private
    126      *
    127      * @since 1.0.1
    128      *
    129      * @var bool
    130      */
    131     private static $has_stopped = false;
    132 
    133     /**
    134      * Constructor.
    135      *
    136      * Get a new instance of the object for a new URL.
    137      *
    138      * @access public
    139      * @since 1.1.0
    140      * @param string $url The remote URL.
    141      */
    142     public function __construct( $url = '' ) {
    143         $this->remote_url = $url;
    144 
    145         // Add a cleanup routine.
    146         $this->schedule_cleanup();
    147         add_action( 'delete_gravatars_folder', array( $this, 'delete_gravatars_folder' ) );
    148     }
    149 
    150     /**
    151      * Get the local file's URL.
    152      *
    153      * @access public
    154      * @since 1.0.0
    155      * @return string
    156      */
    157     public function get_gravatar() {
    158 
    159         // Early exit if we don't want to process.
    160         if ( ! $this->should_process() ) {
    161             return $this->get_fallback_url();
    162         }
    163 
    164         // If the gravatars folder doesn't exist, create it.
    165         if ( ! file_exists( $this->get_base_path() ) ) {
    166             $this->get_filesystem()->mkdir( $this->get_base_path(), FS_CHMOD_DIR );
    167         }
    168 
    169         // Get the filename.
    170         $filename = basename( wp_parse_url( $this->remote_url, PHP_URL_PATH ) );
    171 
    172         $path = $this->get_base_path() . '/' . $filename;
    173 
    174         // Check if the file already exists.
    175         if ( ! file_exists( $path ) ) {
    176 
    177             // require file.php if the download_url function doesn't exist.
    178             if ( ! function_exists( 'download_url' ) ) {
    179                 require_once wp_normalize_path( ABSPATH . '/wp-admin/includes/file.php' );
    180             }
    181 
    182             // Download file to temporary location.
    183             $tmp_path = download_url( $this->remote_url );
    184 
    185             // Make sure there were no errors.
    186             if ( ! is_wp_error( $tmp_path ) ) {
    187                 // Move temp file to final destination.
    188                 $success = $this->get_filesystem()->move( $tmp_path, $path, true );
    189                 if ( ! $success ) {
    190                     return $this->get_fallback_url();
    191                 }
    192             }
    193         }
    194         return $this->get_base_url() . '/' . $filename;
    195     }
    196 
    197     /**
    198      * Get the base path.
    199      *
    200      * @access public
    201      * @since 1.1.0
    202      * @return string
    203      */
    204     public function get_base_path() {
    205         if ( ! $this->base_path ) {
    206             $this->base_path = apply_filters(
    207                 'get_local_gravatars_base_path',
    208                 $this->get_filesystem()->wp_content_dir() . '/gravatars'
    209             );
    210         }
    211         return $this->base_path;
    212     }
    213 
    214     /**
    215      * Get the base URL.
    216      *
    217      * @access public
    218      * @since 1.1.0
    219      * @return string
    220      */
    221     public function get_base_url() {
    222         if ( ! $this->base_url ) {
    223             $this->base_url = apply_filters(
    224                 'get_local_gravatars_base_url',
    225                 content_url() . '/gravatars'
    226             );
    227         }
    228         return $this->base_url;
    229     }
    230 
    231     /**
    232      * Schedule a cleanup.
    233      *
    234      * Deletes the gravatars files on a regular basis.
    235      * This way gravatars will get updated regularly,
    236      * and we avoid edge cases where unused files remain in the server.
    237      *
    238      * @access public
    239      * @since 1.1.0
    240      * @return void
    241      */
    242     public function schedule_cleanup() {
    243         if ( ! is_multisite() || ( is_multisite() && is_main_site() ) ) {
    244             if ( ! wp_next_scheduled( 'delete_gravatars_folder' ) && ! wp_installing() ) {
    245                 wp_schedule_event(
    246                     time(),
    247                     apply_filters( 'get_local_gravatars_cleanup_frequency', self::CLEANUP_FREQUENCY ),
    248                     'delete_gravatars_folder'
    249                 );
    250             }
    251         }
    252     }
    253 
    254     /**
    255      * Delete the gravatars folder.
    256      *
    257      * This runs as part of a cleanup routine.
    258      *
    259      * @access public
    260      * @since 1.1.0
    261      * @return bool
    262      */
    263     public function delete_gravatars_folder() {
    264         return $this->get_filesystem()->delete( $this->get_base_path(), true );
    265     }
    266 
    267     /**
    268      * Get the filesystem.
    269      *
    270      * @access protected
    271      * @since 1.0.0
    272      * @return WP_Filesystem
    273      */
    274     protected function get_filesystem() {
    275         global $wp_filesystem;
    276 
    277         // If the filesystem has not been instantiated yet, do it here.
    278         if ( ! $wp_filesystem ) {
    279             if ( ! function_exists( 'WP_Filesystem' ) ) {
    280                 require_once wp_normalize_path( ABSPATH . '/wp-admin/includes/file.php' );
    281             }
    282             \WP_Filesystem();
    283         }
    284         return $wp_filesystem;
    285     }
    286 
    287     /**
    288      * Should we process or not?
    289      *
    290      * @access public
    291      *
    292      * @since 1.0.1
    293      *
    294      * @return bool
    295      */
    296     public function should_process() {
    297 
    298         // Early exit if we've already determined we want to stop.
    299         if ( self::$has_stopped ) {
    300             return false;
    301         }
    302 
    303         // Set the start time.
    304         if ( ! self::$start_time ) {
    305             self::$start_time = time();
    306         }
    307 
    308         // Return false if we've got over the max time limit.
    309         if ( time() > self::$start_time + $this->get_max_process_time() ) {
    310             self::$has_stopped = true;
    311             return false;
    312         }
    313 
    314         // Falback to true.
    315         return true;
    316     }
    317 
    318     /**
    319      * Get maximum process time in seconds.
    320      *
    321      * @access public
    322      *
    323      * @since 1.0.1
    324      *
    325      * @return int
    326      */
    327     public function get_max_process_time() {
    328         return apply_filters( 'get_local_gravatars_max_process_time', self::MAX_PROCESS_TIME );
    329     }
    330 
    331     /**
    332      * Get fallback image
    333      *
    334      * @access public
    335      *
    336      * @since 1.0.1
    337      *
    338      * @return string
    339      */
    340     public function get_fallback_url() {
    341         return apply_filters( 'get_local_gravatars_fallback_url', '', $this->remote_url );
    342     }
    343 }
  • local-gravatars/trunk/readme.txt

    r3337085 r3390203  
    44Tested up to: 6.8
    55Requires PHP: 5.6
    6 Stable tag: 1.1.2
     6Stable tag: 1.1.3
    77License: MIT
    88License URI: https://opensource.org/licenses/MIT
Note: See TracChangeset for help on using the changeset viewer.