wpfulcrum / shortcode
Fulcrum 短代码模块 - 简化 WordPress 短代码制作。
3.0.3
2017-12-22 01:20 UTC
Requires
- php: ^5.6|^7
- wpfulcrum/config: ^3
- wpfulcrum/foundation: ^3
Requires (Dev)
- brain/monkey: ^2.0
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.8|~5.7.9
- sensiolabs/security-checker: ^4.0
- squizlabs/php_codesniffer: ^3.0
This package is not auto-updated.
Last update: 2024-09-29 04:49:35 UTC
README
Fulcrum 自定义短代码模块可以简化您在项目中添加短代码的工作。传递配置,构建您的视图文件,然后它将为您处理剩余的工作。
功能
- 注册由您处理。
- 合并用户定义的属性与您指定的默认值,即为您处理
shortcode_atts。 - 渲染您在配置中指定的视图文件。
- 使用内置的
Shortcode类或指定您配置文件中的自定义类。 - 存储在 Fulcrum 容器中 - 添加时,自动存储在容器中以供全局使用。
安装
使用此组件的最佳方式是通过 Composer
composer require wpfulcrum/shortcode
依赖项
此模块需要
- 至少 PHP 5.6
- WordPress 4.8+
配置短代码
此模块,就像所有 Fulcrum 模块一样,是配置驱动的,作为 ModularConfig 设计模式的一部分。在您的主题/插件配置文件夹中,您将想要创建一个配置文件。以下是该文件的基本结构
<?php return [ /** * Defines whether the shortcode should autoload when registering with Fulcrum. * * Default is true. */ 'autoload' => true, /** * If you'd like to build your own shortcode class, specify the class name here, such as: * * Fulcrum\Custom\Shortcode\Shortcode * * By default, it will use the built-in Shortcode class. */ 'classname' => '', 'config' => [ /** * Specify the name of the shortcode. This is the "tag" which is used in the content as: [foo]. */ 'shortcode' => 'foo', /** * Set this parameter to `true` when no view file is needed for this shortcode. */ 'noView' => false, /** * Specify the absolute path to this shortcode's view file. * The view file contains the HTML that is built for this shortcode. */ 'view' => __DIR__ . '/views/foo.php', /** * Specify the default attributes for this shortcode. */ 'defaults' => [ 'class' => 'foobar', ], ], ];
视图文件
短代码创建 HTML,并将其返回以包含在发送到浏览器的页面内容中。为您的短代码创建一个视图文件。
示例:视图文件
<p class="<?php echo $this->getClass(); ?>"><?php echo $this->content; ?></p>
可用的属性
以下属性可供您在视图文件中使用
$this->attributes- 合并用户定义的属性与默认值后的数组,使用shortcode_atts。$this->content- 传递到短代码的内容,例如[foo]这是内容[/foo]。
可用的方法
一些常见的短代码属性是 id 和 class 属性。您可以使用以下方法获取视图中的任一属性
$this->getId(),它转义并返回id="the-id"。$this->getClass(),它转义并返回类属性,例如foobar。您可以将true传递给它,在类属性前面添加一个空格,例如$this->getClass()返回foobar。
使其生效
有 2 种方法可以充分利用此模块
- 使用完整的 Fulcrum 插件。
- 或者在没有 Fulcrum 的情况下单独使用。
使用 Fulcrum
在 Fulcrum 中,您的插件是一个附加组件。在您的插件配置文件中,您将有一个 serviceProviders 参数,您在其中列出您想要使用的每个服务提供者。在这种情况下,您将使用 provider.post_type。
例如,使用我们上面的 Book 配置,这将是这样配置的
'serviceProviders' => [
/****************************
* Custom Post Types
****************************/
'foo.shortcode' => array(
'provider' => 'provider.shortcode', // this is the service provider to be used.
'config' => FOO_PLUGIN_DIR . 'config/shortcode/foo.php', // path to the shortcode's configuration file.
),
],
没有 Fulcrum
在没有 Fulcrum 的情况下,您需要实例化每个依赖项和 Shortcode。例如,您会这样做
new Shortcode(
ConfigFactory::create(FOO_PLUGIN_DIR . 'config/shortcode/foo.php'), // path to the shortcode's configuration file.
);
贡献
欢迎所有反馈、错误报告和拉取请求。