前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Genesis框架从入门到精通(8): 框架过滤器和数组

Genesis框架从入门到精通(8): 框架过滤器和数组

作者头像
丘壑
发布2019-03-13 10:15:57
7640
发布2019-03-13 10:15:57
举报
文章被收录于专栏:一丘一壑一丘一壑

原文

Genesis Explained Framework Filters with Arrays

In the previous two articles I have attempted to explain filters. Since each article builds on the previous article I recommend catching up on the entire Genesis Explained series, and at the very l…

Designs By Nick the Geek

译文

在前两篇文章中,我们介绍了过滤器。由于每篇文章都建立在前一篇文章的基础上,我建议你阅读整个Genesis Explained系列,至少是过滤子系列。

上一篇文章讨论了使用过滤器来改变“字符串”,即简单的文本和html短语。我演示了替换字符串替换以及字符串修改的技术。

本文将使用类似的示例和技术,但关注的重点是对象或数组。可以把数组看作一组有序的字符串。数组有两个部分,键和值。可以通过几种不同的方式对数组进行赋值,根据赋值方式的不同在代码中的表现方式也不同。这是一个简单的例子

代码语言:javascript
复制
// WHAT IS AN ARRAY?
$array_1      = array(
	'key1' => 'value1',
	'key2' => 'value2',
);
$array_2      = array( 'value1', 'value2' );
$array_2[]    = 'value3';
$array_2['3'] = 'value4';
$array_3 = [
	'value1',
	'value2',
];
$array_4   = [];
$array_4[] = 'value1';
$array_1 = array(
	'odd'  => array( 1, 3, 5 ),
	'even' => array( 2, 4, 6 ),
);

在第一个例子中,键名“key1”的值为字符串“value1”,键名“key2”的值为字符串“value2”。

第二个示例中没有列出键名,因此它们会自动分配为01。下一个会在现有数组$array_2末尾添加一个键名为2值为字符串’value3’的元素,最后一个示例手动将数组$array_2中的键名“3”赋值为“value4”。

还有一点需要注意,数组可以只包含字符串,或者也可以包含其他对象,即嵌套数组

如上面的最后部分, $array_1由两个对象数组组成,一个具有奇数,另一个具有偶数。

使用数组的原因是这是存储相关信息的高效方式,不需要具有数十个,数百个甚至数千个单独的变量。以使用许多不同的方法给数组解包,但对现在来说这些方法都用不到。

添加到数组

这可能是最容易的使用数组的数组的方法,但它也不简单,因为你需要知道哪些值是有用的。一个很好的例子就是使用评论表单。Genesis使用以下代码来创建评论表单

代码语言:javascript
复制
add_action( 'genesis_comment_form', 'genesis_do_comment_form' );
/**
 * Defines the comment form, hooked to <code>genesis_comments_form()</code>
 *
 * @since 1.0
 *
 * @global unknown $user_identity 
 * @global unknown $id 
 * @return null Returns null if Genesis disables comments for this page or post
 */
function genesis_do_comment_form() {
	global $user_identity, $id;

	// Check
	if ( ( is_page() && ! genesis_get_option( 'comments_pages' ) ) || ( is_single() && ! genesis_get_option( 'comments_posts' ) ) )
		return;

	$commenter = wp_get_current_commenter();
	$req = get_option( 'require_name_email' );
	$aria_req = ( $req ? ' aria-required="true"' : '' );

	$args = array(
		'fields' => array(
			'author' =>	'<p class="comment-form-author">' .
						'<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" tabindex="1"' . $aria_req . ' />' .
						'<label for="author">' . __( 'Name', 'genesis' ) . '</label> ' .
						( $req ? '<span class="required">*</span>' : '' ) .
						'</p><!-- #form-section-author .form-section -->',

			'email' =>	'<p class="comment-form-email">' .
						'<input id="email" name="email" type="text" value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30" tabindex="2"' . $aria_req . ' />' .
						'<label for="email">' . __( 'Email', 'genesis' ) . '</label> ' .
						( $req ? '<span class="required">*</span>' : '' ) .
						'</p><!-- #form-section-email .form-section -->',

			'url' =>		'<p class="comment-form-url">' .
							'<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" tabindex="3" />' .
							'<label for="url">' . __( 'Website', 'genesis' ) . '</label>' .
							'</p><!-- #form-section-url .form-section -->'
		),

		'comment_field' =>	'<p class="comment-form-comment">' .
							'<textarea id="comment" name="comment" cols="45" rows="8" tabindex="4" aria-required="true"></textarea>' .
							'</p><!-- #form-section-comment .form-section -->',

		'title_reply' => __( 'Speak Your Mind', 'genesis' ),
		'comment_notes_before' => '',
		'comment_notes_after' => '',
	);

	comment_form( apply_filters( 'genesis_comment_form_args', $args, $user_identity, $id, $commenter, $req, $aria_req ), $id );
}

以上的代码是Genesis 1.x版本的评论框代码了,现在已经更新了,仅作参考

上面的代码中,给Wordpress的 comment_form()函数传递了相当多的参数,但其实还可以添加更多的选项,参看 这里。为了本教程的目的,我们将更改提交按钮的文字,这意味着我们需要把它添加到参数列表中。我们可以先删除动作,再使用添加一个新动作并挂载上我们自己的函数,这个函数要包含完整的数组,以及要新的按钮文字–label_submit的值,这会产生很多不必要的代码。让我们看看使用过滤器会有多简单。

代码语言:javascript
复制
add_filter( 'genesis_comment_form_args', 'child_comment_form_args' );
function child_comment_form_args( $args ) {
    $args['label_submit'] = 'Publish Comment';
    
    return $args;
}

这就是全部。现在让我们来看看它是怎么做到的。第一行添加了一个过滤器。因为我们只需要1个参数,所以就使用默认值。第二行定义了我们的函数,下一行将键名为label_submit,值为“Publish Comment”的新元素添加到$args数组。通过查看WordPress的codex文档可以看到它是comment_form函数的有效参数,所以我们就改这个键。剩下的就是返回要在 comment_form()函数中使用的对象。

替换数组值

更换值更容易,因为你根据已经赋值的键名替换它的值。不需去弄清楚有哪些隐藏的键名是可以使用的。有一个地方可以做到这一点,那就是面包屑参数。我们来看看这个文件。找到这个函数有点麻烦。它不在 genesis/lib/structure/中,也不在 genesis/ lib/functions/ 中。最后一个可能的地方就是 genesis/lib/classes/。幸运的是那里有一个 breadcrumb.php文件。这是一个类,阅读它们的方式有点不同,所以我把代码贴出来并尝试解释一下怎么去理解它

以上的代码是Genesis 1.x版本的评论框代码了,现在已经更新了,仅作参考,最新的文件名是 `lib/classes/class-genesis-breadcrumb.php`

代码语言:javascript
复制
/**
	 * Constructor. Set up cacheable values and settings.
	 *
	 * @since 1.5
	 *
	 * @param array $args
	 */
	function genesis_breadcrumb() {
		$this->on_front = get_option( 'show_on_front' );

		/** Default arguments **/
		$this->args = array(
			'home'						=> __( 'Home', 'genesis' ),
			'sep'						=> ' / ',
			'list_sep'					=> ', ',
			'prefix'					=> '<div class="breadcrumb">',
			'suffix'					=> '</div>',
			'heirarchial_attachments'	=> true,
			'heirarchial_categories'	=> true,
			'display'					=> true,
			'labels' => array(
				'prefix'	=> __( 'You are here: ', 'genesis' ),
				'author'	=> __( 'Archives for ', 'genesis' ),
				'category'	=> __( 'Archives for ', 'genesis' ),
				'tag'		=> __( 'Archives for ', 'genesis' ),
				'date'		=> __( 'Archives for ', 'genesis' ),
				'search'	=> __( 'Search for ', 'genesis' ),
				'tax'		=> __( 'Archives for ', 'genesis' ),
				'post_type'	=> __( 'Archives for ', 'genesis' ),
				'404'		=> __( 'Not found: ', 'genesis' )
			)
		);

	}

	/**
	 * Return the final completed breadcrumb in markup wrapper. Public.
	 *
	 * @since 1.5
	 *
	 * @return string HTML markup
	 */
	function get_output( $args = array() ) {

		/** Merge and Filter user and default arguments **/
		$this->args = apply_filters( 'genesis_breadcrumb_args', wp_parse_args( $args, $this->args ) );

		return $this->args['prefix'] . $this->args['labels']['prefix'] . $this->build_crumbs() . $this->args['suffix'];

	}

代码很多,文件中还有更多代码,这这是相关的部分。最重要的一行是第46行。其他几行高亮的是我们将要做改变的地方。第46行是特殊的,因为它包含“apply_filters()”,这意味着我们可以改变某些值。也就是 get_output($ args)$this-> args。如果你查看第12行,你会看到 $this-> args,这是我们想要改变的。不错。ok,现在让我们改变这些值。

代码语言:javascript
复制
add_filter( 'genesis_breadcrumb_args', 'child_breadcrumb_args' );
function child_breadcrumb_args( $args ) {
    $args['sep'] = ' | ';
    $args['labels']['prefix'] = 'Path to here: ';
    $args['labels']['category'] = 'Category: ';

    return $args;
}

看,我们所要做的就是提供相同的键名,然后我们就可以更改它的值。对于像“labels”对象这样的嵌套值,你需要先提供对象的键名(”labels“),然后再指定对象中的键名(prefixcategory)。最后,请记住在完成后再返回数组。

更改数组

如果你阅读过上一篇文章,你应该知道在这里会发生什么。我们对数组中的值进行字符串替换。为了演示,我将删除一些验证有效表单性的html。这些有效性验证的html可以保留,一般我建议不要管它,但有时表单验证太过严格的话你必须删除某些html来满足需求,这里我指的是评论表单中的 aria-require 属性。让我们回到的 genesis/lib/structure/comments.php文件。我已经在第一个例子中贴了其中的代码。我们可以用任何值来替换 aria-required属性,但那样会有相当多的代码,只替换一部分更高效。

代码语言:javascript
复制
add_filter( 'genesis_comment_form_args', 'child_comment_form_remove_aria_required' );
function child_comment_form_remove_aria_required( $args ){
    $args = str_replace( ' aria-required="true"', '', $args );
    $args[fields] = str_replace( ' aria-required="true"', '', $args[fields] );
    
    return $args;
}

好的,到目前为止,前两行应该看起来非常熟悉。需要注意的重要一点是,我使用了与第一个示例不同的回调函数。如果我有两个具有相同名称的函数,那么该网站就会报错。始终要为你的函数提供唯一的名称。

后两行就是更改,第三行,$args数组中任何带有’aria-required ="true"‘的字符串都会被替换成空字符串,第四行,fields对象中的就不会再有这个属性值。再重申一次,永远记得在完成后有一个返回值。

过滤器子系列到此结束。我希望现在开始有点清晰了,但学习过滤器仍然感觉像某种黑魔法,有点令人生畏。我完全可以理解。因为当你第一次入坑时,这个主题可能是最模糊的,我不想让你现在就被搞糊涂了,我将在以后的教程中再回顾一下过滤器。在子主题开发的系列教程里,我们将使用几个过滤器,但是以一种稳健而实用的方式。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-02-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 原文
  • 译文
  • 添加到数组
  • 替换数组值
  • 更改数组
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档