'website', 'title' => '', 'description' => '', 'url' => '', 'site_name' => self::site_name(), 'locale' => self::locale(), 'image' => array(), 'published' => '', 'modified' => '', ); $data = array_merge( $defaults, $data ); if ( '' === $data['title'] ) { $data['title'] = $data['site_name']; } return $data; } /** * Home page: the site name and tagline. A static front page may lend its * featured image. * * @param array $settings Cleaned plugin settings. * @return array */ private static function front_page( $settings ) { $description = self::tagline(); $image = array(); $page = is_singular() ? get_queried_object() : null; if ( $page instanceof WP_Post ) { if ( '' === $description ) { $description = self::post_description( $page ); } $image = DevLab_OG_Image::describe( get_post_thumbnail_id( $page ) ); } return array( 'type' => 'website', 'title' => self::site_name(), 'description' => $description, 'url' => home_url( '/' ), 'image' => $image ? $image : self::default_image( $settings ), ); } /** * The page chosen under Settings > Reading as the "posts page". * * @param array $settings Cleaned plugin settings. * @return array */ private static function posts_page( $settings ) { $page_id = (int) get_option( 'page_for_posts' ); $url = $page_id ? get_permalink( $page_id ) : ''; return array( 'type' => 'website', 'title' => DevLab_OG_Text::plain( single_post_title( '', false ) ), 'description' => self::tagline(), 'url' => $url ? $url : home_url( '/' ), 'image' => self::default_image( $settings ), ); } /** * A single post, page or custom post type entry. * * @param array $settings Cleaned plugin settings. * @return array */ private static function singular( $settings ) { $post = get_queried_object(); if ( ! $post instanceof WP_Post ) { return array(); } $description = self::post_description( $post ); if ( '' === $description ) { $description = self::tagline(); } $url = wp_get_canonical_url( $post ); if ( ! $url ) { $url = get_permalink( $post ); } $image = DevLab_OG_Image::describe( get_post_thumbnail_id( $post ) ); return array( 'type' => 'article', 'title' => DevLab_OG_Text::plain( $post->post_title ), 'description' => $description, 'url' => $url ? $url : '', 'image' => $image ? $image : self::default_image( $settings ), 'published' => self::post_time( $post, 'date' ), 'modified' => self::post_time( $post, 'modified' ), ); } /** * Category, tag, custom taxonomy, author, post type and date archives. * * @param array $settings Cleaned plugin settings. * @return array */ private static function archive( $settings ) { // The core title has a prefix such as "Category: ". Share previews // look better without it, so it is switched off for this one call. add_filter( 'get_the_archive_title_prefix', '__return_empty_string', 99 ); $title = DevLab_OG_Text::plain( get_the_archive_title() ); remove_filter( 'get_the_archive_title_prefix', '__return_empty_string', 99 ); $description = DevLab_OG_Text::truncate( DevLab_OG_Text::plain( get_the_archive_description() ), self::EXCERPT_LENGTH ); if ( '' === $description ) { $description = self::tagline(); } return array( 'type' => 'website', 'title' => $title, 'description' => $description, 'url' => self::archive_url(), 'image' => self::default_image( $settings ), ); } /** * Address of the first page of the archive being viewed. * * @return string Empty when WordPress cannot build it. */ private static function archive_url() { $url = ''; if ( is_category() || is_tag() || is_tax() ) { $term = get_queried_object(); $link = $term instanceof WP_Term ? get_term_link( $term ) : ''; $url = is_string( $link ) ? $link : ''; } elseif ( is_author() ) { $url = get_author_posts_url( get_queried_object_id() ); } elseif ( is_post_type_archive() ) { $type = get_query_var( 'post_type' ); $type = is_array( $type ) ? reset( $type ) : $type; $link = $type ? get_post_type_archive_link( $type ) : false; $url = $link ? $link : ''; } elseif ( is_date() ) { $year = (int) get_query_var( 'year' ); $month = (int) get_query_var( 'monthnum' ); $day = (int) get_query_var( 'day' ); // Plain permalinks (?m=202603) fill "m" but not year/monthnum/day. if ( ! $year ) { $compact = preg_replace( '/\D/', '', (string) get_query_var( 'm' ) ); $year = (int) substr( $compact, 0, 4 ); $month = (int) substr( $compact, 4, 2 ); $day = (int) substr( $compact, 6, 2 ); } if ( is_day() ) { $url = get_day_link( $year, $month, $day ); } elseif ( is_month() ) { $url = get_month_link( $year, $month ); } else { $url = get_year_link( $year ); } } return is_string( $url ) ? $url : ''; } /** * Description of a post: the manual excerpt, otherwise the start of the * content. Password-protected posts reveal neither. * * @param WP_Post $post The post. * @return string Empty when there is nothing to show. */ private static function post_description( $post ) { if ( post_password_required( $post ) ) { return ''; } $excerpt = DevLab_OG_Text::plain( $post->post_excerpt ); if ( '' !== $excerpt ) { return DevLab_OG_Text::truncate( $excerpt, self::EXCERPT_LENGTH ); } return DevLab_OG_Text::truncate( DevLab_OG_Text::plain( $post->post_content ), self::DESCRIPTION_LENGTH ); } /** * Publication or modification time as ISO 8601, or '' when unknown. * * @param WP_Post $post The post. * @param string $field 'date' or 'modified'. * @return string */ private static function post_time( $post, $field ) { // Prefer the UTC time. Drafts can lack it, so fall back to local time. foreach ( array( 'gmt', 'local' ) as $source ) { $time = get_post_datetime( $post, $field, $source ); if ( $time ) { return $time->format( 'c' ); } } return ''; } /** * The default image chosen in the settings, resolved once per request. * * @param array $settings Cleaned plugin settings. * @return array See DevLab_OG_Image::describe(). Empty when none is set. */ private static function default_image( $settings ) { $id = (int) $settings['default_image']; if ( ! isset( self::$default_images[ $id ] ) ) { self::$default_images[ $id ] = DevLab_OG_Image::describe( $id ); } return self::$default_images[ $id ]; } /** * Site title as plain text. * * @return string */ private static function site_name() { return DevLab_OG_Text::plain( get_bloginfo( 'name' ) ); } /** * Site tagline as plain text. * * @return string */ private static function tagline() { return DevLab_OG_Text::truncate( DevLab_OG_Text::plain( get_bloginfo( 'description' ) ), self::EXCERPT_LENGTH ); } /** * Locale in the form Facebook expects (language_TERRITORY), or '' when the * WordPress locale does not look like that. * * @return string */ private static function locale() { $locale = get_locale(); return preg_match( '/^[a-z]{2,3}_[A-Z]{2}$/', $locale ) ? $locale : ''; } }