class-social-admin.php 8.5 KB

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