admin
1 year ago
cache
1 year ago
css
1 year ago
images
1 year ago
javascript
1 year ago
vendor
1 year ago
class-cwvpsb-treeshaking.php
1 year ago
cwvpsb_iframe.css
2 years ago
cwvpsb_iframe.js
4 years ago
functions.php
1 year ago
gravatar.php
1 year ago
helper-section.php
2 years ago
gravatar.php
162 lines
| 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly. |
| 4 | } |
| 5 | |
| 6 | add_filter('get_avatar','cwvpsb_gravatar_locally'); |
| 7 | |
| 8 | function cwvpsb_gravatar_locally($avatar){ |
| 9 | preg_match_all( '/srcset=["\']?((?:.(?!["\']?\s+(?:\S+)=|\s*\/?[>"\']))+.)["\']?/', $avatar, $srcset ); |
| 10 | if ( isset( $srcset[1] ) && isset( $srcset[1][0] ) ) { |
| 11 | $url = explode( ' ', $srcset[1][0] )[0]; |
| 12 | $local_gravatars = new CWVPSB_Local_Gravatars( $url ); |
| 13 | $avatar = str_replace( $url, $local_gravatars->get_gravatar(), $avatar ); |
| 14 | } |
| 15 | preg_match_all( '/src=["\']?((?:.(?!["\']?\s+(?:\S+)=|\s*\/?[>"\']))+.)["\']?/', $avatar, $src ); |
| 16 | if ( isset( $src[1] ) && isset( $src[1][0] ) ) { |
| 17 | $url = explode( ' ', $src[1][0] )[0]; |
| 18 | $local_gravatars = new CWVPSB_Local_Gravatars( $url ); |
| 19 | $avatar = str_replace( $url, $local_gravatars->get_gravatar(), $avatar ); |
| 20 | } |
| 21 | return $avatar; |
| 22 | } |
| 23 | |
| 24 | class CWVPSB_Local_Gravatars { |
| 25 | |
| 26 | protected $remote_url; |
| 27 | protected $base_path; |
| 28 | protected $subfolder_name; |
| 29 | protected $base_url; |
| 30 | protected $gravatars_folder; |
| 31 | |
| 32 | const CLEANUP_FREQUENCY = 'weekly'; |
| 33 | const MAX_PROCESS_TIME = 5; |
| 34 | |
| 35 | private static $start_time; |
| 36 | private static $has_stopped = false; |
| 37 | public function __construct( $url = '' ) { |
| 38 | $this->remote_url = $url; |
| 39 | |
| 40 | // Add a cleanup routine. |
| 41 | $this->schedule_cleanup(); |
| 42 | add_action( 'delete_gravatars_folder', array( $this, 'delete_gravatars_folder' ) ); |
| 43 | } |
| 44 | public function get_gravatar() { |
| 45 | |
| 46 | // Early exit if we don't want to process. |
| 47 | if ( ! $this->should_process() ) { |
| 48 | return $this->get_fallback_url(); |
| 49 | } |
| 50 | |
| 51 | // If the gravatars folder doesn't exist, create it. |
| 52 | if ( ! file_exists( $this->get_base_path() ) ) { |
| 53 | $this->get_filesystem()->mkdir( $this->get_base_path(), FS_CHMOD_DIR ); |
| 54 | } |
| 55 | |
| 56 | // Get the filename. |
| 57 | $filename = basename( wp_parse_url( $this->remote_url, PHP_URL_PATH ) ); |
| 58 | |
| 59 | $path = $this->get_base_path() . '/' . $filename; |
| 60 | |
| 61 | // Check if the file already exists. |
| 62 | if ( ! file_exists( $path ) ) { |
| 63 | |
| 64 | // require file.php if the download_url function doesn't exist. |
| 65 | if ( ! function_exists( 'download_url' ) ) { |
| 66 | require_once wp_normalize_path( ABSPATH . '/wp-admin/includes/file.php' ); |
| 67 | } |
| 68 | |
| 69 | // Download file to temporary location. |
| 70 | $tmp_path = download_url( $this->remote_url ); |
| 71 | |
| 72 | // Make sure there were no errors. |
| 73 | if ( ! is_wp_error( $tmp_path ) ) { |
| 74 | // Move temp file to final destination. |
| 75 | $success = $this->get_filesystem()->move( $tmp_path, $path, true ); |
| 76 | if ( ! $success ) { |
| 77 | return $this->get_fallback_url(); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | return $this->get_base_url() . '/' . $filename; |
| 82 | } |
| 83 | |
| 84 | public function get_base_path() { |
| 85 | if ( ! $this->base_path ) { |
| 86 | $this->base_path = apply_filters( |
| 87 | 'get_local_gravatars_base_path', |
| 88 | $this->get_filesystem()->wp_content_dir() . '/gravatars' |
| 89 | ); |
| 90 | } |
| 91 | return $this->base_path; |
| 92 | } |
| 93 | |
| 94 | public function get_base_url() { |
| 95 | if ( ! $this->base_url ) { |
| 96 | $this->base_url = apply_filters( |
| 97 | 'get_local_gravatars_base_url', |
| 98 | content_url() . '/gravatars' |
| 99 | ); |
| 100 | } |
| 101 | return $this->base_url; |
| 102 | } |
| 103 | |
| 104 | public function schedule_cleanup() { |
| 105 | if ( ! is_multisite() || ( is_multisite() && is_main_site() ) ) { |
| 106 | if ( ! wp_next_scheduled( 'delete_gravatars_folder' ) && ! wp_installing() ) { |
| 107 | wp_schedule_event( |
| 108 | time(), |
| 109 | apply_filters( 'get_local_gravatars_cleanup_frequency', self::CLEANUP_FREQUENCY ), |
| 110 | 'delete_gravatars_folder' |
| 111 | ); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public function delete_gravatars_folder() { |
| 117 | return $this->get_filesystem()->delete( $this->get_base_path(), true ); |
| 118 | } |
| 119 | |
| 120 | protected function get_filesystem() { |
| 121 | global $wp_filesystem; |
| 122 | |
| 123 | // If the filesystem has not been instantiated yet, do it here. |
| 124 | if ( ! $wp_filesystem ) { |
| 125 | if ( ! function_exists( 'WP_Filesystem' ) ) { |
| 126 | require_once wp_normalize_path( ABSPATH . '/wp-admin/includes/file.php' ); |
| 127 | } |
| 128 | \WP_Filesystem(); |
| 129 | } |
| 130 | return $wp_filesystem; |
| 131 | } |
| 132 | |
| 133 | public function should_process() { |
| 134 | |
| 135 | // Early exit if we've already determined we want to stop. |
| 136 | if ( self::$has_stopped ) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | // Set the start time. |
| 141 | if ( ! self::$start_time ) { |
| 142 | self::$start_time = time(); |
| 143 | } |
| 144 | |
| 145 | // Return false if we've got over the max time limit. |
| 146 | if ( time() > self::$start_time + $this->get_max_process_time() ) { |
| 147 | self::$has_stopped = true; |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | // Falback to true. |
| 152 | return true; |
| 153 | } |
| 154 | |
| 155 | public function get_max_process_time() { |
| 156 | return apply_filters( 'get_local_gravatars_max_process_time', self::MAX_PROCESS_TIME ); |
| 157 | } |
| 158 | |
| 159 | public function get_fallback_url() { |
| 160 | return apply_filters( 'get_local_gravatars_fallback_url', '', $this->remote_url ); |
| 161 | } |
| 162 | } |