Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTMLのソース上でhrefの値が切り替わらない問題を修正しました #83

Merged
merged 10 commits into from
Aug 21, 2024
61 changes: 35 additions & 26 deletions js/script.js
Original file line number Diff line number Diff line change
@@ -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);
4 changes: 3 additions & 1 deletion js/script.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
25 changes: 16 additions & 9 deletions vk-link-target-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

}

}
Expand Down
Loading