WPSEO_Option_XML

Option: wpseo_xml.

Defined (1)

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

/inc/options/class-wpseo-option-xml.php  
  1. class WPSEO_Option_XML extends WPSEO_Option { 
  2.  
  3. /** 
  4. * @var string option name 
  5. */ 
  6. public $option_name = 'wpseo_xml'; 
  7.  
  8. /** 
  9. * @var string option group name for use in settings forms 
  10. */ 
  11. public $group_name = 'yoast_wpseo_xml_sitemap_options'; 
  12.  
  13. /** 
  14. * @var array Array of defaults for the option 
  15. * Shouldn't be requested directly, use $this->get_defaults(); 
  16. */ 
  17. protected $defaults = array( 
  18. 'disable_author_sitemap' => true,  
  19. 'disable_author_noposts' => true,  
  20. 'enablexmlsitemap' => true,  
  21. 'entries-per-page' => 1000,  
  22. 'excluded-posts' => '',  
  23.  
  24. /** 
  25. * Uses enrich_defaults to add more along the lines of: 
  26. * - 'user_role-' . $role_name . '-not_in_sitemap' => bool 
  27. * - 'post_types-' . $pt->name . '-not_in_sitemap' => bool 
  28. * - 'taxonomies-' . $tax->name . '-not_in_sitemap' => bool 
  29. */ 
  30. ); 
  31.  
  32. /** 
  33. * @var array Array of variable option name patterns for the option 
  34. */ 
  35. protected $variable_array_key_patterns = array( 
  36. 'user_role-',  
  37. 'post_types-',  
  38. 'taxonomies-',  
  39. ); 
  40.  
  41.  
  42. /** 
  43. * Add the actions and filters for the option 
  44. * @todo [JRF => testers] Check if the extra actions below would run into problems if an option 
  45. * is updated early on and if so, change the call to schedule these for a later action on add/update 
  46. * instead of running them straight away 
  47. * @return \WPSEO_Option_XML 
  48. */ 
  49. protected function __construct() { 
  50. parent::__construct(); 
  51. add_action( 'update_option_' . $this->option_name, array( 'WPSEO_Utils', 'clear_rewrites' ) ); 
  52. add_action( 'update_option_' . $this->option_name, array( 'WPSEO_Sitemaps_Cache', 'clear' ) ); 
  53.  
  54.  
  55. /** 
  56. * Get the singleton instance of this class 
  57. * @return object 
  58. */ 
  59. public static function get_instance() { 
  60. if ( ! ( self::$instance instanceof self ) ) { 
  61. self::$instance = new self(); 
  62.  
  63. return self::$instance; 
  64.  
  65. /** 
  66. * Add dynamically created default options based on available post types and taxonomies 
  67. * @return void 
  68. */ 
  69. public function enrich_defaults() { 
  70.  
  71. $user_roles = WPSEO_Utils::get_roles(); 
  72. $filtered_user_roles = apply_filters( 'wpseo_sitemaps_supported_user_roles', $user_roles ); 
  73. if ( is_array( $filtered_user_roles ) && $filtered_user_roles !== array() ) { 
  74. foreach ( $filtered_user_roles as $role_name => $role_value ) { 
  75. $this->defaults[ 'user_role-' . $role_name . '-not_in_sitemap' ] = false; 
  76.  
  77. unset( $user_role ); 
  78. unset( $role_name, $role_value ); 
  79. unset( $user_roles, $filtered_user_roles ); 
  80.  
  81. $post_type_names = get_post_types( array( 'public' => true ), 'names' ); 
  82. $filtered_post_types = apply_filters( 'wpseo_sitemaps_supported_post_types', $post_type_names ); 
  83.  
  84. if ( is_array( $filtered_post_types ) && $filtered_post_types !== array() ) { 
  85. foreach ( $filtered_post_types as $pt ) { 
  86. if ( $pt !== 'attachment' ) { 
  87. $this->defaults[ 'post_types-' . $pt . '-not_in_sitemap' ] = false; 
  88. else { 
  89. $this->defaults[ 'post_types-' . $pt . '-not_in_sitemap' ] = true; 
  90. unset( $pt ); 
  91. unset( $post_type_names, $filtered_post_types ); 
  92.  
  93. $taxonomy_objects = get_taxonomies( array( 'public' => true ), 'objects' ); 
  94. $filtered_taxonomies = apply_filters( 'wpseo_sitemaps_supported_taxonomies', $taxonomy_objects ); 
  95. if ( is_array( $filtered_taxonomies ) && $filtered_taxonomies !== array() ) { 
  96. foreach ( $filtered_taxonomies as $tax ) { 
  97. if ( isset( $tax->labels->name ) && trim( $tax->labels->name ) != '' ) { 
  98. $this->defaults[ 'taxonomies-' . $tax->name . '-not_in_sitemap' ] = false; 
  99. unset( $tax ); 
  100. unset( $taxonomy_objects, $filtered_taxonomies ); 
  101.  
  102.  
  103.  
  104. /** 
  105. * Validate the option 
  106. * @param array $dirty New value for the option. 
  107. * @param array $clean Clean value for the option, normally the defaults. 
  108. * @param array $old Old value of the option. 
  109. * @return array Validated clean value for the option to be saved to the database 
  110. */ 
  111. protected function validate_option( $dirty, $clean, $old ) { 
  112.  
  113. foreach ( $clean as $key => $value ) { 
  114. $switch_key = $this->get_switch_key( $key ); 
  115.  
  116. switch ( $switch_key ) { 
  117. /** integer fields */ 
  118. case 'entries-per-page': 
  119.  
  120. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) { 
  121.  
  122. $int = WPSEO_Utils::validate_int( $dirty[ $key ] ); 
  123.  
  124. if ( $int !== false && $int > 0 ) { 
  125.  
  126. if ( $int > 50000 ) { 
  127.  
  128. $error_message = sprintf( 
  129. /** translators: 1: number; 2: number received from user. */ 
  130. __( '"Max entries per sitemap page" should be below %1$s to meet Google\'s requirements, which %2$s is not.', 'wordpress-seo' ),  
  131. number_format_i18n( 50000 ), '<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>' 
  132. ); 
  133. add_settings_error( $this->group_name, '_' . $key, $error_message, 'error' ); 
  134.  
  135. $int = 50000; 
  136.  
  137. $clean[ $key ] = $int; 
  138. else { 
  139.  
  140. if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) { 
  141.  
  142. $int = WPSEO_Utils::validate_int( $old[ $key ] ); 
  143.  
  144. if ( $int !== false && $int > 0 ) { 
  145. $clean[ $key ] = $int; 
  146.  
  147. $error_message = sprintf( 
  148. __( '"Max entries per sitemap page" should be a positive number, which %s is not. Please correct.', 'wordpress-seo' ),  
  149. '<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>' 
  150. ); 
  151. add_settings_error( $this->group_name, '_' . $key, $error_message, 'error' ); 
  152. unset( $int ); 
  153. break; 
  154.  
  155. case 'excluded-posts' : 
  156. if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) { 
  157. if ( $filtered_array = filter_var_array( explode( ', ', $dirty[ $key ] ), FILTER_VALIDATE_INT ) ) { 
  158. $clean[ $key ] = implode( ', ', array_filter( $filtered_array, 'is_integer' ) ); 
  159.  
  160. unset( $filtered_array ); 
  161.  
  162. break; 
  163.  
  164. /** 
  165. Boolean fields 
  166. */ 
  167.  
  168. /** 
  169. Covers: 
  170. * 'disable_author_sitemap': 
  171. * 'disable_author_noposts': 
  172. * 'enablexmlsitemap': 
  173. * 'user_role-': 
  174. * 'user_role' . $role_name . '-not_in_sitemap' fields 
  175. * 'post_types-': 
  176. * 'post_types-' . $pt->name . '-not_in_sitemap' fields 
  177. * 'taxonomies-': 
  178. * 'taxonomies-' . $tax->name . '-not_in_sitemap' fields 
  179. */ 
  180. default: 
  181. $clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false ); 
  182. break; 
  183.  
  184. return $clean; 
  185.  
  186.  
  187. /** 
  188. * Clean a given option value 
  189. * @param array $option_value Old (not merged with defaults or filtered) option value to 
  190. * clean according to the rules for this option. 
  191. * @param string $current_version (optional) Version from which to upgrade, if not set,  
  192. * version specific upgrades will be disregarded. 
  193. * @param array $all_old_option_values (optional) Only used when importing old options to have 
  194. * access to the real old values, in contrast to the saved ones. 
  195. * @return array Cleaned option 
  196. */ 
  197. protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) { 
  198. /** 
  199. Make sure the values of the variable option key options are cleaned as they 
  200. may be retained and would not be cleaned/validated then 
  201. */ 
  202. if ( is_array( $option_value ) && $option_value !== array() ) { 
  203.  
  204. foreach ( $option_value as $key => $value ) { 
  205. $switch_key = $this->get_switch_key( $key ); 
  206.  
  207. // Similar to validation routine - any changes made there should be made here too. 
  208. switch ( $switch_key ) { 
  209. case 'user_role-': /** 'user_role-' . $role_name. '-not_in_sitemap' fields */ 
  210. case 'post_types-': /** 'post_types-' . $pt->name . '-not_in_sitemap' fields */ 
  211. case 'taxonomies-': /** 'taxonomies-' . $tax->name . '-not_in_sitemap' fields */ 
  212. $option_value[ $key ] = WPSEO_Utils::validate_bool( $value ); 
  213. break; 
  214.  
  215. return $option_value;