updated plugin ActivityPub version 2.4.0

This commit is contained in:
2024-06-27 12:10:38 +00:00
committed by Gitium
parent eeef5ad6e0
commit 4e493c268e
49 changed files with 1368 additions and 491 deletions

View File

@ -854,3 +854,118 @@ function get_masked_wp_version() {
return implode( '.', $version );
}
/**
* Get the enclosures of a post.
*
* @param int $post_id The post ID.
*
* @return array The enclosures.
*/
function get_enclosures( $post_id ) {
$enclosures = get_post_meta( $post_id, 'enclosure' );
if ( ! $enclosures ) {
return array();
}
$enclosures = array_map(
function ( $enclosure ) {
$attributes = explode( "\n", $enclosure );
if ( ! isset( $attributes[0] ) || ! \wp_http_validate_url( $attributes[0] ) ) {
return false;
}
return array(
'url' => $attributes[0],
'length' => isset( $attributes[1] ) ? trim( $attributes[1] ) : null,
'mediaType' => isset( $attributes[2] ) ? trim( $attributes[2] ) : null,
);
},
$enclosures
);
return array_filter( $enclosures );
}
/**
* Retrieves the IDs of the ancestors of a comment.
*
* Adaption of `get_post_ancestors` from WordPress core.
*
* @see https://developer.wordpress.org/reference/functions/get_post_ancestors/
*
* @param int|WP_Comment $comment Comment ID or comment object.
*
* @return WP_Comment[] Array of ancestor comments or empty array if there are none.
*/
function get_comment_ancestors( $comment ) {
$comment = \get_comment( $comment );
// phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
if ( ! $comment || empty( $comment->comment_parent ) || $comment->comment_parent == $comment->comment_ID ) {
return array();
}
$ancestors = array();
$id = (int) $comment->comment_parent;
$ancestors[] = $id;
// phpcs:ignore Generic.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition
while ( $id > 0 ) {
$ancestor = \get_comment( $id );
$parent_id = (int) $ancestor->comment_parent;
// Loop detection: If the ancestor has been seen before, break.
if ( empty( $parent_id ) || ( $parent_id === (int) $comment->comment_ID ) || in_array( $parent_id, $ancestors, true ) ) {
break;
}
$id = $parent_id;
$ancestors[] = $id;
}
return $ancestors;
}
/**
* Change the display of large numbers on the site.
*
* @author Jeremy Herve
*
* @see https://wordpress.org/support/topic/abbreviate-numbers-with-k/
*
* @param string $formatted Converted number in string format.
* @param float $number The number to convert based on locale.
* @param int $decimals Precision of the number of decimal places.
*
* @return string Converted number in string format.
*/
function custom_large_numbers( $formatted, $number, $decimals ) {
global $wp_locale;
$decimals = 0;
$decimal_point = '.';
$thousands_sep = ',';
if ( isset( $wp_locale ) ) {
$decimals = (int) $wp_locale->number_format['decimal_point'];
$decimal_point = $wp_locale->number_format['decimal_point'];
$thousands_sep = $wp_locale->number_format['thousands_sep'];
}
if ( $number < 1000 ) { // any number less than a Thousand.
return \number_format( $number, $decimals, $decimal_point, $thousands_sep );
} elseif ( $number < 1000000 ) { // any number less than a million
return \number_format( $number / 1000, $decimals, $decimal_point, $thousands_sep ) . 'K';
} elseif ( $number < 1000000000 ) { // any number less than a billion
return \number_format( $number / 1000000, $decimals, $decimal_point, $thousands_sep ) . 'M';
} else { // at least a billion
return \number_format( $number / 1000000000, $decimals, $decimal_point, $thousands_sep ) . 'B';
}
// Default fallback. We should not get here.
return $formatted;
}