1
0
Fork 0
mirror of https://github.com/yunluo/gdk.git synced 2024-05-13 11:46:57 +02:00

增加网站地图功能,xml版和HTML版

This commit is contained in:
云落 2020-02-09 15:52:47 +08:00
parent 08474a83a1
commit 84bd485129
6 changed files with 179 additions and 4 deletions

20
class/active.php Normal file
View file

@ -0,0 +1,20 @@
<?php
//激活插件后,主要是创建页面
function weauth_plugin_activate() {
$weixin_login_id = get_option("gdk_option['weixin_login_id']");
if (!$weixin_login_id) {
$post1 = array(
'post_title' => "微信登录", //这里是自动生成页面的页面标题
'post_content' => "[weauth]", //这里是页面的内容
'post_status' => "publish",
'post_type' => 'page',
'post_name' => 'weixin-login',
);
$page_id = wp_insert_post($post1);
update_post_meta($page_id, "_wp_page_template", ""); //这里是生成页面的模板类型
update_option("gdk_option['weixin_login_id']", $page_id);
}
}

View file

@ -214,8 +214,8 @@ $gdk_options = [
'std' => '0'
],
[
'name' => '网站地图 sitemap_xml',
'desc' => '该功能会自动生成网站地图链接:域名/sitemap_xml,开启后建议更新固定链接一次,默认开启',
'name' => '网站地图 sitemap',
'desc' => '该功能会自动生成网站地图[xml版和html版],链接:域名/sitemap.xml,域名/sitemap.html,开启后建议更新固定链接一次,默认开启',
'id' => 'gdk_sitemap_xml',
'type' => 'radio',
'options' => [

View file

@ -3,7 +3,8 @@
//加载网站地图xml
if(gdk_option('gdk_sitemap_xml')){
include('sitemap-xml.php');
include(GDK_ROOT_PATH.'public/sitemap-xml.php');
include(GDK_ROOT_PATH.'public/sitemap.php');
}
// 屏蔽蜘蛛爬取作者页面

View file

@ -3,7 +3,7 @@
Plugin Name: GitCafe Development Kit 极客公园开发套件
Plugin URI: https://gitcafe.net/
Description: 为WordPress主题开发提供底层支持
Version: 0.0.1
Version: 0.0.2
Author: 云落
Author URI: https://gitcafe.net/
Compatible:5.3.2
@ -37,3 +37,6 @@ include('framework/frame_load.php');//加载后台框架
include('class/class_load.php');//加载各种类
include('functions/func_load.php');//加载函数
include('assets/assets_load.php');//加载静态资源
//激活插件之后创建必要页面
//register_activation_hook(__FILE__, 'gdk_plugin_activate');

151
public/sitemap.php Normal file
View file

@ -0,0 +1,151 @@
<?php
/**
* SiteMap HTML
*/
// ----------------------
// 开一个api的统一URL
function gdk_sitemap_html_flush_rules(){
$rules = get_option( 'rewrite_rules' );
if ( !isset( $rules['^sitemap(.*?)\.html$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// 添加自定义URL重写
function gdk_sitemap_html_custom_rewrite_rule() {
add_rewrite_rule('^sitemap(.*?)\.html$','index.php?sitemap=gdkk$matches[1]','top');
}
function gdk_sitemap_html_insert_query_vars( $vars ){
array_push($vars, 'sitemap');
return $vars;
}
function gdk_sitemap_html_cancel_redirect( $redirect_url ) {
$api_type = get_query_var('sitemap');
if ( !empty($api_type) ){
return false;
}else{
return $redirect_url;
}
}
function gdk_sitemap_html_api_handlers( $template ){
$hook = explode('-', get_query_var( 'sitemap' ) );
if( isset( $hook[0] ) && $hook[0] === 'gdkk' ){
if( isset( $hook[1] ) ){
status_header(404);
header('HTTP/1.0 404 Not Found');
$GLOBALS['wp_query']->set_404();
include( get_query_template( '404' ) );
exit;
}
$sitemap = get_transient('gdk-sitemap-html');
if( false === $sitemap || empty( $sitemap ) ){
$sitemap = gdk_create_html_sitemap();
set_transient( 'gdk-sitemap-html', $sitemap );
}
header("Content-type: text/html");
$sitemap;
return;
}
return $template;
}
add_action( 'init', 'gdk_sitemap_html_custom_rewrite_rule' , 10, 0);
add_filter( 'query_vars', 'gdk_sitemap_html_insert_query_vars' );
add_filter( 'redirect_canonical', 'gdk_sitemap_html_cancel_redirect' );
add_action( 'wp_loaded', 'gdk_sitemap_html_flush_rules' );
add_filter( 'template_include', 'gdk_sitemap_html_api_handlers', 99 );
function gdk_create_html_sitemap() {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=<?php bloginfo( 'charset' ); ?>"/>
<title>站点地图 -<?php bloginfo( 'name'); ?></title>
<meta name="keywords" content="站点地图,<?php bloginfo('name'); ?>" />
<meta name="copyright" content="<?php bloginfo('name'); ?>" />
<link rel="canonical" href="<?php echo get_permalink(); ?>" />
<style type="text/css">
body {font-family: Microsoft Yahei,Verdana;font-size:13px;margin:0 auto;color:
#000000;background: #ffffff;width: 990px;margin: 0 auto}a:link,a:visited
{color:#000;text-decoration:none;}a:hover {color:#08d;text-decoration:none;}h1,h2,h3,h4,h5,h6
{font-weight:normal;}img {border:0;}li {margin-top: 8px;}.page {padding:
4px; border-top: 1px #EEEEEE solid}.author {background-color:#EEEEFF; padding:
6px; border-top: 1px #ddddee solid}#nav, #content, #footer {padding: 8px;
border: 1px solid #EEEEEE; clear: both; width: 95%; margin: auto; margin-top:
10px;}
</style>
</head>
<body vlink="#333333" link="#333333">
<h2 style="text-align: center; margin-top: 20px">
<?php bloginfo( 'name'); ?>'s SiteMap</h2>
<div id="nav">
<a href="<?php echo esc_url( home_url() ); ?>/">
<strong><?php bloginfo( 'name'); ?></strong>
</a>
&raquo;
<a href="<?php echo get_permalink(); ?>">站点地图</a>
</div>
<div id="content">
<h3>最新文章</h3>
<ul>
<?php
$previous_year = $year = 0;
$previous_month = $month = 0;
$ul_open = false;
$myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');
foreach($myposts as $post):?>
<li>
<a href="<?php the_permalink($post->ID); ?>" title="<?php echo $post->post_title; ?>" target="_blank"><?php echo $post->post_title; ?></a>
</li>
<?php endforeach;?>
</ul>
</div>
<div id="content">
<h3>分类目录</h3>
<ul><?php wp_list_categories( 'title_li='); ?></ul>
</div>
<div id="content">
<h3>单页面</h3>
<?php wp_page_menu( $args ); ?>
</div>
<div id="footer">
查看博客首页:
<strong>
<a href="<?php echo esc_url( home_url() ); ?>/"><?php bloginfo( 'name'); ?></a>
</strong>
</div>
<br />
<div style="text-align: center; font-size: 11px">
Latest Update:
<?php
global $wpdb;
$last = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");
$last = date('Y-m-d G:i:s', strtotime($last[0]->MAX_m));
echo $last;
?>
<br />
<br />
</div>
</body>
</html>
<?php
}
function gdk_clear_sitemap_html_cache(){
delete_transient( 'gdk-sitemap-html' );
}
add_action('publish_post', 'gdk_clear_sitemap_html_cache');
add_action('publish_page', 'gdk_clear_sitemap_html_cache');
add_action('save_post', 'gdk_clear_sitemap_html_cache' );