WPSEO_Config_Field_Choice_Post_Type

Class WPSEO_Config_Field_Choice_Post_Type.

Defined (1)

The class is defined in the following location(s).

/admin/config-ui/fields/class-field-choice-post-type.php  
  1. class WPSEO_Config_Field_Choice_Post_Type extends WPSEO_Config_Field_Choice { 
  2.  
  3. /** @var string Post type */ 
  4. protected $post_type; 
  5.  
  6. /** 
  7. * WPSEO_Config_Field_Choice_Post_Type constructor. 
  8. * @param string $post_type The post type to add. 
  9. * @param string $label Label to show (translated post type). 
  10. */ 
  11. public function __construct( $post_type, $label ) { 
  12. parent::__construct( 'postType' . ucfirst( $post_type ) ); 
  13.  
  14. $this->post_type = $post_type; 
  15.  
  16. /** Translators: %1$s expands to the name of the post type. The options given to the user are "visible" and "hidden" */ 
  17. $this->set_property( 'label', sprintf( __( 'The post type "%1$s" should be', 'wordpress-seo' ), $label ) ); 
  18.  
  19. $this->add_choice( 'true', __( 'Visible', 'wordpress-seo' ) ); 
  20. $this->add_choice( 'false', __( 'Hidden', 'wordpress-seo' ) ); 
  21.  
  22. /** 
  23. * Set adapter 
  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. array( $this, 'get_data' ),  
  30. array( $this, 'set_data' ) 
  31. ); 
  32.  
  33. /** 
  34. * Get the post type of this field. 
  35. * @return string Post type. 
  36. */ 
  37. public function get_post_type() { 
  38. return $this->post_type; 
  39.  
  40. /** 
  41. * @return bool 
  42. */ 
  43. public function get_data() { 
  44. $option = WPSEO_Options::get_option( 'wpseo_xml' ); 
  45.  
  46. $key = 'post_types-' . $this->get_post_type() . '-not_in_sitemap'; 
  47.  
  48. $stored_data = ! isset( $option[ $key ] ) || false === $option[ $key ]; 
  49.  
  50. if ( $stored_data ) { 
  51. return 'true'; 
  52.  
  53. return 'false'; 
  54.  
  55. /** 
  56. * Set new data 
  57. * @param string $visible Visible (true) or hidden (false). 
  58. * @return bool 
  59. */ 
  60. public function set_data( $visible ) { 
  61. $post_type = $this->get_post_type(); 
  62.  
  63. $option = WPSEO_Options::get_option( 'wpseo_xml' ); 
  64.  
  65. $option[ 'post_types-' . $post_type . '-not_in_sitemap' ] = ( $visible === 'false' ); 
  66.  
  67. update_option( 'wpseo_xml', $option ); 
  68.  
  69. // Check if everything got saved properly. 
  70. $saved_option = WPSEO_Options::get_option( 'wpseo_xml' ); 
  71.  
  72. return ( ( $visible === 'false' ) && $saved_option[ 'post_types-' . $post_type . '-not_in_sitemap' ] === true );