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-query.php
<?php
add_action('parse_query', function (&$wp_query){
	if(!is_admin() && $wp_query->get('post_type') == 'nav_menu_item'){	// 让菜单也支持缓存
		$wp_query->set('suppress_filters', false);
	}
});

add_filter('posts_pre_query', function ($pre, $wp_query){
	if($wp_query->get('orderby') == 'rand'){	// 随机排序就不能缓存了
		return $pre;
	}

	if(!$wp_query->is_main_query() && $wp_query->get('post_type') != 'nav_menu_item' && !$wp_query->get('cache_it') ){	// 只缓存主循环 || 菜单 || 要求缓存的
		return $pre;
	}

	$key			= md5(serialize(array_filter($wp_query->query_vars)).$wp_query->request);
	$last_changed	= wp_cache_get_last_changed('posts');
	$cache_key		= "wpjam_get_posts:$key:$last_changed";

	// $cache_key	= 'wpjam_cache:'.md5(maybe_serialize($wp_query->query_vars)).':'.$last_changed;
	
	$post_ids		= wp_cache_get($cache_key, 'wpjam_post_ids');

	$wp_query->set('cache_key', $cache_key);
	
	if($post_ids === false){
		return $pre;
	}

	if($post_ids && !$wp_query->is_singular() && empty($wp_query->get('nopaging')) && empty($wp_query->get('no_found_rows'))){	// 如果需要缓存总数
		$found_posts	= wp_cache_get($cache_key, 'wpjam_found_posts');

		if($found_posts === false){
			return $pre;
		}

		$wp_query->set('no_found_rows', true);

		$wp_query->found_posts		= $found_posts;
		$wp_query->max_num_pages	= ceil($found_posts/$wp_query->get('posts_per_page'));
	}

	$args	= wp_array_slice_assoc($wp_query->query_vars, ['update_post_term_cache', 'update_post_meta_cache']);

	return wpjam_get_posts($post_ids, $args);	
}, 10, 2); 

add_filter('posts_results',	 function ($posts, $wp_query) {
	$cache_key	= $wp_query->get('cache_key');

	if($cache_key){
		if(count($posts)>1){
			$post_authors	= wp_list_pluck($posts, 'post_author');
			$post_authors	= array_unique($post_authors);
			$post_authors	= array_filter($post_authors);

			if(count($post_authors)>1){
				cache_users($post_authors);
			}
		}

		$post_ids	= wp_cache_get($cache_key, 'wpjam_post_ids');
		if($post_ids === false){
			wp_cache_set($cache_key, array_column($posts, 'ID'), 'wpjam_post_ids', HOUR_IN_SECONDS);
		}
	}

	return $posts;
}, 10, 2);

add_filter('found_posts', function ($found_posts, $wp_query) {
	$cache_key	= $wp_query->get('cache_key');

	if($cache_key){
		wp_cache_set($cache_key, $found_posts, 'wpjam_found_posts', HOUR_IN_SECONDS);
	}
		
	return $found_posts;
}, 10, 2);