Changeset 3390203
- Timestamp:
- 11/05/2025 07:30:46 AM (4 months ago)
- Location:
- local-gravatars
- Files:
-
- 10 added
- 2 edited
-
tags/1.1.3 (added)
-
tags/1.1.3/LICENSE (added)
-
tags/1.1.3/includes (added)
-
tags/1.1.3/includes/class-local-gravatars.php (added)
-
tags/1.1.3/local-gravatars.php (added)
-
tags/1.1.3/readme.txt (added)
-
tags/1.1.3/uninstall.php (added)
-
trunk/includes (added)
-
trunk/includes/class-local-gravatars.php (added)
-
trunk/local-gravatars.php (modified) (4 diffs)
-
trunk/readme.txt (modified) (1 diff)
-
trunk/uninstall.php (added)
Legend:
- Unmodified
- Added
- Removed
-
local-gravatars/trunk/local-gravatars.php
r3337085 r3390203 3 3 * Plugin Name: Local Gravatars 4 4 * Plugin URI: https://github.com/aristath/local-gravatars 5 * Description: Locally host gravatars - for the privacy con cious5 * Description: Locally host gravatars - for the privacy conscious 6 6 * Requires at least: 5.3 7 7 * Requires PHP: 5.6 8 * Version: 1.1. 28 * Version: 1.1.3 9 9 * Author: Ari Stathopoulos 10 10 * Text Domain: local-gravatars … … 14 14 */ 15 15 16 namespace Aristath\LocalGravatars; 16 require_once __DIR__ . '/includes/class-local-gravatars.php'; 17 18 use Aristath\LocalGravatars\LocalGravatars; 19 20 // Initialize the plugin hooks and schedules. 21 LocalGravatars::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 */ 29 function local_gravatars_process_url( $url ) { 30 $local_gravatars = new LocalGravatars( $url ); 31 return $local_gravatars->get_gravatar(); 32 } 17 33 18 34 add_filter( … … 25 41 */ 26 42 function( $avatar ) { 43 // Process srcset attribute. 27 44 preg_match_all( '/srcset=["\']?((?:.(?!["\']?\s+(?:\S+)=|\s*\/?[>"\']))+.)["\']?/', $avatar, $srcset ); 28 45 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 ); 32 48 } 33 49 50 // Process src attribute. 34 51 preg_match_all( '/src=["\']?((?:.(?!["\']?\s+(?:\S+)=|\s*\/?[>"\']))+.)["\']?/', $avatar, $src ); 35 52 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 ); 39 55 } 40 56 return $avatar; … … 42 58 \PHP_INT_MAX 43 59 ); 44 45 /**46 * Download gravatars locally.47 */48 class LocalGravatars {49 50 /**51 * The remote URL.52 *53 * @access protected54 * @since 1.1.055 * @var string56 */57 protected $remote_url;58 59 /**60 * Base path.61 *62 * @access protected63 * @since 1.1.064 * @var string65 */66 protected $base_path;67 68 /**69 * Subfolder name.70 *71 * @access protected72 * @since 1.1.073 * @var string74 */75 protected $subfolder_name;76 77 /**78 * Base URL.79 *80 * @access protected81 * @since 1.1.082 * @var string83 */84 protected $base_url;85 86 /**87 * The gravatars folder.88 *89 * @access protected90 * @since 1.1.091 * @var string92 */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.0104 */105 const MAX_PROCESS_TIME = 5;106 107 /**108 * Start tim of all processes.109 *110 * @static111 *112 * @access private113 *114 * @since 1.0.1115 *116 * @var int117 */118 private static $start_time;119 120 /**121 * Set to true if we want to stop processing.122 *123 * @static124 *125 * @access private126 *127 * @since 1.0.1128 *129 * @var bool130 */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 public139 * @since 1.1.0140 * @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 public154 * @since 1.0.0155 * @return string156 */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 public201 * @since 1.1.0202 * @return string203 */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 public218 * @since 1.1.0219 * @return string220 */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 public239 * @since 1.1.0240 * @return void241 */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 public260 * @since 1.1.0261 * @return bool262 */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 protected271 * @since 1.0.0272 * @return WP_Filesystem273 */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 public291 *292 * @since 1.0.1293 *294 * @return bool295 */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 public322 *323 * @since 1.0.1324 *325 * @return int326 */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 image333 *334 * @access public335 *336 * @since 1.0.1337 *338 * @return string339 */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 4 4 Tested up to: 6.8 5 5 Requires PHP: 5.6 6 Stable tag: 1.1. 26 Stable tag: 1.1.3 7 7 License: MIT 8 8 License URI: https://opensource.org/licenses/MIT
Note: See TracChangeset
for help on using the changeset viewer.