1.6.3 版本
完全重写了 wpavatar 插件,全新功能
341
assets/css/admin.css
Normal file
|
@ -0,0 +1,341 @@
|
||||||
|
|
||||||
|
.wpavatar-settings .form-table th {
|
||||||
|
width: 220px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-settings .description {
|
||||||
|
font-size: 13px;
|
||||||
|
margin: 2px 0 5px;
|
||||||
|
color: #666;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-tabs-wrapper {
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-sync-tabs {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 5px;
|
||||||
|
border-bottom: 1px solid #c3c4c7;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-tab {
|
||||||
|
padding: 8px 16px;
|
||||||
|
border: none;
|
||||||
|
background: none;
|
||||||
|
cursor: pointer;
|
||||||
|
font-size: 14px;
|
||||||
|
border-bottom: 2px solid transparent;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-tab.active {
|
||||||
|
border-bottom: 2px solid #007cba;
|
||||||
|
font-weight: 600;
|
||||||
|
background: #f0f0f1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-tab:hover:not(.active) {
|
||||||
|
background: #f0f0f1;
|
||||||
|
border-bottom-color: #dcdcde;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-card {
|
||||||
|
background: #fff;
|
||||||
|
border: 1px solid #ccd0d4;
|
||||||
|
border-radius: 4px;
|
||||||
|
margin-top: 20px;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-card h2 {
|
||||||
|
margin-top: 0;
|
||||||
|
padding-bottom: 10px;
|
||||||
|
border-bottom: 1px solid #f0f0f1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-section-desc {
|
||||||
|
color: #666;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-stats-card {
|
||||||
|
background: #f9f9f9;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 15px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-stats-card h3 {
|
||||||
|
margin-top: 0;
|
||||||
|
margin-bottom: 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.cache-stats-wrapper {
|
||||||
|
margin-top: 15px;
|
||||||
|
border-left: 4px solid #2271b1;
|
||||||
|
background: #f0f6fc;
|
||||||
|
padding: 12px;
|
||||||
|
margin-bottom: 15px;
|
||||||
|
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);
|
||||||
|
}
|
||||||
|
|
||||||
|
.cache-stats p {
|
||||||
|
margin: 0.5em 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.cache-stats .error {
|
||||||
|
background: #ffebee;
|
||||||
|
padding: 10px;
|
||||||
|
border-left: 4px solid #d32f2f;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-action-buttons {
|
||||||
|
display: flex;
|
||||||
|
gap: 10px;
|
||||||
|
margin-top: 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-submit-wrapper {
|
||||||
|
margin-top: 20px;
|
||||||
|
padding-top: 15px;
|
||||||
|
border-top: 1px solid #f0f0f1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-switch {
|
||||||
|
position: relative;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-switch input {
|
||||||
|
opacity: 0;
|
||||||
|
width: 0px;
|
||||||
|
height: 0;
|
||||||
|
min-width: 0;
|
||||||
|
margin: -.25rem -0.15rem 0 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-slider {
|
||||||
|
position: relative;
|
||||||
|
display: inline-block;
|
||||||
|
width: 40px;
|
||||||
|
height: 20px;
|
||||||
|
background-color: #ccc;
|
||||||
|
border-radius: 34px;
|
||||||
|
transition: .4s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-slider:before {
|
||||||
|
position: absolute;
|
||||||
|
content: "";
|
||||||
|
height: 16px;
|
||||||
|
width: 16px;
|
||||||
|
left: 2px;
|
||||||
|
bottom: 2px;
|
||||||
|
background-color: white;
|
||||||
|
border-radius: 50%;
|
||||||
|
transition: .4s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-switch input:checked + .wpavatar-slider {
|
||||||
|
background-color: #2196F3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-switch input:focus + .wpavatar-slider {
|
||||||
|
box-shadow: 0 0 1px #2196F3;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-switch input:checked + .wpavatar-slider:before {
|
||||||
|
transform: translateX(20px);
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-switch-label {
|
||||||
|
margin-left: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-radio {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-radio-label {
|
||||||
|
margin-left: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-input {
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
padding: 6px 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-input:focus {
|
||||||
|
border-color: #2271b1;
|
||||||
|
box-shadow: 0 0 0 1px #2271b1;
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-select {
|
||||||
|
border: 1px solid #ddd;
|
||||||
|
padding: 6px 10px;
|
||||||
|
border-radius: 4px;
|
||||||
|
height: 36px;
|
||||||
|
min-width: 240px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-select:focus {
|
||||||
|
border-color: #2271b1;
|
||||||
|
box-shadow: 0 0 0 1px #2271b1;
|
||||||
|
outline: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.shortcode-docs {
|
||||||
|
margin-top: 20px;
|
||||||
|
background: #f9f9f9;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-table-wrapper {
|
||||||
|
overflow-x: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-table {
|
||||||
|
border-collapse: collapse;
|
||||||
|
width: 100%;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
border: 1px solid #e2e4e7!important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-table th {
|
||||||
|
background-color: #f6f7f7;
|
||||||
|
text-align: left;
|
||||||
|
padding: 10px;
|
||||||
|
border-bottom: 1px solid #e2e4e7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-table td {
|
||||||
|
padding: 10px;
|
||||||
|
vertical-align: top;
|
||||||
|
border-bottom: 1px solid #f0f0f1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-table code {
|
||||||
|
background: #f0f0f1;
|
||||||
|
padding: 3px 5px;
|
||||||
|
border-radius: 3px;
|
||||||
|
font-size: 13px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-table ul {
|
||||||
|
margin: 0;
|
||||||
|
padding-left: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.notice {
|
||||||
|
padding: 8px 12px;
|
||||||
|
border-radius: 3px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.notice-success {
|
||||||
|
background-color: #dff0d8;
|
||||||
|
border-left: 4px solid #46b450;
|
||||||
|
padding: 1px 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.notice-error {
|
||||||
|
background-color: #f2dede;
|
||||||
|
border-left: 4px solid #dc3232;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-preview-container {
|
||||||
|
background: #f9f9f9;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 15px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-preview-wrapper {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 30px;
|
||||||
|
margin: 15px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-preview-item {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-preview-item h4 {
|
||||||
|
margin-bottom: 10px;
|
||||||
|
font-weight: normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
.avatar-circle {
|
||||||
|
border-radius: 50% !important;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.avatar-rounded {
|
||||||
|
border-radius: 8px !important;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.default-avatar-options {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 15px;
|
||||||
|
margin-bottom: 15px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.default-avatar-options label {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
cursor: pointer;
|
||||||
|
padding: 8px;
|
||||||
|
border: 2px solid transparent;
|
||||||
|
border-radius: 5px;
|
||||||
|
width: 80px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.default-avatar-options label:hover {
|
||||||
|
background-color: #f0f0f1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.default-avatar-options input[type="radio"] {
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.default-avatar-options input[type="radio"]:checked + img {
|
||||||
|
border: 2px solid #2271b1;
|
||||||
|
border-radius: 50%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.avatar-option-name {
|
||||||
|
margin-top: 5px;
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 1.3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 782px) {
|
||||||
|
.wpavatar-settings .form-table th {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-action-buttons {
|
||||||
|
flex-direction: column;
|
||||||
|
}
|
||||||
|
|
||||||
|
.default-avatar-options {
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-preview-wrapper {
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
}
|
BIN
assets/images/cravatar-default.png
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
assets/images/cravatar-logo.png
Normal file
After Width: | Height: | Size: 2.7 KiB |
BIN
assets/images/default-avatar.png
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
assets/images/placeholder-avatar.png
Normal file
After Width: | Height: | Size: 11 KiB |
BIN
assets/images/wapuu-china.png
Normal file
After Width: | Height: | Size: 1.2 MiB |
BIN
assets/images/wapuu.png
Normal file
After Width: | Height: | Size: 28 KiB |
234
assets/js/admin.js
Normal file
|
@ -0,0 +1,234 @@
|
||||||
|
jQuery(document).ready(function($) {
|
||||||
|
if (typeof wpavatar === 'undefined') {
|
||||||
|
console.error('WPAvatar admin script error: wpavatar object not defined');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof wpavatar_l10n === 'undefined') {
|
||||||
|
console.error('WPAvatar admin script error: wpavatar_l10n object not defined');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$('.wpavatar-tab').on('click', function() {
|
||||||
|
var tab = $(this).data('tab');
|
||||||
|
if (!tab) return;
|
||||||
|
|
||||||
|
$('.wpavatar-tab').removeClass('active');
|
||||||
|
$(this).addClass('active');
|
||||||
|
|
||||||
|
$('.wpavatar-section').hide();
|
||||||
|
$('#wpavatar-section-' + tab).show();
|
||||||
|
|
||||||
|
if (tab === 'cache' && $('#cache-stats').is(':empty')) {
|
||||||
|
setTimeout(function() {
|
||||||
|
$('#check-cache').trigger('click');
|
||||||
|
}, 300);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (window.history && window.history.pushState) {
|
||||||
|
var url = new URL(window.location.href);
|
||||||
|
url.searchParams.set('tab', tab);
|
||||||
|
window.history.pushState({}, '', url);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function updateCdnOptions() {
|
||||||
|
var selectedType = $('input[name="wpavatar_cdn_type"]:checked').val();
|
||||||
|
|
||||||
|
$('.cdn-option').hide();
|
||||||
|
|
||||||
|
if (selectedType === 'cravatar_route') {
|
||||||
|
$('.cravatar-route-option').show();
|
||||||
|
forceMd5HashMethod(true);
|
||||||
|
} else if (selectedType === 'third_party') {
|
||||||
|
$('.third-party-option').show();
|
||||||
|
checkIfCravatarRelated($('select[name="wpavatar_third_party_mirror"]').val());
|
||||||
|
} else if (selectedType === 'custom') {
|
||||||
|
$('.custom-cdn-option').show();
|
||||||
|
checkIfCravatarRelated($('input[name="wpavatar_custom_cdn"]').val());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkIfCravatarRelated(value) {
|
||||||
|
if (value && value.toLowerCase().indexOf('cravatar') !== -1) {
|
||||||
|
forceMd5HashMethod(true);
|
||||||
|
} else {
|
||||||
|
// 非Cravatar服务默认选择SHA256,但不强制
|
||||||
|
var currentHashMethod = $('input[name="wpavatar_hash_method"]:checked').val();
|
||||||
|
if (!currentHashMethod) {
|
||||||
|
$('input[name="wpavatar_hash_method"][value="sha256"]').prop('checked', true);
|
||||||
|
}
|
||||||
|
forceMd5HashMethod(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function forceMd5HashMethod(force) {
|
||||||
|
if (force) {
|
||||||
|
$('input[name="wpavatar_hash_method"][value="md5"]').prop('checked', true);
|
||||||
|
$('input[name="wpavatar_hash_method"][value="sha256"]').prop('disabled', true);
|
||||||
|
$('.hash-method-notice').show();
|
||||||
|
} else {
|
||||||
|
$('input[name="wpavatar_hash_method"][value="sha256"]').prop('disabled', false);
|
||||||
|
$('.hash-method-notice').hide();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$('input[name="wpavatar_cdn_type"]').on('change', updateCdnOptions);
|
||||||
|
|
||||||
|
updateCdnOptions();
|
||||||
|
|
||||||
|
$('select[name="wpavatar_third_party_mirror"]').on('change', function() {
|
||||||
|
checkIfCravatarRelated($(this).val());
|
||||||
|
});
|
||||||
|
|
||||||
|
$('input[name="wpavatar_custom_cdn"]').on('input', function() {
|
||||||
|
checkIfCravatarRelated($(this).val());
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#check-cache').on('click', function() {
|
||||||
|
var $button = $(this);
|
||||||
|
var $stats = $('#cache-stats');
|
||||||
|
|
||||||
|
$button.prop('disabled', true).text(wpavatar_l10n.checking);
|
||||||
|
$stats.html('<p>' + wpavatar_l10n.checking_status + '</p>');
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
type: 'POST',
|
||||||
|
url: wpavatar.ajaxurl,
|
||||||
|
data: {
|
||||||
|
action: 'wpavatar_check_cache',
|
||||||
|
nonce: wpavatar.nonce
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.success) {
|
||||||
|
$stats.html(response.data);
|
||||||
|
} else {
|
||||||
|
$stats.html('<div class="error"><p>' + (response.data || wpavatar_l10n.check_failed) + '</p></div>');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function() {
|
||||||
|
$stats.html('<div class="error"><p>' + wpavatar_l10n.request_failed + '</p></div>');
|
||||||
|
},
|
||||||
|
complete: function() {
|
||||||
|
$button.prop('disabled', false).text(wpavatar_l10n.check_cache);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#purge-cache').on('click', function() {
|
||||||
|
var $button = $(this);
|
||||||
|
var $stats = $('#cache-stats');
|
||||||
|
var $status = $('#wpavatar-status');
|
||||||
|
|
||||||
|
if (!confirm(wpavatar_l10n.confirm_purge)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$button.prop('disabled', true).text(wpavatar_l10n.purging);
|
||||||
|
$stats.html('<p>' + wpavatar_l10n.purging_cache + '</p>');
|
||||||
|
|
||||||
|
$.ajax({
|
||||||
|
type: 'POST',
|
||||||
|
url: wpavatar.ajaxurl,
|
||||||
|
data: {
|
||||||
|
action: 'wpavatar_purge_cache',
|
||||||
|
nonce: wpavatar.nonce
|
||||||
|
},
|
||||||
|
success: function(response) {
|
||||||
|
if (response.success) {
|
||||||
|
$status.removeClass('notice-error')
|
||||||
|
.addClass('notice-success')
|
||||||
|
.text(response.data)
|
||||||
|
.show()
|
||||||
|
.delay(3000)
|
||||||
|
.fadeOut();
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
$('#check-cache').trigger('click');
|
||||||
|
}, 1000);
|
||||||
|
} else {
|
||||||
|
$status.removeClass('notice-success')
|
||||||
|
.addClass('notice-error')
|
||||||
|
.text(response.data || wpavatar_l10n.purge_failed)
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
error: function() {
|
||||||
|
$status.removeClass('notice-success')
|
||||||
|
.addClass('notice-error')
|
||||||
|
.text(wpavatar_l10n.request_failed)
|
||||||
|
.show();
|
||||||
|
},
|
||||||
|
complete: function() {
|
||||||
|
$button.prop('disabled', false).text(wpavatar_l10n.purge_cache);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#wpavatar-basic-form, #wpavatar-cache-form, #wpavatar-advanced-form, #wpavatar-shortcodes-form').on('submit', function(e) {
|
||||||
|
var formId = $(this).attr('id');
|
||||||
|
var $status = $('#wpavatar-status');
|
||||||
|
var isValid = true;
|
||||||
|
|
||||||
|
if (formId === 'wpavatar-basic-form' && $('input[name="wpavatar_cdn_type"]:checked').val() === 'custom') {
|
||||||
|
var customCdn = $('input[name="wpavatar_custom_cdn"]').val().trim();
|
||||||
|
if (!customCdn) {
|
||||||
|
$status.removeClass('notice-success')
|
||||||
|
.addClass('notice-error')
|
||||||
|
.text(wpavatar_l10n.enter_custom_cdn)
|
||||||
|
.show();
|
||||||
|
$('input[name="wpavatar_custom_cdn"]').focus();
|
||||||
|
isValid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (formId === 'wpavatar-cache-form' && $('input[name="wpavatar_enable_cache"]:checked').length > 0) {
|
||||||
|
var cachePath = $('input[name="wpavatar_cache_path"]').val().trim();
|
||||||
|
if (!cachePath) {
|
||||||
|
$status.removeClass('notice-success')
|
||||||
|
.addClass('notice-error')
|
||||||
|
.text(wpavatar_l10n.enter_cache_path)
|
||||||
|
.show();
|
||||||
|
$('input[name="wpavatar_cache_path"]').focus();
|
||||||
|
isValid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isValid) {
|
||||||
|
e.preventDefault();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (window.location.search.indexOf('settings-updated=true') > -1) {
|
||||||
|
$('#wpavatar-status')
|
||||||
|
.removeClass('notice-error')
|
||||||
|
.addClass('notice-success')
|
||||||
|
.text(wpavatar_l10n.settings_saved)
|
||||||
|
.show()
|
||||||
|
.delay(3000)
|
||||||
|
.fadeOut();
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentTab = '';
|
||||||
|
|
||||||
|
if (window.location.search.indexOf('tab=') > -1) {
|
||||||
|
var urlParams = new URLSearchParams(window.location.search);
|
||||||
|
currentTab = urlParams.get('tab');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!currentTab) {
|
||||||
|
var $activeTab = $('.wpavatar-tab.active');
|
||||||
|
if ($activeTab.length) {
|
||||||
|
currentTab = $activeTab.data('tab');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentTab) {
|
||||||
|
$('.wpavatar-tab[data-tab="' + currentTab + '"]').trigger('click');
|
||||||
|
} else {
|
||||||
|
$('.wpavatar-tab:first').trigger('click');
|
||||||
|
}
|
||||||
|
});
|
708
includes/admin.php
Normal file
|
@ -0,0 +1,708 @@
|
||||||
|
<?php
|
||||||
|
namespace WPAvatar;
|
||||||
|
|
||||||
|
class Settings {
|
||||||
|
public static function init() {
|
||||||
|
add_action('admin_menu', [__CLASS__, 'add_admin_menu']);
|
||||||
|
add_action('admin_init', [__CLASS__, 'register_settings']);
|
||||||
|
add_action('admin_enqueue_scripts', [__CLASS__, 'enqueue_assets']);
|
||||||
|
add_action('admin_notices', [__CLASS__, 'display_notices']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function add_admin_menu() {
|
||||||
|
add_options_page(
|
||||||
|
__('文派头像设置', 'wpavatar'),
|
||||||
|
__('头像', 'wpavatar'),
|
||||||
|
'manage_options',
|
||||||
|
'wpavatar-settings',
|
||||||
|
[__CLASS__, 'render_settings_page']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function register_settings() {
|
||||||
|
// 注册设置组和设置字段
|
||||||
|
register_setting('wpavatar_basic', 'wpavatar_enable_cravatar', ['type' => 'boolean']);
|
||||||
|
register_setting('wpavatar_basic', 'wpavatar_cdn_type', ['type' => 'string']);
|
||||||
|
register_setting('wpavatar_basic', 'wpavatar_cravatar_route', ['type' => 'string']);
|
||||||
|
register_setting('wpavatar_basic', 'wpavatar_third_party_mirror', ['type' => 'string']);
|
||||||
|
register_setting('wpavatar_basic', 'wpavatar_custom_cdn', ['type' => 'string']);
|
||||||
|
register_setting('wpavatar_basic', 'wpavatar_hash_method', ['type' => 'string']);
|
||||||
|
register_setting('wpavatar_basic', 'wpavatar_timeout', ['type' => 'integer']);
|
||||||
|
|
||||||
|
register_setting('wpavatar_cache', 'wpavatar_enable_cache', ['type' => 'boolean']);
|
||||||
|
register_setting('wpavatar_cache', 'wpavatar_cache_path', [
|
||||||
|
'type' => 'string',
|
||||||
|
'sanitize_callback' => [__CLASS__, 'sanitize_cache_path']
|
||||||
|
]);
|
||||||
|
register_setting('wpavatar_cache', 'wpavatar_cache_expire', ['type' => 'integer']);
|
||||||
|
|
||||||
|
register_setting('wpavatar_advanced', 'wpavatar_seo_alt', ['type' => 'string']);
|
||||||
|
register_setting('wpavatar_advanced', 'wpavatar_fallback_mode', ['type' => 'boolean']);
|
||||||
|
register_setting('wpavatar_advanced', 'wpavatar_fallback_avatar', ['type' => 'string']);
|
||||||
|
|
||||||
|
register_setting('wpavatar_shortcodes', 'wpavatar_shortcode_size', ['type' => 'integer']);
|
||||||
|
register_setting('wpavatar_shortcodes', 'wpavatar_shortcode_class', ['type' => 'string']);
|
||||||
|
register_setting('wpavatar_shortcodes', 'wpavatar_shortcode_shape', ['type' => 'string']);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function sanitize_cache_path($value) {
|
||||||
|
$value = sanitize_text_field($value);
|
||||||
|
$value = rtrim($value, '/\\') . '/';
|
||||||
|
|
||||||
|
if (!preg_match('~^(?:/|\\\\|[a-zA-Z]:)~', $value)) {
|
||||||
|
$value = WP_CONTENT_DIR . '/' . ltrim($value, '/\\');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file_exists($value)) {
|
||||||
|
if (!wp_mkdir_p($value)) {
|
||||||
|
add_settings_error(
|
||||||
|
'wpavatar_cache',
|
||||||
|
'cache_path_invalid',
|
||||||
|
__('无法创建缓存目录,请检查权限', 'wpavatar'),
|
||||||
|
'error'
|
||||||
|
);
|
||||||
|
return get_option('wpavatar_cache_path', WPAVATAR_CACHE_DIR);
|
||||||
|
}
|
||||||
|
} elseif (!is_dir($value)) {
|
||||||
|
add_settings_error(
|
||||||
|
'wpavatar_cache',
|
||||||
|
'cache_path_invalid',
|
||||||
|
__('指定的路径不是有效目录', 'wpavatar'),
|
||||||
|
'error'
|
||||||
|
);
|
||||||
|
return get_option('wpavatar_cache_path', WPAVATAR_CACHE_DIR);
|
||||||
|
} elseif (!is_writable($value)) {
|
||||||
|
add_settings_error(
|
||||||
|
'wpavatar_cache',
|
||||||
|
'cache_path_invalid',
|
||||||
|
__('缓存目录不可写,请检查权限', 'wpavatar'),
|
||||||
|
'error'
|
||||||
|
);
|
||||||
|
return get_option('wpavatar_cache_path', WPAVATAR_CACHE_DIR);
|
||||||
|
}
|
||||||
|
|
||||||
|
$index_file = $value . 'index.php';
|
||||||
|
if (!file_exists($index_file)) {
|
||||||
|
@file_put_contents($index_file, '<?php // Silence is golden.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function enqueue_assets($hook) {
|
||||||
|
if ($hook !== 'settings_page_wpavatar-settings') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_enqueue_style(
|
||||||
|
'wpavatar-admin',
|
||||||
|
WPAVATAR_PLUGIN_URL . 'assets/css/admin.css',
|
||||||
|
[],
|
||||||
|
WPAVATAR_VERSION
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_enqueue_script(
|
||||||
|
'wpavatar-admin',
|
||||||
|
WPAVATAR_PLUGIN_URL . 'assets/js/admin.js',
|
||||||
|
['jquery'],
|
||||||
|
WPAVATAR_VERSION,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
wp_localize_script('wpavatar-admin', 'wpavatar', [
|
||||||
|
'nonce' => wp_create_nonce('wpavatar_admin_nonce'),
|
||||||
|
'ajaxurl' => admin_url('admin-ajax.php'),
|
||||||
|
'cache_path' => get_option('wpavatar_cache_path', WPAVATAR_CACHE_DIR),
|
||||||
|
'plugin_url' => WPAVATAR_PLUGIN_URL,
|
||||||
|
'assets_url' => WPAVATAR_PLUGIN_URL . 'assets/',
|
||||||
|
]);
|
||||||
|
|
||||||
|
wp_localize_script('wpavatar-admin', 'wpavatar_l10n', [
|
||||||
|
'checking' => __('检查中...', 'wpavatar'),
|
||||||
|
'checking_status' => __('正在检查缓存状态...', 'wpavatar'),
|
||||||
|
'check_failed' => __('检查失败,请重试', 'wpavatar'),
|
||||||
|
'request_failed' => __('请求失败,请检查网络连接', 'wpavatar'),
|
||||||
|
'check_cache' => __('检查缓存状态', 'wpavatar'),
|
||||||
|
'confirm_purge' => __('确定要清空所有缓存头像吗?此操作无法撤销。', 'wpavatar'),
|
||||||
|
'purging' => __('清空中...', 'wpavatar'),
|
||||||
|
'purging_cache' => __('正在清空缓存...', 'wpavatar'),
|
||||||
|
'purge_failed' => __('清空失败,请重试', 'wpavatar'),
|
||||||
|
'purge_cache' => __('清空缓存', 'wpavatar'),
|
||||||
|
'enter_custom_cdn' => __('请输入自定义CDN域名', 'wpavatar'),
|
||||||
|
'enter_cache_path' => __('请输入缓存目录路径', 'wpavatar'),
|
||||||
|
'settings_saved' => __('设置已成功保存。', 'wpavatar')
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function display_notices() {
|
||||||
|
settings_errors('wpavatar_basic');
|
||||||
|
settings_errors('wpavatar_cache');
|
||||||
|
settings_errors('wpavatar_advanced');
|
||||||
|
settings_errors('wpavatar_shortcodes');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function render_settings_page() {
|
||||||
|
$active_tab = isset($_GET['tab']) ? sanitize_text_field($_GET['tab']) : 'basic';
|
||||||
|
?>
|
||||||
|
<div class="wrap wpavatar-settings">
|
||||||
|
<h1><?php esc_html_e('文派头像设置', 'wpavatar'); ?>
|
||||||
|
<span style="font-size: 13px; padding-left: 10px;"><?php printf(esc_html__('Version: %s', 'wpavatar'), esc_html(WPAVATAR_VERSION)); ?></span>
|
||||||
|
<a href="https://wpavatar.com/document/" target="_blank" class="button button-secondary" style="margin-left: 10px;"><?php esc_html_e('Document', 'wpavatar'); ?></a>
|
||||||
|
<a href="https://sharecms.com/forums/" target="_blank" class="button button-secondary"><?php esc_html_e('Support', 'wpavatar'); ?></a>
|
||||||
|
</h1>
|
||||||
|
|
||||||
|
<div id="wpavatar-status" class="notice" style="display:none; margin-top: 10px;"></div>
|
||||||
|
<div class="wpavatar-card">
|
||||||
|
<div class="wpavatar-tabs-wrapper">
|
||||||
|
<div class="wpavatar-sync-tabs">
|
||||||
|
<button type="button" class="wpavatar-tab <?php echo $active_tab === 'basic' ? 'active' : ''; ?>" data-tab="basic">
|
||||||
|
<?php _e('基础设置', 'wpavatar'); ?>
|
||||||
|
</button>
|
||||||
|
<button type="button" class="wpavatar-tab <?php echo $active_tab === 'cache' ? 'active' : ''; ?>" data-tab="cache">
|
||||||
|
<?php _e('缓存控制', 'wpavatar'); ?>
|
||||||
|
</button>
|
||||||
|
<button type="button" class="wpavatar-tab <?php echo $active_tab === 'advanced' ? 'active' : ''; ?>" data-tab="advanced">
|
||||||
|
<?php _e('高级设置', 'wpavatar'); ?>
|
||||||
|
</button>
|
||||||
|
<button type="button" class="wpavatar-tab <?php echo $active_tab === 'shortcodes' ? 'active' : ''; ?>" data-tab="shortcodes">
|
||||||
|
<?php _e('头像简码', 'wpavatar'); ?>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wpavatar-section" id="wpavatar-section-basic" style="<?php echo $active_tab !== 'basic' ? 'display: none;' : ''; ?>">
|
||||||
|
<h2><?php _e('基础设置', 'wpavatar'); ?></h2>
|
||||||
|
<p class="wpavatar-section-desc"><?php _e('配置头像服务和CDN设置。', 'wpavatar'); ?></p>
|
||||||
|
|
||||||
|
<form method="post" action="options.php" id="wpavatar-basic-form">
|
||||||
|
<?php
|
||||||
|
settings_fields('wpavatar_basic');
|
||||||
|
|
||||||
|
$enable_cravatar = get_option('wpavatar_enable_cravatar', 1);
|
||||||
|
$cdn_type = get_option('wpavatar_cdn_type', 'cravatar_route');
|
||||||
|
$cravatar_route = get_option('wpavatar_cravatar_route', 'cravatar.com');
|
||||||
|
$third_party_mirror = get_option('wpavatar_third_party_mirror', 'weavatar.com');
|
||||||
|
$custom_cdn = get_option('wpavatar_custom_cdn', '');
|
||||||
|
$hash_method = get_option('wpavatar_hash_method', 'md5');
|
||||||
|
$timeout = get_option('wpavatar_timeout', 5);
|
||||||
|
?>
|
||||||
|
<table class="form-table">
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('启用初认头像', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<label class="wpavatar-switch">
|
||||||
|
<input type="checkbox" name="wpavatar_enable_cravatar" value="1" <?php checked($enable_cravatar); ?>>
|
||||||
|
<span class="wpavatar-slider"></span>
|
||||||
|
<span class="wpavatar-switch-label"><?php _e('替换WordPress默认头像为Cravatar', 'wpavatar'); ?></span>
|
||||||
|
</label>
|
||||||
|
<p class="description"><?php _e('启用后将WordPress默认的Gravatar头像替换为Cravatar,提高国内访问速度', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('线路选择', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<label class="wpavatar-radio">
|
||||||
|
<input type="radio" name="wpavatar_cdn_type" value="cravatar_route" <?php checked($cdn_type, 'cravatar_route'); ?>>
|
||||||
|
<span class="wpavatar-radio-label"><?php _e('Cravatar自选线路', 'wpavatar'); ?></span>
|
||||||
|
</label><br>
|
||||||
|
<label class="wpavatar-radio">
|
||||||
|
<input type="radio" name="wpavatar_cdn_type" value="third_party" <?php checked($cdn_type, 'third_party'); ?>>
|
||||||
|
<span class="wpavatar-radio-label"><?php _e('第三方镜像', 'wpavatar'); ?></span>
|
||||||
|
</label><br>
|
||||||
|
<label class="wpavatar-radio">
|
||||||
|
<input type="radio" name="wpavatar_cdn_type" value="custom" <?php checked($cdn_type, 'custom'); ?>>
|
||||||
|
<span class="wpavatar-radio-label"><?php _e('自定义CDN', 'wpavatar'); ?></span>
|
||||||
|
</label>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr class="cdn-option cravatar-route-option" <?php echo $cdn_type !== 'cravatar_route' ? 'style="display:none;"' : ''; ?>>
|
||||||
|
<th><?php _e('Cravatar官方源', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<select name="wpavatar_cravatar_route" class="wpavatar-select">
|
||||||
|
<option value="cravatar.cn" <?php selected($cravatar_route, 'cravatar.cn'); ?>><?php _e('默认线路 (cravatar.com)', 'wpavatar'); ?></option>
|
||||||
|
<option value="cn.cravatar.com" <?php selected($cravatar_route, 'cn.cravatar.com'); ?>><?php _e('中国 (cn.cravatar.com)', 'wpavatar'); ?></option>
|
||||||
|
<option value="hk.cravatar.com" <?php selected($cravatar_route, 'hk.cravatar.com'); ?>><?php _e('香港 (hk.cravatar.com)', 'wpavatar'); ?></option>
|
||||||
|
<option value="en.cravatar.com" <?php selected($cravatar_route, 'en.cravatar.com'); ?>><?php _e('国际 (en.cravatar.com)', 'wpavatar'); ?></option>
|
||||||
|
</select>
|
||||||
|
<p class="description"><?php _e('选择适合您网站访客的Cravatar线路', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr class="cdn-option third-party-option" <?php echo $cdn_type !== 'third_party' ? 'style="display:none;"' : ''; ?>>
|
||||||
|
<th><?php _e('第三方镜像', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<select name="wpavatar_third_party_mirror" class="wpavatar-select">
|
||||||
|
<option value="weavatar.com" <?php selected($third_party_mirror, 'weavatar.com'); ?>><?php _e('WeAvatar (weavatar.com)', 'wpavatar'); ?></option>
|
||||||
|
<option value="libravatar.org" <?php selected($third_party_mirror, 'libravatar.org'); ?>><?php _e('Libravatar (libravatar.org)', 'wpavatar'); ?></option>
|
||||||
|
<option value="gravatar.loli.net" <?php selected($third_party_mirror, 'gravatar.loli.net'); ?>><?php _e('Loli镜像 (gravatar.loli.net)', 'wpavatar'); ?></option>
|
||||||
|
<option value="gravatar.webp.se/avatar" <?php selected($third_party_mirror, 'gravatar.webp.se/avatar'); ?>><?php _e('Webp源 (gravatar.webp.se)', 'wpavatar'); ?></option>
|
||||||
|
<option value="dn-qiniu-avatar.qbox.me/avatar" <?php selected($third_party_mirror, 'dn-qiniu-avatar.qbox.me/avatar'); ?>><?php _e('七牛镜像 (dn-qiniu-avatar)', 'wpavatar'); ?></option>
|
||||||
|
<option value="gravatar.w3tt.com/avatar" <?php selected($third_party_mirror, 'gravatar.w3tt.com/avatar'); ?>><?php _e('万维网测试小组 (W3TT) ', 'wpavatar'); ?></option>
|
||||||
|
</select>
|
||||||
|
<p class="description"><?php _e('选择第三方头像镜像站', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr class="cdn-option custom-cdn-option" <?php echo $cdn_type !== 'custom' ? 'style="display:none;"' : ''; ?>>
|
||||||
|
<th><?php _e('自定义CDN', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<input type="text" name="wpavatar_custom_cdn" value="<?php echo esc_attr($custom_cdn); ?>" class="regular-text wpavatar-input">
|
||||||
|
<p class="description"><?php _e('输入自定义CDN域名,例如:cdn.example.com', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('头像哈希方法', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<label class="wpavatar-radio">
|
||||||
|
<input type="radio" name="wpavatar_hash_method" value="md5" <?php checked($hash_method, 'md5'); ?>>
|
||||||
|
<span class="wpavatar-radio-label"><?php _e('MD5 (Cravatar默认)', 'wpavatar'); ?></span>
|
||||||
|
</label><br>
|
||||||
|
<label class="wpavatar-radio">
|
||||||
|
<input type="radio" name="wpavatar_hash_method" value="sha256" <?php checked($hash_method, 'sha256'); ?>>
|
||||||
|
<span class="wpavatar-radio-label"><?php _e('SHA256 (Gravatar默认)', 'wpavatar'); ?></span>
|
||||||
|
</label>
|
||||||
|
<p class="description"><?php _e('选择头像邮箱的哈希方法,Cravatar目前使用MD5,一般Gravatar镜像均为SHA256', 'wpavatar'); ?></p>
|
||||||
|
<p class="description hash-method-notice" style="color: #d63638; <?php echo $cdn_type !== 'cravatar_route' ? 'display:none;' : ''; ?>"><?php _e('注意:使用Cravatar服务时,哈希方法将仅使用MD5', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('超时设置', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<input type="number" name="wpavatar_timeout" value="<?php echo esc_attr($timeout); ?>" min="1" max="30" class="small-text wpavatar-input">
|
||||||
|
<?php _e('秒', 'wpavatar'); ?>
|
||||||
|
<p class="description"><?php _e('头像请求的最大等待时间,超过后将使用备用头像', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<div class="wpavatar-submit-wrapper">
|
||||||
|
<?php wp_nonce_field('wpavatar_basic_nonce', 'wpavatar_basic_nonce'); ?>
|
||||||
|
<button type="submit" class="button button-primary"><?php _e('保存设置', 'wpavatar'); ?></button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wpavatar-section" id="wpavatar-section-cache" style="<?php echo $active_tab !== 'cache' ? 'display: none;' : ''; ?>">
|
||||||
|
<h2><?php _e('缓存控制', 'wpavatar'); ?></h2>
|
||||||
|
<p class="wpavatar-section-desc"><?php _e('管理头像缓存设置和操作。', 'wpavatar'); ?></p>
|
||||||
|
|
||||||
|
<div class="wpavatar-stats-card">
|
||||||
|
<h3><?php _e('缓存统计', 'wpavatar'); ?></h3>
|
||||||
|
<div id="cache-stats" class="cache-stats-wrapper"></div>
|
||||||
|
<div class="wpavatar-action-buttons">
|
||||||
|
<button type="button" id="check-cache" class="button button-secondary"><?php _e('检查缓存状态', 'wpavatar'); ?></button>
|
||||||
|
<button type="button" id="purge-cache" class="button button-secondary"><?php _e('清空缓存', 'wpavatar'); ?></button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form method="post" action="options.php" id="wpavatar-cache-form">
|
||||||
|
<?php
|
||||||
|
settings_fields('wpavatar_cache');
|
||||||
|
|
||||||
|
$enable_cache = get_option('wpavatar_enable_cache', 1);
|
||||||
|
$cache_path = get_option('wpavatar_cache_path', WPAVATAR_CACHE_DIR);
|
||||||
|
$cache_expire = get_option('wpavatar_cache_expire', 7);
|
||||||
|
?>
|
||||||
|
<table class="form-table">
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('启用本地缓存', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<label class="wpavatar-switch">
|
||||||
|
<input type="checkbox" name="wpavatar_enable_cache" value="1" <?php checked($enable_cache); ?>>
|
||||||
|
<span class="wpavatar-slider"></span>
|
||||||
|
<span class="wpavatar-switch-label"><?php _e('缓存头像到本地服务器', 'wpavatar'); ?></span>
|
||||||
|
</label>
|
||||||
|
<p class="description"><?php _e('将头像缓存到本地可以减少外部请求,提高网站加载速度', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('缓存目录', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<input type="text" name="wpavatar_cache_path" value="<?php echo esc_attr($cache_path); ?>" class="regular-text wpavatar-input">
|
||||||
|
<p class="description"><?php _e('确保目录可写,建议路径:/wp-content/uploads/cravatar', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('缓存过期时间', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<input type="number" name="wpavatar_cache_expire" value="<?php echo esc_attr($cache_expire); ?>" min="1" max="30" class="small-text wpavatar-input">
|
||||||
|
<?php _e('天', 'wpavatar'); ?>
|
||||||
|
<p class="description"><?php _e('头像缓存的有效期,过期后将重新获取', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<div class="wpavatar-submit-wrapper">
|
||||||
|
<?php wp_nonce_field('wpavatar_cache_nonce', 'wpavatar_cache_nonce'); ?>
|
||||||
|
<button type="submit" class="button button-primary"><?php _e('保存设置', 'wpavatar'); ?></button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wpavatar-section" id="wpavatar-section-advanced" style="<?php echo $active_tab !== 'advanced' ? 'display: none;' : ''; ?>">
|
||||||
|
<h2><?php _e('高级设置', 'wpavatar'); ?></h2>
|
||||||
|
<p class="wpavatar-section-desc"><?php _e('配置头像的SEO和备用方案。', 'wpavatar'); ?></p>
|
||||||
|
|
||||||
|
<form method="post" action="options.php" id="wpavatar-advanced-form">
|
||||||
|
<?php
|
||||||
|
settings_fields('wpavatar_advanced');
|
||||||
|
|
||||||
|
$seo_alt = get_option('wpavatar_seo_alt', '%s的头像');
|
||||||
|
$fallback_mode = get_option('wpavatar_fallback_mode', 1);
|
||||||
|
$fallback_avatar = get_option('wpavatar_fallback_avatar', 'default');
|
||||||
|
|
||||||
|
$local_avatars = \WPAvatar\Cravatar::get_local_avatars();
|
||||||
|
?>
|
||||||
|
<table class="form-table">
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('SEO替代文本', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<input type="text" name="wpavatar_seo_alt" value="<?php echo esc_attr($seo_alt); ?>" class="regular-text wpavatar-input">
|
||||||
|
<p class="description"><?php _e('头像的ALT文本,%s将被替换为用户名', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('头像加载失败处理', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<label class="wpavatar-switch">
|
||||||
|
<input type="checkbox" name="wpavatar_fallback_mode" value="1" <?php checked($fallback_mode); ?>>
|
||||||
|
<span class="wpavatar-slider"></span>
|
||||||
|
<span class="wpavatar-switch-label"><?php _e('启用备用头像', 'wpavatar'); ?></span>
|
||||||
|
</label>
|
||||||
|
<p class="description"><?php _e('当头像服务器无法访问或加载超时时,自动使用本地备用头像,避免拖慢网站加载速度', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('备用头像选择', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<div class="default-avatar-options">
|
||||||
|
<?php foreach ($local_avatars as $key => $avatar) : ?>
|
||||||
|
<label>
|
||||||
|
<input type="radio" name="wpavatar_fallback_avatar" value="<?php echo esc_attr($key); ?>" <?php checked($fallback_avatar, $key); ?>>
|
||||||
|
<img src="<?php echo esc_url($avatar['url']); ?>" alt="<?php echo esc_attr($avatar['name']); ?>" width="48" height="48">
|
||||||
|
<span class="avatar-option-name"><?php echo esc_html($avatar['name']); ?></span>
|
||||||
|
</label>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</div>
|
||||||
|
<p class="description">
|
||||||
|
<?php
|
||||||
|
$wpcy_installed = class_exists('WP_China_Yes'); // 检查是否安装了文派叶子插件
|
||||||
|
$wpcy_link = $wpcy_installed
|
||||||
|
? admin_url('admin.php?page=wp-china-yes')
|
||||||
|
: 'https://wpcy.com';
|
||||||
|
|
||||||
|
printf(
|
||||||
|
__('选择您喜欢的故障备用头像,如需智能线路切换,请使用%s。', 'wpavatar'),
|
||||||
|
sprintf(
|
||||||
|
'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
|
||||||
|
esc_url($wpcy_link),
|
||||||
|
__('文派叶子 🍃(WPCY.COM)', 'wpavatar')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
?>
|
||||||
|
</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<div class="wpavatar-submit-wrapper">
|
||||||
|
<?php wp_nonce_field('wpavatar_advanced_nonce', 'wpavatar_advanced_nonce'); ?>
|
||||||
|
<button type="submit" class="button button-primary"><?php _e('保存设置', 'wpavatar'); ?></button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
<div class="wpavatar-section" id="wpavatar-section-shortcodes" style="<?php echo $active_tab !== 'shortcodes' ? 'display: none;' : ''; ?>">
|
||||||
|
<h2><?php _e('简码设置', 'wpavatar'); ?></h2>
|
||||||
|
<p class="wpavatar-section-desc"><?php _e('配置头像简码的默认参数和预览效果。', 'wpavatar'); ?></p>
|
||||||
|
|
||||||
|
<div class="wpavatar-preview-container">
|
||||||
|
<h3><?php _e('头像预览', 'wpavatar'); ?></h3>
|
||||||
|
<div class="wpavatar-preview-wrapper">
|
||||||
|
<div class="wpavatar-preview-item">
|
||||||
|
<h4><?php _e('方形', 'wpavatar'); ?></h4>
|
||||||
|
<?php echo \WPAvatar\Shortcode::generate_preview(get_current_user_id(), 'square', 80); ?>
|
||||||
|
</div>
|
||||||
|
<div class="wpavatar-preview-item">
|
||||||
|
<h4><?php _e('圆角', 'wpavatar'); ?></h4>
|
||||||
|
<?php echo \WPAvatar\Shortcode::generate_preview(get_current_user_id(), 'rounded', 80); ?>
|
||||||
|
</div>
|
||||||
|
<div class="wpavatar-preview-item">
|
||||||
|
<h4><?php _e('圆形', 'wpavatar'); ?></h4>
|
||||||
|
<?php echo \WPAvatar\Shortcode::generate_preview(get_current_user_id(), 'circle', 80); ?>
|
||||||
|
</div>
|
||||||
|
<div class="wpavatar-preview-item">
|
||||||
|
<h4><?php _e('方形', 'wpavatar'); ?></h4>
|
||||||
|
<img src="<?= WPAVATAR_PLUGIN_URL ?>assets/images/wapuu-china.png" width="80" height="80" class="avatar-square">
|
||||||
|
</div>
|
||||||
|
<div class="wpavatar-preview-item">
|
||||||
|
<h4><?php _e('圆角', 'wpavatar'); ?></h4>
|
||||||
|
<img src="<?= WPAVATAR_PLUGIN_URL ?>assets/images/wapuu-china.png" width="80" height="80" class="avatar-rounded" style="border-radius:8px">
|
||||||
|
</div>
|
||||||
|
<div class="wpavatar-preview-item">
|
||||||
|
<h4><?php _e('圆形', 'wpavatar'); ?></h4>
|
||||||
|
<img src="<?= WPAVATAR_PLUGIN_URL ?>assets/images/wapuu-china.png" width="80" height="80" class="avatar-circle" style="border-radius:50%">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p class="description"><?php _e('预览使用当前登录账户的头像和示例图片', 'wpavatar'); ?></p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form method="post" action="options.php" id="wpavatar-shortcodes-form">
|
||||||
|
<?php
|
||||||
|
settings_fields('wpavatar_shortcodes');
|
||||||
|
|
||||||
|
$shortcode_size = get_option('wpavatar_shortcode_size', 96);
|
||||||
|
$shortcode_class = get_option('wpavatar_shortcode_class', 'wpavatar');
|
||||||
|
$shortcode_shape = get_option('wpavatar_shortcode_shape', 'square');
|
||||||
|
?>
|
||||||
|
<table class="form-table">
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('默认头像大小', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<input type="number" name="wpavatar_shortcode_size" value="<?php echo esc_attr($shortcode_size); ?>" min="16" max="512" class="small-text wpavatar-input">
|
||||||
|
<p class="description"><?php _e('简码默认头像大小(像素)', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('默认CSS类名', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<input type="text" name="wpavatar_shortcode_class" value="<?php echo esc_attr($shortcode_class); ?>" class="regular-text wpavatar-input">
|
||||||
|
<p class="description"><?php _e('简码生成的头像默认CSS类', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('默认头像形状', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<select name="wpavatar_shortcode_shape" class="wpavatar-select">
|
||||||
|
<option value="square" <?php selected($shortcode_shape, 'square'); ?>><?php _e('方形', 'wpavatar'); ?></option>
|
||||||
|
<option value="rounded" <?php selected($shortcode_shape, 'rounded'); ?>><?php _e('圆角方形', 'wpavatar'); ?></option>
|
||||||
|
<option value="circle" <?php selected($shortcode_shape, 'circle'); ?>><?php _e('圆形', 'wpavatar'); ?></option>
|
||||||
|
</select>
|
||||||
|
<p class="description"><?php _e('简码生成的头像默认形状', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<div class="wpavatar-submit-wrapper">
|
||||||
|
<?php wp_nonce_field('wpavatar_shortcodes_nonce', 'wpavatar_shortcodes_nonce'); ?>
|
||||||
|
<button type="submit" class="button button-primary"><?php _e('保存设置', 'wpavatar'); ?></button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="wpavatar-card shortcode-docs">
|
||||||
|
<h3><?php _e('可用简码', 'wpavatar'); ?></h3>
|
||||||
|
<div class="wpavatar-table-wrapper">
|
||||||
|
<table class="widefat wpavatar-table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('简码', 'wpavatar'); ?></th>
|
||||||
|
<th><?php _e('描述', 'wpavatar'); ?></th>
|
||||||
|
<th><?php _e('参数', 'wpavatar'); ?></th>
|
||||||
|
<th><?php _e('示例', 'wpavatar'); ?></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td><code>[wpavatar]</code></td>
|
||||||
|
<td><?php _e('显示用户头像', 'wpavatar'); ?></td>
|
||||||
|
<td>
|
||||||
|
<ul>
|
||||||
|
<li><code>size</code> - <?php _e('头像大小(像素)', 'wpavatar'); ?></li>
|
||||||
|
<li><code>user_id</code> - <?php _e('用户ID,默认为当前用户', 'wpavatar'); ?></li>
|
||||||
|
<li><code>class</code> - <?php _e('CSS类名', 'wpavatar'); ?></li>
|
||||||
|
<li><code>shape</code> - <?php _e('形状:square(方形)、rounded(圆角)、circle(圆形)', 'wpavatar'); ?></li>
|
||||||
|
<li><code>title</code> - <?php _e('可选的标题文本', 'wpavatar'); ?></li>
|
||||||
|
</ul>
|
||||||
|
</td>
|
||||||
|
<td><code>[wpavatar size="128" user_id="1" shape="circle" class="my-avatar"]</code></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><code>[wpavatar_username]</code></td>
|
||||||
|
<td><?php _e('显示用户名', 'wpavatar'); ?></td>
|
||||||
|
<td>
|
||||||
|
<ul>
|
||||||
|
<li><code>user_id</code> - <?php _e('用户ID,默认为当前用户', 'wpavatar'); ?></li>
|
||||||
|
<li><code>before</code> - <?php _e('用户名前的文本', 'wpavatar'); ?></li>
|
||||||
|
<li><code>after</code> - <?php _e('用户名后的文本', 'wpavatar'); ?></li>
|
||||||
|
</ul>
|
||||||
|
</td>
|
||||||
|
<td><code>[wpavatar_username before="欢迎," after="!"]</code></td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<h4><?php _e('在菜单项中使用', 'wpavatar'); ?></h4>
|
||||||
|
<p><?php _e('您可以在菜单项的标题中使用特殊标记来插入头像和用户名:', 'wpavatar'); ?></p>
|
||||||
|
<ul>
|
||||||
|
<li><code>{wpavatar}</code> - <?php _e('插入当前用户的头像(圆形,32像素)', 'wpavatar'); ?></li>
|
||||||
|
<li><code>{wpavatar_username}</code> - <?php _e('插入当前用户的用户名', 'wpavatar'); ?></li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<style>
|
||||||
|
.wpavatar-preview-container {
|
||||||
|
background: #f9f9f9;
|
||||||
|
border-radius: 4px;
|
||||||
|
padding: 15px;
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-preview-wrapper {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 30px;
|
||||||
|
margin: 15px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-preview-item {
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.wpavatar-preview-item h4 {
|
||||||
|
margin-bottom: 10px;
|
||||||
|
font-weight: normal;
|
||||||
|
}
|
||||||
|
|
||||||
|
.avatar-circle {
|
||||||
|
border-radius: 50% !important;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.avatar-rounded {
|
||||||
|
border-radius: 8px !important;
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.default-avatar-options {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 15px;
|
||||||
|
margin-bottom: 15px;
|
||||||
|
}
|
||||||
|
.default-avatar-options label {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
cursor: pointer;
|
||||||
|
padding: 8px;
|
||||||
|
border: 2px solid transparent;
|
||||||
|
border-radius: 5px;
|
||||||
|
width: 80px;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
.default-avatar-options label:hover {
|
||||||
|
background-color: #f0f0f1;
|
||||||
|
}
|
||||||
|
.default-avatar-options input[type="radio"] {
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
.default-avatar-options input[type="radio"]:checked + img {
|
||||||
|
border: 2px solid #2271b1;
|
||||||
|
border-radius: 50%;
|
||||||
|
}
|
||||||
|
.avatar-option-name {
|
||||||
|
margin-top: 5px;
|
||||||
|
font-size: 12px;
|
||||||
|
line-height: 1.3;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Network {
|
||||||
|
public static function init() {
|
||||||
|
if (is_network_admin()) {
|
||||||
|
add_action('network_admin_menu', [__CLASS__, 'add_network_menu']);
|
||||||
|
add_action('network_admin_edit_wpavatar_network', [__CLASS__, 'save_network_settings']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function add_network_menu() {
|
||||||
|
add_submenu_page(
|
||||||
|
'settings.php',
|
||||||
|
__('WPAvatar网络设置', 'wpavatar'),
|
||||||
|
__('WPAvatar', 'wpavatar'),
|
||||||
|
'manage_network_options',
|
||||||
|
'wpavatar-network',
|
||||||
|
[__CLASS__, 'render_network_page']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function render_network_page() {
|
||||||
|
?>
|
||||||
|
<div class="wrap wpavatar-settings">
|
||||||
|
<h1><?php esc_html_e('WPAvatar网络设置', 'wpavatar'); ?></h1>
|
||||||
|
|
||||||
|
<div id="wpavatar-network-status" class="notice" style="display:none; margin-top: 10px;"></div>
|
||||||
|
|
||||||
|
<div class="wpavatar-card">
|
||||||
|
<h2><?php _e('网络范围设置', 'wpavatar'); ?></h2>
|
||||||
|
<p class="wpavatar-section-desc"><?php _e('配置多站点网络的WPAvatar设置。', 'wpavatar'); ?></p>
|
||||||
|
|
||||||
|
<form method="post" action="edit.php?action=wpavatar_network" id="wpavatar-network-form">
|
||||||
|
<?php wp_nonce_field('wpavatar_network_settings'); ?>
|
||||||
|
<table class="form-table">
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('启用网络范围设置', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<label class="wpavatar-switch">
|
||||||
|
<input type="checkbox" name="wpavatar_network_enabled" value="1" <?php checked(get_site_option('wpavatar_network_enabled', 1)); ?>>
|
||||||
|
<span class="wpavatar-slider"></span>
|
||||||
|
<span class="wpavatar-switch-label"><?php _e('在所有站点启用WPAvatar', 'wpavatar'); ?></span>
|
||||||
|
</label>
|
||||||
|
<p class="description"><?php _e('启用后,WPAvatar将在网络中的所有站点上生效', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('默认CDN类型', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<select name="wpavatar_network_cdn_type" class="wpavatar-select">
|
||||||
|
<option value="cravatar_route" <?php selected(get_site_option('wpavatar_network_cdn_type', 'cravatar_route'), 'cravatar_route'); ?>><?php _e('Cravatar线路', 'wpavatar'); ?></option>
|
||||||
|
<option value="third_party" <?php selected(get_site_option('wpavatar_network_cdn_type', 'cravatar_route'), 'third_party'); ?>><?php _e('第三方镜像', 'wpavatar'); ?></option>
|
||||||
|
<option value="custom" <?php selected(get_site_option('wpavatar_network_cdn_type', 'cravatar_route'), 'custom'); ?>><?php _e('自定义CDN', 'wpavatar'); ?></option>
|
||||||
|
</select>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('默认Cravatar线路', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<select name="wpavatar_network_cravatar_route" class="wpavatar-select">
|
||||||
|
<option value="cravatar.com" <?php selected(get_site_option('wpavatar_network_cravatar_route', 'cravatar.com'), 'cravatar.com'); ?>><?php _e('默认线路 (cravatar.com)', 'wpavatar'); ?></option>
|
||||||
|
<option value="cn.cravatar.com" <?php selected(get_site_option('wpavatar_network_cravatar_route', 'cravatar.com'), 'cn.cravatar.com'); ?>><?php _e('中国大陆 (cn.cravatar.com)', 'wpavatar'); ?></option>
|
||||||
|
<option value="hk.cravatar.com" <?php selected(get_site_option('wpavatar_network_cravatar_route', 'cravatar.com'), 'hk.cravatar.com'); ?>><?php _e('香港 (hk.cravatar.com)', 'wpavatar'); ?></option>
|
||||||
|
<option value="en.cravatar.com" <?php selected(get_site_option('wpavatar_network_cravatar_route', 'cravatar.com'), 'en.cravatar.com'); ?>><?php _e('国际 (en.cravatar.com)', 'wpavatar'); ?></option>
|
||||||
|
</select>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<th><?php _e('哈希方法', 'wpavatar'); ?></th>
|
||||||
|
<td>
|
||||||
|
<p class="description" style="color: #d63638;"><?php _e('注意:使用Cravatar服务时,哈希方法将强制使用MD5', 'wpavatar'); ?></p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
<div class="wpavatar-submit-wrapper">
|
||||||
|
<button type="submit" class="button button-primary"><?php _e('保存设置', 'wpavatar'); ?></button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function save_network_settings() {
|
||||||
|
check_admin_referer('wpavatar_network_settings');
|
||||||
|
|
||||||
|
update_site_option('wpavatar_network_enabled', isset($_POST['wpavatar_network_enabled']) ? 1 : 0);
|
||||||
|
|
||||||
|
if (isset($_POST['wpavatar_network_cdn_type'])) {
|
||||||
|
update_site_option('wpavatar_network_cdn_type', sanitize_text_field($_POST['wpavatar_network_cdn_type']));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($_POST['wpavatar_network_cravatar_route'])) {
|
||||||
|
update_site_option('wpavatar_network_cravatar_route', sanitize_text_field($_POST['wpavatar_network_cravatar_route']));
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_redirect(add_query_arg(['page' => 'wpavatar-network', 'updated' => 'true'], network_admin_url('settings.php')));
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
}
|
690
includes/core.php
Normal file
|
@ -0,0 +1,690 @@
|
||||||
|
<?php
|
||||||
|
namespace WPAvatar;
|
||||||
|
|
||||||
|
class Core {
|
||||||
|
public static function init() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Cravatar {
|
||||||
|
public static $gravatar_domains = [
|
||||||
|
'www.gravatar.com',
|
||||||
|
'0.gravatar.com',
|
||||||
|
'1.gravatar.com',
|
||||||
|
'2.gravatar.com',
|
||||||
|
'secure.gravatar.com',
|
||||||
|
's.gravatar.com',
|
||||||
|
'cn.gravatar.com',
|
||||||
|
'gravatar.com'
|
||||||
|
];
|
||||||
|
|
||||||
|
public static function init() {
|
||||||
|
if (get_option('wpavatar_enable_cravatar', true)) {
|
||||||
|
add_filter('get_avatar_url', [__CLASS__, 'replace_avatar_url'], 1);
|
||||||
|
add_filter('um_user_avatar_url_filter', [__CLASS__, 'replace_avatar_url'], 1);
|
||||||
|
add_filter('bp_gravatar_url', [__CLASS__, 'replace_avatar_url'], 1);
|
||||||
|
add_filter('user_profile_picture_description', [__CLASS__, 'modify_profile_picture_description'], 1);
|
||||||
|
add_filter('pre_get_avatar_data', [__CLASS__, 'pre_get_avatar_data'], 9, 2);
|
||||||
|
add_filter('get_avatar', [__CLASS__, 'add_seo_alt'], 10, 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function get_local_avatars() {
|
||||||
|
$avatars = [];
|
||||||
|
|
||||||
|
if (file_exists(WPAVATAR_PLUGIN_DIR . 'assets/images/default-avatar.png')) {
|
||||||
|
$avatars['default'] = [
|
||||||
|
'url' => WPAVATAR_PLUGIN_URL . 'assets/images/default-avatar.png',
|
||||||
|
'name' => __('默认头像', 'wpavatar')
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_exists(WPAVATAR_PLUGIN_DIR . 'assets/images/wapuu.png')) {
|
||||||
|
$avatars['wapuu'] = [
|
||||||
|
'url' => WPAVATAR_PLUGIN_URL . 'assets/images/wapuu.png',
|
||||||
|
'name' => __('文派Wapuu', 'wpavatar')
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_exists(WPAVATAR_PLUGIN_DIR . 'assets/images/wapuu-china.png')) {
|
||||||
|
$avatars['wapuu-china'] = [
|
||||||
|
'url' => WPAVATAR_PLUGIN_URL . 'assets/images/wapuu-china.png',
|
||||||
|
'name' => __('中国Wapuu', 'wpavatar')
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_exists(WPAVATAR_PLUGIN_DIR . 'assets/images/cravatar-default.png')) {
|
||||||
|
$avatars['cravatar'] = [
|
||||||
|
'url' => WPAVATAR_PLUGIN_URL . 'assets/images/cravatar-default.png',
|
||||||
|
'name' => __('Cravatar默认', 'wpavatar')
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_exists(WPAVATAR_PLUGIN_DIR . 'assets/images/cravatar-logo.png')) {
|
||||||
|
$avatars['cravatar-logo'] = [
|
||||||
|
'url' => WPAVATAR_PLUGIN_URL . 'assets/images/cravatar-logo.png',
|
||||||
|
'name' => __('Cravatar标志', 'wpavatar')
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $avatars;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function modify_profile_picture_description() {
|
||||||
|
return '<a href="https://cravatar.com" target="_blank" rel="noopener">' . __('您可以在初认头像修改您的资料图片', 'wpavatar') . '</a>';
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function pre_get_avatar_data($args, $id_or_email) {
|
||||||
|
if (is_null($args)) {
|
||||||
|
$args = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$email = '';
|
||||||
|
if (is_numeric($id_or_email)) {
|
||||||
|
$user = get_user_by('id', (int)$id_or_email);
|
||||||
|
if ($user) {
|
||||||
|
$email = $user->user_email;
|
||||||
|
}
|
||||||
|
} elseif (is_string($id_or_email)) {
|
||||||
|
if (is_email($id_or_email)) {
|
||||||
|
$email = $id_or_email;
|
||||||
|
}
|
||||||
|
} elseif (is_object($id_or_email)) {
|
||||||
|
if (isset($id_or_email->user_id) && $id_or_email->user_id) {
|
||||||
|
$user = get_user_by('id', $id_or_email->user_id);
|
||||||
|
if ($user) {
|
||||||
|
$email = $user->user_email;
|
||||||
|
}
|
||||||
|
} elseif (isset($id_or_email->comment_author_email)) {
|
||||||
|
$email = $id_or_email->comment_author_email;
|
||||||
|
} elseif (isset($id_or_email->user_email)) {
|
||||||
|
$email = $id_or_email->user_email;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($email)) {
|
||||||
|
return $args;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确定使用的哈希方法
|
||||||
|
$cdn_type = get_option('wpavatar_cdn_type', 'cravatar_route');
|
||||||
|
$use_md5 = true;
|
||||||
|
|
||||||
|
// Cravatar只支持MD5,如果使用Cravatar相关服务,强制使用MD5
|
||||||
|
if ($cdn_type !== 'cravatar_route') {
|
||||||
|
// 检查第三方镜像或自定义CDN是否与Cravatar相关
|
||||||
|
$third_party_mirror = get_option('wpavatar_third_party_mirror', '');
|
||||||
|
$custom_cdn = get_option('wpavatar_custom_cdn', '');
|
||||||
|
$is_cravatar_related = (
|
||||||
|
strpos(strtolower($third_party_mirror), 'cravatar') !== false ||
|
||||||
|
strpos(strtolower($custom_cdn), 'cravatar') !== false
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!$is_cravatar_related) {
|
||||||
|
// 非Cravatar服务使用用户设置的哈希方法
|
||||||
|
$hash_method = get_option('wpavatar_hash_method', 'sha256');
|
||||||
|
$use_md5 = ($hash_method === 'md5');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查WordPress版本,决定是否支持SHA256
|
||||||
|
$wp_version = get_bloginfo('version');
|
||||||
|
$use_sha256_support = version_compare($wp_version, '6.8', '>=');
|
||||||
|
|
||||||
|
// 设置哈希方法
|
||||||
|
if (!$use_md5 && $use_sha256_support) {
|
||||||
|
$args['hash_method'] = 'sha256';
|
||||||
|
$args['hash'] = 'sha256';
|
||||||
|
} else {
|
||||||
|
$args['hash_method'] = 'md5';
|
||||||
|
|
||||||
|
if (isset($args['hash']) && $args['hash'] === 'sha256') {
|
||||||
|
unset($args['hash']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算邮箱地址的哈希值
|
||||||
|
if (!isset($args['wpavatar_email_hash'])) {
|
||||||
|
if (!$use_md5) {
|
||||||
|
$args['wpavatar_email_hash'] = hash('sha256', strtolower(trim($email)));
|
||||||
|
} else {
|
||||||
|
$args['wpavatar_email_hash'] = md5(strtolower(trim($email)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置超时属性
|
||||||
|
$timeout = get_option('wpavatar_timeout', 5);
|
||||||
|
$args['extra_attr'] = isset($args['extra_attr']) ? $args['extra_attr'] : '';
|
||||||
|
$args['extra_attr'] .= ' data-timeout="' . esc_attr($timeout) . '"';
|
||||||
|
|
||||||
|
return $args;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function replace_avatar_url($url) {
|
||||||
|
// 遍历所有Gravatar域名,替换为Cravatar相关域名
|
||||||
|
foreach (self::$gravatar_domains as $domain) {
|
||||||
|
if (strpos($url, $domain) !== false) {
|
||||||
|
$cdn_type = get_option('wpavatar_cdn_type', 'cravatar_route');
|
||||||
|
$cdn_domain = '';
|
||||||
|
|
||||||
|
if ($cdn_type === 'cravatar_route') {
|
||||||
|
$cdn_domain = get_option('wpavatar_cravatar_route', 'cravatar.com');
|
||||||
|
} elseif ($cdn_type === 'third_party') {
|
||||||
|
$cdn_domain = get_option('wpavatar_third_party_mirror', 'weavatar.com');
|
||||||
|
} elseif ($cdn_type === 'custom') {
|
||||||
|
$custom_cdn = get_option('wpavatar_custom_cdn', '');
|
||||||
|
if (!empty($custom_cdn)) {
|
||||||
|
$cdn_domain = $custom_cdn;
|
||||||
|
} else {
|
||||||
|
$cdn_domain = 'cravatar.com';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return str_replace($domain, $cdn_domain, $url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function add_seo_alt($avatar, $id_or_email, $size, $default, $alt) {
|
||||||
|
// 添加SEO友好的alt属性
|
||||||
|
$seo_alt = get_option('wpavatar_seo_alt');
|
||||||
|
if (!empty($seo_alt)) {
|
||||||
|
$user = false;
|
||||||
|
if (is_numeric($id_or_email)) {
|
||||||
|
$user = get_user_by('id', $id_or_email);
|
||||||
|
} elseif (is_object($id_or_email)) {
|
||||||
|
if (isset($id_or_email->user_id) && $id_or_email->user_id) {
|
||||||
|
$user = get_user_by('id', $id_or_email->user_id);
|
||||||
|
} elseif (isset($id_or_email->comment_author_email)) {
|
||||||
|
$user = (object) [
|
||||||
|
'display_name' => $id_or_email->comment_author
|
||||||
|
];
|
||||||
|
}
|
||||||
|
} elseif (is_string($id_or_email) && is_email($id_or_email)) {
|
||||||
|
$user = get_user_by('email', $id_or_email);
|
||||||
|
}
|
||||||
|
|
||||||
|
$alt_text = $user ? sprintf($seo_alt, $user->display_name) : __('用户头像', 'wpavatar');
|
||||||
|
$avatar = preg_replace('/alt=([\'"])[^\'"]*\\1/', "alt='$alt_text'", $avatar);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加头像加载失败的备用显示
|
||||||
|
if (get_option('wpavatar_fallback_mode', 1)) {
|
||||||
|
$fallback_type = get_option('wpavatar_fallback_avatar', 'default');
|
||||||
|
$local_avatars = self::get_local_avatars();
|
||||||
|
|
||||||
|
if (isset($local_avatars[$fallback_type])) {
|
||||||
|
$fallback_url = $local_avatars[$fallback_type]['url'];
|
||||||
|
|
||||||
|
if (strpos($avatar, 'onerror') === false) {
|
||||||
|
$avatar = str_replace('<img ', '<img onerror="this.onerror=null;this.src=\'' . esc_url($fallback_url) . '\';" ', $avatar);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $avatar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Cache {
|
||||||
|
public static function init() {
|
||||||
|
add_action('init', [__CLASS__, 'setup_cache_dir']);
|
||||||
|
add_action('init', [__CLASS__, 'schedule_purge']);
|
||||||
|
|
||||||
|
if (get_option('wpavatar_enable_cache', true)) {
|
||||||
|
add_filter('get_avatar_url', [__CLASS__, 'prepare_cache_url'], 99, 2);
|
||||||
|
add_filter('get_avatar', [__CLASS__, 'serve_cached_avatar'], 20, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
add_action('wpavatar_purge_cache', [__CLASS__, 'purge_expired']);
|
||||||
|
|
||||||
|
add_action('comment_post', [__CLASS__, 'cache_comment_avatar'], 10, 2);
|
||||||
|
add_action('profile_update', [__CLASS__, 'cache_user_avatar'], 10, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function setup_cache_dir() {
|
||||||
|
$dir = get_option('wpavatar_cache_path', WPAVATAR_CACHE_DIR);
|
||||||
|
|
||||||
|
$dir = rtrim($dir, '/\\') . '/';
|
||||||
|
|
||||||
|
if (!file_exists($dir)) {
|
||||||
|
if (!wp_mkdir_p($dir)) {
|
||||||
|
add_settings_error(
|
||||||
|
'wpavatar_cache',
|
||||||
|
'cache_dir_error',
|
||||||
|
__('无法创建缓存目录,请检查权限', 'wpavatar'),
|
||||||
|
'error'
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_writable($dir)) {
|
||||||
|
add_settings_error(
|
||||||
|
'wpavatar_cache',
|
||||||
|
'cache_dir_writable',
|
||||||
|
__('缓存目录不可写,请检查权限', 'wpavatar'),
|
||||||
|
'error'
|
||||||
|
);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$index_file = $dir . 'index.php';
|
||||||
|
if (!file_exists($index_file)) {
|
||||||
|
@file_put_contents($index_file, '<?php // Silence is golden.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$htaccess_file = $dir . '.htaccess';
|
||||||
|
if (!file_exists($htaccess_file)) {
|
||||||
|
$htaccess_content = "# Prevent directory listing\n";
|
||||||
|
$htaccess_content .= "Options -Indexes\n";
|
||||||
|
$htaccess_content .= "# Cache images for one week\n";
|
||||||
|
$htaccess_content .= "<IfModule mod_expires.c>\n";
|
||||||
|
$htaccess_content .= "ExpiresActive On\n";
|
||||||
|
$htaccess_content .= "ExpiresByType image/jpeg \"access plus 1 week\"\n";
|
||||||
|
$htaccess_content .= "</IfModule>\n";
|
||||||
|
@file_put_contents($htaccess_file, $htaccess_content);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function prepare_cache_url($url, $id_or_email) {
|
||||||
|
if (!get_option('wpavatar_enable_cache', true)) {
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strpos($url, 'cache-key=') === false) {
|
||||||
|
$hash = self::get_avatar_hash($id_or_email);
|
||||||
|
if ($hash) {
|
||||||
|
$url .= (strpos($url, '?') !== false ? '&' : '?') . 'cache-key=' . $hash;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function serve_cached_avatar($avatar, $id_or_email, $size, $default, $alt) {
|
||||||
|
if (!get_option('wpavatar_enable_cache', true)) {
|
||||||
|
return $avatar;
|
||||||
|
}
|
||||||
|
|
||||||
|
preg_match('/src=([\'"])([^\'"]+)\\1/', $avatar, $matches);
|
||||||
|
if (empty($matches[2])) {
|
||||||
|
return $avatar;
|
||||||
|
}
|
||||||
|
|
||||||
|
$url = $matches[2];
|
||||||
|
|
||||||
|
$is_avatar_url = false;
|
||||||
|
|
||||||
|
$cdn_type = get_option('wpavatar_cdn_type', 'cravatar_route');
|
||||||
|
if ($cdn_type === 'cravatar_route') {
|
||||||
|
$cdn_domain = get_option('wpavatar_cravatar_route', 'cravatar.com');
|
||||||
|
if (strpos($url, $cdn_domain) !== false) {
|
||||||
|
$is_avatar_url = true;
|
||||||
|
}
|
||||||
|
} elseif ($cdn_type === 'third_party') {
|
||||||
|
$cdn_domain = get_option('wpavatar_third_party_mirror', 'weavatar.com');
|
||||||
|
if (strpos($url, $cdn_domain) !== false) {
|
||||||
|
$is_avatar_url = true;
|
||||||
|
}
|
||||||
|
} elseif ($cdn_type === 'custom') {
|
||||||
|
$cdn_domain = get_option('wpavatar_custom_cdn', '');
|
||||||
|
if (!empty($cdn_domain) && strpos($url, $cdn_domain) !== false) {
|
||||||
|
$is_avatar_url = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否为Gravatar URL
|
||||||
|
if (!$is_avatar_url) {
|
||||||
|
foreach (Cravatar::$gravatar_domains as $domain) {
|
||||||
|
if (strpos($url, $domain) !== false) {
|
||||||
|
$is_avatar_url = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$is_avatar_url) {
|
||||||
|
return $avatar;
|
||||||
|
}
|
||||||
|
|
||||||
|
$hash = self::get_avatar_hash($id_or_email);
|
||||||
|
$cache_file = self::get_cache_path($hash, $size);
|
||||||
|
|
||||||
|
$cache_expire = get_option('wpavatar_cache_expire', 7) * DAY_IN_SECONDS;
|
||||||
|
if (file_exists($cache_file) && filemtime($cache_file) > (time() - $cache_expire)) {
|
||||||
|
$cached_url = content_url(str_replace(WP_CONTENT_DIR, '', $cache_file));
|
||||||
|
return str_replace($url, esc_url($cached_url), $avatar);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (self::cache_remote_avatar($url, $cache_file)) {
|
||||||
|
$cached_url = content_url(str_replace(WP_CONTENT_DIR, '', $cache_file));
|
||||||
|
return str_replace($url, esc_url($cached_url), $avatar);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $avatar;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function get_avatar_hash($id_or_email) {
|
||||||
|
$email = '';
|
||||||
|
|
||||||
|
if (is_object($id_or_email)) {
|
||||||
|
if (isset($id_or_email->comment_author_email)) {
|
||||||
|
$email = $id_or_email->comment_author_email;
|
||||||
|
} elseif (isset($id_or_email->user_email)) {
|
||||||
|
$email = $id_or_email->user_email;
|
||||||
|
}
|
||||||
|
} elseif (is_numeric($id_or_email)) {
|
||||||
|
$user = get_user_by('id', $id_or_email);
|
||||||
|
$email = $user ? $user->user_email : '';
|
||||||
|
} elseif (is_string($id_or_email) && is_email($id_or_email)) {
|
||||||
|
$email = $id_or_email;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($email)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$cdn_type = get_option('wpavatar_cdn_type', 'cravatar_route');
|
||||||
|
$use_md5 = true;
|
||||||
|
|
||||||
|
// 确定使用的哈希方法
|
||||||
|
if ($cdn_type !== 'cravatar_route' &&
|
||||||
|
strpos(get_option('wpavatar_third_party_mirror', ''), 'cravatar') === false &&
|
||||||
|
strpos(get_option('wpavatar_custom_cdn', ''), 'cravatar') === false) {
|
||||||
|
$hash_method = get_option('wpavatar_hash_method', 'md5');
|
||||||
|
$use_md5 = ($hash_method === 'md5');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$use_md5) {
|
||||||
|
return hash('sha256', strtolower(trim($email)));
|
||||||
|
} else {
|
||||||
|
return md5(strtolower(trim($email)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function get_cache_path($hash, $size) {
|
||||||
|
$dir = get_option('wpavatar_cache_path', WPAVATAR_CACHE_DIR);
|
||||||
|
$dir = trailingslashit($dir);
|
||||||
|
wp_mkdir_p($dir);
|
||||||
|
return $dir . "{$hash}-{$size}.jpg";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function cache_remote_avatar($url, $dest) {
|
||||||
|
$dir = dirname($dest);
|
||||||
|
if (!file_exists($dir)) {
|
||||||
|
if (!wp_mkdir_p($dir)) {
|
||||||
|
error_log('WPAvatar: Failed to create cache directory: ' . $dir);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_writable($dir)) {
|
||||||
|
error_log('WPAvatar: Cache directory is not writable: ' . $dir);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$timeout = get_option('wpavatar_timeout', 5);
|
||||||
|
|
||||||
|
$response = wp_remote_get($url, [
|
||||||
|
'timeout' => $timeout,
|
||||||
|
'user-agent' => 'WPAvatar/' . WPAVATAR_VERSION . '; ' . home_url()
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (is_wp_error($response)) {
|
||||||
|
error_log('WPAvatar: Error fetching avatar: ' . $response->get_error_message());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$response_code = wp_remote_retrieve_response_code($response);
|
||||||
|
if ($response_code !== 200) {
|
||||||
|
error_log('WPAvatar: Bad response code: ' . $response_code);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$image = wp_remote_retrieve_body($response);
|
||||||
|
if (empty($image)) {
|
||||||
|
error_log('WPAvatar: Empty image received');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$content_type = wp_remote_retrieve_header($response, 'content-type');
|
||||||
|
if (strpos($content_type, 'image/') !== 0) {
|
||||||
|
error_log('WPAvatar: Invalid content type: ' . $content_type);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = file_put_contents($dest, $image);
|
||||||
|
if ($result === false) {
|
||||||
|
error_log('WPAvatar: Failed to write cache file: ' . $dest);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@chmod($dest, 0644);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function cache_comment_avatar($comment_id, $comment_approved) {
|
||||||
|
if ($comment_approved !== 1 || !get_option('wpavatar_enable_cache', true)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$comment = get_comment($comment_id);
|
||||||
|
if (!$comment || empty($comment->comment_author_email)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$email = $comment->comment_author_email;
|
||||||
|
$size = get_option('wpavatar_shortcode_size', 96);
|
||||||
|
|
||||||
|
$avatar_url = get_avatar_url($email, ['size' => $size]);
|
||||||
|
|
||||||
|
$hash = self::get_avatar_hash($email);
|
||||||
|
if (empty($hash)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$cache_file = self::get_cache_path($hash, $size);
|
||||||
|
|
||||||
|
self::cache_remote_avatar($avatar_url, $cache_file);
|
||||||
|
|
||||||
|
// 缓存2x尺寸的头像,用于高分辨率显示器
|
||||||
|
$retina_url = get_avatar_url($email, ['size' => $size * 2]);
|
||||||
|
$retina_cache_file = self::get_cache_path($hash, $size * 2);
|
||||||
|
self::cache_remote_avatar($retina_url, $retina_cache_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function cache_user_avatar($user_id) {
|
||||||
|
if (!get_option('wpavatar_enable_cache', true)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = get_user_by('id', $user_id);
|
||||||
|
if (!$user) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$email = $user->user_email;
|
||||||
|
$size = get_option('wpavatar_shortcode_size', 96);
|
||||||
|
|
||||||
|
$avatar_url = get_avatar_url($email, ['size' => $size]);
|
||||||
|
|
||||||
|
$hash = self::get_avatar_hash($email);
|
||||||
|
if (empty($hash)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$cache_file = self::get_cache_path($hash, $size);
|
||||||
|
|
||||||
|
self::cache_remote_avatar($avatar_url, $cache_file);
|
||||||
|
|
||||||
|
// 缓存2x尺寸的头像,用于高分辨率显示器
|
||||||
|
$retina_url = get_avatar_url($email, ['size' => $size * 2]);
|
||||||
|
$retina_cache_file = self::get_cache_path($hash, $size * 2);
|
||||||
|
self::cache_remote_avatar($retina_url, $retina_cache_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function purge_expired() {
|
||||||
|
$dir = get_option('wpavatar_cache_path', WPAVATAR_CACHE_DIR);
|
||||||
|
if (!file_exists($dir) || !is_dir($dir)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$files = glob(trailingslashit($dir) . '*.jpg');
|
||||||
|
if (!$files) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$expire_days = get_option('wpavatar_cache_expire', 7);
|
||||||
|
$expire_time = time() - ($expire_days * DAY_IN_SECONDS);
|
||||||
|
|
||||||
|
foreach ($files as $file) {
|
||||||
|
if (filemtime($file) < $expire_time) {
|
||||||
|
@unlink($file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function schedule_purge() {
|
||||||
|
if (!wp_next_scheduled('wpavatar_purge_cache')) {
|
||||||
|
wp_schedule_event(time(), 'daily', 'wpavatar_purge_cache');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function check_cache_status() {
|
||||||
|
$dir = get_option('wpavatar_cache_path', WPAVATAR_CACHE_DIR);
|
||||||
|
$stats = [
|
||||||
|
'path' => $dir,
|
||||||
|
'exists' => file_exists($dir) && is_dir($dir),
|
||||||
|
'writable' => is_writable($dir),
|
||||||
|
'file_count' => 0,
|
||||||
|
'size' => 0
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($stats['exists']) {
|
||||||
|
$files = glob(trailingslashit($dir) . '*.jpg');
|
||||||
|
$stats['file_count'] = count($files ?: []);
|
||||||
|
foreach ($files ?: [] as $file) {
|
||||||
|
$stats['size'] += filesize($file);
|
||||||
|
}
|
||||||
|
$stats['size'] = size_format($stats['size']);
|
||||||
|
}
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
?>
|
||||||
|
<div class="cache-stats">
|
||||||
|
<p><?php printf(__('缓存目录: %s', 'wpavatar'), esc_html($stats['path'])); ?></p>
|
||||||
|
<p><?php printf(__('目录存在: %s', 'wpavatar'), $stats['exists'] ? __('是', 'wpavatar') : __('否', 'wpavatar')); ?></p>
|
||||||
|
<p><?php printf(__('目录可写: %s', 'wpavatar'), $stats['writable'] ? __('是', 'wpavatar') : __('否', 'wpavatar')); ?></p>
|
||||||
|
<p><?php printf(__('缓存文件数: %d', 'wpavatar'), $stats['file_count']); ?></p>
|
||||||
|
<p><?php printf(__('缓存总大小: %s', 'wpavatar'), $stats['size']); ?></p>
|
||||||
|
</div>
|
||||||
|
<?php
|
||||||
|
return ob_get_clean();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Shortcode {
|
||||||
|
public static function init() {
|
||||||
|
add_shortcode('wpavatar', [__CLASS__, 'render_avatar']);
|
||||||
|
add_shortcode('wpavatar_username', [__CLASS__, 'render_username']);
|
||||||
|
add_filter('walker_nav_menu_start_el', [__CLASS__, 'menu_item_replace'], 10, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function render_avatar($atts) {
|
||||||
|
$default_size = get_option('wpavatar_shortcode_size', 96);
|
||||||
|
$default_class = get_option('wpavatar_shortcode_class', 'wpavatar');
|
||||||
|
$default_shape = get_option('wpavatar_shortcode_shape', 'square');
|
||||||
|
|
||||||
|
$atts = shortcode_atts([
|
||||||
|
'size' => $default_size,
|
||||||
|
'user_id' => get_current_user_id(),
|
||||||
|
'class' => $default_class,
|
||||||
|
'email' => '',
|
||||||
|
'shape' => $default_shape,
|
||||||
|
'title' => ''
|
||||||
|
], $atts);
|
||||||
|
|
||||||
|
if (empty($atts['user_id']) && empty($atts['email'])) {
|
||||||
|
$atts['user_id'] = get_current_user_id();
|
||||||
|
}
|
||||||
|
|
||||||
|
$classes = [$atts['class']];
|
||||||
|
|
||||||
|
if ($atts['shape'] === 'circle') {
|
||||||
|
$classes[] = 'avatar-circle';
|
||||||
|
$style = 'style="border-radius: 50%; overflow: hidden;"';
|
||||||
|
} elseif ($atts['shape'] === 'rounded') {
|
||||||
|
$classes[] = 'avatar-rounded';
|
||||||
|
$style = 'style="border-radius: 8px; overflow: hidden;"';
|
||||||
|
} else {
|
||||||
|
$classes[] = 'avatar-square';
|
||||||
|
$style = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$avatar_args = [
|
||||||
|
'class' => implode(' ', $classes),
|
||||||
|
'size' => intval($atts['size']),
|
||||||
|
'extra_attr' => $style
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!empty($atts['title'])) {
|
||||||
|
$avatar_args['extra_attr'] .= ' title="' . esc_attr($atts['title']) . '"';
|
||||||
|
}
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
if (!empty($atts['email'])) {
|
||||||
|
echo get_avatar($atts['email'], intval($atts['size']), 'default', '', $avatar_args);
|
||||||
|
} else {
|
||||||
|
echo get_avatar($atts['user_id'], intval($atts['size']), 'default', '', $avatar_args);
|
||||||
|
}
|
||||||
|
return ob_get_clean();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function render_username($atts) {
|
||||||
|
$atts = shortcode_atts([
|
||||||
|
'user_id' => get_current_user_id(),
|
||||||
|
'before' => '',
|
||||||
|
'after' => ''
|
||||||
|
], $atts);
|
||||||
|
|
||||||
|
if (!empty($atts['user_id'])) {
|
||||||
|
$user = get_user_by('id', $atts['user_id']);
|
||||||
|
} else {
|
||||||
|
$user = wp_get_current_user();
|
||||||
|
}
|
||||||
|
|
||||||
|
$username = $user && $user->display_name ? $user->display_name : __('匿名用户', 'wpavatar');
|
||||||
|
return $atts['before'] . $username . $atts['after'];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function menu_item_replace($item_output, $item, $depth, $args) {
|
||||||
|
if (strpos($item_output, '{wpavatar}') !== false) {
|
||||||
|
$item_output = str_replace('{wpavatar}', do_shortcode('[wpavatar shape="circle" size="32"]'), $item_output);
|
||||||
|
}
|
||||||
|
if (strpos($item_output, '{wpavatar_username}') !== false) {
|
||||||
|
$item_output = str_replace('{wpavatar_username}', do_shortcode('[wpavatar_username]'), $item_output);
|
||||||
|
}
|
||||||
|
return $item_output;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function generate_preview($user_id = 0, $shape = 'square', $size = 96) {
|
||||||
|
if (!$user_id) {
|
||||||
|
$user_id = get_current_user_id();
|
||||||
|
}
|
||||||
|
|
||||||
|
$atts = [
|
||||||
|
'user_id' => $user_id,
|
||||||
|
'shape' => $shape,
|
||||||
|
'size' => $size,
|
||||||
|
'class' => 'wpavatar-preview'
|
||||||
|
];
|
||||||
|
|
||||||
|
return self::render_avatar($atts);
|
||||||
|
}
|
||||||
|
}
|
266
wpavatar.php
|
@ -1,44 +1,258 @@
|
||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Plugin Name: WPAvatar
|
* Plugin Name: WPAvatar
|
||||||
|
* Version: 1.6.3
|
||||||
* Plugin URI: https://wpavatar.com/download
|
* Plugin URI: https://wpavatar.com/download
|
||||||
* Description: Replace Gravatar with Cravatar, a perfect replacement of Gravatar in China.
|
* Description: Replace Gravatar with Cravatar, a perfect replacement of Gravatar in China.
|
||||||
* Author: WPfanyi
|
* Author: WPfanyi
|
||||||
* Author URI: https://wpfanyi.com/
|
* Author URI: https://wpfanyi.com/
|
||||||
* Text Domain: wpavatar
|
* Text Domain: wpavatar
|
||||||
* Domain Path: /languages
|
* Domain Path: /languages
|
||||||
* Version: 1.0
|
|
||||||
* License: GPLv2 or later
|
|
||||||
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
|
||||||
*
|
|
||||||
* WP Avatar is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 2 of the License, or
|
|
||||||
* any later version.
|
|
||||||
*
|
|
||||||
* WP Avatar is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if (!defined('ABSPATH')) {
|
defined('ABSPATH') || exit;
|
||||||
exit; // Exit if accessed directly
|
|
||||||
|
define('WPAVATAR_VERSION', '1.6.3');
|
||||||
|
define('WPAVATAR_PLUGIN_DIR', plugin_dir_path(__FILE__));
|
||||||
|
define('WPAVATAR_PLUGIN_URL', plugin_dir_url(__FILE__));
|
||||||
|
define('WPAVATAR_CACHE_DIR', WP_CONTENT_DIR . '/uploads/cravatar');
|
||||||
|
|
||||||
|
if (!file_exists(WPAVATAR_PLUGIN_DIR . 'assets')) {
|
||||||
|
wp_mkdir_p(WPAVATAR_PLUGIN_DIR . 'assets');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!file_exists(WPAVATAR_PLUGIN_DIR . 'assets/css')) {
|
||||||
|
wp_mkdir_p(WPAVATAR_PLUGIN_DIR . 'assets/css');
|
||||||
|
|
||||||
require_once( plugin_dir_path( __FILE__ ) . 'includes/cravatar.php' );
|
$css_file = WPAVATAR_PLUGIN_DIR . 'admin.css';
|
||||||
|
if (file_exists($css_file)) {
|
||||||
|
copy($css_file, WPAVATAR_PLUGIN_DIR . 'assets/css/admin.css');
|
||||||
register_activation_hook( __FILE__, 'wpavatar_activate' );
|
}
|
||||||
|
|
||||||
function wpavatar_activate() {
|
|
||||||
update_option( 'wpavatar_enable_cravatar', '1' );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!file_exists(WPAVATAR_PLUGIN_DIR . 'assets/js')) {
|
||||||
|
wp_mkdir_p(WPAVATAR_PLUGIN_DIR . 'assets/js');
|
||||||
|
|
||||||
// Load translation
|
$js_file = WPAVATAR_PLUGIN_DIR . 'admin.js';
|
||||||
add_action( 'init', 'wpavatar_load_textdomain' );
|
if (file_exists($js_file)) {
|
||||||
function wpavatar_load_textdomain() {
|
copy($js_file, WPAVATAR_PLUGIN_DIR . 'assets/js/admin.js');
|
||||||
load_plugin_textdomain( 'wpavatar', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!file_exists(WPAVATAR_PLUGIN_DIR . 'assets/images')) {
|
||||||
|
wp_mkdir_p(WPAVATAR_PLUGIN_DIR . 'assets/images');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file_exists(WPAVATAR_PLUGIN_DIR . 'includes')) {
|
||||||
|
wp_mkdir_p(WPAVATAR_PLUGIN_DIR . 'includes');
|
||||||
|
|
||||||
|
if (file_exists(WPAVATAR_PLUGIN_DIR . 'core.php')) {
|
||||||
|
copy(WPAVATAR_PLUGIN_DIR . 'core.php', WPAVATAR_PLUGIN_DIR . 'includes/core.php');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_exists(WPAVATAR_PLUGIN_DIR . 'admin.php')) {
|
||||||
|
copy(WPAVATAR_PLUGIN_DIR . 'admin.php', WPAVATAR_PLUGIN_DIR . 'includes/admin.php');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
require_once WPAVATAR_PLUGIN_DIR . 'includes/core.php';
|
||||||
|
require_once WPAVATAR_PLUGIN_DIR . 'includes/admin.php';
|
||||||
|
|
||||||
|
add_action('wp_ajax_wpavatar_purge_cache', 'wpavatar_purge_cache_ajax');
|
||||||
|
function wpavatar_purge_cache_ajax() {
|
||||||
|
check_ajax_referer('wpavatar_admin_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('manage_options')) {
|
||||||
|
wp_send_json_error(__('权限不足', 'wpavatar'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$dir = get_option('wpavatar_cache_path', WPAVATAR_CACHE_DIR);
|
||||||
|
$dir = rtrim($dir, '/\\') . '/';
|
||||||
|
|
||||||
|
if (!file_exists($dir) || !is_dir($dir)) {
|
||||||
|
wp_send_json_error(__('缓存目录不存在', 'wpavatar'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$files = glob($dir . '*.jpg');
|
||||||
|
$count = 0;
|
||||||
|
|
||||||
|
if ($files) {
|
||||||
|
foreach ($files as $file) {
|
||||||
|
if (strpos($file, $dir) === 0 && file_exists($file)) {
|
||||||
|
if (@unlink($file)) {
|
||||||
|
$count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_send_json_success(sprintf(__('已清空 %d 个缓存文件', 'wpavatar'), $count));
|
||||||
|
}
|
||||||
|
|
||||||
|
add_action('wp_ajax_wpavatar_check_cache', 'wpavatar_check_cache_ajax');
|
||||||
|
function wpavatar_check_cache_ajax() {
|
||||||
|
check_ajax_referer('wpavatar_admin_nonce', 'nonce');
|
||||||
|
|
||||||
|
if (!current_user_can('manage_options')) {
|
||||||
|
wp_send_json_error(__('权限不足', 'wpavatar'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$result = \WPAvatar\Cache::check_cache_status();
|
||||||
|
wp_send_json_success($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
add_action('plugins_loaded', function () {
|
||||||
|
load_plugin_textdomain('wpavatar', false, dirname(plugin_basename(__FILE__)) . '/languages');
|
||||||
|
|
||||||
|
$default_avatar = WPAVATAR_PLUGIN_DIR . 'assets/images/default-avatar.png';
|
||||||
|
if (!file_exists($default_avatar)) {
|
||||||
|
$placeholder = WPAVATAR_PLUGIN_DIR . 'assets/images/placeholder-avatar.png';
|
||||||
|
|
||||||
|
if (file_exists($placeholder)) {
|
||||||
|
copy($placeholder, $default_avatar);
|
||||||
|
} else {
|
||||||
|
$avatar_image = imagecreatetruecolor(96, 96);
|
||||||
|
$bg_color = imagecolorallocate($avatar_image, 238, 238, 238);
|
||||||
|
$text_color = imagecolorallocate($avatar_image, 68, 68, 68);
|
||||||
|
|
||||||
|
imagefill($avatar_image, 0, 0, $bg_color);
|
||||||
|
imagestring($avatar_image, 5, 30, 40, 'Avatar', $text_color);
|
||||||
|
|
||||||
|
wp_mkdir_p(dirname($default_avatar));
|
||||||
|
imagepng($avatar_image, $default_avatar);
|
||||||
|
imagedestroy($avatar_image);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$cravatar_logo = WPAVATAR_PLUGIN_DIR . 'assets/images/cravatar-logo.png';
|
||||||
|
if (!file_exists($cravatar_logo) && function_exists('file_get_contents')) {
|
||||||
|
$logo_url = 'https://cn.cravatar.com/avatar/00000000000000000000000000000000?d=cravatar_logo';
|
||||||
|
$logo_data = @file_get_contents($logo_url);
|
||||||
|
if ($logo_data) {
|
||||||
|
wp_mkdir_p(dirname($cravatar_logo));
|
||||||
|
@file_put_contents($cravatar_logo, $logo_data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_multisite()) {
|
||||||
|
\WPAvatar\Network::init();
|
||||||
|
}
|
||||||
|
|
||||||
|
\WPAvatar\Core::init();
|
||||||
|
\WPAvatar\Cravatar::init();
|
||||||
|
\WPAvatar\Cache::init();
|
||||||
|
\WPAvatar\Shortcode::init();
|
||||||
|
|
||||||
|
if (is_admin()) {
|
||||||
|
\WPAvatar\Settings::init();
|
||||||
|
}
|
||||||
|
|
||||||
|
add_filter('gettext', 'wpavatar_replace_gravatar_text', 20, 3);
|
||||||
|
add_filter('ngettext', 'wpavatar_replace_gravatar_text_plural', 20, 4);
|
||||||
|
});
|
||||||
|
|
||||||
|
function wpavatar_replace_gravatar_text($translated_text, $text, $domain) {
|
||||||
|
if (!get_option('wpavatar_enable_cravatar', 1)) {
|
||||||
|
return $translated_text;
|
||||||
|
}
|
||||||
|
|
||||||
|
$current_screen = function_exists('get_current_screen') ? get_current_screen() : null;
|
||||||
|
$screen_id = $current_screen ? $current_screen->id : '';
|
||||||
|
|
||||||
|
$is_discussion_page = ($screen_id === 'options-discussion' ||
|
||||||
|
(isset($_GET['page']) && $_GET['page'] === 'discussion'));
|
||||||
|
|
||||||
|
$is_comments_page = ($screen_id === 'edit-comments' ||
|
||||||
|
$screen_id === 'comment');
|
||||||
|
|
||||||
|
$is_profile_page = ($screen_id === 'profile' ||
|
||||||
|
$screen_id === 'user-edit');
|
||||||
|
|
||||||
|
if ($is_discussion_page || $is_comments_page || $is_profile_page) {
|
||||||
|
$translated_text = str_replace('Gravatar', 'Cravatar', $translated_text);
|
||||||
|
$translated_text = str_replace('gravatar', 'cravatar', $translated_text);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $translated_text;
|
||||||
|
}
|
||||||
|
|
||||||
|
function wpavatar_replace_gravatar_text_plural($translated_text, $single, $plural, $number) {
|
||||||
|
if (!get_option('wpavatar_enable_cravatar', 1)) {
|
||||||
|
return $translated_text;
|
||||||
|
}
|
||||||
|
|
||||||
|
$current_screen = function_exists('get_current_screen') ? get_current_screen() : null;
|
||||||
|
$screen_id = $current_screen ? $current_screen->id : '';
|
||||||
|
|
||||||
|
$is_relevant_page = ($screen_id === 'options-discussion' ||
|
||||||
|
$screen_id === 'edit-comments' ||
|
||||||
|
$screen_id === 'comment' ||
|
||||||
|
$screen_id === 'profile' ||
|
||||||
|
$screen_id === 'user-edit' ||
|
||||||
|
(isset($_GET['page']) && $_GET['page'] === 'discussion'));
|
||||||
|
|
||||||
|
if ($is_relevant_page) {
|
||||||
|
$translated_text = str_replace('Gravatar', 'Cravatar', $translated_text);
|
||||||
|
$translated_text = str_replace('gravatar', 'cravatar', $translated_text);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $translated_text;
|
||||||
|
}
|
||||||
|
|
||||||
|
register_activation_hook(__FILE__, function() {
|
||||||
|
add_option('wpavatar_enable_cravatar', 1);
|
||||||
|
add_option('wpavatar_cdn_type', 'cravatar_route');
|
||||||
|
add_option('wpavatar_cravatar_route', 'cravatar.com');
|
||||||
|
add_option('wpavatar_third_party_mirror', 'weavatar.com');
|
||||||
|
add_option('wpavatar_custom_cdn', '');
|
||||||
|
add_option('wpavatar_hash_method', 'md5');
|
||||||
|
add_option('wpavatar_timeout', 5);
|
||||||
|
|
||||||
|
add_option('wpavatar_enable_cache', 1);
|
||||||
|
add_option('wpavatar_cache_path', WPAVATAR_CACHE_DIR);
|
||||||
|
add_option('wpavatar_cache_expire', 15);
|
||||||
|
|
||||||
|
add_option('wpavatar_seo_alt', '%s的头像');
|
||||||
|
add_option('wpavatar_fallback_mode', 1);
|
||||||
|
add_option('wpavatar_fallback_avatar', 'default');
|
||||||
|
|
||||||
|
add_option('wpavatar_shortcode_size', 96);
|
||||||
|
add_option('wpavatar_shortcode_class', 'wpavatar');
|
||||||
|
add_option('wpavatar_shortcode_shape', 'square');
|
||||||
|
|
||||||
|
wp_mkdir_p(WPAVATAR_CACHE_DIR);
|
||||||
|
|
||||||
|
$index_file = rtrim(WPAVATAR_CACHE_DIR, '/\\') . '/index.php';
|
||||||
|
if (!file_exists($index_file)) {
|
||||||
|
@file_put_contents($index_file, '<?php // Silence is golden.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$htaccess_file = rtrim(WPAVATAR_CACHE_DIR, '/\\') . '/.htaccess';
|
||||||
|
if (!file_exists($htaccess_file)) {
|
||||||
|
$htaccess_content = "# Prevent directory listing\n";
|
||||||
|
$htaccess_content .= "Options -Indexes\n";
|
||||||
|
$htaccess_content .= "# Cache images for one week\n";
|
||||||
|
$htaccess_content .= "<IfModule mod_expires.c>\n";
|
||||||
|
$htaccess_content .= "ExpiresActive On\n";
|
||||||
|
$htaccess_content .= "ExpiresByType image/jpeg \"access plus 1 week\"\n";
|
||||||
|
$htaccess_content .= "</IfModule>\n";
|
||||||
|
@file_put_contents($htaccess_file, $htaccess_content);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_multisite()) {
|
||||||
|
add_site_option('wpavatar_network_enabled', 1);
|
||||||
|
add_site_option('wpavatar_network_cdn_type', 'cravatar_route');
|
||||||
|
add_site_option('wpavatar_network_cravatar_route', 'cravatar.com');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!wp_next_scheduled('wpavatar_purge_cache')) {
|
||||||
|
wp_schedule_event(time(), 'daily', 'wpavatar_purge_cache');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
register_deactivation_hook(__FILE__, function() {
|
||||||
|
wp_clear_scheduled_hook('wpavatar_purge_cache');
|
||||||
|
});
|
||||||
|
|