/wp-content/plugins/wordpress-seo/admin/config-ui/fields/class-field-choice-post-type.php

https://bitbucket.org/carloskikea/helpet · PHP · 81 lines · 32 code · 13 blank · 36 comment · 1 complexity · 1159dd4542238ec26a77f5e02807fc6b MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\ConfigurationUI
  6. */
  7. /**
  8. * Class WPSEO_Config_Field_Choice_Post_Type
  9. */
  10. class WPSEO_Config_Field_Choice_Post_Type extends WPSEO_Config_Field_Choice {
  11. /** @var string Post type */
  12. protected $post_type;
  13. /**
  14. * WPSEO_Config_Field_Choice_Post_Type constructor.
  15. *
  16. * @param string $post_type The post type to add.
  17. * @param string $label Label to show (translated post type).
  18. */
  19. public function __construct( $post_type, $label ) {
  20. parent::__construct( 'postType' . ucfirst( $post_type ) );
  21. $this->post_type = $post_type;
  22. /* Translators: %1$s expands to the name of the post type. The options given to the user are "visible" and "hidden" */
  23. $this->set_property( 'label', sprintf( __( 'Search engines should show "%1$s" in search results:', 'wordpress-seo' ), $label ) );
  24. $this->add_choice( 'true', __( 'Yes', 'wordpress-seo' ) );
  25. $this->add_choice( 'false', __( 'No', 'wordpress-seo' ) );
  26. }
  27. /**
  28. * Set adapter
  29. *
  30. * @param WPSEO_Configuration_Options_Adapter $adapter Adapter to register lookup on.
  31. */
  32. public function set_adapter( WPSEO_Configuration_Options_Adapter $adapter ) {
  33. $adapter->add_custom_lookup(
  34. $this->get_identifier(),
  35. array( $this, 'get_data' ),
  36. array( $this, 'set_data' )
  37. );
  38. }
  39. /**
  40. * Get the post type of this field.
  41. *
  42. * @return string Post type.
  43. */
  44. public function get_post_type() {
  45. return $this->post_type;
  46. }
  47. /**
  48. * @return bool
  49. */
  50. public function get_data() {
  51. $key = 'noindex-' . $this->get_post_type();
  52. if ( WPSEO_Options::get( $key, false ) === false ) {
  53. return 'true';
  54. }
  55. return 'false';
  56. }
  57. /**
  58. * Set new data
  59. *
  60. * @param string $visible Visible (true) or hidden (false).
  61. *
  62. * @return bool
  63. */
  64. public function set_data( $visible ) {
  65. $post_type = $this->get_post_type();
  66. return WPSEO_Options::set( 'noindex-' . $post_type, ( $visible === 'false' ) );
  67. }
  68. }