由Typecho驱动 - 主题WordBook

Typecho中的Widget_Options用法

更新:2022-05-26 访问:619

通过Typecho的Widget_Options,可以方便地获取Typecho的系统信息,或者方便地获取相关配置、资源路径等。这里列出常用的Widget_Options函数和用法,方便各位筒子方便查阅。
通过Widget_Options获取路径信息
获取内置URL
通过这类api,可以获取TE内置的一些特定URL。

$options = Helper::options();

#1  $options->feedUrl(); //或者 echo $op-feedUrl;
#2  $options->feedRssUrl();
#3  $options->feedAtomUrl();
#4  $options->commentsFeedUrl();
#5  $options->commentsFeedRssUrl();
#6  $options->commentsFeedAtomUrl();
#7  $options->xmlRpcUrl();
#8  $options->loginUrl();  //输出Typecho的登录URL地址
#9  $options->registerUrl(); //输出Typecho的注册URL地址
#10 $options->registerAction(); //输出Typecho的注册Action请求地址
#11 $options->profileUrl(); //输出Typecho用户的用户信息页面地址
#12 $options->logoutUrl(); //输出Typecho的注销地址,用户点击之后可实现注销操作。

将输出

#1  https://www.typechodev.com/index.php/feed/
#2  https://www.typechodev.com/index.php/feed/rss/
#3  https://www.typechodev.com/index.php/feed/atom/
#4  https://www.typechodev.com/index.php/feed/comments/
#5  https://www.typechodev.com/index.php/feed/rss/comments/
#6  https://www.typechodev.com/index.php/feed/atom/comments/
#7  https://www.typechodev.com/index.php/action/xmlrpc
#8  https://www.typechodev.com/admin/login.php
#9  https://www.typechodev.com/admin/register.php
#10 https://www.typechodev.com/index.php/action/register?_=99788b0bb32d48206a3da981c342a590
#11 https://www.typechodev.com/admin/profile.php
#12 https://www.typechodev.com/index.php/action/logout

举例:

<div class="sidebar-section">
    <ul>
        <?php if($this->user->hasLogin()): ?>
            <li><a href="<?php $this->options->adminUrl(); ?>">进入后台</a></li>
            <li><a href="<?php $this->options->logoutUrl(); ?>">退出</a></li>
        <?php else: ?>
            <li><a href="<?php $this->options->loginUrl(); ?>">登录</a></li>
        <?php endif; ?>
    </ul>
</div>

获取动态URL
通过这类api,可以动态获取相对于当前主题、当前插件等的URL。

Widget_Options::siteUrl()

$options->siteUrl(); //或者echo $options->siteUrl
$options->siteUrl('mypage.php');
// https://www.typechodev.com/
// https://www.typechodev.com/mypage.php

siteUrl()函数将输出相对于域名的url。如果参数为空,则输出网站所在域名,如果参数非空,则输出相对于/目录所在的URL。

Widget_Options::index()

$options->index(); //或者echo $options->index
$options->index('mypage.php');
// https://www.typechodev.com/index.php/
// https://www.typechodev.com/index.php/mypage.php

index函数输出相对于index.php的url,这个十分常用。如果参数为空,则输出index.php,如果参数非空,则输出相对于index.php所在的url。

注意:如果typecho后台开启了伪静态,那么Widget_Options::index()将会隐藏url中的index.php。

l3mv3zxl.png

那么:

$options->index(); //或者echo $options->index
$options->index('mypage.php');
// https://www.typechodev.com/
// https://www.typechodev.com/mypage.php

注意,相对上面,url中少了index.php。

Widget_Options::themeUrl()

$options->themeUrl();
$options->themeUrl('mypage.php');
// https://www.typechodev.com/usr/themes/current_theme/
// https://www.typechodev.com/usr/themes/current_theme/mypage.php

themeUrl函数将获取主题模板所在的目录。如果不带参数,将获取当前模板的/路径,如果参数非空,则获取相对于当前模板根目录的路径。

Widget_Options::pluginUrl()

$options->pluginUrl();
$options->pluginUrl('myplugin/some_script.php');
// https://www.typechodev.com/usr/plugins/
// https://www.typechodev.com/usr/plugins/myplugin/some_script.php

pluginUrl函数和themeUrl类似,不过pluginUrl没有“当前”插件的概念,即themeUrl会定位到当前启用的插件目录,即usr/theme/current_theme/,而pluginUrl仅定位到plugins目录,即usr/plugins/

Widget_Options::adminUrl()

$options->adminUrl();
$options->adminUrl('mypage.php');
// https://www.typechodev.com/admin/
// https://www.typechodev.com/admin/mypage.php

adminUrl和pluginUrl类似,可以动态定位到admin目录。

通过Widget_Options获取系统信息


$options->gmtTime();
//1424352251
$options->software();
//Typecho
$options->version();
//1.0/14.10.10
$options->contentType();
//text/html
echo $options->themeFile('my_theme');
// /path_to_your_site_ducutment/usr/themes/my_theme/
echo $options->themeFile('my_theme','mypage.php');
// /path_to_your_site_ducutment/usr/themes/my_theme/mypage.php 
echo $options->pluginDir('TypechoDev');//[注意]
// /path_to_your_site_ducutment/usr/plugins
注意,通过阅读代码,当前版本(1.0/14.10.10)的Typecho中,pluginDir函数应该有bug,即此函数的参数没有生效,各位筒子在使用中注意一下。

通过Widget_Options获取后台配置
Widget_Options::plugin(‘TypechoDev’)函数,将获取到TypechoDev的配置。

$plugin_config = $options->plugin('TypechoDev');
echo get_class($plugin_config);
//Typecho_Config

假如插件TypechoDev下的Plugin.php文件,存在如下代码:

public static function config(Typecho_Widget_Helper_Form $form){
    $enable_menu = new Typecho_Widget_Helper_Form_Element_Radio(
        'enable_menu', array ('0' => '禁用', '1' => '启用'), '',
        ':', '');
    $form->addInput($enable_menu);
}
public static function config(Typecho_Widget_Helper_Form $form){
    $enable_menu = new Typecho_Widget_Helper_Form_Element_Radio(
        'enable_menu', array ('0' => '禁用', '1' => '启用'), '',
        ':', '');
    $form->addInput($enable_menu);
}

那么在模板或者插件代码中,可以通过如此方式获取后台配置值:

$options = Helper::options();
$plugin_config = $options->plugin('TypechoDev');
$plugin_config->enable_menu;//可以获取到后台配置的值,即0或者1
$options = Helper::options();
$plugin_config = $options->plugin('TypechoDev');
$plugin_config->enable_menu;//可以获取到后台配置的值,即0或者1

注意:但要注意,当前版本中,如果对应的插件没有启用,那么可能会导致上述代码执行失败,从而导致系统挂掉。