ID; $content = $post->post_content; $f = SXSEO_Fields::all_custom( $id ); $terms = self::term_names( $post ); $is_home = ( 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === $id ); $title_plain = sxseo_title_text( $post ); $focus = '' !== $f['focus'] ? $f['focus'] : SXSEO_Auto::focus( $title_plain ); $pt = 'page' === $post->post_type ? 'page' : 'post'; $tpl = $is_home ? sxseo_opt( 'title_home' ) : sxseo_opt( 'title_' . $pt ); $vars = array( 'title' => $title_plain, 'category' => $terms['primary'], 'author' => get_the_author_meta( 'display_name', $post->post_author ), 'date' => get_the_date( '', $post ), 'focus' => $focus, ); $title = '' !== $f['title'] ? SXSEO_Auto::render_template( $f['title'], $vars ) : SXSEO_Auto::render_template( $tpl, $vars ); $desc_auto = SXSEO_Auto::description( $content, has_excerpt( $post ) ? $post->post_excerpt : '' ); if ( $is_home && '' !== (string) sxseo_opt( 'home_description' ) ) { $desc_auto = (string) sxseo_opt( 'home_description' ); } if ( '' === trim( $desc_auto ) ) { // Template-driven pages have no editor content: describe them with their title and the site's own summary. $site_desc = (string) sxseo_opt( 'home_description' ) ?: get_bloginfo( 'description' ); $desc_auto = sxseo_trim( trim( $title_plain . ( '' !== $site_desc ? ' — ' . $site_desc : '' ) ), (int) sxseo_opt( 'description_len', 155 ) ); } $desc = '' !== $f['desc'] ? SXSEO_Auto::render_template( $f['desc'], $vars ) : $desc_auto; $keywords = '' !== $f['keywords'] ? array_values( array_filter( array_map( 'trim', explode( ',', sxseo_lower( $f['keywords'] ) ) ) ) ) : SXSEO_Auto::keywords( $title_plain, $content, $terms['all'], $focus ); $image = '' !== $f['og_image'] ? $f['og_image'] : self::post_image( $post ); $url = '' !== $f['canonical'] ? $f['canonical'] : get_permalink( $post ); $robots = array(); if ( '1' === $f['noindex'] || post_password_required( $post ) || 'publish' !== $post->post_status ) { $robots[] = 'noindex'; } if ( '1' === $f['nofollow'] ) { $robots[] = 'nofollow'; } foreach ( array( 'noarchive', 'nosnippet', 'noimageindex' ) as $adv ) { if ( '1' === $f[ $adv ] ) { $robots[] = $adv; } } return array( 'id' => $id, 'title' => $title, 'title_auto'=> '' === $f['title'], 'desc' => $desc, 'desc_auto' => '' === $f['desc'], 'focus' => $focus, 'focus_auto'=> '' === $f['focus'], 'keywords' => $keywords, 'image' => $image, 'url' => $url, 'robots' => $robots, 'og_title' => '' !== $f['og_title'] ? $f['og_title'] : ( '' !== $f['title'] ? $title : $title_plain ), 'og_desc' => '' !== $f['og_desc'] ? $f['og_desc'] : $desc, 'custom' => $f, 'terms' => $terms, ); } public static function term_names( $post ) { $all = array(); $primary = ''; foreach ( get_object_taxonomies( $post->post_type, 'objects' ) as $tax ) { if ( ! $tax->public ) { continue; } $terms = get_the_terms( $post, $tax->name ); if ( ! $terms || is_wp_error( $terms ) ) { continue; } foreach ( $terms as $t ) { if ( 'uncategorized' === $t->slug ) { continue; } $all[] = $t->name; if ( '' === $primary && 'category' === $tax->name ) { $primary = $t->name; } } } return array( 'all' => array_values( array_unique( $all ) ), 'primary' => $primary ); } public static function post_image( $post ) { $thumb = get_post_thumbnail_id( $post ); if ( $thumb ) { $src = wp_get_attachment_image_url( $thumb, 'full' ); if ( $src ) { return $src; } } $first = SXSEO_Auto::first_image( $post->post_content ); if ( $first ) { return $first; } return (string) sxseo_opt( 'default_image', '' ) ?: (string) sxseo_opt( 'org_logo', '' ); } // ---------------------------------------------------------------- request private static function build() { $c = array( 'type' => 'other', 'object' => null, 'title' => '', 'desc' => '', 'url' => '', 'robots' => array(), 'image' => '', 'keywords' => array(), 'og_type' => 'website', 'og_title' => '', 'og_desc' => '', 'post' => null, 'ctx' => array(), ); $paged = max( 1, (int) get_query_var( 'paged' ), (int) get_query_var( 'page' ) ); $page_suffix = $paged > 1 ? ' ' . sprintf( /* translators: %d: page number */ __( 'Page %d', 'sentrixa-seo' ), $paged ) : ''; $site = array( 'sitename' => get_bloginfo( 'name' ), 'tagline' => get_bloginfo( 'description' ) ); if ( is_singular() ) { $post = get_queried_object(); if ( $post instanceof WP_Post ) { $ctx = self::for_post( $post ); $c = array_merge( $c, array( 'type' => is_front_page() ? 'front' : ( 'page' === $post->post_type ? 'page' : 'post' ), 'object' => $post, 'post' => $post, 'ctx' => $ctx, 'title' => $ctx['title'], 'desc' => $ctx['desc'], 'url' => $ctx['url'], 'robots' => $ctx['robots'], 'image' => $ctx['image'], 'keywords' => $ctx['keywords'], 'og_type' => 'post' === $post->post_type || 'article' === $post->post_type ? 'article' : 'website', 'og_title' => $ctx['og_title'], 'og_desc' => $ctx['og_desc'], ) ); if ( is_attachment() ) { $c['robots'][] = 'noindex'; } $page = (int) get_query_var( 'page' ); if ( $page > 1 && '' === $ctx['custom']['canonical'] ) { $c['url'] = trailingslashit( get_permalink( $post ) ) . user_trailingslashit( (string) $page, 'single_paged' ); } return $c; } } if ( is_front_page() || is_home() ) { $posts_page = 'page' === get_option( 'show_on_front' ) ? (int) get_option( 'page_for_posts' ) : 0; if ( is_home() && $posts_page ) { $post = get_post( $posts_page ); $ctx = self::for_post( $post ); $title = $ctx['title']; $desc = $ctx['desc']; } else { $title = SXSEO_Auto::render_template( sxseo_opt( 'title_home' ), $site ); $desc = (string) sxseo_opt( 'home_description' ); if ( '' === $desc ) { $desc = get_bloginfo( 'description' ); } } $c['type'] = 'home'; $c['title'] = $title . $page_suffix; $c['desc'] = $desc; $c['url'] = $paged > 1 ? get_pagenum_link( $paged ) : ( ( is_home() && $posts_page ) ? get_permalink( $posts_page ) : home_url( '/' ) ); $c['image'] = (string) sxseo_opt( 'default_image', '' ) ?: (string) sxseo_opt( 'org_logo', '' ); $c['keywords'] = SXSEO_Auto::keywords( get_bloginfo( 'name' ), get_bloginfo( 'description' ), array(), '', 8 ); $c['og_title'] = $title; $c['og_desc'] = $desc; return $c; } if ( is_category() || is_tag() || is_tax() ) { $term = get_queried_object(); if ( $term instanceof WP_Term ) { $custom_t = (string) get_term_meta( $term->term_id, 'sxseo_title', true ); $custom_d = (string) get_term_meta( $term->term_id, 'sxseo_desc', true ); $title = SXSEO_Auto::render_template( '' !== $custom_t ? $custom_t : sxseo_opt( 'title_term' ), array( 'term' => $term->name ) ); $desc = '' !== $custom_d ? $custom_d : sxseo_trim( sxseo_plain( term_description( $term->term_id ) ), (int) sxseo_opt( 'description_len', 155 ) ); if ( '' === $desc ) { /* translators: 1: term name, 2: site name */ $desc = sprintf( __( 'Everything on %2$s filed under %1$s.', 'sentrixa-seo' ), $term->name, get_bloginfo( 'name' ) ); } $c['type'] = 'term'; $c['object'] = $term; $c['title'] = $title . $page_suffix; $c['desc'] = $desc; $c['url'] = get_pagenum_link( $paged ); $c['keywords'] = array_slice( array_unique( array_filter( array( sxseo_lower( $term->name ), sxseo_lower( get_bloginfo( 'name' ) ) ) ) ), 0, 8 ); $c['og_title'] = $title; $c['og_desc'] = $desc; $c['image'] = (string) sxseo_opt( 'default_image', '' ) ?: (string) sxseo_opt( 'org_logo', '' ); if ( ! sxseo_term_indexable( $term ) ) { $c['robots'][] = 'noindex'; } return $c; } } if ( is_author() ) { $user = get_queried_object(); $name = $user ? $user->display_name : ''; $bio = $user ? sxseo_plain( get_the_author_meta( 'description', $user->ID ) ) : ''; $c['type'] = 'author'; $c['object'] = $user; $c['title'] = SXSEO_Auto::render_template( sxseo_opt( 'title_author' ), array( 'author' => $name ) ) . $page_suffix; $c['desc'] = '' !== $bio ? sxseo_trim( $bio, 155 ) : sprintf( /* translators: %s: author */ __( 'Articles written by %s.', 'sentrixa-seo' ), $name ); $c['url'] = get_pagenum_link( $paged ); $c['og_title'] = $c['title']; $c['og_desc'] = $c['desc']; $c['image'] = (string) sxseo_opt( 'default_image', '' ); $mode = sxseo_opt( 'noindex_author', 'auto' ); if ( '1' === (string) $mode || ( 'auto' === $mode && count( get_users( array( 'who' => 'authors', 'fields' => 'ID', 'number' => 2 ) ) ) < 2 ) ) { $c['robots'][] = 'noindex'; } return $c; } if ( is_search() ) { $c['type'] = 'search'; $c['title'] = SXSEO_Auto::render_template( sxseo_opt( 'title_search' ), array( 'searchphrase' => get_search_query( false ) ) ); $c['url'] = home_url( '/' ); $c['robots'][] = 'noindex'; return $c; } if ( is_404() ) { $c['type'] = '404'; $c['title'] = SXSEO_Auto::render_template( sxseo_opt( 'title_404' ), array() ); $c['robots'][] = 'noindex'; return $c; } if ( is_date() ) { $c['type'] = 'date'; $c['title'] = SXSEO_Auto::render_template( sxseo_opt( 'title_archive' ), array( 'archive' => wp_strip_all_tags( get_the_archive_title() ) ) ) . $page_suffix; $c['url'] = get_pagenum_link( $paged ); if ( sxseo_opt( 'noindex_date' ) ) { $c['robots'][] = 'noindex'; } return $c; } if ( is_post_type_archive() ) { $pt = get_queried_object(); $name = $pt ? $pt->labels->name : ''; $c['type'] = 'archive'; $c['title'] = SXSEO_Auto::render_template( sxseo_opt( 'title_archive' ), array( 'archive' => $name ) ) . $page_suffix; $c['desc'] = $pt && ! empty( $pt->description ) ? $pt->description : sprintf( /* translators: 1: type, 2: site */ __( '%1$s from %2$s.', 'sentrixa-seo' ), $name, get_bloginfo( 'name' ) ); $c['url'] = get_pagenum_link( $paged ); $c['og_title'] = $c['title']; $c['og_desc'] = $c['desc']; return $c; } return $c; } }