AIOSEOのWritingAssistantPost.phpの警告が表示されないようにする方法

AIOSEOのWritingAssistantPost.phpの警告

WordPressでAIOSEO(All in One SEO)プラグインを使用していると、環境によっては新規投稿画面を表示する際に一瞬だけ画面上に以下のような警告メッセージが表示されることがあります。

Warning: Undefined property: AIOSEO\Plugin\Common\Models\WritingAssistantPost::$content_analysis in /home/iwbjp/iwb.jp/public_html/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/WritingAssistantPost.php on line 62

iwb.jpのWordPressの各バージョンは以下の通りなので、同じ環境の方は警告が表示されていると思います。

WordPress6.7.1
PHP8.2.22
AIOSEO(All in One SEO)4.7.7

AIOSEOのWarningが出る原因

この警告はAIOSEO (All in One SEO)プラグインのコード内で未定義のプロパティ $content_analysis にアクセスしようとしていることが原因です。

この問題はAIOSEOのPlugin Supportの人も把握しており、4日前に修正すると返信していましたが、現在も修正されていない状態になっています。

https://wordpress.org/support/topic/writingassistantpost-php-error

WritingAssistantPost.phpの警告を表示させない方法

プラグインが修正されるまで待てない方は、以下の手順で自分で修正できます。

まず、/wp-content/plugins/all-in-one-seo-pack/app/Common/Models/WritingAssistantPost.php にあるファイルを開きます。

そして、content_analysisのプロパティの存在チェックと初期化処理を追加します。

return isset( $post->content_analysis ) && is_object( $post->content_analysis )
	? (array) $post->content_analysis
	: [];
if ( ! $post->exists() ) {
	$post->post_id = $postId;
	$post->content_analysis = null;
}

これらの処理の追加により警告が表示されなくなります。

修正したコードの全文は以下の通りです。

WritingAssistantPost.php
<?php
namespace AIOSEO\Plugin\Common\Models;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Posts.
 *
 * @since 4.7.4
 */
class WritingAssistantPost extends Model {
	/**
	 * The name of the table in the database, without the prefix.
	 *
	 * @since 4.7.4
	 *
	 * @var string
	 */
	protected $table = 'aioseo_writing_assistant_posts';

	/**
	 * Fields that should be integer values.
	 *
	 * @since 4.7.4
	 *
	 * @var array
	 */
	protected $integerFields = [ 'id', 'post_id', 'keyword_id' ];

	/**
	 * Fields that should be boolean values.
	 *
	 * @since 4.7.4
	 *
	 * @var array
	 */
	protected $booleanFields = [];

	/**
	 * Fields that should be encoded/decoded on save/get.
	 *
	 * @since 4.7.4
	 *
	 * @var array
	 */
	protected $jsonFields = [ 'content_analysis' ];

	/**
	 * Gets a post's content analysis.
	 *
	 * @since 4.7.4
	 *
	 * @param  int   $postId A post ID.
	 * @return array         The post content's analysis.
	 */
	public static function getContentAnalysis( $postId ) {
		$post = self::getPost( $postId );

		return isset( $post->content_analysis ) && is_object( $post->content_analysis )
			? (array) $post->content_analysis
			: [];
	}

	/**
	 * Gets a writing assistant post.
	 *
	 * @since 4.7.4
	 *
	 * @param  int                  $postId A post ID.
	 * @return WritingAssistantPost         The post object.
	 */
	public static function getPost( $postId ) {
		$post = aioseo()->core->db->start( 'aioseo_writing_assistant_posts' )
			->where( 'post_id', $postId )
			->run()
			->model( 'AIOSEO\Plugin\Common\Models\WritingAssistantPost' );

			if ( ! $post->exists() ) {
				$post->post_id = $postId;
				$post->content_analysis = null;
			}

		return $post;
	}

	/**
	 * Gets a post's current keyword.
	 *
	 * @since 4.7.4
	 *
	 * @param  int                          $postId A post ID.
	 * @return WritingAssistantKeyword|bool         An attached keyword.
	 */
	public static function getKeyword( $postId ) {
		$post = self::getPost( $postId );
		if ( ! $post->exists() || empty( $post->keyword_id ) ) {
			return false;
		}

		$keyword = aioseo()->core->db->start( 'aioseo_writing_assistant_keywords' )
			->where( 'id', $post->keyword_id )
			->run()
			->model( 'AIOSEO\Plugin\Common\Models\WritingAssistantKeyword' );

		// This is here so this property is reactive in the frontend.
		if ( ! empty( $keyword->keywords ) ) {
			foreach ( $keyword->keywords as &$keyph ) {
				$keyph->contentCount = 0;
			}
		}

		// Help sorting in the frontend.
		if ( ! empty( $keyword->competitors->competitors ) ) {
			foreach ( $keyword->competitors->competitors as &$competitor ) {
				$competitor->wasAnalyzed = true;
				if ( 0 >= $competitor->wordCount ) {
					$competitor->wordCount        = 0;
					$competitor->readabilityScore = 999;
					$competitor->readabilityGrade = '';
					$competitor->gradeScore       = 0;
					$competitor->grade            = '';
					$competitor->wasAnalyzed      = false;
				}

				$competitor->readabilityScore = (float) $competitor->readabilityScore;
			}
		}

		return $keyword;
	}

	/**
	 * Return if a post has a keyword.
	 *
	 * @since 4.7.4
	 *
	 * @param  int     $postId A post ID.
	 * @return boolean         Has a keyword.
	 */
	public static function hasKeyword( $postId ) {
		$post = self::getPost( $postId );

		return (bool) $post->keyword_id;
	}

	/**
	 * Attaches a keyword to a post.
	 *
	 * @since 4.7.4
	 *
	 * @param  int  $keywordId The keyword ID.
	 * @return void
	 */
	public function attachKeyword( $keywordId ) {
		$this->keyword_id = $keywordId;
		$this->save();
	}
}

以上のコードをWritingAssistantPost.phpに貼り付ければ警告文が表示されなくなります。