wppaste
WordPress

WP_Comment_Query::parse_orderby( string $orderby ): string|false

Since
4.2.0
Source
wp-includes/class-wp-comment-query.php:1192
Parse and sanitize 'orderby' keys passed to the comment query.

Compatibility

WordPress
since 4.2.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$orderbystring
Alias for the field to order by.

Return value

string|false
Value to used in the ORDER clause. False otherwise.

Performance profile

How much work a call to WP_Comment_Query::parse_orderby() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
19–60

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 85.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Comment_Query::parse_orderby() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always19–40->get_clauses()
always27–48->get_clauses(), array_keys(), array_merge()
$orderby !== false && $orderby !== "meta_value" && $orderby !== "meta_value_num" && $orderby === "comment__in"36–44->get_clauses(), array_map()
$orderby !== false && $orderby !== "meta_value" && $orderby !== "meta_value_num" && $orderby !== "comment__in" && $orderby && isset($meta_query_clauses[$orderby])44–52->get_clauses(), esc_sql(), esc_sql()
$orderby !== false && $orderby !== "meta_value" && $orderby !== "meta_value_num" && $orderby === "comment__in"44–52->get_clauses(), array_keys(), array_merge(), array_map()
$orderby !== false && $orderby !== "meta_value" && $orderby !== "meta_value_num" && $orderby !== "comment__in" && $orderby && isset($meta_query_clauses[$orderby])52–60->get_clauses(), array_keys(), array_merge(), esc_sql(), esc_sql()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev8519–608
8.58519–608
8.48519–6088 fewer instructions than PHP 8.3
8.39319–648
8.29319–648
8.19319–648
7.49319–648

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

  • esc_sql()Escapes data for use in a MySQL query.

Used by · 1

Source code

	protected function parse_orderby( $orderby ) {		global $wpdb; 		$allowed_keys = array(			'comment_agent',			'comment_approved',			'comment_author',			'comment_author_email',			'comment_author_IP',			'comment_author_url',			'comment_content',			'comment_date',			'comment_date_gmt',			'comment_ID',			'comment_karma',			'comment_parent',			'comment_post_ID',			'comment_type',			'user_id',		); 		if ( ! empty( $this->query_vars['meta_key'] ) ) {			$allowed_keys[] = $this->query_vars['meta_key'];			$allowed_keys[] = 'meta_value';			$allowed_keys[] = 'meta_value_num';		} 		$meta_query_clauses = $this->meta_query->get_clauses();		if ( $meta_query_clauses ) {			$allowed_keys = array_merge( $allowed_keys, array_keys( $meta_query_clauses ) );		} 		$parsed = false;		if ( $this->query_vars['meta_key'] === $orderby || 'meta_value' === $orderby ) {			$parsed = "$wpdb->commentmeta.meta_value";		} elseif ( 'meta_value_num' === $orderby ) {			$parsed = "$wpdb->commentmeta.meta_value+0";		} elseif ( 'comment__in' === $orderby ) {			$comment__in = implode( ',', array_map( 'absint', $this->query_vars['comment__in'] ) );			$parsed      = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )";		} elseif ( in_array( $orderby, $allowed_keys, true ) ) { 			if ( isset( $meta_query_clauses[ $orderby ] ) ) {				$meta_clause = $meta_query_clauses[ $orderby ];				$parsed      = sprintf( 'CAST(%s.meta_value AS %s)', esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );			} else {				$parsed = "$wpdb->comments.$orderby";			}		} 		return $parsed;	}

Changelog

Introduced in 4.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-comment-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.