devgeniem/wp-exhale

为您的数据在第三方集成上创建开发者友好的xml API端点。

安装: 830

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 10

分支: 1

开放问题: 2

类型:wordpress-muplugin

0.1.5 2016-12-01 13:00 UTC

This package is auto-updated.

Last update: 2024-09-20 15:35:09 UTC


README

WordPress从XML数据导出创建的开发者友好框架。这仅适用于php7.0或更高版本,因为我们使用了标量类型提示。

安装插件

$ composer require devgeniem/wp-exhale
$ wp plugin activate wp-exhale

使用方法

您需要定义一个扩展我们的父类 Exhale\Base\XML 的类。当合适的请求到来时,这个类会自动调用,您无需在任何地方挂钩它。

例如,它可能看起来像这样

<?php
/**
 * This class exists so that users of Exhale can start producing xml really quickly
 */
class MyProviderName implements \Exhale\Type\XML {
    /**
     * Returns exportable apartments to Vuokraovi
     */
    static public function get_export_data() : array {
        return array('item' => 'value');
    }

    /**
     * If you need to wrap your data into custom element you can use this
     * Empty array is ignored
     */
    static public function xml_root_element() : array {
        return array(
            'name' => 'wrapper'
        );
    }

    /**
     * This function can be used to map custom namespaces into the xml
     * Empty array is ignored
     */
    static public function xml_namespaces() : array {
        return array(
            'http://www.w3.org/2001/XMLSchema-instance' => 'xsi',
        );
    }
}

这个类现在自动提供了从以下地址的数据导出: http://yoursite.com/api/export/myprovidername.xml

内容如下

<wrapper xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <item>value</item>
</wrapper>

它是如何工作的?

此插件内部使用 sabre/xml。从 get_export_data() 函数中的数组映射到 sabre/xml 中的 XML 写操作。这样,您就可以从 sabre/xml 获得所有好处。

如果请求了 exhale URL,XML 将在 plugins_loaded 钩子中生成,然后立即停止处理。

设置

您可以通过在 wp-config 中定义来覆盖默认的导出器 URL

define('EXHALE_URL_PREFIX','/my-custom/api/url/');

前面的示例现在可以从以下地址访问:http://yoursite.com/api/export/myprovidername.xml

特殊情况

如果您想向元素添加属性或拥有具有相同键的多个元素,可以使用与 sabre/xml 兼容的自定义数组来实现

static public function get_export_data() {
        return array(
            [
                'name' => 'item',
                'attributes' => [
                    'url' => 'http://yoursite.com'
                ],
                'value' => 'value'
            ],
            [
                'name' => 'item',
                'attributes' => [
                    'url' => 'http://example.com'
                ],
                'value' => 'nothing'
            ],
        );
    }

这将生成以下 XML

<item url="http://yoursite.com">value</item>
<item url="http://example.com">nothing</item>

许可证

GPLv3