Skip to content

Commit

Permalink
- added og:logo
Browse files Browse the repository at this point in the history
- added og:locale:alternate (WPML integration)
- added check for wp_get_attachment_image_src() result
  • Loading branch information
iworks committed Apr 18, 2023
1 parent 0b0fe21 commit 8a37a5b
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 68 deletions.
140 changes: 129 additions & 11 deletions includes/iworks/class-iworks-opengraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class iWorks_OpenGraph {
private $is_schema_org_enabled = true;

public function __construct() {
$this->debug = defined( 'WP_DEBUG' ) && WP_DEBUG;
$this->debug = apply_filters( 'og_debug', defined( 'WP_DEBUG' ) && WP_DEBUG );
/**
* WordPress Hooks
*/
Expand Down Expand Up @@ -268,6 +268,7 @@ public function wp_head() {
'type' => 'website',
'locale' => $this->get_locale(),
'site_name' => get_bloginfo( 'name' ),
'logo' => $this->get_site_logo(),
),
'article' => array(
'tag' => array(),
Expand All @@ -280,7 +281,7 @@ public function wp_head() {
'player' => apply_filters( 'og_video_init', array() ),
),
'schema' => array(),
);
);
/**
* plugin: Facebook Page Publish
*/
Expand Down Expand Up @@ -328,13 +329,20 @@ public function wp_head() {
if ( has_post_thumbnail( $post->ID ) ) {
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
$thumbnail_src = wp_get_attachment_image_src( $post_thumbnail_id, 'full' );
$src = esc_url( $thumbnail_src[0] );
/**
* Feature image should be first!
* check response
*
* @since 3.1.1
* @since 3.2.0
*/
array_unshift( $og['og']['image'], $this->get_image_dimensions( $thumbnail_src, $post_thumbnail_id ) );
if ( false !== $thumbnail_src ) {
$src = esc_url( $thumbnail_src[0] );
/**
* Feature image should be first!
*
* @since 3.1.1
*/
array_unshift( $og['og']['image'], $this->get_image_dimensions( $thumbnail_src, $post_thumbnail_id ) );
}
}
}
/**
Expand Down Expand Up @@ -941,7 +949,7 @@ public function wp_head() {
*
* @since 2.9.3
*/
foreach ( $og as $key => $data ) {
foreach ( $og as $key => $data ) {
$og[ $key ] = apply_filters( 'og_' . $key . '_array', $data );
}
/**
Expand Down Expand Up @@ -995,7 +1003,20 @@ private function echo_array( $og, $parent = array() ) {
if ( 'labels' === $tag && count( $parent ) && 'twitter' === $parent[0] ) {
$this->echo_twiter_labels( $data );
} elseif ( is_array( $data ) ) {
$this->echo_array( $data, $tags );
/**
* og:logo exception
*
* @since 3.2.0
*/
if (
2 === count( $parent )
&& 'og' === $parent[0]
&& 'logo' === $parent[1]
) {
$this->echo_one_with_array_of_params( $parent, $data );
} else {
$this->echo_array( $data, $tags );
}
} else {
if ( 'schema' === $tags[0] ) {
if ( apply_filters( 'og_is_schema_org_enabled', $this->is_schema_org_enabled ) ) {
Expand All @@ -1010,6 +1031,51 @@ private function echo_array( $og, $parent = array() ) {
}
}

/**
* print with params
*
* @since 3.2.0
*/
private function echo_one_with_array_of_params( $property, $params ) {
$meta_property = $property;
if ( is_array( $property ) ) {
$meta_property = implode( ':', $property );
}
if ( ! is_array( $params ) ) {
$this->echo_one( $property, $params );
return;
}
$attrs = array();
foreach ( $params as $key => $value ) {
$attrs[] = sprintf(
'%s="%s"',
esc_attr( $key ),
esc_attr( $value )
);
}
if ( empty( $attrs ) ) {
return;
}
/**
* Property filter string
* @since 2.7.7
*/
$property_filter_string = preg_replace( '/:/', '_', $meta_property );
/**
* Filter to change whole meta
*/
$filter_name = sprintf( 'og_%s_meta', $property_filter_string );
echo apply_filters(
$filter_name,
sprintf(
'<meta property="%s" %s />%s',
esc_attr( $meta_property ),
implode( ' ', $attrs ),
$this->debug ? PHP_EOL : ''
)
);
}

/**
* Echo one row
*
Expand Down Expand Up @@ -1175,12 +1241,15 @@ private function get_attachment_id( $url ) {
*
* @since 2.6.0
*/
private function get_transient_key( $post_id ) {
private function get_transient_key( $post_id ) {
if ( $this->debug ) {
return false;
}
$key = sprintf( 'og_%d_%s', $post_id, $this->version );
$locale = $this->get_locale();
if ( ! empty( $locale ) ) {
$key .= '_' . $locale;
}
}
return $key;
}

Expand Down Expand Up @@ -1224,7 +1293,8 @@ public function load_integrations() {
*/
if ( preg_match( '/yarpp\.php$/', $plugin ) ) {
include_once $root . '/class-iworks-opengraph-integrations-yarpp.php';
new iWorks_OpenGraph_Integrations_YARPP;
new iWorks_OpenGraph_Integrations_YARPP;
continue;
}
/**
* Reading Time WP
Expand All @@ -1235,6 +1305,7 @@ public function load_integrations() {
if ( preg_match( '/rt-reading-time\.php$/', $plugin ) ) {
include_once $root . '/class-iworks-opengraph-integrations-reading-time-wp.php';
new iWorks_OpenGraph_Integrations_Reading_Time_WP;
continue;
}
/**
* Categories Images
Expand All @@ -1245,6 +1316,7 @@ public function load_integrations() {
if ( preg_match( '/categories-images\.php$/', $plugin ) ) {
include_once $root . '/class-iworks-opengraph-integrations-categories-images.php';
new iWorks_OpenGraph_Integrations_Categories_Images;
continue;
}
/**
* PublishPress Future: Automatically Unpublish WordPress Posts
Expand All @@ -1255,6 +1327,7 @@ public function load_integrations() {
if ( preg_match( '/post-expirator\.php$/', $plugin ) ) {
include_once $root . '/class-iworks-opengraph-integrations-post-expirator.php';
new iWorks_OpenGraph_Integrations_Post_Expirator;
continue;
}
/**
* Contextual Related Posts
Expand All @@ -1265,6 +1338,7 @@ public function load_integrations() {
if ( preg_match( '/contextual-related-posts\.php$/', $plugin ) ) {
include_once $root . '/class-iworks-opengraph-integrations-contextual-related-posts.php';
new iWorks_OpenGraph_Integrations_Contextual_Related_Posts;
continue;
}
/**
* Contextual Related Posts
Expand All @@ -1275,6 +1349,7 @@ public function load_integrations() {
if ( preg_match( '/related-posts-for-wp\.php$/', $plugin ) ) {
include_once $root . '/class-iworks-opengraph-integrations-related-posts-for-wp.php';
new iWorks_OpenGraph_Integrations_Related_Posts_for_WordPress;
continue;
}
/**
* Twitter
Expand All @@ -1285,6 +1360,18 @@ public function load_integrations() {
if ( preg_match( '/twitter\.php$/', $plugin ) ) {
include_once $root . '/class-iworks-opengraph-integrations-twitter.php';
new iWorks_OpenGraph_Integrations_Twitter;
continue;
}
/**
* The WordPress Multilingual Plugin (WPML)
* https://wpml.org/
*
* @since 3.2.0
*/
if ( preg_match( '/sitepress\.php$/', $plugin ) ) {
include_once $root . '/class-iworks-opengraph-integrations-sitepress-multilingual-cms.php';
new iWorks_OpenGraph_Integrations_Sitepress_Multilingual_CMS;
continue;
}
}
}
Expand Down Expand Up @@ -1497,4 +1584,35 @@ private function set_twitter_image( $og ) {
return $og;
}

/**
* OG:logo
*
* @since 3.2.0
*/
public function get_site_logo() {
$logos = array();
$sizes = apply_filters(
'og_logo_sizes',
array(
128,
236,
512,
1024,
)
);
$last = null;
foreach ( $sizes as $size ) {
$url = get_site_icon_url( $size );
if ( $last === $url ) {
continue;
}
$logos[] = array(
'content' => $url,
'size' => sprintf( '%1$dx%1$d', $size ),
);
$last = $url;
}
return $logos;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* The WordPress Multilingual Plugin
* https://wpml.org/
*
* @since 3.2.0
*/
class iWorks_OpenGraph_Integrations_Sitepress_Multilingual_CMS extends iWorks_OpenGraph_Integrations {

public function __construct() {
add_filter( 'og_og_array', array( $this, 'maybe_add_alternate_locale' ) );
}

/**
* Try to add alternate locale
*
* @since 3.2.0
*/
public function maybe_add_alternate_locale( $data ) {
if ( ! is_singular() ) {
return $data;
}
if ( ! function_exists( 'icl_get_languages_locales' ) ) {
return $data;
}
$result = icl_get_languages_locales();
if ( empty( $result ) ) {
return $data;
}
$type = apply_filters( 'wpml_element_type', get_post_type( get_the_ID() ) );
if ( empty( $type ) ) {
return $data;
}
$trid = apply_filters( 'wpml_element_trid', false, get_the_ID(), $type );
if ( empty( $trid ) ) {
return $data;
}
$translations = apply_filters( 'wpml_get_element_translations', array(), $trid, $type );
if ( empty( $translations ) ) {
return $data;
}
foreach ( $translations as $lang => $translation ) {
if ( ! is_array( $data['locale'] ) ) {
$data['locale'] = array(
$data['locale'],
'alternate' => array(),
);
}
if (
isset( $result[ $translation->language_code ] )
& $data['locale'][0] !== $result[ $translation->language_code ]
) {
$data['locale']['alternate'][] = $result[ $translation->language_code ];
}
}
return $data;
}
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"title": "OG — Better Share on Social Media",
"tagline": "The simple method to add Open Graph metadata to your entries so that they look great when shared on sites.",
"description": "Very tiny Open Graph plugin - add featured image as facebook image. This plugin do not have any configuration - you can check how it works looking into page source.",
"version": "3.1.9",
"version": "3.2.0",
"homepage": "http://og.iworks.pl/",
"author": [
{
Expand Down
Loading

0 comments on commit 8a37a5b

Please sign in to comment.