2017-11-10 11:45:43 +01:00
< ? php
/**
2018-04-16 15:29:38 +02:00
* WPSEO plugin file .
*
2017-11-10 11:45:43 +01:00
* @ package WPSEO\Admin
*/
/**
* This class adds the Social tab to the Yoast SEO metabox and makes sure the settings are saved .
*/
class WPSEO_Social_Admin extends WPSEO_Metabox {
/**
2017-12-11 16:34:19 +01:00
* Class constructor .
2017-11-10 11:45:43 +01:00
*/
public function __construct () {
self :: translate_meta_boxes ();
add_filter ( 'wpseo_save_metaboxes' , array ( $this , 'save_meta_boxes' ), 10 , 1 );
add_action ( 'wpseo_save_compare_data' , array ( $this , 'og_data_compare' ), 10 , 1 );
}
/**
2017-12-11 16:34:19 +01:00
* Translate text strings for use in the meta box .
2017-11-10 11:45:43 +01:00
*
* IMPORTANT : if you want to add a new string ( option ) somewhere , make sure you add that array key to
* the main meta box definition array in the class WPSEO_Meta () as well !!!!
*/
public static function translate_meta_boxes () {
2017-12-11 16:34:19 +01:00
/* translators: %s expands to the social network's name. */
$title_text = __ ( 'If you don\'t want to use the post title for sharing the post on %s but instead want another title there, write it here.' , 'wordpress-seo' );
/* translators: %s expands to the social network's name. */
2017-11-10 11:45:43 +01:00
$description_text = __ ( 'If you don\'t want to use the meta description for sharing the post on %s but want another description there, write it here.' , 'wordpress-seo' );
2017-12-11 16:34:19 +01:00
/* translators: %s expands to the social network's name. */
$image_text = __ ( 'If you want to override the image used on %s for this post, upload / choose an image or add the URL here.' , 'wordpress-seo' );
/* translators: %1$s expands to the social network, %2$s to the recommended image size. */
$image_size_text = __ ( 'The recommended image size for %1$s is %2$s pixels.' , 'wordpress-seo' );
2017-11-10 11:45:43 +01:00
$social_networks = array (
2018-04-16 15:29:38 +02:00
'opengraph' => __ ( 'Facebook' , 'wordpress-seo' ),
'twitter' => __ ( 'Twitter' , 'wordpress-seo' ),
2017-11-10 11:45:43 +01:00
);
// Source: https://blog.bufferapp.com/ideal-image-sizes-social-media-posts.
$recommended_image_sizes = array (
/* translators: %1$s expands to the image recommended width, %2$s to its height. */
2018-04-16 15:29:38 +02:00
'opengraph' => sprintf ( __ ( '%1$s by %2$s' , 'wordpress-seo' ), '1200' , '630' ),
// Source: https://developers.facebook.com/docs/sharing/best-practices#images.
2017-11-10 11:45:43 +01:00
/* translators: %1$s expands to the image recommended width, %2$s to its height. */
2018-04-16 15:29:38 +02:00
'twitter' => sprintf ( __ ( '%1$s by %2$s' , 'wordpress-seo' ), '1024' , '512' ),
2017-11-10 11:45:43 +01:00
);
foreach ( $social_networks as $network => $label ) {
2018-04-16 15:29:38 +02:00
if ( true === WPSEO_Options :: get ( $network , false ) ) {
2017-11-10 11:45:43 +01:00
/* translators: %s expands to the name of a social network. */
self :: $meta_fields [ 'social' ][ $network . '-title' ][ 'title' ] = sprintf ( __ ( '%s Title' , 'wordpress-seo' ), $label );
self :: $meta_fields [ 'social' ][ $network . '-title' ][ 'description' ] = sprintf ( $title_text , $label );
/* translators: %s expands to the name of a social network. */
self :: $meta_fields [ 'social' ][ $network . '-description' ][ 'title' ] = sprintf ( __ ( '%s Description' , 'wordpress-seo' ), $label );
self :: $meta_fields [ 'social' ][ $network . '-description' ][ 'description' ] = sprintf ( $description_text , $label );
/* translators: %s expands to the name of a social network. */
self :: $meta_fields [ 'social' ][ $network . '-image' ][ 'title' ] = sprintf ( __ ( '%s Image' , 'wordpress-seo' ), $label );
self :: $meta_fields [ 'social' ][ $network . '-image' ][ 'description' ] = sprintf ( $image_text , $label ) . ' ' . sprintf ( $image_size_text , $label , $recommended_image_sizes [ $network ] );
}
}
}
/**
* Returns the metabox section for the social settings .
*
* @ return WPSEO_Metabox_Tab_Section
*/
public function get_meta_section () {
2017-12-11 16:34:19 +01:00
$tabs = array ();
2017-11-10 11:45:43 +01:00
$social_meta_fields = $this -> get_meta_field_defs ( 'social' );
2017-12-11 16:34:19 +01:00
$single = true ;
2017-11-10 11:45:43 +01:00
2018-04-16 15:29:38 +02:00
$opengraph = WPSEO_Options :: get ( 'opengraph' );
$twitter = WPSEO_Options :: get ( 'twitter' );
if ( $opengraph === true && $twitter === true ) {
2017-11-10 11:45:43 +01:00
$single = null ;
}
2018-04-16 15:29:38 +02:00
if ( $opengraph === true ) {
2017-11-10 11:45:43 +01:00
$tabs [] = new WPSEO_Metabox_Form_Tab (
'facebook' ,
$this -> get_social_tab_content ( 'opengraph' , $social_meta_fields ),
'<span class="screen-reader-text">' . __ ( 'Facebook / Open Graph metadata' , 'wordpress-seo' ) . '</span><span class="dashicons dashicons-facebook-alt"></span>' ,
array (
'link_aria_label' => __ ( 'Facebook / Open Graph metadata' , 'wordpress-seo' ),
'link_class' => 'yoast-tooltip yoast-tooltip-se' ,
'single' => $single ,
)
);
}
2018-04-16 15:29:38 +02:00
if ( $twitter === true ) {
2017-11-10 11:45:43 +01:00
$tabs [] = new WPSEO_Metabox_Form_Tab (
'twitter' ,
$this -> get_social_tab_content ( 'twitter' , $social_meta_fields ),
'<span class="screen-reader-text">' . __ ( 'Twitter metadata' , 'wordpress-seo' ) . '</span><span class="dashicons dashicons-twitter"></span>' ,
array (
'link_aria_label' => __ ( 'Twitter metadata' , 'wordpress-seo' ),
'link_class' => 'yoast-tooltip yoast-tooltip-se' ,
'single' => $single ,
)
);
}
return new WPSEO_Metabox_Tab_Section (
'social' ,
'<span class="screen-reader-text">' . __ ( 'Social' , 'wordpress-seo' ) . '</span><span class="dashicons dashicons-share"></span>' ,
$tabs ,
array (
'link_aria_label' => __ ( 'Social' , 'wordpress-seo' ),
'link_class' => 'yoast-tooltip yoast-tooltip-e' ,
)
);
}
/**
* Generates the html for a social settings tab for one of the supported social media .
*
2017-12-11 16:34:19 +01:00
* @ param string $medium Medium . Can be 'opengraph' or 'twitter' .
2017-11-10 11:45:43 +01:00
* @ param array $meta_field_defs The social meta field definitions .
*
* @ return string
*/
private function get_social_tab_content ( $medium , $meta_field_defs ) {
$field_names = array (
$medium . '-title' ,
$medium . '-description' ,
$medium . '-image' ,
);
$tab_content = $this -> get_premium_notice ( $medium );
foreach ( $field_names as $field_name ) {
$tab_content .= $this -> do_meta_box ( $meta_field_defs [ $field_name ], $field_name );
}
return $tab_content ;
}
/**
* Returns the Upgrade to Premium notice .
*
* @ param string $network The social network .
2018-04-16 15:29:38 +02:00
*
2017-11-10 11:45:43 +01:00
* @ return string The notice HTML on the free version , empty string on premium .
*/
public function get_premium_notice ( $network ) {
$features = new WPSEO_Features ();
if ( $features -> is_premium () ) {
return '' ;
}
$network_name = __ ( 'Facebook' , 'wordpress-seo' );
if ( 'twitter' === $network ) {
$network_name = __ ( 'Twitter' , 'wordpress-seo' );
}
2018-04-16 15:29:38 +02:00
return sprintf (
' < div class = " notice inline yoast-notice yoast-notice-go-premium " >
< p >% 1 $s </ p >
< p >< a href = " %2 $s " target = " _blank " >% 3 $s </ a ></ p >
</ div > ' ,
sprintf (
/* translators: %1$s expands to the social network's name, %2$s to Yoast SEO Premium. */
esc_html__ ( 'Do you want to preview what it will look like if people share this post on %1$s? You can, with %2$s.' , 'wordpress-seo' ),
esc_html ( $network_name ),
2017-11-10 11:45:43 +01:00
'<strong>Yoast SEO Premium</strong>'
),
2018-04-16 15:29:38 +02:00
esc_url ( WPSEO_Shortlinker :: get ( 'https://yoa.st/179' ) ),
sprintf (
/* translators: %s expands to Yoast SEO Premium. */
esc_html__ ( 'Find out why you should upgrade to %s' , 'wordpress-seo' ),
'Yoast SEO Premium'
)
2017-11-10 11:45:43 +01:00
);
}
/**
* Filter over the meta boxes to save , this function adds the Social meta boxes .
*
* @ param array $field_defs Array of metaboxes to save .
*
* @ return array
*/
public function save_meta_boxes ( $field_defs ) {
return array_merge ( $field_defs , $this -> get_meta_field_defs ( 'social' ) );
}
/**
* This method will compare opengraph fields with the posted values .
*
* When fields are changed , the facebook cache will be purge .
*
* @ param WP_Post $post Post instance .
*/
public function og_data_compare ( $post ) {
// Check if post data is available, if post_id is set and if original post_status is publish.
// @codingStandardsIgnoreStart
if (
2017-12-11 16:34:19 +01:00
! empty ( $_POST ) && ! empty ( $post -> ID ) && $post -> post_status === 'publish' &&
2017-11-10 11:45:43 +01:00
isset ( $_POST [ 'original_post_status' ] ) && $_POST [ 'original_post_status' ] === 'publish'
) {
// @codingStandardsIgnoreEnd
$fields_to_compare = array (
'opengraph-title' ,
'opengraph-description' ,
'opengraph-image' ,
);
$reset_facebook_cache = false ;
foreach ( $fields_to_compare as $field_to_compare ) {
$old_value = self :: get_value ( $field_to_compare , $post -> ID );
$new_value = self :: get_post_value ( self :: $form_prefix . $field_to_compare );
if ( $old_value !== $new_value ) {
$reset_facebook_cache = true ;
break ;
}
}
unset ( $field_to_compare , $old_value , $new_value );
if ( $reset_facebook_cache ) {
wp_remote_get (
'https://graph.facebook.com/?id=' . get_permalink ( $post -> ID ) . '&scrape=true&method=post'
);
}
}
}
} /* End of class */