/wp-content/plugins/wordpress-seo/admin/watchers/class-slug-change-watcher.php

https://bitbucket.org/carloskikea/helpet · PHP · 120 lines · 57 code · 17 blank · 46 comment · 8 complexity · 77342c01132fe2912d278cd7f5f1355a MD5 · raw file

  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\Watchers
  6. */
  7. /**
  8. * Class WPSEO_Slug_Change_Watcher
  9. */
  10. class WPSEO_Slug_Change_Watcher implements WPSEO_WordPress_Integration {
  11. /**
  12. * Registers all hooks to WordPress.
  13. *
  14. * @return void
  15. */
  16. public function register_hooks() {
  17. // If the current plugin is Yoast SEO Premium, stop registering.
  18. if ( WPSEO_Utils::is_yoast_seo_premium() ) {
  19. return;
  20. }
  21. // Detect a post slug change.
  22. add_action( 'post_updated', array( $this, 'detect_slug_change' ), 12, 3 );
  23. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
  24. }
  25. /**
  26. * Enqueues the quick edit handler.
  27. */
  28. public function enqueue_assets() {
  29. global $pagenow;
  30. if ( ! in_array( $pagenow, array( 'edit.php' ), true ) ) {
  31. return;
  32. }
  33. $asset_manager = new WPSEO_Admin_Asset_Manager();
  34. $asset_manager->enqueue_script( 'quick-edit-handler' );
  35. }
  36. /**
  37. * Detects if the slug changed, hooked into 'post_updated'.
  38. *
  39. * @param integer $post_id The ID of the post. Unused.
  40. * @param WP_Post $post The post with the new values.
  41. * @param WP_Post $post_before The post with the previous values.
  42. *
  43. * @return void
  44. */
  45. public function detect_slug_change( $post_id, $post, $post_before ) {
  46. // If post is a revision do not advise creating a redirect.
  47. if ( wp_is_post_revision( $post_before ) && wp_is_post_revision( $post ) ) {
  48. return;
  49. }
  50. // There is no slug change.
  51. if ( $post->post_name === $post_before->post_name ) {
  52. return;
  53. }
  54. // If the post URL wasn't visible before, or isn't visible now, don't advise creating a redirect.
  55. if ( ! $this->check_visible_post_status( $post_before->post_status ) || ! $this->check_visible_post_status( $post->post_status ) ) {
  56. return;
  57. }
  58. $post_type_object = get_post_type_object( $post->post_type );
  59. // If the post type of this post wasn't registered default back to post.
  60. if ( $post_type_object === null ) {
  61. $post_type_object = get_post_type_object( 'post' );
  62. }
  63. $this->add_notification( $post_type_object->labels->singular_name );
  64. }
  65. /**
  66. * Checks whether the given post status is visible or not.
  67. *
  68. * @param string $post_status The post status to check.
  69. *
  70. * @return bool Whether or not the post is visible.
  71. */
  72. protected function check_visible_post_status( $post_status ) {
  73. $visible_post_statuses = array(
  74. 'publish',
  75. 'static',
  76. 'private',
  77. );
  78. return in_array( $post_status, $visible_post_statuses, true );
  79. }
  80. /**
  81. * Adds a notification to be shown on the next page request since posts are updated in an ajax request.
  82. *
  83. * @param string $post_type_label The singular_name label from a post_type_object.
  84. *
  85. * @return void
  86. */
  87. protected function add_notification( $post_type_label ) {
  88. $notification = new Yoast_Notification(
  89. sprintf(
  90. /* translators: %1$s expands to the translated name of the post type, %2$s expands to the anchor opening tag, %3$s to the anchor closing tag. */
  91. __(
  92. 'You just changed the URL of this %1$s. To ensure your visitors do not see a 404 on the old URL, you should create a redirect. %2$sLearn how to create redirects here.%3$s',
  93. 'wordpress-seo'
  94. ),
  95. $post_type_label,
  96. '<a href="' . WPSEO_Shortlinker::get( 'https://yoa.st/1d0' ) . '" target="_blank">',
  97. '</a>'
  98. ), array( 'type' => 'notice-info' )
  99. );
  100. $notification_center = Yoast_Notification_Center::get();
  101. $notification_center->add_notification( $notification );
  102. }
  103. }