class-field-multiple-authors.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\ConfigurationUI
  6. */
  7. /**
  8. * Class WPSEO_Config_Field_Multiple_Authors.
  9. */
  10. class WPSEO_Config_Field_Multiple_Authors extends WPSEO_Config_Field_Choice {
  11. /**
  12. * WPSEO_Config_Field_Multiple_Authors constructor.
  13. */
  14. public function __construct() {
  15. parent::__construct( 'multipleAuthors' );
  16. $this->set_property( 'label', __( 'Does, or will, your site have multiple authors?', 'wordpress-seo' ) );
  17. $this->set_property( 'description', __( 'If you choose no, your author archives will be deactivated to prevent duplicate content issues.', 'wordpress-seo' ) );
  18. $this->add_choice( 'yes', __( 'Yes', 'wordpress-seo' ) );
  19. $this->add_choice( 'no', __( 'No', 'wordpress-seo' ) );
  20. }
  21. /**
  22. * Set adapter.
  23. *
  24. * @param WPSEO_Configuration_Options_Adapter $adapter Adapter to register lookup on.
  25. */
  26. public function set_adapter( WPSEO_Configuration_Options_Adapter $adapter ) {
  27. $adapter->add_custom_lookup(
  28. $this->get_identifier(),
  29. [ $this, 'get_data' ],
  30. [ $this, 'set_data' ]
  31. );
  32. }
  33. /**
  34. * Get the data from the stored options.
  35. *
  36. * @return null|string
  37. */
  38. public function get_data() {
  39. if ( WPSEO_Options::get( 'has_multiple_authors', false ) ) {
  40. $value = WPSEO_Options::get( 'has_multiple_authors' );
  41. }
  42. if ( ! isset( $value ) || is_null( $value ) ) {
  43. // If there are more than one users with level > 1 default to multiple authors.
  44. $user_criteria = [
  45. 'fields' => 'IDs',
  46. 'who' => 'authors',
  47. ];
  48. $users = get_users( $user_criteria );
  49. $value = count( $users ) > 1;
  50. }
  51. return ( $value ) ? 'yes' : 'no';
  52. }
  53. /**
  54. * Set the data in the options.
  55. *
  56. * @param string $data The data to set for the field.
  57. *
  58. * @return bool Returns true or false for successful storing the data.
  59. */
  60. public function set_data( $data ) {
  61. $value = ( $data === 'yes' );
  62. // Set multiple authors option.
  63. $result_multiple_authors = WPSEO_Options::set( 'has_multiple_authors', $value );
  64. /*
  65. * Set disable author archives option. When multiple authors is set to true,
  66. * the disable author option has to be false. Because of this the $value is inversed.
  67. */
  68. $result_author_archives = WPSEO_Options::set( 'disable-author', ! $value );
  69. return ( $result_multiple_authors === true && $result_author_archives === true );
  70. }
  71. }