cp_object_relationship_exists( int $left_object_id, string $left_object_type, string $right_object_type, int $right_object_id )
Check if a relationship exists between two recognized objects.
Description
Relationship is bi-directional so the objects may be given in either order.
Objects must be among the array produced by cp_recognized_relationship_objects().
Parameters
- $left_object_id
-
(Required) ID of first object.
- $left_object_type
-
(Required) Name of type of first object.
- $right_object_type
-
(Required) Name of type of second object.
- $right_object_id
-
(Required) ID of second object.
Return
(int|WP_Error) $relationship_id Relationship ID or 0 if none exists.<br> WP_Error when a param of an incorrect type is specified.
Source
File: wp-includes/object-relationships.php
function cp_object_relationship_exists( $left_object_id, $left_object_type, $right_object_type, $right_object_id ) {
// Error if $left_object_id is not a positive integer.
if ( filter_var( $left_object_id, FILTER_VALIDATE_INT ) === false ) {
$item = 'string';
if ( $left_object_id === 0 ) {
$item = 0;
} elseif ( is_object( $left_object_id ) ) {
$item = 'object';
} elseif ( is_array( $left_object_id ) ) {
$item = 'array';
}
/* translators: %s: String, 0, object, or array. */
$message = sprintf( __( 'cp_add_object_relationship() expects parameter 1 to a positive integer; %s given.' ), $item );
return new WP_Error( 'left_object_id', $message );
}
// Error if $right_object_id is not a positive integer.
if ( filter_var( $right_object_id, FILTER_VALIDATE_INT ) === false ) {
$item = 'string';
if ( $right_object_id === 0 ) {
$item = 0;
} elseif ( is_object( $right_object_id ) ) {
$item = 'object';
} elseif ( is_array( $right_object_id ) ) {
$item = 'array';
}
/* translators: %s: String, 0, object, or array. */
$message = sprintf( __( 'cp_add_object_relationship() expects parameter 4 to a positive integer; %s given.' ), $item );
return new WP_Error( 'right_object_id', $message );
}
// Get array, and create list, of recognized relationship objects.
$recognized_relationship_objects = cp_recognized_relationship_objects();
$object_list = implode( ', ', $recognized_relationship_objects );
// Error if $left_object_type is not a non-null string of an appropriate value.
if ( ! in_array( $left_object_type, $recognized_relationship_objects ) ) {
$item = $left_object_type;
if ( is_int( $left_object_type ) ) {
$item = 'integer';
} elseif ( is_object( $left_object_type ) ) {
$item = 'object';
} elseif ( is_array( $left_object_type ) ) {
$item = 'array';
}
/* translators: 1: List of recognized relationship objects, 2: Integer, object, or array. */
$message = sprintf( __( 'cp_add_object_relationship() expects parameter 2 to be one of %s; %s given.' ), $object_list, $item );
return new WP_Error( 'left_object_type', $message );
}
// Error if $right_object_type is not a non-null string of an appropriate value
if ( ! in_array( $right_object_type, $recognized_relationship_objects ) ) {
$item = $right_object_type;
if ( is_int( $right_object_type ) ) {
$item = 'integer';
} elseif ( is_object( $right_object_type ) ) {
$item = 'object';
} elseif ( is_array( $right_object_type ) ) {
$item = 'array';
}
/* translators: 1: List of recognized relationship objects, 2: Integer, object, or array. */
$message = sprintf( __( 'cp_add_object_relationship() expects parameter 3 to be one of %s; %s given.' ), $object_list, $item );
return new WP_Error( 'right_object_type', $message );
}
// Good to go, so query database table.
global $wpdb;
$table_name = $wpdb->prefix . 'object_relationships';
$relationship_id = 0;
$relationship_array = array(
'left_object_id' => $left_object_id,
'left_object_type' => $left_object_type,
'right_object_type' => $right_object_type,
'right_object_id' => $right_object_id,
);
// Check if this relationship already exists.
$sql1 = $wpdb->prepare( "SELECT relationship_id FROM $table_name WHERE left_object_id = %d AND left_object_type = %s AND right_object_type = %s AND right_object_id = %d", $left_object_id, $left_object_type, $right_object_type, $right_object_id );
// If so, return the relationship ID as an integer.
$row = $wpdb->get_row( $sql1 );
if ( is_object( $row ) ) {
$relationship_id = (int) $row->relationship_id;
}
if ( empty( $relationship_id ) ) {
// Also query database table right to left if no match so far.
$sql2 = $wpdb->prepare( "SELECT relationship_id FROM $table_name WHERE right_object_id = %d AND right_object_type = %s AND left_object_type = %s AND left_object_id = %d", $left_object_id, $left_object_type, $right_object_type, $right_object_id );
// If this relationship exists, return the relationship ID as an integer.
$row = $wpdb->get_row( $sql2 );
if ( is_object( $row ) ) {
$relationship_id = (int) $row->relationship_id;
}
}
// Hook when search for pre-existing relationship completed.
do_action( 'existing_object_relationship', $relationship_id, $left_object_id, $left_object_type, $right_object_type, $right_object_id );
// Return relationship ID (which will be 0 if none exists).
return $relationship_id;
}
Changelog
Version | Description |
---|---|
CP-2.2.0 | Introduced. CP-2.2.0 |