prefix を除いた部分)。 */ const TABLE_SUFFIX = 'acs_article_buyers'; /** 旧プラグインのディレクトリ名の接頭辞。 */ const PLUGIN_DIR_PREFIX = 'acs-woocommerce-content'; /** 併存警告を出す管理画面通知の識別子(CSS クラス用ではなくテスト用)。 */ const NOTICE_ID = 'acscw-legacy-active'; /** * フックを登録する。 */ public function __construct() { add_action( 'admin_notices', array( $this, 'render_coexistence_notice' ) ); } /* * ----------------------------------------------------------------------- * 旧テーブル * ----------------------------------------------------------------------- */ /** * 旧購入者テーブルの物理名を返す。 * * @return string {prefix}acs_article_buyers */ public static function get_table() { global $wpdb; return $wpdb->prefix . self::TABLE_SUFFIX; } /** * 旧購入者テーブルが存在するかどうか。 * * @return bool */ public static function table_exists() { global $wpdb; $table = self::get_table(); // テーブル名の _ は LIKE のワイルドカードなのでエスケープする。 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange $found = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ); return $table === $found; } /** * 旧購入者テーブルの行数を返す。 * * @return int テーブルが無ければ 0。 */ public static function count_rows() { global $wpdb; if ( ! self::table_exists() ) { return 0; } // テーブル名は $wpdb->prefix から自前で組み立てた識別子で、外部入力を含まない。 $table = self::get_table(); $sql = "SELECT COUNT(*) FROM `{$table}`"; // phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- Table name is a fixed value from ACSCW_Install, not user input; every value is bound through $wpdb->prepare(). Direct query is required and results are intentionally uncached. return (int) $wpdb->get_var( $sql ); // phpcs:enable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared } /* * ----------------------------------------------------------------------- * 旧プラグインの有効判定 * ----------------------------------------------------------------------- */ /** * 有効になっている旧プラグインの basename 一覧を返す。 * * is_plugin_active() は wp-admin/includes/plugin.php に依存するため使わない。 * option を直接読む方が、読み込み順に左右されず確実。 * * @return string[] 例: array( 'acs-woocommerce-content/acs-woocommerce-content.php' ) */ public static function get_active_legacy_plugins() { $active = (array) get_option( 'active_plugins', array() ); if ( function_exists( 'is_multisite' ) && is_multisite() ) { // サイト全体で有効化されたプラグインはキーが basename。 $network = (array) get_site_option( 'active_sitewide_plugins', array() ); $active = array_merge( $active, array_keys( $network ) ); } $found = array(); foreach ( $active as $basename ) { $basename = (string) $basename; if ( defined( 'ACSCW_PLUGIN_BASENAME' ) && ACSCW_PLUGIN_BASENAME === $basename ) { // 自分自身は当然除く。 continue; } if ( ! self::is_legacy_basename( $basename ) ) { continue; } $found[] = $basename; } return array_values( array_unique( $found ) ); } /** * basename が旧プラグインのものかどうか。 * * `acs-woocommerce-content/` と `acs-woocommerce-content-pro/` のような * 派生ディレクトリの両方を拾う。新スラッグ * `acs-content-for-woocommerce` には**一致しない**(語順が違う)。 * * @param string $basename plugin_basename() 形式の識別子。 * @return bool */ public static function is_legacy_basename( $basename ) { $basename = (string) $basename; if ( false === strpos( $basename, '/' ) ) { return false; } $dir = substr( $basename, 0, strpos( $basename, '/' ) ); if ( self::PLUGIN_DIR_PREFIX === $dir ) { return true; } return 0 === strpos( $dir, self::PLUGIN_DIR_PREFIX . '-' ); } /** * 旧プラグインが有効かどうか。 * * @return bool */ public static function is_plugin_active() { $active = self::get_active_legacy_plugins(); return ! empty( $active ); } /* * ----------------------------------------------------------------------- * 併存警告 * ----------------------------------------------------------------------- */ /** * 旧プラグインが有効なら警告を出す。 * * 出す条件は2つとも満たすときだけ。 * - 旧プラグインが有効 * - 閲覧者がプラグインを無効化できる権限を持つ(できない人に見せても操作できない) * * @return void */ public function render_coexistence_notice() { if ( ! current_user_can( 'activate_plugins' ) ) { return; } if ( ! self::is_plugin_active() ) { return; } printf( '
%2$s