プラグインが 利用中の WordPressバージョン で動作確認済かチェックして警告する

WordPressはバージョンアップするけどプラグインはそのままなケースが割と多く見かけます。
すぐ対応(動作確認してバージョンアップ)してくれるプラグインもありますが、それ以外のプラグインは動作確認済なのかどうかはすぐにはわかりません。
たいていのプラグインは問題なく動作していますが 動作確認済かどうかは知っておきたいですよね。

そこでインストールしているプラグインが 現在利用中のWordPressバージョンで動作確認済かどうかチェックして未確認の場合は警告を表示するTipsを2つ紹介します。
 

概要

プラグインの readme.txt の「Requires at least」と「Tested up to」を読んで WordPressコアのバージョンと比較してプラグインが対応外の場合に警告を表示するようにしています。

=== my-plugin ===
Contributors: nendeb
Tags: my-plugin
Requires: my-plugin
Requires at least: 4.4
Tested up to: 4.8
Stable tag: 1.0.0
  ・
  ・

「Requires at least」は 動作に必要な最低限のWordPressバージョン で
「Tested up to」は 動作確認済の最新WordPressバージョン です。

 
 

自作のプラグイン(例えば my-plugin)だけチェックして表示する

プラグインをチェック
以下のコードを my-plugin.php に張り付けてみてください。


if ( !class_exists( 'Nendebcom_My_Plugin_WP_Version_Warnings' ) ):
/*
 * プラグインの readme.txt から WordPressバージョンをチェックして警告する
 *
 * License: GPLv2 or later
 */
Class Nendebcom_My_Plugin_WP_Version_Warnings {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_action( 'admin_init', array( $this, 'nendebcom_my_plugin_wp_version_check' ) );
	}

	/**
	 * Compare readme.txt and wordpress versions.
	 */
	public function nendebcom_my_plugin_wp_version_check() {

		global $wp_version;
		global $pagenow;

		// dashboard page onry
		if ( isset( $pagenow ) && $pagenow == 'index.php' ) {

			// readme.txt 読込
			$default_headers = array( 'requires_at_least' => 'Requires at least', 'tested_up_to' => 'Tested up to' );
			$headers = get_file_data( dirname( __FILE__ ) . '/readme.txt', $default_headers );

			$tested_up_to		= isset( $headers['tested_up_to'] )      ? $headers['tested_up_to'] : '';
			$requires_at_least	= isset( $headers['requires_at_least'] ) ? $headers['requires_at_least'] : '';

			if ( $tested_up_to || $requires_at_least ) {

				//最新WordPressバージョンは メジャーバージョンでチェックする
				$tested_up_to	= substr( $tested_up_to, 0, 3 );
				$tmp_wp_version	= substr( $wp_version, 0, 3 );

				if ( ( $requires_at_least && version_compare( $wp_version, $requires_at_least, '<' ) ) || ( $tested_up_to && version_compare( $tmp_wp_version, $tested_up_to, '>' ) ) ) {
					// admin notice to dashboard
					add_action( 'admin_notices', array( $this, 'nendebcom_my_plugin_wp_version_warning' ) );
				}
			}
		} else {

			return;
		}
	}

	/**
	 * Display administrator notice to dashboard.
	 */
	public function nendebcom_my_plugin_wp_version_warning() {
		global $wp_version;

		echo '<div id="wordpress_version_message"  class="error notice is-dismissible">';
		echo '	<p>';
		echo '	<strong>「my_plugin」は お使いのバージョン (WordPress ver' . $wp_version . ') では テストされていません。</strong>';
		echo '	</p>';
		echo '</div>';
	}

}	//Class end

//do Class
new Nendebcom_My_Plugin_WP_Version_Warnings();

endif; //class_exists

これで「my-plugin」はWordPressのバージョンでテスト済かどうかチェックしている事になります。他の自作プラグインで試す場合は以下の説明を参考にしてください。
※他の自作プラグインの場合はそれぞれの自作プラグインに設置してください。
※Class名 や 関数名が被らないように 「Nendebcom_My_Plugin_WP_Version_Warnings」の Nendebcom_My_Plugin 部分(prefix)は 設置するプラグイン名 で変更してください。
※警告表示の「my_plugin」は・・・ の所も 設置するプラグイン名に変更してください
※「//最新WordPressバージョンは メジャーバージョンでチェックする」の所はマイナバージョンだとWordPress自動アップデートにすぐひっかかるので「Tested up to」の所はマイナバージョン部分は切り捨てて比較しています。
※警告表示はダッシュボードだけにしていますが 変更したい場合は以下の例のように変更してください。【例】インストール済プラグイン(プラグイン一覧)ページ に表示

// dashboard page onry
if ( isset( $pagenow ) && $pagenow == 'index.php' ) {

 ↓

// plugins page onry
if ( isset( $pagenow ) && $pagenow == 'plugins.php' ) {

 
 

全てのプラグインをチェックして表示する

プラグインをチェック

以下のコードを my-plugin.php に張り付けてみてください。

if ( !class_exists( 'Nendebcom_ALL_Plugins_WP_Version_Warnings' ) ):
/*
 * 全てのプラグインの readme.txt から WordPressバージョンをチェックして警告する
 *
 */
Class Nendebcom_ALL_Plugins_WP_Version_Warnings {

	/**
	 * Constructor.
	 */
	public function __construct() {
		add_filter( 'plugin_row_meta', array( $this, 'nendebcom_all_plugins_wp_version_check'), 999, 2  );
	}

	/**
	 * Compare all plugins readme.txt and wordpress versions.
	 */
	public function nendebcom_all_plugins_wp_version_check( $links, $file ) {

		global $wp_version;

		//プラグインフォルダ名取得
		$pos = strpos( $file, '/' );
		if( false !== $pos ){
			$plugin_name = substr( $file , 0, $pos );
		}else{
			$plugin_name = '';	//Hello Dolly
		}

		// readme.txt 位置
		$readme_dir = WP_PLUGIN_DIR . '/' . $plugin_name . '/readme.txt';

		// readme.txt 存在チェック
		if ( empty( $plugin_name ) || !file_exists( $readme_dir ) ) {
			return $links;
		}

		// readme.txt 読込
		$default_headers = array( 'requires_at_least' => 'Requires at least', 'tested_up_to' => 'Tested up to' );
		$headers = get_file_data( $readme_dir, $default_headers );

		$tested_up_to		= isset( $headers['tested_up_to'] )      ? $headers['tested_up_to'] : '';
		$requires_at_least	= isset( $headers['requires_at_least'] ) ? $headers['requires_at_least'] : '';

		if ( $tested_up_to || $requires_at_least ) {

			//最新WordPressバージョンは メジャーバージョンでチェックする
			$tested_up_to	 = substr( $tested_up_to, 0, 3 );
			$tmp_wp_version	 = substr( $wp_version, 0, 3 );

			if ( ( $requires_at_least && version_compare( $wp_version, $requires_at_least, '<' ) ) || ( $tested_up_to && version_compare( $tmp_wp_version, $tested_up_to, '>' ) ) ) {
				$links[] = '<br>※お使いのバージョンの WordPress ではテストされていません。';
			}
		}

		return $links;
	}
}	//Class end

//do Class
new Nendebcom_ALL_Plugins_WP_Version_Warnings();

endif; //class_exists

 
 

参考

Plugin API/Action Reference/admin notices « WordPress Codex
https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices

プラグイン API/アクションフック一覧/admin init – WordPress Codex 日本語版
https://wpdocs.osdn.jp/%E3%83%97%E3%83%A9%E3%82%B0%E3%82%A4%E3%83%B3_API/%E3%82%A2%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3%E3%83%95%E3%83%83%E3%82%AF%E4%B8%80%E8%A6%A7/admin_init

get_file_data() | Function | WordPress Developer Resources
https://developer.wordpress.org/reference/functions/get_file_data/

Plugin API/Filter Reference/plugin row meta « WordPress Codex
https://codex.wordpress.org/Plugin_API/Filter_Reference/plugin_row_meta

Plugin Readmes | Plugin Developer Handbook | WordPress Developer Resources
https://developer.wordpress.org/plugins/wordpress-org/how-your-readme-txt-works/

グローバル変数 – WordPress Codex 日本語版
https://wpdocs.osdn.jp/%E3%82%B0%E3%83%AD%E3%83%BC%E3%83%90%E3%83%AB%E5%A4%89%E6%95%B0