权限没有,则隐藏 function forum_list_access_filter($forumlist, $gid, $allow = 'allowread') { global $grouplist; if (empty($forumlist)) return array(); if (1 == $gid) return $forumlist; $forumlist_filter = $forumlist; $group = $grouplist[$gid]; foreach ($forumlist_filter as $fid => $forum) { if (empty($forum['accesson']) && empty($group[$allow]) || !empty($forum['accesson']) && empty($forum['accesslist'][$gid][$allow])) { unset($forumlist_filter[$fid]); } unset($forumlist_filter[$fid]['accesslist']); } return $forumlist_filter; } function forum_filter_moduid($moduids) { $moduids = trim($moduids); if (empty($moduids)) return ''; $arr = explode(',', $moduids); $r = array(); foreach ($arr as $_uid) { $_uid = intval($_uid); $_user = user_read($_uid); if (empty($_user)) continue; if ($_user['gid'] > 4) continue; $r[] = $_uid; } return implode(',', $r); } function forum_safe_info($forum) { //unset($forum['moduids']); return $forum; } function forum_filter($forumlist) { foreach ($forumlist as &$val) { unset($val['brief'], $val['announcement'], $val['seo_title'], $val['seo_keywords'], $val['create_date_fmt'], $val['icon_url'], $val['modlist']); } return $forumlist; } function forum_format_url($forum) { global $conf; if (0 == $forum['category']) { // 列表URL $url = url('list-' . $forum['fid'], '', FALSE); } elseif (1 == $forum['category']) { // 频道 $url = url('category-' . $forum['fid'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = url('read-' . trim($forum['brief']), '', FALSE); } if ($conf['url_rewrite_on'] > 1 && $forum['well_alias']) { if (0 == $forum['category'] || 1 == $forum['category']) { $url = url($forum['well_alias'], '', FALSE); } elseif (2 == $forum['category']) { // 单页 $url = ($forum['threads'] && $forum['brief']) ? url($forum['well_alias'] . '-' . trim($forum['brief']), '', FALSE) : url($forum['well_alias'], '', FALSE); } } return $url; } function well_forum_alias() { $forumlist = forum_list_cache(); if (empty($forumlist)) return ''; $key = 'forum-alias'; static $cache = array(); if (isset($cache[$key])) return $cache[$key]; $cache[$key] = array(); foreach ($forumlist as $val) { if ($val['well_alias']) $cache[$key][$val['fid']] = $val['well_alias']; } return array_flip($cache[$key]); } function well_forum_alias_cache() { global $conf; $key = 'forum-alias-cache'; static $cache = array(); // 用静态变量只能在当前 request 生命周期缓存,跨进程需要再加一层缓存:redis/memcached/xcache/apc if (isset($cache[$key])) return $cache[$key]; if ('mysql' == $conf['cache']['type']) { $arr = well_forum_alias(); } else { $arr = cache_get($key); if (NULL === $arr) { $arr = well_forum_alias(); !empty($arr) AND cache_set($key, $arr); } } $cache[$key] = empty($arr) ? '' : $arr; return $cache[$key]; } ?>plugins - The function called on the wp head hook becomes null
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - The function called on the wp head hook becomes null

programmeradmin9浏览0评论

Sample code:

function test_loaded() {

require_once 'file.php';
$ins = Sample_Class::instance();

require_once 'file2.php';

}

add_action('plugins_loaded', 'test_loaded');

in file.php

class Sample_Class {

public static $instance = null;

public static function instance() {
if(null == self::$instance) {
self::$instance = new Sample_Class();
}
return self::$instance;
}

public static function sample($a1) {

$a2 = get_option($a1);

return $a2;
}

}

in file2.php

$a3 = Sample_Class::instance();

function sample2() {
global $a3;
echo $a3->sample('test');
}

add_action('wp_head', 'sample2');

function sample3() {
global $a3;
echo $a3->sample('test2');
}

add_action('wp_footer', 'sample3');

Problem:

Sample2 expected output head section: get_option('test') -> this value But $a3 is output null (i use var_dump($a3) in function) No error appears on sample3 function side and outputs $ a3 is object. (i use var_dump())

Anyone have any ideas? Why am I having trouble using the wp_head hook.

My workaround is: To redefine $a3 inside the sample2 function instead of using global. It works this way. But I don't know why I should use it like that.

Sample code:

function test_loaded() {

require_once 'file.php';
$ins = Sample_Class::instance();

require_once 'file2.php';

}

add_action('plugins_loaded', 'test_loaded');

in file.php

class Sample_Class {

public static $instance = null;

public static function instance() {
if(null == self::$instance) {
self::$instance = new Sample_Class();
}
return self::$instance;
}

public static function sample($a1) {

$a2 = get_option($a1);

return $a2;
}

}

in file2.php

$a3 = Sample_Class::instance();

function sample2() {
global $a3;
echo $a3->sample('test');
}

add_action('wp_head', 'sample2');

function sample3() {
global $a3;
echo $a3->sample('test2');
}

add_action('wp_footer', 'sample3');

Problem:

Sample2 expected output head section: get_option('test') -> this value But $a3 is output null (i use var_dump($a3) in function) No error appears on sample3 function side and outputs $ a3 is object. (i use var_dump())

Anyone have any ideas? Why am I having trouble using the wp_head hook.

My workaround is: To redefine $a3 inside the sample2 function instead of using global. It works this way. But I don't know why I should use it like that.

Share Improve this question asked Sep 21, 2020 at 20:58 Akın OcakAkın Ocak 32 bronze badges 1
  • 1 In file2 you're trying to use global variables named $a3 but that variable doesn't exist, nowhere do you declare a global variable then create it. You should not use global variables to build your logic, or singletons with only static functions. The problem here has nothing to do with hooks – Tom J Nowell Commented Sep 21, 2020 at 22:19
Add a comment  | 

1 Answer 1

Reset to default 0

As an alternative you could try declaring it with $GLOBALS['a3'] in file2 to make sure it becomes a global:

$GLOBALS['a3'] = Sample_Class::instance();

I'd guess the current $a3 declaration in file2's top level scope doesn't count as global because it gets inluded inside the test_loaded() scope. From the 'include' docs (which cover require and require_once too):

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

So it looks like variables declared at file scope in an included file are an exception to true global scope. However I'm surprised there's not an equivalent exception to allow them to be accessed as globals from file2 anyway, since that changes how $a3 behaves depending on how the file can be included.


If you don't need $a3 to be global outside file2, another option might be to change sample2() into an anonymous closure that includes $a3 with a 'use':

$a3 = Sample_Class::instance();

add_action( 'wp_head', function() use ($a3) {
    echo $a3->sample('test');
});
发布评论

评论列表(0)

  1. 暂无评论