Skip to content

Commit

Permalink
Merge pull request #16850 from Yoast/P3-378-update-presentation-class…
Browse files Browse the repository at this point in the history
…es-with-GST

P3-378 update presentation classes with Global Social Templates
  • Loading branch information
karlijnbok committed Apr 22, 2021
2 parents 31dcb02 + 3c45137 commit 2297401
Show file tree
Hide file tree
Showing 19 changed files with 716 additions and 56 deletions.
76 changes: 76 additions & 0 deletions inc/class-upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,9 @@ private function upgrade_162() {
*/
private function upgrade_163() {
$this->migrate_og_settings_from_social_to_titles();

// Run after the WPSEO_Options::enrich_defaults method which has priority 99.
\add_action( 'init', [ $this, 'set_og_settings_from_seo_values' ], 100 );
}

/**
Expand Down Expand Up @@ -1171,4 +1174,77 @@ public function migrate_og_settings_from_social_to_titles() {
\update_option( 'wpseo_social', $wpseo_social );
\update_option( 'wpseo_titles', $wpseo_titles );
}

/**
* Overwrites the social options defaults with the values from the matching SEO options.
*
* @return void
*/
public function set_og_settings_from_seo_values() {
$wpseo_titles = get_option( 'wpseo_titles' );
$updated_options = [];

$options = [
'title-author-wpseo' => 'social-title-author-wpseo',
'title-archive-wpseo' => 'social-title-archive-wpseo',
'metadesc-author-wpseo' => 'social-description-author-wpseo',
'metadesc-archive-wpseo' => 'social-description-archive-wpseo',
];

$options_templates_post_types = [
'title-' => 'social-title-',
'metadesc-' => 'social-description-',
];

$options_templates_post_types_archive = [
'title-ptarchive-' => 'social-title-ptarchive-',
'metadesc-ptarchive-' => 'social-description-ptarchive-',
];

$options_templates_term_archive = [
'title-tax-' => 'social-title-tax-',
'metadesc-tax-' => 'social-description-tax-',
];

foreach ( $options as $seo => $social ) {
if ( isset( $wpseo_titles[ $seo ] ) ) {
$updated_options[ $social ] = $wpseo_titles[ $seo ];
}
}

$post_type_objects = get_post_types( [ 'public' => true ], 'objects' );

if ( $post_type_objects ) {
foreach ( $post_type_objects as $pt ) {
// Post types.
foreach ( $options_templates_post_types as $seo => $social ) {
if ( isset( $wpseo_titles[ $seo . $pt->name ] ) ) {
$updated_options[ $social . $pt->name ] = $wpseo_titles[ $seo . $pt->name ];
}
}
// Post type archives.
foreach ( $options_templates_post_types_archive as $seo_archive => $social_archive ) {
if ( isset( $wpseo_titles[ $seo_archive . $pt->name ] ) ) {
$updated_options[ $social_archive . $pt->name ] = $wpseo_titles[ $seo_archive . $pt->name ];
}
}
}
}

$taxonomy_objects = get_taxonomies( [ 'public' => true ], 'object' );

if ( $taxonomy_objects ) {
foreach ( $taxonomy_objects as $tax ) {
foreach ( $options_templates_term_archive as $seo => $social ) {
if ( isset( $wpseo_titles[ $seo . $tax->name ] ) ) {
$updated_options[ $social . $tax->name ] = $wpseo_titles[ $seo . $tax->name ];
}
}
}
}

$wpseo_titles = array_merge( $wpseo_titles, $updated_options );

update_option( 'wpseo_titles', $wpseo_titles );
}
}
32 changes: 17 additions & 15 deletions inc/options/class-wpseo-option-titles.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ class WPSEO_Option_Titles extends WPSEO_Option {
'title-search-wpseo' => '', // Text field.
'title-404-wpseo' => '', // Text field.

'social-title-author-wpseo' => '%%title%%', // Text field.
'social-title-archive-wpseo' => '%%title%%', // Text field.
'social-description-author-wpseo' => '%%excerpt%%', // Text area.
'social-description-archive-wpseo' => '%%excerpt%%', // Text area.
'social-title-author-wpseo' => '', // Text field.
'social-title-archive-wpseo' => '%%date%% %%page%% %%sep%% %%sitename%%', // Text field.
'social-description-author-wpseo' => '', // Text area.
'social-description-archive-wpseo' => '', // Text area.
'social-image-url-author-wpseo' => '', // Hidden input field.
'social-image-url-archive-wpseo' => '', // Hidden input field.
'social-image-id-author-wpseo' => '', // Hidden input field.
'social-image-id-archive-wpseo' => '', // Hidden input field.
'social-image-id-author-wpseo' => 0, // Hidden input field.
'social-image-id-archive-wpseo' => 0, // Hidden input field.

'metadesc-home-wpseo' => '', // Text area.
'metadesc-author-wpseo' => '', // Text area.
Expand Down Expand Up @@ -245,6 +245,8 @@ public function get_separator_options_for_display() {
public function translate_defaults() {
/* translators: 1: Author name; 2: Site name. */
$this->defaults['title-author-wpseo'] = sprintf( __( '%1$s, Author at %2$s', 'wordpress-seo' ), '%%name%%', '%%sitename%%' ) . ' %%page%% ';
/* translators: 1: Author name, 2: Site name. */
$this->defaults['social-title-author-wpseo'] = sprintf( __( '%1$s, Author at %2$s', 'wordpress-seo' ), '%%name%%', '%%sitename%%' ) . ' %%page%% ';
/* translators: %s expands to the search phrase. */
$this->defaults['title-search-wpseo'] = sprintf( __( 'You searched for %s', 'wordpress-seo' ), '%%searchphrase%%' ) . ' %%page%% %%sep%% %%sitename%%';
$this->defaults['title-404-wpseo'] = __( 'Page not found', 'wordpress-seo' ) . ' %%sep%% %%sitename%%';
Expand Down Expand Up @@ -291,20 +293,20 @@ public function enrich_defaults() {
$enriched_defaults[ 'post_types-' . $pt->name . '-maintax' ] = 0; // Select box.
$enriched_defaults[ 'schema-page-type-' . $pt->name ] = 'WebPage';
$enriched_defaults[ 'schema-article-type-' . $pt->name ] = ( YoastSEO()->helpers->schema->article->is_article_post_type( $pt->name ) ) ? 'Article' : 'None';
$enriched_defaults[ 'social-title-' . $pt->name ] = '%%title%%'; // Text field.
$enriched_defaults[ 'social-description-' . $pt->name ] = '%%excerpt%%'; // Text area.
$enriched_defaults[ 'social-title-' . $pt->name ] = '%%title%% %%page%% %%sep%% %%sitename%%'; // Text field.
$enriched_defaults[ 'social-description-' . $pt->name ] = ''; // Text area.
$enriched_defaults[ 'social-image-url-' . $pt->name ] = ''; // Hidden input field.
$enriched_defaults[ 'social-image-id-' . $pt->name ] = ''; // Hidden input field.
$enriched_defaults[ 'social-image-id-' . $pt->name ] = 0; // Hidden input field.

if ( ! $pt->_builtin && WPSEO_Post_Type::has_archive( $pt ) ) {
$enriched_defaults[ 'title-ptarchive-' . $pt->name ] = $archive . ' %%page%% %%sep%% %%sitename%%'; // Text field.
$enriched_defaults[ 'metadesc-ptarchive-' . $pt->name ] = ''; // Text area.
$enriched_defaults[ 'bctitle-ptarchive-' . $pt->name ] = ''; // Text field.
$enriched_defaults[ 'noindex-ptarchive-' . $pt->name ] = false;
$enriched_defaults[ 'social-title-ptarchive-' . $pt->name ] = '%%title%%'; // Text field.
$enriched_defaults[ 'social-description-ptarchive-' . $pt->name ] = '%%excerpt%%'; // Text area.
$enriched_defaults[ 'social-title-ptarchive-' . $pt->name ] = $archive . ' %%page%% %%sep%% %%sitename%%'; // Text field.
$enriched_defaults[ 'social-description-ptarchive-' . $pt->name ] = ''; // Text area.
$enriched_defaults[ 'social-image-url-ptarchive-' . $pt->name ] = ''; // Hidden input field.
$enriched_defaults[ 'social-image-id-ptarchive-' . $pt->name ] = ''; // Hidden input field.
$enriched_defaults[ 'social-image-id-ptarchive-' . $pt->name ] = 0; // Hidden input field.
}
}
}
Expand All @@ -322,10 +324,10 @@ public function enrich_defaults() {

$enriched_defaults[ 'noindex-tax-' . $tax->name ] = ( $tax->name === 'post_format' );

$enriched_defaults[ 'social-title-tax-' . $tax->name ] = '%%title%%'; // Text field.
$enriched_defaults[ 'social-description-tax-' . $tax->name ] = '%%excerpt%%'; // Text area.
$enriched_defaults[ 'social-title-tax-' . $tax->name ] = $archives . ' %%page%% %%sep%% %%sitename%%'; // Text field.
$enriched_defaults[ 'social-description-tax-' . $tax->name ] = ''; // Text area.
$enriched_defaults[ 'social-image-url-tax-' . $tax->name ] = ''; // Hidden input field.
$enriched_defaults[ 'social-image-id-tax-' . $tax->name ] = ''; // Hidden input field.
$enriched_defaults[ 'social-image-id-tax-' . $tax->name ] = 0; // Hidden input field.

if ( ! $tax->_builtin ) {
$enriched_defaults[ 'taxonomy-' . $tax->name . '-ptparent' ] = 0; // Select box;.
Expand Down
43 changes: 43 additions & 0 deletions src/generators/open-graph-image-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,33 @@ public function generate( Meta_Tags_Context $context ) {
$image_container = $backup_image_container;
}

$this->add_from_templates( $context, $image_container );
$this->add_from_default( $image_container );

return $image_container->get_images();
}

/**
* Retrieves the images for an author archive indexable.
*
* This is a custom method to address the case of Author Archives, since they always have an Open Graph image
* set in the indexable (even if it is an empty default Gravatar).
*
* @param Meta_Tags_Context $context The context.
*
* @return array The images.
*/
public function generate_for_author_archive( Meta_Tags_Context $context ) {
$image_container = $this->get_image_container();

$this->add_from_templates( $context, $image_container );
if ( $image_container->has_images() ) {
return $image_container->get_images();
}

return $this->generate( $context );
}

/**
* Adds an image based on the given indexable.
*
Expand Down Expand Up @@ -168,6 +190,27 @@ protected function add_from_default( Images $image_container ) {
}
}

/**
* Retrieves the default Open Graph image.
*
* @param Meta_Tags_Context $context The context.
* @param Images $image_container The image container.
*/
protected function add_from_templates( Meta_Tags_Context $context, Images $image_container ) {
if ( $image_container->has_images() ) {
return;
}

if ( $context->presentation->open_graph_image_id ) {
$image_container->add_image_by_id( $context->presentation->open_graph_image_id );
return;
}

if ( $context->presentation->open_graph_image ) {
$image_container->add_image_by_url( $context->presentation->open_graph_image );
}
}

/**
* Retrieves an instance of the image container.
*
Expand Down
85 changes: 85 additions & 0 deletions src/helpers/open-graph/values-helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Yoast\WP\SEO\Helpers\Open_Graph;

/**
* A helper object for the filtering of values.
*/
class Values_Helper {

/**
* Filters the Open Graph title.
*
* @param string $title The default title.
* @param string $object_type The object type.
* @param string $object_subtype The object subtype.
*
* @return string The open graph title.
*/
public function get_open_graph_title( $title, $object_type, $object_subtype ) {
/**
* Allow changing the Open Graph title.
*
* @param string $title The default title.
* @param string $object_subtype The object subtype.
*/
return \apply_filters( 'Yoast\WP\SEO\open_graph_title_' . $object_type, $title, $object_subtype );
}

/**
* Filters the Open Graph description.
*
* @param string $description The default description.
* @param string $object_type The object type.
* @param string $object_subtype The object subtype.
*
* @return string The open graph description.
*/
public function get_open_graph_description( $description, $object_type, $object_subtype ) {
/**
* Allow changing the Open Graph description.
*
* @param string $description The default description.
* @param string $object_subtype The object subtype.
*/
return \apply_filters( 'Yoast\WP\SEO\open_graph_description_' . $object_type, $description, $object_subtype );
}

/**
* Filters the Open Graph image ID.
*
* @param int $image_id The default image ID.
* @param string $object_type The object type.
* @param string $object_subtype The object subtype.
*
* @return string The open graph image ID.
*/
public function get_open_graph_image_id( $image_id, $object_type, $object_subtype ) {
/**
* Allow changing the Open Graph image ID.
*
* @param int $image_id The default image ID.
* @param string $object_subtype The object subtype.
*/
return \apply_filters( 'Yoast\WP\SEO\open_graph_image_id_' . $object_type, $image_id, $object_subtype );
}

/**
* Filters the Open Graph image URL.
*
* @param string $image The default image URL.
* @param string $object_type The object type.
* @param string $object_subtype The object subtype.
*
* @return string The open graph image URL.
*/
public function get_open_graph_image( $image, $object_type, $object_subtype ) {
/**
* Allow changing the Open Graph image URL.
*
* @param string $image The default image URL.
* @param string $object_subtype The object subtype.
*/
return \apply_filters( 'Yoast\WP\SEO\open_graph_image_' . $object_type, $image, $object_subtype );
}
}
13 changes: 13 additions & 0 deletions src/presentations/indexable-author-archive-presentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,19 @@ public function generate_open_graph_type() {
return 'profile';
}

/**
* Generates the open graph images.
*
* @return array The open graph images.
*/
public function generate_open_graph_images() {
if ( $this->context->open_graph_enabled === false ) {
return [];
}

return $this->open_graph_image_generator->generate_for_author_archive( $this->context );
}

/**
* Generates the source.
*
Expand Down
8 changes: 4 additions & 4 deletions src/presentations/indexable-post-type-presentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,17 +180,17 @@ public function generate_meta_description() {
*/
public function generate_open_graph_description() {
if ( $this->model->open_graph_description ) {
$open_graph_description = $this->model->open_graph_description;
return $this->model->open_graph_description;
}

if ( empty( $open_graph_description ) ) {
$open_graph_description = $this->meta_description;
}
$open_graph_description = $this->meta_description;

if ( empty( $open_graph_description ) ) {
$open_graph_description = $this->post->get_the_excerpt( $this->model->object_id );
}

$open_graph_description = $this->values_helper->get_open_graph_description( $open_graph_description, $this->model->object_type, $this->model->object_sub_type );

return $this->post->strip_shortcodes( $open_graph_description );
}

Expand Down

0 comments on commit 2297401

Please sign in to comment.