diff --git a/js/script.js b/js/script.js index b7897e4..5aa99e9 100644 --- a/js/script.js +++ b/js/script.js @@ -1,30 +1,39 @@ -document.addEventListener("DOMContentLoaded",function(e){ -var $=jQuery; -// .post でサーバーへ通信する -const pathToServer = vkLtc.ajaxurl; -const sendData = {action : 'ids',}; -$.post( - pathToServer, - sendData, - function(ps) { - // ps : 対象の投稿データっぽい - if(!$.isEmptyObject(ps)){ +document.addEventListener("DOMContentLoaded", function(e) { + var $ = jQuery; + const pathToServer = vkLtc.ajaxurl; + const sendData = { action: 'ids' }; + + $.post(pathToServer, sendData, function(ps) { + if (!$.isEmptyObject(ps)) { $.each(ps, function(id, ls) { - // ls - // [0]リプレースURL - // [1]変換元URL - try{ // 例外エラーが発生しるかもしれない処理 - ls.forEach(function(l){ - // c : 対象セレクタ - var c = $('.post-'+id+' a[href="'+l+'"]'); - // 対象が存在したら - if(c.length){ - // 対象セレクタの要素に _blank 属性を付与 - $(c).attr('target','_blank').attr('rel','noreferrer noopener'); + var originalUrl = decodeURIComponent(ls[1]); + var c = $('.post-' + id + ' a').filter(function() { + return decodeURIComponent($(this).attr('href')) === originalUrl; + }); + + if (c.length) { + // リダイレクトURLが空でない場合のみhref属性を更新 + if (ls[0]) { + $(c).attr('href', ls[0]); + } + + // ターゲット属性を更新 + if (ls[2] === '1') { + $(c).attr('target', '_blank'); + } else { + $(c).attr('target', '_self'); + } + + // targetが_blankである場合にのみrel属性を追加 + if ($(c).attr('target') === '_blank') { + if (!$(c).attr('rel')) { + $(c).attr('rel', 'noreferrer noopener'); } - }); - }finally{} + } else { + $(c).removeAttr('rel'); + } + } }); } - } -);},false); + }); +}, false); diff --git a/js/script.min.js b/js/script.min.js index 356b387..cc11db2 100644 --- a/js/script.min.js +++ b/js/script.min.js @@ -1,2 +1,4 @@ -document.addEventListener("DOMContentLoaded",function(e){var $=jQuery;const pathToServer=vkLtc.ajaxurl;const sendData={action:'ids',};$.post(pathToServer,sendData,function(ps){if(!$.isEmptyObject(ps)){$.each(ps,function(id,ls){try{ls.forEach(function(l){var c=$('.post-'+id+' a[href="'+l+'"]');if(c.length){$(c).attr('target','_blank').attr('rel','noreferrer noopener');}});}finally{}});}});},false); \ No newline at end of file +document.addEventListener("DOMContentLoaded",function(e){var $=jQuery;const pathToServer=vkLtc.ajaxurl;const sendData={action:'ids'};$.post(pathToServer,sendData,function(ps){if(!$.isEmptyObject(ps)){$.each(ps,function(id,ls){var originalUrl=decodeURIComponent(ls[1]);var c=$('.post-'+id+' a').filter(function(){return decodeURIComponent($(this).attr('href'))===originalUrl;});if(c.length){if(ls[0]){$(c).attr('href',ls[0]);} +if(ls[2]==='1'){$(c).attr('target','_blank');}else{$(c).attr('target','_self');} +if($(c).attr('target')==='_blank'){if(!$(c).attr('rel')){$(c).attr('rel','noreferrer noopener');}}else{$(c).removeAttr('rel');}}});}});},false); \ No newline at end of file diff --git a/readme.txt b/readme.txt index f40d025..f343444 100644 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: Tags: redirection,link,recent posts,list,page,post Requires at least: 5.3 Tested up to: 6.6 -Stable tag: 1.7.5.1 +Stable tag: 1.7.6.0 License: GPL2 License URI: https://www.gnu.org/licenses/gpl-2.0.html @@ -109,6 +109,9 @@ But we have a .pot file available so feel free to translate it in your language == Changelog == += 1.7.6 = +* [ Bug fix ] Handle links other than the_permalink. + = 1.7.5 = * [ Other ] Delete unnecessary files diff --git a/vk-link-target-controller.php b/vk-link-target-controller.php index 9e6cce4..030cf43 100755 --- a/vk-link-target-controller.php +++ b/vk-link-target-controller.php @@ -3,7 +3,7 @@ Plugin Name: VK Link Target Controller Plugin URI: https://github.com/kurudrive/vk-link-target-controller Description: Allow you to link a post title from the recent posts list to another page (internal or external link) rather than link to the actual post page -Version: 1.7.5.1 +Version: 1.7.6.0 Author: Vektor,Inc. Author URI: http://www.vektor-inc.co.jp/ License: GPL2 @@ -618,39 +618,46 @@ function candidate_post_type() { function ajax_rewrite_ids() { $ids = array(); - + $post_types = $this->get_public_post_types(); $post_types_slugs = array_keys( $post_types ); $post_types_slugs[] = 'page'; - + // get posts with specific post meta and post meta value. $args = array( 'posts_per_page' => -1, 'paged' => 0, 'post_type' => $post_types_slugs, - 'meta_key' => 'vk-ltc-target', - 'meta_value' => 1, + 'meta_key' => 'vk-ltc-link', ); $query = new WP_Query( $args ); - + // create an array( 'id' => 'link' ) of ids from the posts found in the query. if ( $query->found_posts > 0 ) { $matching_posts = $query->posts; foreach ( $matching_posts as $post ) { - $ids[ $post->ID ][] = html_entity_decode( $this->rewrite_link( $post->ID ) ); + $link = get_post_meta( $post->ID, 'vk-ltc-link', true ); + $target = get_post_meta( $post->ID, 'vk-ltc-target', true ); + + // リダイレクト先のURLが空でない場合のみ情報を追加 + if (!empty($link)) { + $ids[ $post->ID ][] = html_entity_decode( $link ); + } $ids[ $post->ID ][] = get_permalink( $post->ID ); + $ids[ $post->ID ][] = $target; // ターゲットの情報を追加 $ids[ $post->ID ] = array_unique( $ids[ $post->ID ] ); } } - + // Convert php array to json format for use in jQuery. $json_ids = json_encode( $ids ); - + // Send data to the front. header( 'Content-Type: application/json' ); echo $json_ids; exit; } + } }