WPSEO_Social_Admin

This class adds the Social tab to the Yoast SEO metabox and makes sure the settings are saved.

Defined (1)

The class is defined in the following location(s).

/admin/class-social-admin.php  
  1. class WPSEO_Social_Admin extends WPSEO_Metabox { 
  2.  
  3. /** 
  4. * @var array 
  5. */ 
  6. private $options; 
  7.  
  8. /** 
  9. * Class constructor 
  10. */ 
  11. public function __construct() { 
  12. $this->options = WPSEO_Options::get_option( 'wpseo_social' ); 
  13. self::translate_meta_boxes(); 
  14. add_filter( 'wpseo_save_metaboxes', array( $this, 'save_meta_boxes' ), 10, 1 ); 
  15. add_action( 'wpseo_save_compare_data', array( $this, 'og_data_compare' ), 10, 1 ); 
  16.  
  17. /** 
  18. * Translate text strings for use in the meta box 
  19. * IMPORTANT: if you want to add a new string (option) somewhere, make sure you add that array key to 
  20. * the main meta box definition array in the class WPSEO_Meta() as well!!!! 
  21. */ 
  22. public static function translate_meta_boxes() { 
  23. /** translators: %s expands to the social network's name */ 
  24. $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' ); 
  25. /** translators: %s expands to the social network's name */ 
  26. $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' ); 
  27. /** translators: %s expands to the social network's name */ 
  28. $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' ); 
  29. /** translators: %1$s expands to the social network, %2$s to the recommended image size */ 
  30. $image_size_text = __( 'The recommended image size for %1$s is %2$s pixels.', 'wordpress-seo' ); 
  31.  
  32. $options = WPSEO_Options::get_option( 'wpseo_social' ); 
  33.  
  34. $social_networks = array( 
  35. 'opengraph' => __( 'Facebook', 'wordpress-seo' ),  
  36. 'twitter' => __( 'Twitter', 'wordpress-seo' ),  
  37. ); 
  38.  
  39. // Source: https://blog.bufferapp.com/ideal-image-sizes-social-media-posts. 
  40. $recommended_image_sizes = array( 
  41. /** translators: %1$s expands to the image recommended width, %2$s to its height. */ 
  42. 'opengraph' => sprintf( __( '%1$s by %2$s', 'wordpress-seo' ), '1200', '630' ), // Source: https://developers.facebook.com/docs/sharing/best-practices#images. 
  43. /** translators: %1$s expands to the image recommended width, %2$s to its height. */ 
  44. 'twitter' => sprintf( __( '%1$s by %2$s', 'wordpress-seo' ), '1024', '512' ),  
  45. ); 
  46.  
  47. foreach ( $social_networks as $network => $label ) { 
  48. if ( true === $options[ $network ] ) { 
  49.  
  50. self::$meta_fields['social'][ $network . '-title' ]['title'] = sprintf( __( '%s Title', 'wordpress-seo' ), $label ); 
  51. self::$meta_fields['social'][ $network . '-title' ]['description'] = sprintf( $title_text, $label ); 
  52.  
  53. self::$meta_fields['social'][ $network . '-description' ]['title'] = sprintf( __( '%s Description', 'wordpress-seo' ), $label ); 
  54. self::$meta_fields['social'][ $network . '-description' ]['description'] = sprintf( $description_text, $label ); 
  55.  
  56. self::$meta_fields['social'][ $network . '-image' ]['title'] = sprintf( __( '%s Image', 'wordpress-seo' ), $label ); 
  57. self::$meta_fields['social'][ $network . '-image' ]['description'] = sprintf( $image_text, $label ) . ' ' . sprintf( $image_size_text, $label, $recommended_image_sizes[ $network ] ); 
  58.  
  59. /** 
  60. * Returns the metabox section for the social settings. 
  61. * @return WPSEO_Metabox_Tab_Section 
  62. */ 
  63. public function get_meta_section() { 
  64. $tabs = array(); 
  65. $social_meta_fields = $this->get_meta_field_defs( 'social' ); 
  66. $single = true; 
  67.  
  68. if ( $this->options['opengraph'] === true && $this->options['twitter'] === true ) { 
  69. $single = null; 
  70.  
  71. if ( $this->options['opengraph'] === true ) { 
  72. $tabs[] = new WPSEO_Metabox_Form_Tab( 
  73. 'facebook',  
  74. $this->get_social_tab_content( 'opengraph', $social_meta_fields ),  
  75. '<span class="screen-reader-text">' . __( 'Facebook / Open Graph metadata', 'wordpress-seo' ) . '</span><span class="dashicons dashicons-facebook-alt"></span>',  
  76. array( 
  77. 'link_aria_label' => __( 'Facebook / Open Graph metadata', 'wordpress-seo' ),  
  78. 'link_class' => 'yoast-tooltip yoast-tooltip-se',  
  79. 'single' => $single,  
  80. ); 
  81.  
  82. if ( $this->options['twitter'] === true ) { 
  83. $tabs[] = new WPSEO_Metabox_Form_Tab( 
  84. 'twitter',  
  85. $this->get_social_tab_content( 'twitter', $social_meta_fields ),  
  86. '<span class="screen-reader-text">' . __( 'Twitter metadata', 'wordpress-seo' ) . '</span><span class="dashicons dashicons-twitter"></span>',  
  87. array( 
  88. 'link_aria_label' => __( 'Twitter metadata', 'wordpress-seo' ),  
  89. 'link_class' => 'yoast-tooltip yoast-tooltip-se',  
  90. 'single' => $single,  
  91. ); 
  92.  
  93. return new WPSEO_Metabox_Tab_Section( 
  94. 'social',  
  95. '<span class="screen-reader-text">' . __( 'Social', 'wordpress-seo' ) . '</span><span class="dashicons dashicons-share"></span>',  
  96. $tabs,  
  97. array( 
  98. 'link_aria_label' => __( 'Social', 'wordpress-seo' ),  
  99. 'link_class' => 'yoast-tooltip yoast-tooltip-e',  
  100. ); 
  101.  
  102. /** 
  103. * Generates the html for a social settings tab for one of the supported social media. 
  104. * @param string $medium can be 'opengraph' or 'twitter'. 
  105. * @param array $meta_field_defs The social meta field definitions. 
  106. * @return string 
  107. */ 
  108. private function get_social_tab_content( $medium, $meta_field_defs ) { 
  109. $field_names = array( 
  110. $medium . '-title',  
  111. $medium . '-description',  
  112. $medium . '-image',  
  113. ); 
  114.  
  115. $tab_content = ''; 
  116.  
  117. foreach ( $field_names as $field_name ) { 
  118. $tab_content .= $this->do_meta_box( $meta_field_defs[ $field_name ], $field_name ); 
  119.  
  120. return $tab_content; 
  121.  
  122. /** 
  123. * Filter over the meta boxes to save, this function adds the Social meta boxes. 
  124. * @param array $field_defs Array of metaboxes to save. 
  125. * @return array 
  126. */ 
  127. public function save_meta_boxes( $field_defs ) { 
  128. return array_merge( $field_defs, $this->get_meta_field_defs( 'social' ) ); 
  129.  
  130. /** 
  131. * This method will compare opengraph fields with the posted values. 
  132. * When fields are changed, the facebook cache will be purge. 
  133. * @param WP_Post $post Post instance. 
  134. */ 
  135. public function og_data_compare( $post ) { 
  136.  
  137. // Check if post data is available, if post_id is set and if original post_status is publish. 
  138. if ( 
  139. ! empty( $_POST ) && ! empty( $post->ID ) && $post->post_status == 'publish' && 
  140. isset( $_POST['original_post_status'] ) && $_POST['original_post_status'] === 'publish' 
  141. ) { 
  142.  
  143. $fields_to_compare = array( 
  144. 'opengraph-title',  
  145. 'opengraph-description',  
  146. 'opengraph-image',  
  147. ); 
  148.  
  149. $reset_facebook_cache = false; 
  150.  
  151. foreach ( $fields_to_compare as $field_to_compare ) { 
  152. $old_value = self::get_value( $field_to_compare, $post->ID ); 
  153. $new_value = self::get_post_value( self::$form_prefix . $field_to_compare ); 
  154.  
  155. if ( $old_value !== $new_value ) { 
  156. $reset_facebook_cache = true; 
  157. break; 
  158. unset( $field_to_compare, $old_value, $new_value ); 
  159.  
  160. if ( $reset_facebook_cache ) { 
  161. wp_remote_get( 
  162. 'https://graph.facebook.com/?id=' . get_permalink( $post->ID ) . '&scrape=true&method=post' 
  163. ); 
  164.  
  165.  
  166. /********************** DEPRECATED METHODS **********************/ 
  167.  
  168. // @codeCoverageIgnoreStart 
  169. /** 
  170. * Define the meta boxes for the Social tab 
  171. * @deprecated 1.5.0 
  172. * @deprecated use WPSEO_Meta::get_meta_field_defs() 
  173. * @see WPSEO_Meta::get_meta_field_defs() 
  174. * @param string $post_type Optional post type string. 
  175. * @return array Array containing the meta boxes 
  176. */ 
  177. public function get_meta_boxes( $post_type = 'post' ) { 
  178. _deprecated_function( __METHOD__, 'WPSEO 1.5.0', 'WPSEO_Meta::get_meta_field_defs()' ); 
  179.  
  180. return $this->get_meta_field_defs( 'social' ); 
  181.  
  182. /** 
  183. * @deprecated 3.0 Removed. 
  184. * @return string 
  185. */ 
  186. public function tab_header() { 
  187. _deprecated_function( __METHOD__, 'WPSEO 3.0' ); 
  188.  
  189. return ''; 
  190.  
  191. /** 
  192. * @deprecated 3.0 Removed. 
  193. * @return string 
  194. */ 
  195. public function tab_content() { 
  196. _deprecated_function( __METHOD__, 'WPSEO 3.0' ); 
  197.  
  198. return ''; 
  199. // @codeCoverageIgnoreEnd 
  200. } /** End of class */