PageRenderTime 21ms CodeModel.GetById 16ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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