marcbelletre/acf-rrule 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

marcbelletre/acf-rrule

Composer 安装命令:

composer require marcbelletre/acf-rrule

包简介

RRule field for Advanced Custom Fields

README 文档

README

Create recurring rules within a single ACF field and retrieve all the dates using the simshaun/recurr package.

ACF RRule Screenshot

Usage

<?php $rrule = get_field('rrule'); ?>

The RRule field returns an array with the following attributes:

Attribute Type Description
rrule string The RRule string describing the recurring rule
start_date string The start date of the recurrence (Ymd)
start_time string The start time of the recurrence (H:i:s)
frequency string The selected frequency (DAILY|WEEKLY|MONTHLY|YEARLY)
interval int The interval set for the frequency
weekdays array<string> An array of days for the weekly frequency
monthdays array<string> An array of days for the monthly frequency
months array<int> An array of months for the yearly frequency
monthly_by string The selected option for the monthly frequency (monthdays|setpos)
bysetpos array<int> The starting numbers for the monthly "setpos" option
byweekday array<string> The selected days for the monthly "setpos" option
end_type string The end of the recurrence (date|count)
end_date string The end date in YYYYMMDD format for the recurrence when end_type is set to "date"
occurence_count int The number of occurences for the recurrence when end_type is set to "count"
dates_collection array<DateTime> An array containing all the DateTime objects generated by your recurring rule
text string A text representation for your recurring rule
first_date DateTime The first occurrence of the recurrence (since v1.4.0)
last_date DateTime The last occurrence of the recurrence (since v1.4.0)

Advanced usage

A common use case for this plugin is creating an agenda-style display for your events. Here is how I usually do it.

In the following example we will assume you have an event custom post type with an ACF RRule field named rrule.

The first step is to use the acf/save_post hook to save the first and last dates in database. This is necessary for querying our events later.

/**
 * Save first & last occurrences of an event in database.
 *
 * @param  int|string  $post_id
 * @return void
 */
add_action('acf/save_post', function (int|string $post_id) {
    if (! $post_id || get_post_type($post_id) !== 'event') {
        return;
    }

    $rrule = get_field('rrule');

    update_post_meta($post_id, 'start_date', $rrule['first_date']->format('Y-m-d'));
    update_post_meta($post_id, 'end_date', $rrule['last_date']->format('Y-m-d'));
});

You will then be able to use the start_date and end_date meta values in a custom WP_Query to filter events that may have occurrences between the specified dates.

$startDate = date('Y-m-d'); // Today
$endDate = date('Y-m-d', strtotime('+1 month', strtotime($startDate))); // Today + 1 month

// Retrieve events starting before the end date
// and ending after the start date
$eventsQuery = new WP_Query([
    'post_type' => 'event',
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'meta_query' => [
        'relation' => 'AND',
        [
            'key' => 'start_date',
            'compare' => '<=',
            'value' => $endDate,
            'type' => 'DATE',
        ],
        [
            'key' => 'end_date',
            'compare' => '>=',
            'value' => $startDate,
            'type' => 'DATE',
        ],
    ],
]);

The next and last step is to create an associative array of dates. Each date will be an array of events that occurs at the given date.

// Instanciate an array for our list of dates
$dates = [];

while ($eventsQuery->have_posts()) {
    $eventsQuery->the_post();

    $recurrence = get_field('rrule');

    // Loop through the individual dates for the recurrence
    foreach ($recurrence['dates_collection'] as $datetime) {
        $date = $datetime->format('Y-m-d');

        if ($date < $startDate) {
            // If the date is before the start date, jump directly to the next one
            continue;
        } elseif ($date > $endDate) {
            // If the date is after the end date, break the loop
            break;
        }

        // Create the date if it doesn't exist yet
        if (! isset($dates[$date])) {
            // Each date will contain an array of events
            $dates[$date] = [];
        }

        // Use the event ID as key to avoid duplicates
        $dates[$date][$post->ID] = $post;
    }

    // Sort array by key
    ksort($dates);
}

Of course this is a very basic example that you will have to adapt to your use case.

Testing

vendor/bin/phpcs -p . --standard=PHPCompatibilityWP --extensions=php --runtime-set testVersion 7.2-

marcbelletre/acf-rrule 适用场景与选型建议

marcbelletre/acf-rrule 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40 次下载、GitHub Stars 达 22, 最近一次更新时间为 2020 年 07 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「date」 「recurring」 「rrule」 「acf」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 marcbelletre/acf-rrule 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 marcbelletre/acf-rrule 我们能提供哪些服务?
定制开发 / 二次开发

基于 marcbelletre/acf-rrule 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 40
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 22
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 22
  • Watchers: 1
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-17