单独制作了一个日记本来记录关于孩子的一些事情,为方便浏览需要将 post 发布日期改成类似 QQ 亲子相册那样,根据孩子出生日期和照片拍摄日期计算出来年龄,形如:生出、7天、1个月、 3岁5个月、12岁生日。如:5岁3个月

这个功能实现起来需要指定出生日期、获取Post的发布日期,剩下的就是日期判断,代码大概如下,放置于主题的 function.php 文件中:

// get_brave_year_of_age
function get_brave_year_of_age($birth_date) {
    $post_date = date_create(get_the_time('Y-m-d'));
    $interval = date_diff($birth_date, $post_date);
    $prefix = '';
    if ($interval->format('%R') === '-') {
        $prefix = '出生前';
    }
    $result = '';
    if ($interval->format('%y') === '0') {
        if ($interval->format('%m') === '0') {
            if ($interval->format('%d') === '0') {
                $result = '出生';
            } else if ($interval->format('%d') > 0) {
                $result = $interval->format('%d天');
            }
        } else if ($interval->format('%m') > 0) {
            $result = $interval->format('%m个月');
        }
        } else if ($interval->format('%y') > 0) {
            if ($interval->format('%m') === '0') {
                if ($interval->format('%d') === '0') {
                    $result = $interval->format('%y岁生日');
                } else if ($interval->format('%d') > 0) {
                    $result = $interval->format('%y岁');
                }
        } else if ($interval->format('%m') > 0) {
            $result = $interval->format('%y岁%m个月');
        }
    }
    echo $prefix . $result;
}

在需要显示的地方加上下面这段输出即可:

<?php get_brave_year_of_age(); ?>

极个别情况会发一下私密的东西,有时疏忽会忘记设置私密,以下代码可以实现在特定形式在发布时自动设置私密的功能,这段代码在浏览器上是生效的,在 WordPress APP 上却无法正常工作,不知何故,已支持 WordPress APP,之前的问题在于使用get_post_format()获取日志格式未加参数$post_id,加上参数即可,更新后的代码如下:

// aside 形式发布时自动置为私密
function set_post_to_private( $data, $postarr ) {
$brave_post_format = get_post_format($postarr['ID']);
if ( $brave_post_format === 'aside' && $data['post_status'] === 'publish' ) {
$data['post_status'] = 'private'; // 发布时状态设置为私密
}
return $data;
}
add_filter( 'wp_insert_post_data', 'set_post_to_private', 10, 2 );

参考:wp_insert_post_data

Android 平台的 WordPress app 有这样一个功能:预置一段自定义的话,发布时会自动附加在文末,如“发自 WordPress for Android”,见下图,iOS平台始终没有这个功能。最近给网站添加了类似功能,实现在发布时后台自动获取当前终端信息,获取到的终端信息被添加到自定义字段中,在页面上想要展示的位置展示之。加到自定义字段的好处是可以自由控制,实现更多个性化的功能。

实现的关键是 Mobile_Detect 插件和 save_post 钩子,步骤如下:

获取 Mobile_Detect 插件

第一步,需要用到插件 Mobile_Detect
以获取发布终端的设备信息,这个插件就一个文件,名为 Mobile_Detect.php,点此下载最新的 Mobile_Detect.php 文件,解压找到 Mobile_Detect.php 文件放至 WordPress 主题目录(示例代码放在主题文件夹下的p目文件夹里)。

save_post 处理

第二步,将以下两段代码放到在用主题的 functions.php 文件中,保存。第一段代码的作用是发布时获取终端设备信息并存入当前日志的自定义字段;第二段代码的作用是定义存放于自定义代码中的设备信息在前端界面的展示逻辑。

// Auto_save post device meta data
function brave_post_device_meta( $post_id ) {

//自动保存时不处理
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}

$current_post_device_name = get_post_meta($post->ID, 'post_device_name', true);
$current_post_device_ver = get_post_meta($post->ID, 'post_device_ver', true);
//已存在记录的、已发布的、私密的不做更新
if ( current_user_can( 'edit_post', $post_id ) && ! in_array( get_post_status ( $post_id ) , array( 'private', 'publish') )) {
//加载插件
include_once( get_template_directory() . '/p/Mobile_Detect.php' );
$detect = new Mobile_Detect;
if ( $detect->isMobile() && !$detect->isTablet()) {
//手机
if ( $detect->isiPhone() ){
$device_name = 'iPhone';
$device_ver = $detect->version('iOS');
} else if ( $detect->isAndroidOS() ){
$device_name = 'Android';
$device_ver = $detect->version('Android');
}
} else if ( $detect->isMobile() && $detect->isTablet() ) {
//平板
if ( $detect->isiPad() ){
$device_name = 'iPad';
$device_ver = $detect->version('iOS');
} else if ( $detect->isKindle() ){
$device_name = 'Kindle';
} else if ( $detect->isAndroidOS() ){
$device_name = 'Android Tablet';
$device_ver = $detect->version('Android');
}
} else {
//桌面
if ( $detect->version('Chrome') ){
$device_name = 'Chrome';
$device_ver = $detect->version('Chrome');
} else if ( $detect->version('Firefox') ){
$device_name = 'Firefox';
$device_ver = $detect->version('Firefox');
} else if ( $detect->version('Safari') ){
$device_name = 'Safari';
$device_ver = $detect->version('Safari');
}
}
$current_post_device_name = $device_name;
$current_post_device_ver = $device_ver;
update_post_meta( $post_id, 'post_device_name', $current_post_device_name );
update_post_meta( $post_id, 'post_device_ver', $current_post_device_ver );
} else {
return;
}
}
add_action( 'save_post', 'brave_post_device_meta');

//前端展示,自由调整样式

//brave_post_device
function brave_post_device() {
global $post;
$post_device_name = get_post_meta($post->ID, 'post_device_name', true);
$post_device_ver = get_post_meta($post->ID, 'post_device_ver', true);
$post_device_ver = str_replace(array('_', ' ', '/'), '.', $post_device_ver);//版本号替换:将间以_空格/的间隔符替换为.
$post_device_ver = ' '.substr($post_device_ver,0,strpos($post_device_ver,'.'));//移除第一个小数点后的数字
$post_device_pfx = 'via '; //前缀
$post_device = $post_device_pfx . $post_device_name;

if ( $post_device_name == "iPhone" ) {
echo "<span class='ml'>$post_device</span>";
} else if ( $post_device_name == "Android" ) {
echo "<span class='ml'>$post_device</span>";
} else if ( $post_device_name == "iPad" ) {
echo "<span class='ml'>$post_device</span>";
} else if ( $post_device_name == "Android Tablet" ) {
echo "<span class='ml'>Android平板</span>";
} else if ( $post_device_name == "Kindle" ) {
echo "<span class='ml'>$post_device</span>";
} else if ( $post_device_name == "Firefox" ) {
echo "<span class='ml'>$post_device</span>";
} else if ( $post_device_name == "Chrome" ) {
echo "<span class='ml'>$post_device</span>";
} else if ( $post_device_name == "Safari" ) {
echo "<span class='ml'>$post_device</span>";
}
}

第三步,在你希望展示的地方加上下面代码,行了,最终效果见下图。

<?php echo brave_pub_device(); ?>

这里仅粗略检测设备平台,mobiledetect 插件功能非常强大,颗粒度能够做的更细,如输出形如这样的平台和版本号信息:iOS/Android、iPhone OS 13.6、Firefox/Chrome。

参考:
mobiledetect:http://mobiledetect.net
save_post:https://developer.wordpress.org/reference/hooks/save_post/

最近,在业务上遇到一个问题:公司的会员卡号要支持合并了,一位顾客注册了 A 、 B 两张会员卡,系统支持将 A 会员卡合并到 B 会员卡,A 会员卡的会员权益和相关记录将在合并发生后转移到 B 会员卡。同时,A 到 B 的合并关系会记录在一张数据表里。现在,要实现通过 sql 查询出 A 会员卡合并到 B 会员卡这样的关系。

若仅仅是由 A 合并到 B,则很容易实现。偏偏这里的合并未做限制,由于羊毛党猖獗,加上营销部门有时为了数据好看,现实中往往存在这样的合并路径:A 合并到 B,B 合并到 C 、 E 合并到 C 、 C 合并到 D,这样 A 最后合并到了 D,E 最后也合并到了 D 。通过 SQL 查询这样的合并结构,这里需要用到递归查询。

with 查询

使用递归查询前先了解下 with 查询,with 提供了一种方式来书写在一个大型查询中使用的辅助语句。这些语句通常被称为公共表表达式或 CTE,它们可以被看成是定义在当前查询中存在的临时表。例如:

with a as ( --先用 with 创建一个名为 a 的对班级男同学的查询
select stu.stu_num,stu.stu_name from student as stu
where stu.stu_sex='男'
)
--接着引用这个临时表找到名字叫 “李岩” 的这位同学
select stu_num,stu_name from a where stu_name='李岩';

在查询中使用 with 有诸多好处:

  1. 使 sql 语句结构简单,层次分明:特别是大型查询,动辄几百行,这种情况下出现几个长嵌套,不要说给别人看了,一段时间后连自己都搞不清楚了。若使用 with 查询替代嵌套的子查询其模块化表达,结构清晰,易于阅读和理解;
  2. 提升效率:with 查询的另一个有用的特性是在每一次的父查询执行中它们通常只被计算一次,即便它们被父查询或兄弟 with 查询多次引用。 因此,在多个地方需要的昂贵计算可以被放在一个 with 查询中来避免冗余工作。比如要对某组指定的会员做不同维度的数据分析,那么,对 “这组指定人员” 就可用使用 with 查询,其他兄弟查询直接引用结果即可,当需要对 “这组指定人员” 进行调整时,单独调整 with 查询就行了。
  3. 递归查询:完成递归查询是 with 语句最重要的特性之一,也接下来要详细介绍的。

递归查询

通过对 with 语句增加 RECURSIVE 修饰符以实现递归查询的目的,常用于层级结构查询,如组织层级、人员层级、物料清单等场景,递归查询格式如下:

with RECURSIVE recursive_name as (
非递归查询 --必须
union --必须,也可以是 union all
递归查询 --必须
)
select * from recursive_name;

下面以本次面对的问题展开:
创建会员合并记录表 cust_merge_dtl

CREATE table if not EXISTS cust_merge_dtl (
merge_id integer, --id
src_cust_id integer, --合并前的会员卡
desc_cust_id integer, --合并后的会员卡
op_time TIMESTAMP --合并时间
);

写入几条会员合并记录

insert into cust_merge_dtl (merge_id,src_cust_id,desc_cust_id,op_time)
VALUES ('1','10056','10071','2019-09-21 8:38:17');
insert into cust_merge_dtl (merge_id,src_cust_id,desc_cust_id,op_time)
VALUES ('2','10071','10092','2020-03-12 16:18:55');
insert into cust_merge_dtl (merge_id,src_cust_id,desc_cust_id,op_time)
VALUES ('3','10088','10092','2020-11-11 10:08:27');
insert into cust_merge_dtl (merge_id,src_cust_id,desc_cust_id,op_time)
VALUES ('4','10092','10112','2020-12-30 18:30:06');
insert into cust_merge_dtl (merge_id,src_cust_id,desc_cust_id,op_time)
VALUES ('5','10010','10112','2021-03-12 12:44:46');

查询会员合并记录表 cust_merge_dtl

select * from cust_merge_dtl;


如上图所示,以 10056 会员卡为例,它只有一条合并记录,即 10056->10071,但是 10071 紧接着又做了一次合并,10071->10092,最后 10092 合并到了 10112,推算出 10056 最终合并到了 10112,完整的合并路径是这样的:10056 --> 10071 --> 10092 --> 10112

现在用递归查询生成这种合并关系。

with recursive cust_merge as (
select dtl1.src_cust_id as original_cust_id,--初始会员卡
dtl1.src_cust_id,--合并前的会员卡
dtl1.desc_cust_id,--合并后的会员卡
dtl1.op_time
from cust_merge_dtl dtl1 where dtl1.src_cust_id='10056' --非递归查询,此处的查询结果构成整个递归查询的基本结果形式
union
select cm.original_cust_id,dtl2.src_cust_id,dtl2.desc_cust_id,dtl2.op_time
from cust_merge cm
inner join cust_merge_dtl dtl2 on cm.desc_cust_id = dtl2.src_cust_id --递归查询
)
select * from cust_merge;

得到原始会员卡及过程中每一次的变动记录。

递归查询的执行过程是这样的:

  1. 查询首先从执行一次非递归查询开始,非递归查询的查询结果被保存在一个临时表中作为输出(本次查询以及之后的每次递归查询,生成的输出结果都将被保存在这个临时表中),以供之后的递归查询引用(通过紧跟 recursive 关键字的名字引用,示例通过 cust_merge 引用);
  2. 递归查询部分引用临时表作为输入,执行递归查询,生成的递归查询输出仍被保存在这个临时表中
  3. 本次递归输出不为空时,回到步骤 2,循环执行递归查询,直到递归输出结果为空,循环停止,递归结束;
  4. 最后的结果可以简单地认为是步骤 1 的查询结果加上步骤 2 的每次递归输出结果的并集(是否会去重复,这取决于非递归查询部分和递归查询部分两者之间使用的是 union 还是 union all,union 会去重复,union all 则不做去重)。

如果不关注中间的过程只想知道这些合并的会员卡最终合并到哪个会员卡了,稍作调整即可:

with recursive cust_merge as (
select dtl1.src_cust_id as original_cust_id,dtl1.src_cust_id,dtl1.desc_cust_id,dtl1.op_time
from cust_merge_dtl dtl1 --where dtl1.src_cust_id='10056'
union all
select cm.original_cust_id,dtl2.src_cust_id,dtl2.desc_cust_id,dtl2.op_time
from cust_merge cm
inner join cust_merge_dtl dtl2 on cm.desc_cust_id = dtl2.src_cust_id
),
last_record as (
--原始会员对应的最新迭代合并记录
select cm.original_cust_id,max(op_time) as last_merge_time
from cust_merge cm
GROUP BY cm.original_cust_id
)
select cm.original_cust_id,cm.desc_cust_id
from cust_merge cm
where exists (
select 1 from last_record lr where lr.last_merge_time=cm.op_time and lr.original_cust_id=cm.original_cust_id
);

参考:http://postgres.cn/docs/12/queries-with.html

5月底,我种的月季开始出现黑斑病。整个过程表现为:初期出现褐色斑点,逐步扩大演变成黑斑,进而整株叶片由下往上逐渐枯落,枝梢、花骨朵渐渐衰败枯萎,直至叶片落光,抗病性弱的品种甚至整株枯死。

黑斑病是由真菌感染引起的,病菌的生长发育温度范围为10-35摄氏度,最适温度为20-25摄氏度。一年中的3月下旬至6月中旬和9月下旬至11月为发病盛期。

进入5月气温逐渐升高,进入黑斑病高发期,我的月季发生黑斑病不足为怪,目前一株发病较早的品种已经整株枯死。

月季黑斑病叶片症状
月季黑斑病叶片症状

治疗措施

药物治疗

施药:使用代森锰锌戊唑醇交替喷药至少3天。

生长环境治理

环境的改善有助于治疗或预防月季黑斑病。

  1. 环境清理和改善:清理枯落、染病的枝叶,减少传染源;保持月季生长环境通风;
  2. 改进浇水方式和时间:应从盆沿浇入,避免喷浇;不在晚间浇水;
  3. 品种选择:选择抗病性强的品种,这点可能很难做到,喜爱的品种可能抗病性差,抗病性强的品种可能不受欢迎;
  4. 辅助治疗:木糠、煤灰覆盖表土,厚度8毫米左右,可抑杀地表部分病菌,减少侵染机会;
  5. 早发现早治疗。
感染黑斑病后逐渐恢复的月季
黑斑病后逐渐恢复的月季

本文参考:
湖南省林业局-月季黑斑病的症状及防治
郑州市林业局-月季黑斑病
中国农业信息网-月季黑斑病的防治