jascha030/wp-post-iterator

WP_Query 类的简单适配器,主要用于但不限于与 twig/twig 一起使用。

0.0.1 2022-04-03 12:56 UTC

This package is auto-updated.

Last update: 2024-09-30 01:41:54 UTC


README

这是一个简单的适配器,扩展了 WordPress 的 \WP_Query 类,同时实现了 PHP 的核心 Iterator 接口。

主要针对,但不限于与 PHP 的模板语言 twig 一起使用。

入门指南

先决条件

  • Php ^8.0
  • Composer ^2.2 (不是必需的,但推荐使用)

安装

以下是一些安装说明,包括如下示例。

composer require jascha030/composer-template

使用方法

根据你正在构建的内容,在主插件文件或 functions.php 中要求 vendor/autoload.php 文件。

最有效的方式

使用此包最简单、最有效的方式是通过使用 PostCollection 类。它基于 Php 的 IteratorAggregate 接口,并受到(例如)Laravel 的 Collection 类和 Symfony 的 Finder 和 LazyIterator 类的启发。

它提供了三个静态方法来从各种类型的参数创建实例。

<?php

/**
 * Create instance from any arguments you would pass to a WP_Query.
 */
public static function fromQueryArgs(array $args, bool $keyByPostId = false): PostCollection

/**
 * Create instance from an existing WP_Query instance.
 */
public static function fromQuery(WP_Query $query, bool $keyByPostId = false): PostCollection

/**
 * Create instance inside a globally accessible post loop.
 */
public static function fromLoop(bool $keyByPostId = false): PostCollection

使用 PostCollection 的示例

<?php

// Require Composer's autoloader
require_once __DIR__ . '/vendor/autoload.php';

$args = [
    'post_type' => 'post',
    'posts_per_page' => -1,
    'post_status' => 'publish'
];

// Create from arguments you would pass to a WP_Query instance (Recommended).
$fromArgs = \Jascha030\WpPostIterator\PostCollection::fromQueryArgs($args);

// Or from an existing instance.
$query = new WP_Query($args);
$fromQuery = \Jascha030\WpPostIterator\PostCollection::fromQuery($query);

这个类的主要目的是,你可以像使用数组一样遍历它,但它比实际的数组更节省内存。

<?php

$collection = \Jascha030\WpPostIterator\PostCollection::fromQueryArgs($args);

foreach($collection as $post) {
    echo "<h1>{$post->post_title}</h1>";
    
    echo "<p>{$post->post_content}</p>";
}

主要实现

<?php

/**
 * Simple implementation, extends WP_Query and uses its methods to implement the Iterator interface.
 */
class PostIterator extends \WP_Query implements \Iterator
{
    public function __construct($query = '')

    /**
     * Tells the `key` method whether we should return the post ID when called.
     * If so, this doesn't necessarily mean the posts would be ordered ID.
     */
    final public function keyByPostId(bool $enabled = true): \Iterator

    public function current(): ?\WP_Post

    public function next()

    public function key(): ?int

    public function valid(): bool

    public function rewind(): void
}

适配器

如果你想要为现有的 WP_Query 实例创建一个迭代器,请使用 PostIteratorAdapter 类。它使用了 PostIteratorTrait

<?php

class PostIteratorAdapter implements \Iterator
{
    use PostIteratorTrait;

    public function __construct(WP_Query $query)

    /**
     * Provide a WP_Query object to defer our calls to.
     */
    private function getQuery(): WP_Query
    
    /**
     * Tells the `key` method whether we should return the post ID when called.
     * If so, this doesn't necessarily mean the posts would be ordered ID.
     */
    private function keyedByPostIds(): bool

    final public function keyByPostId(bool $enabled = true): \Iterator
}

替代方案

如果你想要使用另一个实现此功能而不牺牲现有继承链的类,你可以使用 PostIteratorTrait

它提供了如下所示的所有 Iterator 方法,只需实现如下所示的两个方法即可

/**
 * Provide a WP_Query object to defer our calls to.
 */
abstract private function getQuery(): WP_Query;

/**
 * Tells the `key` method whether we should return the post ID when called.
 * If so, this doesn't necessarily mean the posts would be ordered ID.
 */
abstract private function keyedByPostIds(): bool;

开发

以下是一些关于开发的信息(因此你可以自由地分叉此存储库)。

代码风格 & 格式化

包含了一个用于 friendsofphp/php-cs-fixer 的代码风格配置,定义在 .php-cs-fixer.dist.php 中。默认情况下,它包括 PSR-1PSR-12 预设。你可以在 .php-cs-fixer.dist.php 中自定义或添加规则。

要使用 php-cs-fixer 而不必全局安装它,还包含了一个 composer 脚本命令,用于使用提供的配置文件和 php-cs-fixer 的供应商二进制文件格式化 PHP 代码。

运行 php-cs-fixer

composer run format

上面的命令是一个别名,你也可以使用

composer run php-cs-fixer

许可协议

GPL-2.0+

jascha030/wp-post-iterator - Adapter for WordPress' WP_Query, implementing Iterator interface.

Copyright (C) 2022  Jascha van Aalst.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

提供完整许可协议的链接。