-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial release.
- Loading branch information
0 parents
commit 1b4b0ac
Showing
14 changed files
with
694 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; // Exit if accessed directly. | ||
} | ||
|
||
if( ! class_exists( 'CCC_Post_Thumbnail' ) ) { | ||
class CCC_Post_Thumbnail { | ||
/*** 投稿本文の中から1枚目の画像のURLを取得 ***/ | ||
public static function get_first_image_url( $post=null ) { | ||
$first_img = rtrim( plugin_dir_url( __FILE__ ), '/').'/no-image.svg'; // 投稿内で画像がなかったときのためのデフォルト画像を指定 | ||
if( empty($post) ) { | ||
global $post; | ||
} // endif | ||
$post_content = $post->post_content; | ||
if( $post_content ) { | ||
ob_start(); | ||
ob_end_clean(); | ||
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post_content, $matches); | ||
if( $matches[1][0] ) { | ||
$first_img = $matches[1][0]; //投稿本文の中から1枚目の画像のURLを取得 | ||
} | ||
} // endif | ||
return esc_url( $first_img ); | ||
} // endfunction | ||
} // endclass | ||
} // endif |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
@charset "utf-8"; | ||
|
||
#ccc-browsing_history-list { | ||
position: relative; | ||
overflow: hidden; | ||
} | ||
#content-ccc_browsing_history .title-section { | ||
text-align: center; | ||
margin-bottom: 2em; | ||
} | ||
#post-ccc_browsing_history { | ||
font-size: 0; | ||
width: calc(100% + 1%); | ||
} | ||
#post-ccc_browsing_history .list-ccc_browsing_history { | ||
font-size: 15px; | ||
display: inline-block; | ||
vertical-align: top; | ||
box-sizing: border-box; | ||
width: calc(12.5% - 1%); | ||
margin: 0 1% 1% 0; | ||
position: relative; | ||
} | ||
#post-ccc_browsing_history .title-post { | ||
font-size: 0.9em; | ||
font-weight: 500; | ||
line-height: 1.3; | ||
margin: 0.25em 0; | ||
} | ||
#post-ccc_browsing_history .title-post .dotted-line { | ||
display: inline-block; | ||
vertical-align: top; | ||
max-height: 2.5em; | ||
overflow: hidden; | ||
} | ||
#post-ccc_browsing_history .ccc-favorite-post-toggle { | ||
line-height: 1.3; | ||
margin-top: -19px; | ||
} | ||
|
||
|
||
/* SP | ||
----------------------------------------------------------*/ | ||
@media only screen and (max-width: 750px) { | ||
#post-ccc_browsing_history { | ||
width: calc(100% + 3%); | ||
} | ||
#post-ccc_browsing_history .list-ccc_browsing_history { | ||
width: calc(33.33% - 3%); | ||
margin: 0 3% 3% 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Author: Takashi Matsuyama | ||
* Author URI: https://profiles.wordpress.org/takashimatsuyama/ | ||
* Description: WordPressで投稿の閲覧履歴を一覧で表示 | ||
* Version: 1.0.0 | ||
*/ | ||
|
||
/* | ||
* /browsing-history/save.js:別途、読み込みが必要(サブネームスペースで共通の変数と共通の関数を定義しているため) | ||
*/ | ||
|
||
/* グローバルネームスペース */ | ||
/* MYAPP = CCC */ | ||
var CCC = CCC || {}; | ||
|
||
(function($) { | ||
var content_area_elm = $('#content-ccc_browsing_history'); | ||
var post_area_elm = $('#ccc-browsing_history-list'); | ||
var posts_per_page_value = post_area_elm.data('ccc_history-posts_per_page'); | ||
|
||
var history_key = CCC.browsing_history.storage_key(); // 過去に閲覧した投稿を保存するストレージキーの名前を変数に格納(CCC.favoriteのstorage_key関数を呼び出し) | ||
var history_value = localStorage.getItem(history_key); // ローカルストレージから指定したキーの値を取得 | ||
var data_set = {}; // オブジェクトのキーに変数を使用:ES5までの書き方(IE11以下への対応のため) | ||
data_set['action'] = CCC_BROWSING_HISTORY_LIST.action; | ||
data_set['nonce'] = CCC_BROWSING_HISTORY_LIST.nonce; | ||
data_set[history_key] = history_value; | ||
data_set['ccc-posts_per_page'] = posts_per_page_value; | ||
|
||
$.ajax({ | ||
type: 'POST', | ||
url: CCC_BROWSING_HISTORY_LIST.api, | ||
data: data_set | ||
}).fail( function(){ | ||
alert('error'); | ||
}).done( function(response){ | ||
post_area_elm.html(response); | ||
if( post_area_elm.find('.list-ccc_browsing_history').length < 1 ) { | ||
content_area_elm.hide(); | ||
} | ||
}); | ||
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; // Exit if accessed directly. | ||
} //endif | ||
|
||
if( ! class_exists( 'CCC_Browsing_History_List' ) ) { | ||
class CCC_Browsing_History_List { | ||
|
||
public static function action() { | ||
/*** お気に入りの投稿のデータ(カンマ連結の文字列)を取得 ***/ | ||
if ( is_user_logged_in() === false ) { | ||
$post_ccc_browsing_history = sanitize_text_field( $_POST['ccc-browsing_history'] ); | ||
$browsing_history_post_ids = explode(',', $post_ccc_browsing_history); // 指定した値で分割した文字列の配列を返す | ||
} else { | ||
/* MySQLのユーザーメタ(usermeta)からユーザーが選んだ投稿IDを取得 */ | ||
$user_browsing_history_post_ids = get_user_meta( wp_get_current_user()->ID, CCC_Browsing_History::CCC_BROWSING_HISTORY_POST_IDS, true ); | ||
//var_dump($favorite_post_user); | ||
$browsing_history_post_ids = explode(',', $user_browsing_history_post_ids); | ||
} | ||
$browsing_history_post_ids = array_map('htmlspecialchars', $browsing_history_post_ids); // 配列データを一括でサニタイズする | ||
|
||
/*** 表示数の定義(指定が無ければ管理画面の表示設定(表示する最大投稿数)の値を取得) ***/ | ||
$ccc_posts_per_page = absint( $_POST['ccc-posts_per_page'] ); //負ではない整数に変換 | ||
if( $ccc_posts_per_page ) { | ||
$posts_per_page = $ccc_posts_per_page; | ||
} else { | ||
$posts_per_page = get_option('posts_per_page'); | ||
} | ||
|
||
$args= array( | ||
'post_type' => 'any', // リビジョンと 'exclude_from_search' が true にセットされたものを除き、すべてのタイプを含める | ||
'posts_per_page' => $posts_per_page, | ||
'post__in' => $browsing_history_post_ids, | ||
'orderby' => 'post__in', | ||
); | ||
$the_query = new WP_Query($args); | ||
?> | ||
|
||
<?php if( $the_query->have_posts() ) { ?> | ||
<div id="post-ccc_browsing_history"> | ||
<?php | ||
$count = 0; | ||
while( $the_query->have_posts() ) { | ||
$the_query->the_post(); | ||
$count++; | ||
?> | ||
<div class="list-ccc_browsing_history clearfix"> | ||
<div class="img-post"> | ||
<a href="<?php the_permalink(); ?>"> | ||
<?php | ||
if( has_post_thumbnail() ) { | ||
echo '<div class="img-post-thumbnail" style="background-image: url('.get_the_post_thumbnail_url($the_query->post->ID, 'medium').');"></div>'; | ||
} else { | ||
echo '<div class="img-post-thumbnail" style="background-image: url('.CCC_Post_Thumbnail::get_first_image_url($the_query->post).');"></div>'; | ||
} | ||
?> | ||
</a> | ||
</div><!-- /.img-post --> | ||
<h3 class="title-post"><a href="<?php the_permalink(); ?>" class="dotted-line"><?php the_title(); ?></a></h3><!-- /.title-post --> | ||
</div><!-- /.list-series --> | ||
<?php } //endwhile ?> | ||
</div><!-- /.post-series --> | ||
<?php | ||
wp_reset_postdata(); /* オリジナルの投稿データを復元 */ | ||
}//endif | ||
|
||
} //endfunction | ||
}//endclass | ||
}//endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Author: Takashi Matsuyama | ||
* Author URI: https://profiles.wordpress.org/takashimatsuyama/ | ||
* Description: WordPressで投稿の閲覧履歴をローカルストレージとMySQLのユーザーメタ(wp_usermeta)に保存 | ||
* Version: 1.0.0 | ||
*/ | ||
|
||
/* グローバルネームスペース */ | ||
/* MYAPP = CCC */ | ||
var CCC = CCC || {}; | ||
|
||
(function($){ | ||
/* サブネームスペース */ | ||
CCC.browsing_history = { | ||
|
||
/* 初期設定(グローバル変数を設定) */ | ||
//data_elm : $('body'), | ||
//data_key : 'post_id-history', | ||
|
||
storage_key : function() { | ||
var key = 'ccc-browsing_history'; // 過去に閲覧した投稿を保存するストレージキーの名前を指定 | ||
return key; | ||
}, // メンバのメソッドを定義 | ||
|
||
action : function(history_key, history_value) { | ||
/*** 実行本体:過去に閲覧した投稿を保存する関数 ***/ | ||
var post_id = CCC_BROWSING_HISTORY_UPDATE.post_id; | ||
//console.log(post_id); | ||
if( post_id ) { | ||
/* 過去に閲覧した投稿を配列に追加(または新規作成) */ | ||
if (history_value === null) { | ||
var history_value_array = []; // 新たに配列を作成 | ||
history_value_array.unshift(post_id); // 配列の最初に1つ以上の要素を追加し、新しい配列の長さを返す | ||
} else { | ||
var history_value_array = history_value.split(','); // カンマで分割して配列にする | ||
var value_index = history_value_array.indexOf(String(post_id)); // ストレージの値は文字列に変換しているので、indexOfの指定も数値を文字列に変換する必要がある | ||
if (value_index === -1) { | ||
history_value_array.unshift(post_id); // 配列の最初に1つ以上の要素を追加し、新しい配列の長さを返す | ||
} else { | ||
//console.log("重複している投稿ID:"+ post_id); | ||
history_value_array.splice(value_index, 1); // インデックスn番目から、1つの要素を削除(重複してたら、それを削除) | ||
history_value_array.unshift(post_id); // 配列の最初に1つ以上の要素を追加し、新しい配列の長さを返す(重複してたら、既存を削除してから改めて先頭に追加) | ||
} | ||
} | ||
/* 配列から「null」や「undefined」、「"" 空文字」が入った要素を削除する */ | ||
history_value_array = history_value_array.filter(function(v){ | ||
return !(v === null || v === undefined || v === ""); // 0は除くためfilter(Boolean)は使わない | ||
}); | ||
/* 保存する投稿の数を制限:配列の数がX個以上ある場合、Y個に減らす */ | ||
if( history_value_array.length > 100 ){ | ||
history_value_array = history_value_array.slice( 0, 100 ); /* 開始位置と終了位置を指定(開始位置は0から数えて終了位置の値は含まない) */ | ||
} | ||
history_value_array_str = history_value_array.join(','); // 配列要素の連結・結合:配列を連結して1つの文字列に変換 | ||
/* ログインユーザーでは無い場合 */ | ||
if( CCC_BROWSING_HISTORY_UPDATE.user_logged_in == false ) { | ||
localStorage.setItem(history_key, history_value_array_str); // 指定したキーのローカルストレージに過去に閲覧した投稿の文字列データを保存 | ||
} else { | ||
/* ログインユーザーの場合は過去に閲覧した投稿をMySQLのユーザーメタ(wp_usermeta)に保存 */ | ||
$.ajax({ | ||
url : CCC_BROWSING_HISTORY_UPDATE.api, // admin-ajax.phpのパスをローカライズ(wp_localize_script関数) | ||
type : 'POST', | ||
data : { | ||
action : CCC_BROWSING_HISTORY_UPDATE.action, // wp_ajax_フックのサフィックス | ||
nonce : CCC_BROWSING_HISTORY_UPDATE.nonce, | ||
post_ids : history_value_array_str | ||
}, | ||
}).fail( function(){ | ||
console.log('browsing_history_update : ajax error'); | ||
}).done( function(response){ | ||
//console.log(response) | ||
}); | ||
} //endif | ||
} //endif | ||
} // メンバのメソッドを定義 | ||
|
||
}; // サブネームスペース | ||
|
||
|
||
|
||
|
||
|
||
/*** 初回ロード:過去に閲覧した投稿を保存 ***/ | ||
/* ログインユーザーでは無い場合 */ | ||
if( CCC_BROWSING_HISTORY_UPDATE.post_id ) { | ||
if( CCC_BROWSING_HISTORY_UPDATE.user_logged_in == false ) { | ||
var history_key = CCC.browsing_history.storage_key(); // 過去に閲覧した投稿を保存するストレージキーの名前を変数に格納(CCC.browsing_historyのstorage_key関数を呼び出し) | ||
var history_value = localStorage.getItem(history_key); // ローカルストレージから指定したキーの値を取得 | ||
CCC.browsing_history.action(history_key, history_value); // CCC.browsing_historyのaction関数を呼び出し | ||
} else { | ||
$.ajax({ | ||
url : CCC_BROWSING_HISTORY_GET.api, // admin-ajax.phpのパスをローカライズ(wp_localize_script関数) | ||
type : 'POST', | ||
data : { | ||
action : CCC_BROWSING_HISTORY_GET.action, // wp_ajax_フックのサフィックス | ||
nonce : CCC_BROWSING_HISTORY_GET.nonce // wp_ajax_フックのnonce | ||
}, | ||
}).fail( function(){ | ||
console.log('browsing_history_get : ajax error'); | ||
}).done( function(response){ | ||
var history_key = ''; // ログインユーザーはローカルストレージを使用しないのでストレージキーは不要 | ||
var history_value = response; // MySQLのユーザーメタ(wp_usermeta)から過去に閲覧した投稿の値を取得 | ||
CCC.browsing_history.action(history_key, history_value); // CCC.browsing_historyのaction関数を呼び出し | ||
}); | ||
} | ||
} //endif | ||
|
||
})(jQuery); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; // Exit if accessed directly. | ||
} | ||
|
||
if( ! class_exists( 'CCC_Browsing_History_ShortCode_List' ) ) { | ||
|
||
add_shortcode('ccc_browsing_history_list_results', array('CCC_Browsing_History_ShortCode_List', 'results') ); | ||
|
||
class CCC_Browsing_History_ShortCode_List { | ||
|
||
public static function results($atts) { | ||
$atts = shortcode_atts(array( | ||
"title" => '', | ||
"posts_per_page" => '', | ||
"class" => '', | ||
),$atts); | ||
if( $atts['title'] ) { | ||
$title = $atts['title']; | ||
} else { | ||
$title = __('Browsing history', CCCBROWSINGHISTORY_TEXT_DOMAIN); | ||
} | ||
if( $atts['posts_per_page'] and ctype_digit($atts['posts_per_page']) ) { | ||
$posts_per_page = $atts['posts_per_page']; | ||
} else { | ||
$posts_per_page = 8; | ||
} | ||
if( $atts['class'] ) { | ||
$class = 'class="'.$atts['class'].'"'; | ||
} else { | ||
$class = null; | ||
} | ||
$data = '<div id="content-ccc_browsing_history">'; | ||
$data .= '<p class="title-section">'.$title.'</p>'; | ||
$data .= '<div id="ccc-browsing_history-list" data-ccc_history-posts_per_page="'.$posts_per_page.'" '.$class.'></div>'; //<!-- /#ccc-browsing_history-list --> | ||
$data .= '</div>'; //<!-- /#content-ccc_browsing_history --> | ||
return $data; | ||
} //endfunction | ||
|
||
} //endclass | ||
} //endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; // Exit if accessed directly. | ||
} | ||
|
||
if( ! class_exists( 'CCC_Browsing_History_Uninstall' ) ) { | ||
class CCC_Browsing_History_Uninstall { | ||
public static function delete_usermeta() { | ||
$meta_key = 'ccc_browsing_history_post_ids'; | ||
delete_metadata( 'user', null, $meta_key, '', true ); | ||
} //endfunction | ||
} //endclass | ||
} //endif |
Oops, something went wrong.