class-taxonomy-social-fields.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * This class parses all the values for the social tab in the Yoast SEO settings metabox.
  9. */
  10. class WPSEO_Taxonomy_Social_Fields extends WPSEO_Taxonomy_Fields {
  11. /**
  12. * List of social networks.
  13. *
  14. * @var array
  15. */
  16. protected $networks;
  17. /**
  18. * Setting the class properties.
  19. *
  20. * @param stdClass|WP_Term $term The current taxonomy.
  21. */
  22. public function __construct( $term ) {
  23. parent::__construct( $term );
  24. $this->networks = $this->get_social_networks();
  25. }
  26. /**
  27. * When this method returns false, the social tab in the meta box will be hidden.
  28. *
  29. * @return bool
  30. */
  31. public function show_social() {
  32. return ( WPSEO_Options::get( 'opengraph', false ) || WPSEO_Options::get( 'twitter', false ) );
  33. }
  34. /**
  35. * Gets the social meta fields by social network for the taxonomy.
  36. *
  37. * @param string $network The social network for which to fetch the fields.
  38. *
  39. * @return array
  40. */
  41. public function get_by_network( $network ) {
  42. $settings = $this->networks[ $network ];
  43. return [
  44. $settings['network'] . '-title' => $this->get_field_config(
  45. /* translators: %s expands to the social network name */
  46. sprintf( __( '%s Title', 'wordpress-seo' ), $settings['label'] ),
  47. /* translators: %1$s expands to the social network name */
  48. sprintf( esc_html__( 'If you don\'t want to use the title for sharing on %1$s but instead want another title there, write it here.', 'wordpress-seo' ), $settings['label'] ),
  49. 'text',
  50. [ 'class' => 'large-text' ]
  51. ),
  52. $settings['network'] . '-description' => $this->get_field_config(
  53. /* translators: %s expands to the social network name */
  54. sprintf( __( '%s Description', 'wordpress-seo' ), $settings['label'] ),
  55. /* translators: %1$s expands to the social network name */
  56. sprintf( esc_html__( 'If you don\'t want to use the meta description for sharing on %1$s but want another description there, write it here.', 'wordpress-seo' ), $settings['label'] ),
  57. 'textarea'
  58. ),
  59. $settings['network'] . '-image' => $this->get_field_config(
  60. /* translators: %s expands to the social network name */
  61. sprintf( __( '%s Image', 'wordpress-seo' ), $settings['label'] ),
  62. /* translators: %1$s expands to the social network name */
  63. sprintf( esc_html__( 'If you want to use an image for sharing on %1$s, you can upload / choose an image or add the image URL here.', 'wordpress-seo' ), $settings['label'] ) . '<br />' .
  64. /* translators: %1$s expands to the social network name, %2$s expands to the image size */
  65. sprintf( __( 'The recommended image size for %1$s is %2$s pixels.', 'wordpress-seo' ), $settings['label'], $settings['size'] ),
  66. 'upload'
  67. ),
  68. $settings['network'] . '-image-id' => $this->get_field_config(
  69. '',
  70. '',
  71. 'hidden'
  72. ),
  73. ];
  74. }
  75. /**
  76. * Returning the fields for the social media tab.
  77. *
  78. * @return array
  79. */
  80. public function get() {
  81. $fields = [];
  82. foreach ( $this->networks as $option => $settings ) {
  83. $fields_to_push = $this->get_by_network( $option );
  84. $fields = array_merge( $fields, $fields_to_push );
  85. }
  86. return $this->filter_hidden_fields( $fields );
  87. }
  88. /**
  89. * Getting array with the social networks.
  90. *
  91. * @return array
  92. */
  93. private function get_social_networks() {
  94. // Source: https://developers.facebook.com/docs/sharing/best-practices#images.
  95. $fb_image_size = sprintf(
  96. /* translators: %1$s expands to the image recommended width, %2$s to its height. */
  97. __( '%1$s by %2$s', 'wordpress-seo' ),
  98. '1200',
  99. '630'
  100. );
  101. $twitter_image_size = sprintf(
  102. /* translators: %1$s expands to the image recommended width, %2$s to its height. */
  103. __( '%1$s by %2$s', 'wordpress-seo' ),
  104. '1024',
  105. '512'
  106. );
  107. $social_networks = [
  108. 'opengraph' => $this->social_network( 'opengraph', __( 'Facebook', 'wordpress-seo' ), $fb_image_size ),
  109. 'twitter' => $this->social_network( 'twitter', __( 'Twitter', 'wordpress-seo' ), $twitter_image_size ),
  110. ];
  111. return $this->filter_social_networks( $social_networks );
  112. }
  113. /**
  114. * Returns array with the config fields for the social network.
  115. *
  116. * @param string $network The name of the social network.
  117. * @param string $label The label for the social network.
  118. * @param string $image_size The image dimensions.
  119. *
  120. * @return array
  121. */
  122. private function social_network( $network, $label, $image_size ) {
  123. return [
  124. 'network' => $network,
  125. 'label' => $label,
  126. 'size' => $image_size,
  127. ];
  128. }
  129. /**
  130. * Filter the social networks which are disabled in the configuration.
  131. *
  132. * @param array $social_networks Array with the social networks that have to be filtered.
  133. *
  134. * @return array
  135. */
  136. private function filter_social_networks( array $social_networks ) {
  137. foreach ( $social_networks as $social_network => $settings ) {
  138. if ( WPSEO_Options::get( $social_network, false ) === false ) {
  139. unset( $social_networks[ $social_network ] );
  140. }
  141. }
  142. return $social_networks;
  143. }
  144. }