プラグインActivityPub(7.6.1)の私的修正箇所

のプラグイン の #バグ を解消するため、テーマファイルエディターのfunctions.phpではなく、プラグインのコードを直接編集してあるので、ver.7.6.1 の段階での編集箇所をまとめて記載しておく。

 まず、 で表示される日本語の #ハッシュタグ が「エンコード文字列から%が抜けた文字列」になってしまう #バグ への対処に関して。

/wp-content/plugins/activitypub/includes/functions.php

/**
 * Escapes a Tag, to be used as a hashtag.
 *
 * @param string $input The string to escape.
 *
 * @return string The escaped hashtag.
 */
function esc_hashtag( $input ) {

	$hashtag = \wp_specialchars_decode( $input, ENT_QUOTES );
	// Remove all characters that are not letters, numbers, or hyphens.
	$hashtag = \preg_replace( '/emoji-regex(*SKIP)(?!)|[^\p{L}\p{Nd}\-%]+/u', '-', $hashtag );

	// Capitalize every letter that is preceded by a hyphen.
	$hashtag = preg_replace_callback(
		'/-+(.)/',
		function ( $matches ) {
			return strtoupper( $matches[1] );
		},
		$hashtag
	);

	// Add a hashtag to the beginning of the string.
	$hashtag = ltrim( $hashtag, '#' );
	$hashtag = trim( $hashtag, '-' );
	$hashtag = '#' . $hashtag;

	/**
	 * Allow defining your own custom hashtag generation rules.
	 *
	 * @param string $hashtag The hashtag to be returned.
	 * @param string $input   The original string.
	 */
	$hashtag = apply_filters( 'activitypub_esc_hashtag', $hashtag, $input );
$hashtag = urldecode( $hashtag );
	return esc_html( $hashtag );
}
修正行の元のコード:
$hashtag = \preg_replace( '/emoji-regex(*SKIP)(?!)|[^\p{L}\p{Nd}-]+/u', '-', $hashtag );
追加したコード:
$hashtag = urldecode( $hashtag );

 英語の大文字が小文字になったり、ドイツ語のウムラウトなどが通常のアルファベットの小文字になってしまう問題(例: "ActivityPub"→"activitypub"、"Wälder"→"walder")の解消を期待した修正箇所。

/wp-content/plugins/activitypub/includes/transformer/class-post.php

/**
 * Returns a list of Tags, used in the Post.
 *
 * This includes Hash-Tags and Mentions.
 *
 * @return array The list of Tags.
 */
protected function get_tag() {
	$tags = parent::get_tag();
		$post_tags = \get_the_tags( $this->item->ID );
	if ( $post_tags ) {
		foreach ( $post_tags as $post_tag ) {
			// Tag can be empty.
			if ( ! $post_tag ) {
				continue;
			}
				$tags[] = array(
				'type' => 'Hashtag',
				'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ),
				'name' => esc_hashtag( $post_tag->name ),
			);
		}
	}
		return \array_unique( $tags, SORT_REGULAR );
}
修正行の元のコード:
'name' => esc_hashtag( $post_tag->slug ),

 バグ例("テスト"→"E38386E382b9E38388")の画像:

#テスト というハッシュタグのバグ例

 wp_termsテーブル内の"name"と"slug"の例:

wp_termsテーブル内の"name"と"slug"の例

追加修正:

 プラグイン の設定「投稿コンテンツ」に指定した[ap_hashtags]に相当する関数の修正。

/wp-content/plugins/activitypub/includes/class-shortcodes.php

/**
 * Generates output for the 'ap_hashtags' shortcode.
 *
 * @return string The post tags as hashtags.
 */
public static function hashtags() {
	$item = self::get_item();
		if ( ! $item ) {
		return '';
	}
		$tags = \get_the_tags( $item->ID );
		if ( ! $tags ) {
		return '';
	}
		$hash_tags = array();
		foreach ( $tags as $tag ) {
		// Tag can be empty.
		if ( ! $tag ) {
			continue;
		}
			$hash_tags[] = \sprintf(
			'<a rel="tag" class="hashtag u-tag u-category" href="%s">%s</a>',
			\esc_url( \get_tag_link( $tag ) ),
			esc_hashtag( $tag->name )
		);
	}
		return \implode( ' ', $hash_tags );
}
修正行の元のコード:
esc_hashtag( $tag->slug )
未分類
管理人のマストドンアカウントへのリンクなど

コメント

  1. ishii ishii より:

    この投稿のap_outboxの、タグに関するJSONは次の通りで、アルファベットが小文字になっていない。

    "published":"2025-11-24T15:39:07Z","tag":[{"type":"Hashtag","href":"https://ishii00141.stars.ne.jp/tag/activitypub/","name":"#ActivityPub"},{"type":"Hashtag","href":"https://ishii00141.stars.ne.jp/tag/mastodon/","name":"#Mastodon"},{"type":"Hashtag","href":"https://ishii00141.stars.ne.jp/tag/test-for-tags-with-spaces/","name":"#TestForTagsWithSpaces"},{"type":"Hashtag","href":"https://ishii00141.stars.ne.jp/tag/walder/","name":"#W\u00e4lder"},{"type":"Hashtag","href":"https://ishii00141.stars.ne.jp/tag/wordpress/","name":"#WordPress"},{"type":"Hashtag","href":"https://ishii00141.stars.ne.jp/tag/%e3%83%90%e3%82%b0/","name":"#\u30d0\u30b0"},{"type":"Hashtag","href":"https://ishii00141.stars.ne.jp/tag/%e3%83%97%e3%83%a9%e3%82%b0%e3%82%a4%e3%83%b3/","name":"#\u30d7\u30e9\u30b0\u30a4\u30f3"}],
    • ishii ishii より:

      #WordPress #ActivityPub #Mastodon
      ap_outboxのJSONとは異なる表示をMastodonが行っている原因 #Gemini に尋ねたら、
      「Mastodonはap_outboxのデータではない、別の場所を見ているわけではありません。 Mastodonが見ているのは、ap_outboxに保存されたJSONをテンプレートとして利用し、送信直前にプラグインが上書き・変換した最終的なペイロードです。」とのこと。
      「つまり、ap_outboxのJSONデータは、プラグインが内部的にタグ情報として保存しているだけで、最終的な送信データではタグのスラッグ情報などを使って、よりFediverse標準に合わせた形に上書きされている、ということです。」とのこと。
      別のコードも変えないといけないのか…。

      • ishii ishii より:

        #ActivityPub #WordPress
        "この投稿のap_outboxの、タグに関するJSONは次の通りで、アルファベットが小文字になっていない。
        "
        プラグインActivityPub(7.6.1)の私的修正箇所 | いしい@試行錯誤
        https://ishii00141.stars.ne.jp/20251125-0039-4145/#comment-1161

        それにもかかわらず、#Mastodon で小文字になってしまう理由を、コードを示しながら #Gemini に調べてもらったけれど、結局は ap_outbox の生成コードに戻ってしまって、くるくるくる繰り返すだけで、疲れた。

        • ishii ishii より:

          ちなみに、v4.1.25 のmstdn.jpでも小文字化しているので、 #Mastodon の仕様変更とは考えにくい。
          mastodon.social でも、プラグインActivityPub(7.6.0)になる前は大文字が維持されていて、JSONのコードも、上記と同じ構造だった。

    • ishii ishii より:

      この記事の
      #Mastodon (mstdn.jp、v4.1.25)で表示されたハッシュタグ:

      #activitypub #mastodon #testForTagsWithSpaces #walder #wordpress #バグ #プラグイン

      以下は #WordPress のプラグイン #ActivityPub が7.6.0になる前の例。

      outlook.comのブックマークからアクセスできない | いしい@試行錯誤
      https://ishii00141.stars.ne.jp/20251111-1858-3873/

      #Mastodon (mstdn.jp、v4.1.25)で表示されたハッシュタグ:

      #Copilot #Outlook #Windows #エラー #仕様

      JSONのタグ部分:

      "published":"2025-11-11T09:58:07Z","tag":[{"type":"Hashtag","href":"https://ishii00141.stars.ne.jp/tag/copilot/","name":"#Copilot"},{"type":"Hashtag","href":"https://ishii00141.stars.ne.jp/tag/outlook/","name":"#Outlook"},{"type":"Hashtag","href":"https://ishii00141.stars.ne.jp/tag/windows/","name":"#Windows"},{"type":"Hashtag","href":"https://ishii00141.stars.ne.jp/tag/%e3%82%a8%e3%83%a9%e3%83%bc/","name":"#\u30a8\u30e9\u30fc"},{"type":"Hashtag","href":"https://ishii00141.stars.ne.jp/tag/%e4%bb%95%e6%a7%98/","name":"#\u4ed5\u69d8"}],
      • ishii ishii より:

        見つけた。追加して修正すべき箇所。
        #Mastodon に送信するデータを指定する設定にあるショートコード[ap_hashtags]。
        この関数のコードを修正する必要がある。
        /wp-content/plugins/activitypub/includes/class-shortcodes.php
        修正前のコード

        /**
         * Generates output for the 'ap_hashtags' shortcode.
         *
         * @return string The post tags as hashtags.
         */
        public static function hashtags() {
        	$item = self::get_item();
        		if ( ! $item ) {
        		return '';
        	}
        		$tags = \get_the_tags( $item->ID );
        		if ( ! $tags ) {
        		return '';
        	}
        		$hash_tags = array();
        		foreach ( $tags as $tag ) {
        		// Tag can be empty.
        		if ( ! $tag ) {
        			continue;
        		}
        			$hash_tags[] = \sprintf(
        			'<a class="hashtag u-tag u-category" href="%s" rel="tag nofollow ugc">%s</a>',
        			\esc_url( \get_tag_link( $tag ) ),
        			esc_hashtag( $tag->slug )
        		);
        	}
        		return \implode( ' ', $hash_tags );
        }
        • ishii ishii より:

          このコードの修正だけで十分だったかもしれない。
          esc_hashtag()関数に送られるデータが"$tag->name"なので、私の場合は"%"が含まれていないはず(タグに"%"は使わない)だから、置換コードで"%"を除外する必要はないし、get_tag()関数の方は、ショートコード[ap_hashtags]とは関係なさそうなので、無視しても良さそう。

タイトルとURLをコピーしました