wppaste
WordPress

get_bookmark( int|stdClass $bookmark, string $output = OBJECT, string $filter = 'raw' ): array|object|null

Since
2.1.0
Source
wp-includes/bookmark.php:24
Retrieves bookmark data.

Compatibility

WordPress
since 2.1.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

$bookmarkint|stdClass
$outputstringoptional
The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to an stdClass object, an associative array, or a numeric array, respectively. Default OBJECT.Default: OBJECT
$filterstringoptional
How to sanitize bookmark fields. Default 'raw'.Default: 'raw'

Return value

array|object|null
Type returned depends on $output value.

Performance profile

How much work a call to get_bookmark() 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
Heavy

Reaches the database via ->get_row().

Scaling
Constant

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

Instructions
12–74

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
7

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

What it touches

  • cacheobject cachewp_cache_add()called directly
  • querycontent querywp_get_object_terms()called directly
  • sqldatabase query->get_row()called directly
  • hookthird-party callbacksapply_filters()one call below get_bookmark()

Further down the call graph this can also reach option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 16 distinct outcomes

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

WhenInstructionsCalls it makes
always12–19none
!empty($bookmark) && is_object($bookmark)19wp_cache_add()
always20–33sanitize_bookmark()
!empty($bookmark) && !is_object($bookmark)25–35wp_cache_get(), sanitize_bookmark()
always26–33sanitize_bookmark(), get_object_vars()
!empty($bookmark) && is_object($bookmark)27–33wp_cache_add(), sanitize_bookmark()
!empty($bookmark) && !is_object($bookmark)30–34wp_cache_get(), ->prepare(), ->get_row()
!empty($bookmark) && !is_object($bookmark)31–35wp_cache_get(), sanitize_bookmark(), get_object_vars()
always32–39sanitize_bookmark(), get_object_vars(), array_values()
!empty($bookmark) && is_object($bookmark)33wp_cache_add(), sanitize_bookmark(), get_object_vars()
!empty($bookmark) && !is_object($bookmark)37–41wp_cache_get(), sanitize_bookmark(), get_object_vars(), array_values()
!empty($bookmark) && is_object($bookmark)39wp_cache_add(), sanitize_bookmark(), get_object_vars(), array_values()
4 further outcomes, up to 74 instructions
!empty($bookmark) && !is_object($bookmark)50–54wp_cache_get(), ->prepare(), ->get_row(), wp_get_object_terms(), array_unique(), wp_cache_add()
!empty($bookmark) && !is_object($bookmark)58–68wp_cache_get(), ->prepare(), ->get_row(), wp_get_object_terms(), array_unique(), wp_cache_add(), sanitize_bookmark()
!empty($bookmark) && !is_object($bookmark)64–68wp_cache_get(), ->prepare(), ->get_row(), wp_get_object_terms(), array_unique(), wp_cache_add(), sanitize_bookmark(), get_object_vars()
!empty($bookmark) && !is_object($bookmark)70–74wp_cache_get(), ->prepare(), ->get_row(), wp_get_object_terms(), array_unique(), wp_cache_add(), sanitize_bookmark(), get_object_vars(), array_values()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10012–7411
8.510012–7411
8.410012–7411
8.310012–7411
8.210012–7411
8.110012–74115 fewer instructions than PHP 7.4
7.410513–7611

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 · 4

Used by · 7

Source code

function get_bookmark( $bookmark, $output = OBJECT, $filter = 'raw' ) {	global $wpdb; 	if ( empty( $bookmark ) ) {		if ( isset( $GLOBALS['link'] ) ) {			$_bookmark = & $GLOBALS['link'];		} else {			$_bookmark = null;		}	} elseif ( is_object( $bookmark ) ) {		wp_cache_add( $bookmark->link_id, $bookmark, 'bookmark' );		$_bookmark = $bookmark;	} else {		if ( isset( $GLOBALS['link'] ) && ( $GLOBALS['link']->link_id === $bookmark ) ) {			$_bookmark = & $GLOBALS['link'];		} else {			$_bookmark = wp_cache_get( $bookmark, 'bookmark' );			if ( ! $_bookmark ) {				$_bookmark = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->links WHERE link_id = %d LIMIT 1", $bookmark ) );				if ( $_bookmark ) {					$_bookmark->link_category = array_unique( wp_get_object_terms( $_bookmark->link_id, 'link_category', array( 'fields' => 'ids' ) ) );					wp_cache_add( $_bookmark->link_id, $_bookmark, 'bookmark' );				}			}		}	} 	if ( ! $_bookmark ) {		return $_bookmark;	} 	$_bookmark = sanitize_bookmark( $_bookmark, $filter ); 	if ( OBJECT === $output ) {		return $_bookmark;	} elseif ( ARRAY_A === $output ) {		return get_object_vars( $_bookmark );	} elseif ( ARRAY_N === $output ) {		return array_values( get_object_vars( $_bookmark ) );	} else {		return $_bookmark;	}}

Changelog

Introduced in 2.1.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 6.9.7 tag, from src/wp-includes/bookmark.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.