. */ class ACSSEO_Social { /** * Singleton instance. * * @var ACSSEO_Social|null */ private static $instance = null; /** * Return the shared instance. * * @return ACSSEO_Social */ public static function instance() { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } /** * Hook the output. */ private function __construct() { add_action( 'wp_head', array( $this, 'output' ), 4 ); } /** * Resolve the image used for sharing. * * @param int $post_id Post ID, or 0 for the front page. * @return string Image URL, or an empty string. */ public static function resolve_image( $post_id ) { if ( $post_id ) { $custom = (string) get_post_meta( $post_id, '_acsseo_social_image', true ); if ( '' !== $custom ) { return $custom; } if ( has_post_thumbnail( $post_id ) ) { $thumbnail = get_the_post_thumbnail_url( $post_id, 'large' ); if ( $thumbnail ) { return (string) $thumbnail; } } } return (string) ACSSEO_Settings::get( 'default_social_image', '' ); } /** * Print the Open Graph and Twitter Card tags. * * @return void */ public function output() { if ( ! ACSSEO_Settings::get( 'output_social' ) ) { return; } $is_front = is_front_page(); $is_singular = is_singular() && ! $is_front; if ( ! $is_singular && ! $is_front && ! is_home() ) { return; } if ( $is_front ) { // A static front page is still the site home, so it is shared as the // website rather than as an article named after the page. $post_id = 0; $title = ACSSEO_Settings::home_title(); $description = ACSSEO_Settings::home_description(); $url = home_url( '/' ); $type = 'website'; } elseif ( $is_singular ) { $post_id = get_queried_object_id(); $title = (string) get_post_meta( $post_id, '_acsseo_title', true ); $title = '' !== $title ? $title : get_the_title( $post_id ); $description = ACSSEO_Meta::resolve_description( $post_id ); $url = (string) get_permalink( $post_id ); $type = 'article'; } else { $post_id = 0; $title = get_bloginfo( 'name', 'display' ); $description = get_bloginfo( 'description', 'display' ); $url = home_url( '/' ); $type = 'website'; } $image = self::resolve_image( $post_id ); echo '' . "\n"; echo '' . "\n"; if ( '' !== $description ) { echo '' . "\n"; } echo '' . "\n"; echo '' . "\n"; echo '' . "\n"; if ( 'article' === $type ) { $published = get_the_date( DATE_W3C, $post_id ); $modified = get_the_modified_date( DATE_W3C, $post_id ); if ( $published ) { echo '' . "\n"; } if ( $modified ) { echo '' . "\n"; } } if ( '' !== $image ) { echo '' . "\n"; } echo '' . "\n"; echo '' . "\n"; if ( '' !== $description ) { echo '' . "\n"; } if ( '' !== $image ) { echo '' . "\n"; } $twitter_site = (string) ACSSEO_Settings::get( 'twitter_site', '' ); if ( '' !== $twitter_site ) { echo '' . "\n"; } } }