HEX
Server: nginx/1.16.1
System: Linux VM-0-14-centos 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed Dec 22 13:25:12 UTC 2021 x86_64
User: www (1000)
PHP: 8.3.31
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/1.scey.cn/wp-content/themes/Leading/extends/wpjam-cache/public/post-meta.php
<?php
add_filter('update_post_metadata', function($check, $post_id, $meta_key, $meta_value){
	if($meta_key == 'views'){
		if($meta_value % 10 != 0){
			$check	= true;

			wp_cache_set($post_id, $meta_value, 'views');
		}else{
			wp_cache_delete($post_id, 'views');
		}
	}elseif($meta_key == '_edit_lock'){
		return wp_cache_set($post_id, $meta_value, '_edit_lock', 300);
	}elseif($meta_key == '_edit_last'){
		if(get_post($post_id)->post_author == $meta_value){
			if(get_post_meta($post_id, $meta_key, true) != $meta_value){
				delete_post_meta($post_id, $meta_key);
			}
			
			return true;
		}
	}

	return $check;
}, 1, 4);

// 防止以经常更新浏览数,破坏列表缓存
add_action('updated_post_meta', function($meta_id, $object_id, $meta_key, $_meta_value){
	if($meta_key == 'views' && $_meta_value % 50 != 0){
		remove_action('updated_post_meta', 'wp_cache_set_posts_last_changed');
	}
}, 1, 4);


add_filter('add_post_metadata', function($check, $post_id, $meta_key, $meta_value){
	if($meta_key == '_edit_lock'){
		return wp_cache_set($post_id, $meta_value, '_edit_lock', 300);
	}elseif($meta_key == '_edit_last'){
		if(get_post($post_id)->post_author == $meta_value){
			return true;
		}
	}elseif($meta_key == '_wp_old_slug'){
		if(strpos($meta_value, '%') !== false){	// 含有 % 说明不是英文,含有中文和特殊字符
			return true;
		}
	}

	return $check;
}, 1, 4);

add_filter('get_post_metadata', function($pre, $post_id, $meta_key){
	$cache_keys	= ['views', '_edit_lock'];
	
	if(in_array($meta_key, $cache_keys)){
		$meta_value	= wp_cache_get($post_id, $meta_key);

		if($meta_value !== false){
			return [$meta_value];
		}
	}elseif($meta_key == '_edit_last'){
		$meta_values	= get_post_meta($post_id);

		if(!isset($meta_values['_edit_last'])){
			$meta_values['_edit_last']	= [get_post($post_id)->post_author];
		}
		
		return $meta_values['_edit_last'];
	}elseif($meta_key == ''){
		$meta_cache	= wp_cache_get($post_id, 'post_meta');

		if($meta_cache === false) {
			$meta_cache	= update_meta_cache('post', [$post_id]);
			$meta_cache	= $meta_cache[$post_id];
		}

		foreach($cache_keys as $mkey){
			$mval	= wp_cache_get($post_id, $mkey);
			if($mval !== false){
				$meta_cache[$mkey]	= [$mval];
			}
		}

		return $meta_cache;	
	}

	return $pre;
}, 1, 3);