web-dev-qa-db-ja.com

ブログ情報オブジェクトはありますか?

関数 get_bloginfo ()とbloginfo()はどちらもブログに関する情報を返しますが、毎回アクセスする必要はないようです。 bloginfoをプロパティ付きのオブジェクトとして返す関数はありますか?

詳しい情報:
マークアップにトークンがあります。これらのトークンをブログの情報に置き換えます。私は複数の投稿でトークンを置き換えているので、get_bloginfo("name");を複数回呼び出したくありません。代わりに、呼び出しごとに再利用できるオブジェクトに入れたいと思います。

4
1.21 gigawatts

これが存在しない場合の回避策は次のとおりです。

/**
 * Get an object with blog info values
 */
function getBlogInfo() {
    $info = new stdClass();

    $info->name                 = get_bloginfo("name");
    $info->description          = get_bloginfo("description");
    $info->wpurl                = get_bloginfo("wpurl");
    $info->url                  = get_bloginfo("url");
    $info->admin_email          = get_bloginfo("admin_email");
    $info->charset              = get_bloginfo("charset");
    $info->version              = get_bloginfo("version");
    $info->html_type            = get_bloginfo("html_type");
    $info->text_direction       = get_bloginfo("text_direction");
    $info->language             = get_bloginfo("language");
    $info->stylesheet_url       = get_bloginfo("stylesheet_url");
    $info->stylesheet_directory = get_bloginfo("stylesheet_directory");
    $info->template_url         = get_bloginfo("template_url");
    $info->template_directory   = get_bloginfo("template_url");
    $info->pingback_url         = get_bloginfo("pingback_url");
    $info->atom_url             = get_bloginfo("atom_url");
    $info->rdf_url              = get_bloginfo("rdf_url");
    $info->rss_url              = get_bloginfo("rss_url");
    $info->rss2_url             = get_bloginfo("rss2_url");
    $info->comments_atom_url    = get_bloginfo("comments_atom_url");
    $info->comments_rss2_url    = get_bloginfo("comments_rss2_url");
    $info->siteurl              = home_url();
    $info->home                 = home_url();

    return $info;
}

// the following is pseudo code to give you example of what I'm doing
$info = getBlogInfo();

for ($i=0;i<count(posts);$i++) {
    $post = $posts[i];
    $value = $post->value.replace("{name}", $info->name);
    $value = $post->value.replace("{description}", $info->description);
    $postsArray.Push($post);
}

これを答えとして選んだのは、オブジェクトのプロパティに複数回アクセスする必要があるからです。そのため、一度作成されると、その後の呼び出しで、関数を繰り返し呼び出さないで値が取得されます。知りません。

また、質疑応答では、「最善の」方法であなたができることを求めているのではなく、具体的なことの仕方を尋ねているので、この答えは問題で言及されている具体的なことに当てはまります。私はこれをすべて言っているのです。なぜなら、人々は彼らが教えられたやり方で、あるいは「最善の」やり方で物事をやらないために常に投票するからです。

更新:ユースケースを追加したので、関数とメソッドの使い方を確認できます。私はいつもこれをするわけではありませんが、それは物事を説明すると思います

0
1.21 gigawatts

get_bloginfo() のソースコードから抜粋した、これはあなたが自由に利用できる非常に単純なクラスです。

私はメソッドを利用することにしました、クラスからプロパティをパブリックにすることは本当に素晴らしいコーディングではなく、お勧めもしません。私はWordpressが公共の財産で成功することを知っていますが、それがWordpressです。

これは、クラス(です。適切な名前空間を使用するために変換する必要があります。これは、非常に単純でわかりやすいクラスです)。

class GetBlogInfo
{
    // Return the home URL
    public function homeURL() 
    {
        return home_url();
    }

    // Return the site URL
    public function siteURL() 
    {
        return site_url();
    }

    // Return the blog description
    public function description() 
    {
        return get_option('blogdescription');
    }

    // Get the feed links
    public function getFeedLink( $link = '' ) 
    {
        switch( $link ) {
            case 'rdf_url':
                $output = 'rdf';
                break;
            case 'rss_url':
                $output = 'rss';
                break;
            case 'rss2_url':
                $output = 'rss2';
                break;
            case 'atom_url':
                $output = 'atom';
                break;
            case 'comments_atom_url':
                $output = 'comments_atom';
                break;
            case 'comments_rss2_url':
                $output = 'comments_rss2';
                break;
            default:
                $output = false;
                break;
        }

        if ( $output ) {
            return get_feed_link( $output );
        } else {
            return false;
        }
    }

    // Return the blog options. Default is name
    public function getOptions( $option = 'name' ) 
    {
        switch( $option ) {
            case 'admin_email':
                $output = 'admin_email';
                break;
            case 'charset':
                $output = 'blog_charset';
                break;
            case 'html_type':
                $output = 'html_type';
                break;
            case 'name':
            default:
                $output = 'blogname';
                break;
        }

        return get_option( $output );
    }

    // Return the blog language setting
    public function language() 
    {
        return str_replace( '_', '-', get_locale() );
    }

    // Return the Wordpress version
    public function version() 
    {
        global $wp_version;
        return $wp_version;
    }

    // Return the pingback URL
    public function pingbackURL() 
    {
        return site_url( 'xmlrpc.php' );
    }

    // Return the path to main stylesheet
    public function stylesheetURL() 
    {
        return get_stylesheet_uri();
    }

    // Return the stylesheet directory uri
    public function stylesheetDirectory() 
    {
        return get_stylesheet_directory_uri();
    }

    // Return the template directory uri
    public function templateDirectory() 
    {
        return get_template_directory_uri();
    }
}

次のようにクラスを使うことができます:

$q = new GetBlogInfo();
echo $q->homeURL() . '</br>';
echo $q->siteURL() . '</br>';
echo $q->description() . '</br>';
echo $q->getFeedLink( 'rdf_url' ) . '</br>';
echo $q->getFeedLink( 'rss_url' ) . '</br>';
echo $q->getFeedLink( 'rss2_url' ) . '</br>';
echo $q->getFeedLink( 'atom_url' ) . '</br>';
echo $q->getFeedLink( 'comments_atom_url' ) . '</br>';
echo $q->getFeedLink( 'comments_rss2_url' ) . '</br>';
echo $q->getOptions( 'name' ) . '</br>';
echo $q->getOptions( 'admin_email' ) . '</br>';
echo $q->getOptions( 'charset' ) . '</br>';
echo $q->getOptions( 'html_type' ) . '</br>';
echo $q->language() . '</br>';
echo $q->version() . '</br>';
echo $q->pingbackURL() . '</br>';
echo $q->stylesheetURL() . '</br>';
echo $q->stylesheetDirectory() . '</br>';
echo $q->templateDirectory() . '</br>';

これは私のテストインストールでテストされたように以下を出力しました

http://localhost/wordpress
http://localhost/wordpress
Trots Afrikaans - Praat Afrikaans of hou jou bek!!!
http://localhost/wordpress/feed/rdf/
http://localhost/wordpress/feed/rss/
http://localhost/wordpress/feed/
http://localhost/wordpress/feed/atom/
http://localhost/wordpress/comments/feed/atom/
http://localhost/wordpress/comments/feed/
Pieter Goosen
[email protected]
UTF-8
text/html
af-AF
4.3.1
http://localhost/wordpress/xmlrpc.php
http://localhost/wordpress/wp-content/themes/pietergoosen2014/style.css
http://localhost/wordpress/wp-content/themes/pietergoosen2014
http://localhost/wordpress/wp-content/themes/pietergoosen2014
4
Pieter Goosen

これらの答えはすべてget_bloginfoを普通に使うよりも遅いです。

Get_bloginfo関数が取得できるさまざまなもののほとんどは、組み込みのWordPressメモリキャッシングシステムを使用します。データベースから来るオプションやその他のものはデータが最初に検索されるときにキャッシュされるので、それらは一般にスピードの問題から何度も呼ばれることに悩まされません。

ただし、このようなある種の「セットアップ」ステップを事前に行うと、そのすべてのデータを最初に照会するのに不要な作業が大量に行われることになります。実際に持っている必要はありません。

1
Otto