Skip to content

Commit

Permalink
ブログカードブロックの改行対応(テスト)
Browse files Browse the repository at this point in the history
  • Loading branch information
yhira committed Jul 12, 2019
1 parent 754db5e commit b79e146
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/blogcard-in.php
Expand Up @@ -185,6 +185,9 @@ function url_to_internal_blogcard_tag($url){
//本文中のURLをブログカードタグに変更する
if ( !function_exists( 'url_to_internal_blogcard' ) ):
function url_to_internal_blogcard($the_content) {
//ブロックエディターのブログカード用の本文整形
$the_content = fix_blogcard_content($the_content);
//_v($the_content);
$res = preg_match_all('/^(<p>)?(<a[^>]+?>)?https?:\/\/'.preg_quote(get_the_site_domain()).'(\/)?([-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)?(<\/a>)?(<\/p>)?/im', $the_content,$m);
//_v($m);
foreach ($m[0] as $match) {
Expand All @@ -205,7 +208,6 @@ function url_to_internal_blogcard($the_content) {


if ( !$tag ) continue;//IDを取得できない場合はループを飛ばす

//本文中のURLをブログカードタグで置換
$the_content = preg_replace('{^'.preg_quote($match, '{}').'}im', $tag , $the_content, 1);
wp_reset_postdata();
Expand Down
2 changes: 2 additions & 0 deletions lib/blogcard-out.php
Expand Up @@ -30,6 +30,8 @@ function url_to_external_blog_card_tag($url){
//本文中の外部URLをはてなブログカードタグに変更する
if ( !function_exists( 'url_to_external_blog_card' ) ):
function url_to_external_blog_card($the_content) {
//ブロックエディターのブログカード用の本文整形
$the_content = fix_blogcard_content($the_content);
//1行にURLのみが期待されている行(URL)を全て$mに取得
$res = preg_match_all('/^(<p>)?(<a[^>]+?>)?https?:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+(<\/a>)?(?!.*<br *\/?>).*?(<\/p>)?/im', $the_content,$m);

Expand Down
10 changes: 10 additions & 0 deletions lib/utils.php
Expand Up @@ -2994,3 +2994,13 @@ function get_abs_htaccess_file(){
return get_abs_home_path().'.htaccess';
}
endif;

//ブログカードコンテンツ
if ( !function_exists( 'fix_blogcard_content' ) ):
function fix_blogcard_content($the_content){
$the_content = str_replace('</p><p>', "</p>\n<p>", $the_content);
$the_content = preg_replace('{<p> +}', '<p>', $the_content);
$the_content = preg_replace('{ +</p>}', '</p>', $the_content);
return $the_content;
}
endif;

0 comments on commit b79e146

Please sign in to comment.