Wordpress插件未输出HTML


Wordpress Plugin Not Outputting HTML

我继承了一个自主开发的Wordpress插件,它会吐出一段代码,并根据用户在设置中指定的内容对样式表进行排队。直到几天前,它还运行良好,现在样式表仍在排队并加载良好,但html($identityWrapper和$footerLogo)没有加载到页面上。这告诉我这不是服务器上的权限错误,因为脚本正在执行某些操作。我已经编辑了一些识别信息,还省略了设置部分,因为这一切似乎都很好。

我知道这是很多代码,但我不想遗漏可能很重要的部分。我花了很多时间试图自己弄清楚这一点,发现EOS是"字符串的末尾",但我还没有在任何其他Wordpress插件中看到任何使用它的例子。。。我认为这可能就是问题所在。

<?php
    /**
    * @package  Branding Bar
    * @version 1.0
    */
    /*
    Plugin Name:  [Redacted]
    Description: Uses output buffering to insert the branding bar after the body tag opens.
    Version: 1.0
    */
    add_action('wp_enqueue_scripts', array('BrandingBar', 'enqueue_stylesheet'), 10, 1);
    add_action('wp_head', array('BrandingBar', 'echo_styles'), 1000, 1);
    add_action('wp_footer', array('BrandingBar', 'add_real_logo'), 1000, 1);
    // Start output buffering in wp_head and try to flush ASAP. It will be
    // flushed when the request ends if, for some strange reason, no further
    // actions are called.
    add_action('wp_head', array('BrandingBar', 'start_output_buffering'), 10, 1);
    add_action('get_search_form', array('BrandingBar', 'end_output_buffering'), 10, 1);
    add_action('loop_start', array('BrandingBar', 'end_output_buffering'), 10, 1);
    add_action('get_sidebar', array('BrandingBar', 'end_output_buffering'), 10, 1);
    add_action('dynamic_sidebar', array('BrandingBar', 'end_output_buffering'), 10, 1);
    add_action('wp_meta', array('BrandingBar', 'end_output_buffering'), 10, 1);
    add_action('wp_footer', array('BrandingBar', 'end_output_buffering'), 10, 1);
class BrandingBar
{
    private static $styles = array(
    "body" => "background-position-y:60px",
    "#footerLogo h1 a" => "background-size: 280px 30px",
    "#footerLogo h2 a" => "background-size: 280px 15px",
    );
    public static $identityWrapper =<<<EOS
    <div id="IdentityWrapper">
        <header id="Identity">
            <hgroup>
                <h1>
                    <a target="_blank" href="#">[Redacted]</a>
                </h1>
                <h2>
                    <a target="_blank" href="#">[Redacted]</a>
                </h2>
            </hgroup>
        </header>
    </div>
    EOS;
    public static $footerLogo =<<<EOS
    <div id="footerLogo">
        <hgroup>
            <h1>
                <a target="_blank" href="#">[Redacted]</a>
            </h1>
            <h2>
                <a target="_blank" href="#">[Redacted]</a>
            </h2>
        </hgroup>
    </div>
    EOS;
public function enqueue_stylesheet()
{
    $color = get_option('branding_bar_color', 'black');
    $format = get_option('branding_bar_format', 'responsive');
    wp_enqueue_style('BrandingCss', plugins_url("widgets/branding/$color/$format/css/branding-main-2.0.css", __FILE__));
}
public function start_output_buffering()
{
    ob_start(array(self, 'insert_branding_bar'));
}
public function insert_branding_bar($buffer)
{
    return (preg_replace('/(<body[^>]+>)/', "$1'n".self::$identityWrapper, $buffer));
}
function end_output_buffering()
{
    $status = ob_get_status();
    if ($status['name'] === 'self::insert_branding_bar') {
        ob_end_flush();
    }
}
function echo_styles()
{
    echo "<style>'n";
    foreach (self::$styles as $selector => $declaration) {
        printf("%s{%s}'n", $selector, $declaration);
    }
    echo "@media screen and (max-width: 767px) {";
        echo<<<EOS
    #Identity h1 a { background-size: 99px 35px !important; }
EOS;
    echo "</style>'n";
}
public function add_real_logo()
{
    if ('responsive' === get_option('BrandingBarFormat', 'responsive'))     {
        echo self::$footerLogo;
    }
}
}

您看到的语法:

$var =<<<EOS    OR  echo <<<EOS
    ...
EOS;

是php的字符串heredoc语法。

这是一种在代码中嵌入大字符串的好方法,但它需要非常特定的语法才能工作。

终止符(在您的情况下为EOS;)不能有前导或尾随空格。因此,请确保E位于编辑器的第1列,并确保;和行尾之间没有空格。

如果编辑器有"显示控件非打印字符"选项,请启用此选项。它有助于调试heredocs。一个好的语法高亮显示应该捕获一个无效的终止符,并将代码的其余部分显示为字符串的一部分。

您尚未提供的某些代码实际上是通过调用这些函数(enqueue_stylesheet、start_output_buffering、end_output_buffering)导致输出的。因此,代码似乎在做一些不同的事情,因为某些原因,我们无法通过观察来猜测。