WPSEO_Option_InternalLinks

Option: wpseo_internallinks.

Defined (1)

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

/inc/options/class-wpseo-option-internallinks.php  
  1. class WPSEO_Option_InternalLinks extends WPSEO_Option { 
  2.  
  3. /** 
  4. * @var string option name 
  5. */ 
  6. public $option_name = 'wpseo_internallinks'; 
  7.  
  8. /** 
  9. * @var array Array of defaults for the option 
  10. * Shouldn't be requested directly, use $this->get_defaults(); 
  11. * @internal Note: Some of the default values are added via the translate_defaults() method 
  12. */ 
  13. protected $defaults = array( 
  14. 'breadcrumbs-404crumb' => '', // Text field. 
  15. 'breadcrumbs-blog-remove' => false,  
  16. 'breadcrumbs-boldlast' => false,  
  17. 'breadcrumbs-archiveprefix' => '', // Text field. 
  18. 'breadcrumbs-enable' => false,  
  19. 'breadcrumbs-home' => '', // Text field. 
  20. 'breadcrumbs-prefix' => '', // Text field. 
  21. 'breadcrumbs-searchprefix' => '', // Text field. 
  22. 'breadcrumbs-sep' => '»', // Text field. 
  23.  
  24. /** 
  25. * Uses enrich_defaults() to add more along the lines of: 
  26. * - 'post_types-' . $pt->name . '-maintax' => 0 / string 
  27. * - 'taxonomy-' . $tax->name . '-ptparent' => 0 / string 
  28. */ 
  29. ); 
  30.  
  31. /** 
  32. * @var array Array of variable option name patterns for the option 
  33. */ 
  34. protected $variable_array_key_patterns = array( 
  35. 'post_types-',  
  36. 'taxonomy-',  
  37. ); 
  38.  
  39.  
  40. /** 
  41. * Get the singleton instance of this class 
  42. * @return object 
  43. */ 
  44. public static function get_instance() { 
  45. if ( ! ( self::$instance instanceof self ) ) { 
  46. self::$instance = new self(); 
  47.  
  48. return self::$instance; 
  49.  
  50.  
  51. /** 
  52. * Translate strings used in the option defaults 
  53. * @return void 
  54. */ 
  55. public function translate_defaults() { 
  56. $this->defaults['breadcrumbs-404crumb'] = __( 'Error 404: Page not found', 'wordpress-seo' ); 
  57. $this->defaults['breadcrumbs-archiveprefix'] = __( 'Archives for', 'wordpress-seo' ); 
  58. $this->defaults['breadcrumbs-home'] = __( 'Home', 'wordpress-seo' ); 
  59. $this->defaults['breadcrumbs-searchprefix'] = __( 'You searched for', 'wordpress-seo' ); 
  60.  
  61.  
  62. /** 
  63. * Add dynamically created default options based on available post types and taxonomies 
  64. * @return void 
  65. */ 
  66. public function enrich_defaults() { 
  67.  
  68. // Retrieve all the relevant post type and taxonomy arrays. 
  69. $post_type_names = get_post_types( array( 'public' => true ), 'names' ); 
  70. $taxonomy_names_custom = get_taxonomies( array( 'public' => true, '_builtin' => false ), 'names' ); 
  71.  
  72. if ( $post_type_names !== array() ) { 
  73. foreach ( $post_type_names as $pt ) { 
  74. $pto_taxonomies = get_object_taxonomies( $pt, 'names' ); 
  75. if ( $pto_taxonomies !== array() ) { 
  76. $this->defaults[ 'post_types-' . $pt . '-maintax' ] = 0; // Select box. 
  77. unset( $pto_taxonomies ); 
  78. unset( $pt ); 
  79.  
  80. if ( $taxonomy_names_custom !== array() ) { 
  81. foreach ( $taxonomy_names_custom as $tax ) { 
  82. $this->defaults[ 'taxonomy-' . $tax . '-ptparent' ] = 0; // Select box;. 
  83. unset( $tax ); 
  84.  
  85.  
  86. /** 
  87. * Validate the option 
  88. * @param array $dirty New value for the option. 
  89. * @param array $clean Clean value for the option, normally the defaults. 
  90. * @param array $old Old value of the option. 
  91. * @return array Validated clean value for the option to be saved to the database 
  92. */ 
  93. protected function validate_option( $dirty, $clean, $old ) { 
  94.  
  95. $allowed_post_types = $this->get_allowed_post_types(); 
  96.  
  97. foreach ( $clean as $key => $value ) { 
  98.  
  99. $switch_key = $this->get_switch_key( $key ); 
  100.  
  101. switch ( $switch_key ) { 
  102. /** text fields */ 
  103. case 'breadcrumbs-404crumb': 
  104. case 'breadcrumbs-archiveprefix': 
  105. case 'breadcrumbs-home': 
  106. case 'breadcrumbs-prefix': 
  107. case 'breadcrumbs-searchprefix': 
  108. case 'breadcrumbs-sep': 
  109. if ( isset( $dirty[ $key ] ) ) { 
  110. $clean[ $key ] = wp_kses_post( $dirty[ $key ] ); 
  111. break; 
  112.  
  113.  
  114. /** 'post_types-' . $pt->name . '-maintax' fields */ 
  115. case 'post_types-': 
  116. $post_type = str_replace( array( 'post_types-', '-maintax' ), '', $key ); 
  117. $taxonomies = get_object_taxonomies( $post_type, 'names' ); 
  118.  
  119. if ( isset( $dirty[ $key ] ) ) { 
  120. if ( $taxonomies !== array() && in_array( $dirty[ $key ], $taxonomies, true ) ) { 
  121. $clean[ $key ] = $dirty[ $key ]; 
  122. elseif ( (string) $dirty[ $key ] === '0' || (string) $dirty[ $key ] === '' ) { 
  123. $clean[ $key ] = 0; 
  124. elseif ( sanitize_title_with_dashes( $dirty[ $key ] ) === $dirty[ $key ] ) { 
  125. // Allow taxonomies which may not be registered yet. 
  126. $clean[ $key ] = $dirty[ $key ]; 
  127. else { 
  128. if ( isset( $old[ $key ] ) ) { 
  129. $clean[ $key ] = sanitize_title_with_dashes( $old[ $key ] ); 
  130. if ( function_exists( 'add_settings_error' ) ) { 
  131. /** 
  132. * @todo [JRF => whomever] maybe change the untranslated $pt name in the 
  133. * error message to the nicely translated label ? 
  134. */ 
  135. add_settings_error( 
  136. $this->group_name, // Slug title of the setting. 
  137. '_' . $key, // Suffix-id for the error message box. 
  138. sprintf( __( 'Please select a valid taxonomy for post type "%s"', 'wordpress-seo' ), $post_type ), // The error message. 
  139. 'error' // Error type, either 'error' or 'updated'. 
  140. ); 
  141. elseif ( isset( $old[ $key ] ) ) { 
  142. $clean[ $key ] = sanitize_title_with_dashes( $old[ $key ] ); 
  143. unset( $taxonomies, $post_type ); 
  144. break; 
  145.  
  146.  
  147. /** 'taxonomy-' . $tax->name . '-ptparent' fields */ 
  148. case 'taxonomy-': 
  149. if ( isset( $dirty[ $key ] ) ) { 
  150. if ( $allowed_post_types !== array() && in_array( $dirty[ $key ], $allowed_post_types, true ) ) { 
  151. $clean[ $key ] = $dirty[ $key ]; 
  152. elseif ( (string) $dirty[ $key ] === '0' || (string) $dirty[ $key ] === '' ) { 
  153. $clean[ $key ] = 0; 
  154. elseif ( sanitize_key( $dirty[ $key ] ) === $dirty[ $key ] ) { 
  155. // Allow taxonomies which may not be registered yet. 
  156. $clean[ $key ] = $dirty[ $key ]; 
  157. else { 
  158. if ( isset( $old[ $key ] ) ) { 
  159. $clean[ $key ] = sanitize_key( $old[ $key ] ); 
  160. if ( function_exists( 'add_settings_error' ) ) { 
  161. /** 
  162. * @todo [JRF =? whomever] maybe change the untranslated $tax name in the 
  163. * error message to the nicely translated label ? 
  164. */ 
  165. $tax = str_replace( array( 'taxonomy-', '-ptparent' ), '', $key ); 
  166. add_settings_error( 
  167. $this->group_name, // Slug title of the setting. 
  168. '_' . $tax, // Suffix-id for the error message box. 
  169. sprintf( __( 'Please select a valid post type for taxonomy "%s"', 'wordpress-seo' ), $tax ), // The error message. 
  170. 'error' // Error type, either 'error' or 'updated'. 
  171. ); 
  172. unset( $tax ); 
  173. elseif ( isset( $old[ $key ] ) ) { 
  174. $clean[ $key ] = sanitize_key( $old[ $key ] ); 
  175. break; 
  176.  
  177.  
  178. /** 
  179. Boolean fields 
  180. */ 
  181.  
  182. /** 
  183. Covers: 
  184. * 'breadcrumbs-blog-remove' 
  185. * 'breadcrumbs-boldlast' 
  186. * 'breadcrumbs-enable' 
  187. */ 
  188. default: 
  189. $clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false ); 
  190. break; 
  191.  
  192. return $clean; 
  193.  
  194.  
  195. /** 
  196. * Retrieve a list of the allowed post types as breadcrumb parent for a taxonomy 
  197. * Helper method for validation 
  198. * @internal don't make static as new types may still be registered 
  199. * @return array 
  200. */ 
  201. protected function get_allowed_post_types() { 
  202. $allowed_post_types = array(); 
  203.  
  204. $post_types = get_post_types( array( 'public' => true ), 'objects' ); 
  205.  
  206. if ( get_option( 'show_on_front' ) == 'page' && get_option( 'page_for_posts' ) > 0 ) { 
  207. $allowed_post_types[] = 'post'; 
  208.  
  209. if ( is_array( $post_types ) && $post_types !== array() ) { 
  210. foreach ( $post_types as $type ) { 
  211. if ( $type->has_archive ) { 
  212. $allowed_post_types[] = $type->name; 
  213.  
  214. return $allowed_post_types; 
  215.  
  216.  
  217. /** 
  218. * Clean a given option value 
  219. * @param array $option_value Old (not merged with defaults or filtered) option value to 
  220. * clean according to the rules for this option. 
  221. * @param string $current_version (optional) Version from which to upgrade, if not set,  
  222. * version specific upgrades will be disregarded. 
  223. * @param array $all_old_option_values (optional) Only used when importing old options to have 
  224. * access to the real old values, in contrast to the saved ones. 
  225. * @return array Cleaned option 
  226. */ 
  227. protected function clean_option( $option_value, $current_version = null, $all_old_option_values = null ) { 
  228.  
  229. /** Make sure the old fall-back defaults for empty option keys are now added to the option */ 
  230. if ( isset( $current_version ) && version_compare( $current_version, '1.5.2.3', '<' ) ) { 
  231. if ( has_action( 'init', array( 'WPSEO_Options', 'bring_back_breadcrumb_defaults' ) ) === false ) { 
  232. add_action( 'init', array( 'WPSEO_Options', 'bring_back_breadcrumb_defaults' ), 3 ); 
  233.  
  234. /** 
  235. Make sure the values of the variable option key options are cleaned as they 
  236. may be retained and would not be cleaned/validated then 
  237. */ 
  238. if ( is_array( $option_value ) && $option_value !== array() ) { 
  239.  
  240. $allowed_post_types = $this->get_allowed_post_types(); 
  241.  
  242. foreach ( $option_value as $key => $value ) { 
  243. $switch_key = $this->get_switch_key( $key ); 
  244.  
  245. // Similar to validation routine - any changes made there should be made here too. 
  246. switch ( $switch_key ) { 
  247. /** 'post_types-' . $pt->name . '-maintax' fields */ 
  248. case 'post_types-': 
  249. $post_type = str_replace( array( 'post_types-', '-maintax' ), '', $key ); 
  250. $taxonomies = get_object_taxonomies( $post_type, 'names' ); 
  251.  
  252. if ( $taxonomies !== array() && in_array( $value, $taxonomies, true ) ) { 
  253. $option_value[ $key ] = $value; 
  254. elseif ( (string) $value === '0' || (string) $value === '' ) { 
  255. $option_value[ $key ] = 0; 
  256. elseif ( sanitize_title_with_dashes( $value ) === $value ) { 
  257. // Allow taxonomies which may not be registered yet. 
  258. $option_value[ $key ] = $value; 
  259. unset( $taxonomies, $post_type ); 
  260. break; 
  261.  
  262.  
  263. /** 'taxonomy-' . $tax->name . '-ptparent' fields */ 
  264. case 'taxonomy-': 
  265. if ( $allowed_post_types !== array() && in_array( $value, $allowed_post_types, true ) ) { 
  266. $option_value[ $key ] = $value; 
  267. elseif ( (string) $value === '0' || (string) $value === '' ) { 
  268. $option_value[ $key ] = 0; 
  269. elseif ( sanitize_key( $option_value[ $key ] ) === $option_value[ $key ] ) { 
  270. // Allow post types which may not be registered yet. 
  271. $option_value[ $key ] = $value; 
  272. break; 
  273.  
  274. return $option_value; 
  275.  
  276. /** 
  277. * With the changes to v1.5, the defaults for some of the textual breadcrumb settings are added 
  278. * dynamically, but empty strings are allowed. 
  279. * This caused issues for people who left the fields empty on purpose relying on the defaults. 
  280. * This little routine fixes that. 
  281. * Needs to be run on 'init' hook at prio 3 to make sure the defaults are translated. 
  282. */ 
  283. public function bring_back_defaults() { 
  284. $option = get_option( $this->option_name ); 
  285.  
  286. $values_to_bring_back = array( 
  287. 'breadcrumbs-404crumb',  
  288. 'breadcrumbs-archiveprefix',  
  289. 'breadcrumbs-home',  
  290. 'breadcrumbs-searchprefix',  
  291. 'breadcrumbs-sep',  
  292. ); 
  293. foreach ( $values_to_bring_back as $key ) { 
  294. if ( $option[ $key ] === '' && $this->defaults[ $key ] !== '' ) { 
  295. $option[ $key ] = $this->defaults[ $key ]; 
  296. update_option( $this->option_name, $option );