Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: '2'
services:
db:
image: mysql:5.7
restart: always
environment:
MYSQL_ROOT_PASSWORD: wordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress

wordpress:
depends_on:
- db
image: wordpress:latest
links:
- db
ports:
- "8777:80"
volumes:
- .:/var/www/html/wp-content/plugins/opengraph
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DEBUG: 1
72 changes: 61 additions & 11 deletions opengraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Adds Open Graph metadata to your pages
* Author: Will Norris
* Author URI: https://willnorris.com/
* Version: 1.11.3
* Version: 1.12.0
* License: Apache License, Version 2.0
* License URI: http://www.apache.org/licenses/LICENSE-2.0.html
* Text Domain: opengraph
Expand Down Expand Up @@ -108,6 +108,14 @@ function opengraph_metadata() {
$metadata[ "twitter:$property" ] = apply_filters( $filter, '' );
}

$fediverse_properties = array( 'creator' );

foreach ( $fediverse_properties as $property ) {
$filter = 'fediverse_' . $property;

$metadata[ "fediverse:$property" ] = apply_filters( $filter, '' );
}

return apply_filters( 'opengraph_metadata', $metadata );
}

Expand Down Expand Up @@ -140,6 +148,9 @@ function opengraph_default_metadata() {
// twitter card metadata
add_filter( 'twitter_card', 'twitter_default_card', 5 );
add_filter( 'twitter_creator', 'twitter_default_creator', 5 );

// fediverse creator metadata
add_filter( 'fediverse_creator', 'fediverse_default_creator', 5 );
}
add_action( 'wp', 'opengraph_default_metadata' );

Expand Down Expand Up @@ -469,6 +480,31 @@ function twitter_default_creator( $creator ) {
}


/**
* Default fediverse creator.
*
* @see https://github.com/mastodon/mastodon/pull/30398
*/
function fediverse_default_creator( $creator ) {
if ( ! is_singular() ) {
return $creator;
}

$post = get_queried_object();
$author = $post->post_author;
$webfinger = get_the_author_meta( 'fediverse', $author );

if ( ! $webfinger ) {
return $creator;
}

$webfinger = ltrim( $webfinger, '@' );
$webfinger = ltrim( $webfinger, 'acct:' );

return $webfinger;
}


/**
* Output Open Graph <meta> tags in the page header.
*/
Expand All @@ -482,16 +518,11 @@ function opengraph_meta_tags() {

foreach ( $value as $v ) {
// check if "strict mode" is enabled
if ( OPENGRAPH_STRICT_MODE === false ) {
// use "property" and "name"
printf(
'<meta property="%1$s" name="%1$s" content="%2$s" />' . PHP_EOL,
esc_attr( $key ),
esc_attr( $v )
);
} else {
// use "name" attribute for Twitter Cards
if ( stripos( $key, 'twitter:' ) === 0 ) {
if ( OPENGRAPH_STRICT_MODE === true ) {
if ( // use "name" attribute for Twitter Cards
str_starts_with( $key, 'twitter:' ) ||
str_starts_with( $key, 'fediverse:' )
) {
printf(
'<meta name="%1$s" content="%2$s" />' . PHP_EOL,
esc_attr( $key ),
Expand All @@ -504,6 +535,13 @@ function opengraph_meta_tags() {
esc_attr( $v )
);
}
} else {
// use "property" and "name"
printf(
'<meta property="%1$s" name="%1$s" content="%2$s" />' . PHP_EOL,
esc_attr( $key ),
esc_attr( $v )
);
}
}
}
Expand Down Expand Up @@ -576,6 +614,7 @@ function opengraph_article_metadata( $metadata ) {
function opengraph_user_contactmethods( $user_contactmethods ) {
$user_contactmethods['twitter'] = __( 'Twitter', 'opengraph' );
$user_contactmethods['facebook'] = __( 'Facebook (Profile URL)', 'opengraph' );
$user_contactmethods['fediverse'] = __( 'Fediverse (username@host.tld)', 'opengraph' );

return $user_contactmethods;
}
Expand Down Expand Up @@ -606,3 +645,14 @@ function opengraph_trim_text( $text, $length = 55 ) {

return wp_trim_words( $text, $excerpt_length, $excerpt_more );
}

/**
* str_starts_with function for PHP < 8.0
*
* @see https://www.php.net/manual/en/function.str-starts-with
*/
if ( ! function_exists( 'str_starts_with' ) ) {
function str_starts_with( $haystack, $needle ) {
return 0 === strncmp( $haystack, $needle, \strlen( $needle ) );
}
}
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: willnorris, pfefferle
Tags: social, opengraph, ogp, facebook
Requires at least: 2.3
Tested up to: 6.5
Stable tag: 1.11.3
Stable tag: 1.12.0
License: Apache License, Version 2.0
License URI: https://www.apache.org/licenses/LICENSE-2.0.html

Expand Down Expand Up @@ -62,6 +62,9 @@ The plugin populates the meta 'name' attribute alongside the 'property' attribut

Project maintained on github at [willnorris/wordpress-opengraph](https://github.com/willnorris/wordpress-opengraph).

= version 1.12.0 (Jul 3, 2024) =
- support `<meta name="fediverse:creator" />`

= version 1.11.3 (Jun 4, 2024) =
- don't return description for password protected posts

Expand Down