class-wpseo-options-backfill.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internals\Options
  6. */
  7. /**
  8. * Backfill the removed options.
  9. *
  10. * @since 7.0.2
  11. */
  12. class WPSEO_Options_Backfill implements WPSEO_WordPress_Integration {
  13. /** @var bool Are the filters hooked or not. */
  14. protected $hooked = false;
  15. /**
  16. * Registers all hooks to WordPress.
  17. */
  18. public function register_hooks() {
  19. // Make sure we don't hook multiple times.
  20. if ( $this->hooked ) {
  21. return;
  22. }
  23. $this->hooked = true;
  24. // Backfill options that were removed.
  25. foreach ( $this->get_lookups() as $option ) {
  26. add_filter( 'pre_option_' . $option, array( $this, 'backfill_option' ), 10, 2 );
  27. }
  28. // Make sure renamed meta key is backfilled.
  29. add_filter( 'get_user_metadata', array( $this, 'backfill_usermeta' ), 10, 3 );
  30. // Extend the options that have removed items.
  31. add_filter( 'option_wpseo_titles', array( $this, 'extend_wpseo_titles' ), 10, 1 );
  32. add_filter( 'option_wpseo', array( $this, 'extend_wpseo' ), 10, 1 );
  33. }
  34. /**
  35. * Removes the option filters.
  36. */
  37. public function remove_hooks() {
  38. // Remove backfill options filter.
  39. foreach ( $this->get_lookups() as $option ) {
  40. remove_filter( 'pre_option_' . $option, array( $this, 'backfill_option' ), 10 );
  41. }
  42. // Remove user meta filter.
  43. remove_filter( 'get_user_metadata', array( $this, 'backfill_usermeta' ), 10 );
  44. // Remove option extending filters.
  45. remove_filter( 'option_wpseo_titles', array( $this, 'extend_wpseo_titles' ), 10 );
  46. remove_filter( 'option_wpseo', array( $this, 'extend_wpseo' ), 10 );
  47. $this->hooked = false;
  48. }
  49. /**
  50. * Retrieves the options that need to be backfilled.
  51. *
  52. * @since 7.0.2
  53. *
  54. * @return array List of options that need to be backfilled.
  55. */
  56. protected function get_lookups() {
  57. return array(
  58. 'wpseo_internallinks',
  59. 'wpseo_rss',
  60. 'wpseo_xml',
  61. 'wpseo_permalinks',
  62. );
  63. }
  64. /**
  65. * Retrieves the settings for the specified option.
  66. *
  67. * @since 7.0.2
  68. *
  69. * @param string $option The option to get the settings for.
  70. *
  71. * @return array The settings for the provided option.
  72. */
  73. protected function get_settings( $option ) {
  74. $settings = array(
  75. 'wpseo' => array(
  76. 'website_name' => 'website_name',
  77. 'alternate_website_name' => 'alternate_website_name',
  78. 'company_logo' => 'company_logo',
  79. 'company_name' => 'company_name',
  80. 'company_or_person' => 'company_or_person',
  81. 'person_name' => 'person_name',
  82. ),
  83. 'wpseo_internallinks' => array(
  84. 'breadcrumbs-404crumb' => 'breadcrumbs-404crumb',
  85. 'breadcrumbs-blog-remove' => 'breadcrumbs-display-blog-page',
  86. 'breadcrumbs-boldlast' => 'breadcrumbs-boldlast',
  87. 'breadcrumbs-archiveprefix' => 'breadcrumbs-archiveprefix',
  88. 'breadcrumbs-enable' => 'breadcrumbs-enable',
  89. 'breadcrumbs-home' => 'breadcrumbs-home',
  90. 'breadcrumbs-prefix' => 'breadcrumbs-prefix',
  91. 'breadcrumbs-searchprefix' => 'breadcrumbs-searchprefix',
  92. 'breadcrumbs-sep' => 'breadcrumbs-sep',
  93. ),
  94. 'wpseo_rss' => array(
  95. 'rssbefore' => 'rssbefore',
  96. 'rssafter' => 'rssafter',
  97. ),
  98. 'wpseo_xml' => array(
  99. 'enablexmlsitemap' => 'enable_xml_sitemap',
  100. 'disable_author_sitemap' => 'noindex-author-wpseo',
  101. 'disable_author_noposts' => 'noindex-author-noposts-wpseo',
  102. ),
  103. 'wpseo_permalinks' => array(
  104. 'redirectattachment' => 'disable-attachment',
  105. 'stripcategorybase' => 'stripcategorybase',
  106. ),
  107. );
  108. if ( ! isset( $settings[ $option ] ) ) {
  109. return array();
  110. }
  111. return $settings[ $option ];
  112. }
  113. /**
  114. * Extends the WPSEO option with the removed option values.
  115. *
  116. * @since 7.0.2
  117. *
  118. * @param array $data The data of the option.
  119. *
  120. * @return array Modified data.
  121. */
  122. public function extend_wpseo( $data ) {
  123. // Make sure we don't get stuck in an infinite loop.
  124. static $running = false;
  125. // If we are already running, don't run again.
  126. if ( $running ) {
  127. return $data;
  128. }
  129. $running = true;
  130. foreach ( $this->get_settings( 'wpseo' ) as $old_key => $new_key ) {
  131. $data[ $old_key ] = WPSEO_Options::get( $new_key );
  132. }
  133. // Ended running.
  134. $running = false;
  135. return $data;
  136. }
  137. /**
  138. * Extends the WPSEO Title with removed attributes.
  139. *
  140. * @since 7.0.2
  141. *
  142. * @param array $data Data of the option.
  143. *
  144. * @return array Extended data.
  145. */
  146. public function extend_wpseo_titles( $data ) {
  147. // Make sure we don't get stuck in an infinite loop.
  148. static $running = false;
  149. // If we are already running, don't run again.
  150. if ( $running ) {
  151. return $data;
  152. }
  153. $running = true;
  154. $data['breadcrumbs-blog-remove'] = ! WPSEO_Options::get( 'breadcrumbs-display-blog-page' );
  155. $running = false;
  156. $data = $this->add_hideeditbox( $data );
  157. return $data;
  158. }
  159. /**
  160. * Backfills the options that have been removed with the current values.
  161. *
  162. * @since 7.0.2
  163. *
  164. * @param mixed $value Current value for the option.
  165. * @param string $option Name of the option.
  166. *
  167. * @return array Option data.
  168. */
  169. public function backfill_option( $value, $option ) {
  170. $output = array();
  171. foreach ( $this->get_settings( $option ) as $old_key => $new_key ) {
  172. $output[ $old_key ] = WPSEO_Options::get( $new_key );
  173. }
  174. $output = $this->apply_permalinks_settings( $output, $option );
  175. $output = $this->apply_xml_settings( $output, $option );
  176. return $output;
  177. }
  178. /**
  179. * Backfills removed user meta fields.
  180. *
  181. * @since 7.0.2
  182. *
  183. * @param mixed $value The current value.
  184. * @param int $object_id The user ID.
  185. * @param string $meta_key The meta key.
  186. *
  187. * @return mixed The backfilled value if applicable.
  188. */
  189. public function backfill_usermeta( $value, $object_id, $meta_key ) {
  190. if ( $meta_key !== 'wpseo_excludeauthorsitemap' ) {
  191. return $value;
  192. }
  193. return get_user_meta( $object_id, 'wpseo_noindex_author' );
  194. }
  195. /**
  196. * Extends the data of the option with the deprecated values.
  197. *
  198. * @since 7.0.2
  199. *
  200. * @param array $data Current data of the option.
  201. *
  202. * @return array Extended data.
  203. */
  204. protected function add_hideeditbox( $data ) {
  205. foreach ( $data as $key => $value ) {
  206. if ( strpos( $key, 'display-metabox-tax-' ) === 0 ) {
  207. $taxonomy = substr( $key, strlen( 'display-metabox-tax-' ) );
  208. $data[ 'hideeditbox-tax-' . $taxonomy ] = ! $value;
  209. continue;
  210. }
  211. if ( strpos( $key, 'display-metabox-pt-' ) === 0 ) {
  212. $post_type = substr( $key, strlen( 'display-metabox-pt-' ) );
  213. $data[ 'hideeditbox-' . $post_type ] = ! $value;
  214. continue;
  215. }
  216. }
  217. return $data;
  218. }
  219. /**
  220. * Adds the permalinks specific data to the option when requested.
  221. *
  222. * @since 7.0.2
  223. *
  224. * @param array $data Current data.
  225. * @param string $option The option that is being parsed.
  226. *
  227. * @return array Extended data.
  228. */
  229. protected function apply_permalinks_settings( $data, $option ) {
  230. if ( $option !== 'wpseo_permalinks' ) {
  231. return $data;
  232. }
  233. // Add defaults for completely removed settings in the option.
  234. return array_merge(
  235. $data,
  236. array(
  237. 'cleanpermalinks' => false,
  238. 'cleanpermalink-extravars' => '',
  239. 'cleanpermalink-googlecampaign' => false,
  240. 'cleanpermalink-googlesitesearch' => false,
  241. 'cleanreplytocom' => false,
  242. 'cleanslugs' => false,
  243. 'trailingslash' => false,
  244. )
  245. );
  246. }
  247. /**
  248. * Adds the XML specific data to the option when requested.
  249. *
  250. * @since 7.0.2
  251. *
  252. * @param array $data Current data.
  253. * @param string $option The option that is being parsed.
  254. *
  255. * @return array Extended data.
  256. */
  257. protected function apply_xml_settings( $data, $option ) {
  258. if ( $option !== 'wpseo_xml' ) {
  259. return $data;
  260. }
  261. // Add dynamic implementations for settings that are not in any option anymore.
  262. return array_merge(
  263. $data,
  264. array(
  265. 'entries-per-page' => (int) apply_filters( 'wpseo_sitemap_entries_per_page', 1000 ),
  266. 'excluded-posts' => apply_filters( 'wpseo_exclude_from_sitemap_by_post_ids', array() ),
  267. )
  268. );
  269. }
  270. }