PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wordpress-seo/inc/options/class-wpseo-option-xml.php

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