class-taxonomy-social-fields.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /** @var array List of social networks */
  12. protected $networks;
  13. /**
  14. * Setting the class properties
  15. *
  16. * @param stdClass|WP_Term $term The current taxonomy.
  17. */
  18. public function __construct( $term ) {
  19. parent::__construct( $term );
  20. $this->networks = $this->get_social_networks();
  21. }
  22. /**
  23. * When this method returns false, the social tab in the meta box will be hidden
  24. *
  25. * @return bool
  26. */
  27. public function show_social() {
  28. return ( WPSEO_Options::get( 'opengraph', false ) || WPSEO_Options::get( 'twitter', false ) );
  29. }
  30. /**
  31. * Gets the social meta fields by social network for the taxonomy.
  32. *
  33. * @param string $network The social network for which to fetch the fields.
  34. *
  35. * @return array
  36. */
  37. public function get_by_network( $network ) {
  38. $settings = $this->networks[ $network ];
  39. return array(
  40. $settings['network'] . '-title' => $this->get_field_config(
  41. /* translators: %s expands to the social network name */
  42. sprintf( __( '%s Title', 'wordpress-seo' ), $settings['label'] ),
  43. /* translators: %1$s expands to the social network name */
  44. 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'] ),
  45. 'text',
  46. array( 'class' => 'large-text' )
  47. ),
  48. $settings['network'] . '-description' => $this->get_field_config(
  49. /* translators: %s expands to the social network name */
  50. sprintf( __( '%s Description', 'wordpress-seo' ), $settings['label'] ),
  51. /* translators: %1$s expands to the social network name */
  52. 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'] ),
  53. 'textarea'
  54. ),
  55. $settings['network'] . '-image' => $this->get_field_config(
  56. /* translators: %s expands to the social network name */
  57. sprintf( __( '%s Image', 'wordpress-seo' ), $settings['label'] ),
  58. /* translators: %1$s expands to the social network name */
  59. 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 />' .
  60. /* translators: %1$s expands to the social network name, %2$s expands to the image size */
  61. sprintf( __( 'The recommended image size for %1$s is %2$s pixels.', 'wordpress-seo' ), $settings['label'], $settings['size'] ),
  62. 'upload'
  63. ),
  64. );
  65. }
  66. /**
  67. * Returning the fields for the social media tab
  68. *
  69. * @return array
  70. */
  71. public function get() {
  72. $fields = array();
  73. foreach ( $this->networks as $option => $settings ) {
  74. $fields_to_push = $this->get_by_network( $option );
  75. $fields = array_merge( $fields, $fields_to_push );
  76. }
  77. return $this->filter_hidden_fields( $fields );
  78. }
  79. /**
  80. * Getting array with the social networks
  81. *
  82. * @return array
  83. */
  84. private function get_social_networks() {
  85. $social_networks = array(
  86. // Source: https://developers.facebook.com/docs/sharing/best-practices#images.
  87. 'opengraph' => $this->social_network( 'opengraph', __( 'Facebook', 'wordpress-seo' ), sprintf(
  88. /* translators: %1$s expands to the image recommended width, %2$s to its height. */
  89. __( '%1$s by %2$s', 'wordpress-seo' ), '1200', '630'
  90. ) ),
  91. 'twitter' => $this->social_network( 'twitter', __( 'Twitter', 'wordpress-seo' ), sprintf(
  92. /* translators: %1$s expands to the image recommended width, %2$s to its height. */
  93. __( '%1$s by %2$s', 'wordpress-seo' ), '1024', '512'
  94. ) ),
  95. );
  96. return $this->filter_social_networks( $social_networks );
  97. }
  98. /**
  99. * Returns array with the config fields for the social network
  100. *
  101. * @param string $network The name of the social network.
  102. * @param string $label The label for the social network.
  103. * @param string $image_size The image dimensions.
  104. *
  105. * @return array
  106. */
  107. private function social_network( $network, $label, $image_size ) {
  108. return array(
  109. 'network' => $network,
  110. 'label' => $label,
  111. 'size' => $image_size,
  112. );
  113. }
  114. /**
  115. * Filter the social networks which are disabled in the configuration
  116. *
  117. * @param array $social_networks Array with the social networks that have to be filtered.
  118. *
  119. * @return array
  120. */
  121. private function filter_social_networks( array $social_networks ) {
  122. foreach ( $social_networks as $social_network => $settings ) {
  123. if ( WPSEO_Options::get( $social_network, false ) === false ) {
  124. unset( $social_networks[ $social_network ] );
  125. }
  126. }
  127. return $social_networks;
  128. }
  129. }