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