]+?) (srcset|data-srcset)=[^>]+?sizes="[^"]+?"([^>]*?>)/ism', '$1$3', $contents );
// ドラッグスクロール用の class を付与 ( AMP では付けない )
if( stripos( $contents, 'dragscroll-on' ) !== false ) {
preg_match_all( '/
]+?class="wp-block-luxe-blocks-scroll-block[^"]+?dragscroll-on[^"]*?"[^>]*?>\s*?<(figure|pre)[^>]+?>/im', $contents, $matches );
foreach( array_unique( $matches[0] ) as $scroll_block ) {
if( stripos( $scroll_block, 'wp-block-luxe-blocks-syntaxhighlighter' ) === false ) {
$replaced = preg_replace( '/(<(figure|pre)[^>]+?)class="([^"]+?)"([^>]*?>)/im', '$1class="$3 dragscroll"$4', $scroll_block );
$contents = str_replace( $scroll_block, $replaced, $contents );
}
}
// ドラッグスクロール用スクリプト読み込み
wp_enqueue_script( 'luxe-dragscroll', TURI . '/js/dragscroll.min.js', array(), false );
}
}
}
}
// クォーテーションが HTML エンティティで書いても全角になっちゃうという WordPress 特有の謎変換への対処
$contents = str_replace( '”', '"', $contents );
$contents = str_replace( '‘', ''', $contents );
return $contents;
}
add_filter( 'thk_content', 'thk_the_content', 1073741824 );
//add_filter( 'the_content', 'thk_the_content', 2147483647 );
endif;
/*---------------------------------------------------------------------------
* 目次生成関数(ウィジェットでも使用するので、thk_the_content から分離)
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_create_toc' ) === false ):
function thk_create_toc( $contents, $contents_replace = false ) {
$toc = '';
$pos = '';
$paging = false;
// 少なくとも H2 か h3 がある場合だけ
if( stripos( $contents, '
post_content, '' ) !== false ) {
global $wp_rewrite;
$post_content_array = explode( '', $post->post_content );
$target_page = 0;
$permalink = get_permalink();
$dynamic = false;
$slash1 = '';
$slash2 = '/';
if( is_object( $wp_rewrite ) === true && $wp_rewrite->using_permalinks() === false ) {
$dynamic = true;
}
else {
// URL の最後がスラッシュで終わってないパターン
if( substr( $permalink, -1 ) !== '/' ) {
$slash1 = '/';
$slash2 = '';
}
}
$paging = true;
}
// H2 が無いにも関わらず、いきなり H3 から始まってる場合でも一応処理はする
if( stripos( $contents, 'post_content に一部拡大
$preg_result = 0;
$matches = array();
if( $paging === true ) {
$preg_result = preg_match_all( '/]*?>(.*?)<\/h\1>/ism', wptexturize( $post->post_content ), $matches, PREG_SET_ORDER );
}
else {
$preg_result = preg_match_all( '/]*?>(.*?)<\/h\1>/ism', wptexturize( $contents ), $matches, PREG_SET_ORDER );
}
if( $preg_result > 0 ) {
if( empty( $matches ) ) return $contents;
$pos = strpos( $contents, $matches[0][0] );
$deps_array = array();
foreach( (array)$matches as $m ) {
if( isset( $m[1] ) ) $deps_array[] = $m[1];
}
if( empty( $deps_array ) ) return array( $contents, $toc );
$min_deps = min( $deps_array ); unset( $deps_array );
$current_deps = $min_deps - 1;
$sub_deps = array( '2' => 0, '3' => 0, '4' => 0, '5' => 0, '6' => 0 );
$a = 0;
foreach( (array)$matches as $m ) {
$deps = (int)$m[1];
$text = $m[2];
$quote_text = preg_quote( $text, '/' );
$temp = mb_substr_replace( $text, mb_substr( $text, 0, 1 ) . "#*\t" . $a . "\t*#", 0 , 1 );
$contents = preg_replace( '/<(h[2-6]{1}[^>]*?)>' . $quote_text . '(<\/h[2-6]>)/i', '<$1>' . $temp . '$2', $contents, 1 );
while( $current_deps > $deps ) {
$toc .= '';
--$current_deps;
}
if( $current_deps === $deps ) {
$toc .= '';
}
else {
while( $current_deps < $deps ) {
$toc .= '- ';
++$current_deps;
}
for( $i = $current_deps; $i < count( $sub_deps ); ++$i ) {
$sub_deps[$i] = 0;
}
}
++$sub_deps[$current_deps];
$full_deps = array();
for( $i = $min_deps; $i <= $deps; ++$i ) {
$full_deps[] = $sub_deps[$i];
}
$target = 'toc_id_' . implode( '_', $full_deps );
$full_deps_number = implode( '.', $full_deps );
$target_point = '';
// 改ページされてる場合
if( $paging === true ) {
foreach( (array)$post_content_array as $key => $val ) {
if( preg_match( '/<(h[2-6]{1}[^>]*?)>' . $quote_text . '(<\/h[2-6]>)/i', $val ) ) {
$target_page = $key + 1;
break;
}
}
// 2ページ目以降
if( $target_page !== 1 ) {
// URL が動的か静的かで分岐
if( $dynamic === true ) {
if( isset( $luxe['amp'] ) ) {
$target_point = $permalink . '&=1&page=' . $target_page . '#' . $target;
}
else {
$target_point = $permalink . '&page=' . $target_page . '#' . $target;
}
}
else {
if( isset( $luxe['amp'] ) ) {
$target_point = $permalink . $slash1 . $target_page . $slash2 . '?amp=1#' . $target;
}
else {
$target_point = $permalink . $slash1 . $target_page . $slash2 . '#' . $target;
}
}
}
else {
if( isset( $luxe['amp'] ) ) {
if( $dynamic === true ) {
$target_point = $permalink . '&=1#' . $target;
}
else {
$target_point = $permalink . $slash1 . 'amp' . $slash2 . '#' . $target;
}
}
else {
$target_point = $permalink . '#' . $target;
}
}
}
else {
$target_point = '#' . $target;
}
end( $full_deps );
$parent_deps = prev( $full_deps );
if( empty( $parent_deps ) ) $parent_deps = 1;
$toc .= '' . $full_deps_number . '. ' . $text . '';
if( $contents_replace === true ) {
// 投稿本文置換
$quote_temp = preg_quote( $temp, '/' );
/*
preg_match( '/]*?>' . $quote_temp . '<\/span><\/h[2-6]{1}>/im', $contents, $already );
if( !isset( $already[1] ) ) {
$contents = preg_replace( '/(]*?>)' . $quote_temp . '(<\/h[2-6]{1}>)/im', '$1' . $temp . '$2', $contents, 1 );
}
else {
$toc = str_replace( '', '', $toc );
}
*/
$contents = preg_replace( '/(]*?>)' . $quote_temp . '(<\/h[2-6]{1}>)/im', '$1' . $temp . '$2', $contents, 1 );
}
++$a;
}
while( $current_deps >= $min_deps ) {
$toc .= '
';
--$current_deps;
}
$contents = preg_replace( '/<(h[2-6]{1}[^>]*?)>(.+?)' . '#\*\t[0-9]+?\t\*#' . '(.*?<\/h[2-6]{1}>)/ism', '<$1>$2$3', $contents );
}
}
return array( $contents, $toc, $pos );
}
endif;
/*---------------------------------------------------------------------------
* コピーライト生成
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_create_copyright' ) === false ):
function thk_create_copyright( $type, $auth, $since = null, $text = null ) {
$ret = '';
if( $type !== 'free' || ( $type === 'free' && !empty( $text ) ) ) {
$ret .= '';
}
switch( $type ) {
case 'ccsra':
$ret .= 'Copyright © ' . $since . '-' . date('Y') . ' ' . $auth . ' All Rights Reserved.';
break;
case 'ccsa':
$ret .= 'Copyright © ' . $since . ' ' . $auth . ' All Rights Reserved.';
break;
case 'cca':
$ret .= 'Copyright © ' . $auth . ' All Rights Reserved.';
break;
case 'ccsr':
$ret .= 'Copyright © ' . $since . '-' . date('Y') . ' ' . $auth . '';
break;
case 'ccs':
$ret .= 'Copyright © ' . $since . ' ' . $auth . '';
break;
case 'cc':
$ret .= 'Copyright © ' . $auth . '';
break;
case 'csr':
$ret .= '© ' . $since . '-' . date('Y') . ' ' . $auth . '';
break;
case 'cs':
$ret .= '© ' . $since . ' ' . $auth . '';
break;
case 'c':
$ret .= '© ' . $auth . '';
break;
case 'free':
if( !empty( $text ) ) {
$ret .= $text;
}
break;
default:
$ret .= 'Copyright © ' . $since . ' ' . $auth . '. All Rights Reserved.';
break;
}
if( $type !== 'free' || ( $type === 'free' && !empty( $text ) ) ) {
$ret .= '
';
}
return $ret;
}
endif;
/*---------------------------------------------------------------------------
* カスタマイズで hentry 削除にチェックがついてたら hentry 削除
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_remove_hentry' ) === false ):
function thk_remove_hentry( $ret ) {
$ret = array_diff( $ret, array('hentry') );
return $ret;
}
endif;
/*---------------------------------------------------------------------------
* 全体イメージの CSS ファイル名取得
*---------------------------------------------------------------------------*/
if( function_exists( 'get_overall_image' ) === false ):
function get_overall_image() {
global $luxe;
$overall = isset( $luxe['overall_image'] ) ? $luxe['overall_image'] : 'white';
if( $overall !== 'white' ) {
$overall = 'styles/style-' . $overall . '.css';
}
else {
$overall = 'style.css';
}
return $overall;
}
endif;
/*---------------------------------------------------------------------------
* CSS を HTML に直接埋め込む場合 (パス変換済みの CSS を require する)
* または、テンプレートごとにカラム数が違う場合
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_direct_style' ) === false ):
function thk_direct_style( $require_file ) {
if( file_exists( $require_file ) === false ) return;
return thk_fgc( $require_file );
}
endif;
/*---------------------------------------------------------------------------
* 管理者のみ閲覧できるコメントの表示
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_admin_comments' ) === false ):
function thk_admin_comments( $comment_content, $comment ) {
global $_is;
if( isset( $comment->comment_ID ) ) {
if( get_comment_meta( $comment->comment_ID, 'luxe_admin_comments', true ) == 1 ) {
$ret = '' . __( 'Only administrators can see this comment.', 'luxeritas' ) . '
';
if( $_is['edit_theme_options'] === true ) {
$ret .= $comment_content;
}
return $ret;
}
else {
return $comment_content;
}
}
else {
return $comment_content;
}
}
endif;
/*---------------------------------------------------------------------------
* 管理者のみ閲覧できるコメントの保存
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_admin_comment_post' ) === false ):
function thk_admin_comment_post( $comment_id ) {
if( isset( $_POST['luxe_admin_comments'] ) ) {
$value = in_array( $_POST['luxe_admin_comments'], array( false, true ) ) ? $_POST['luxe_admin_comments'] : false;
}
else {
$value = false;
}
update_comment_meta( $comment_id, 'luxe_admin_comments', $value );
}
endif;
/*---------------------------------------------------------------------------
* ブログカードのキャッシュを Ajax 経由で作成する場合の処理
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_blogcard_cache' ) === false ):
function thk_blogcard_cache(){
if( wp_verify_nonce( $_POST['blogcard_cache_nonce'], 'blogcard_cache' ) ) {
$blogcard = new THK_Blogcard();
$bc_url = $_POST['bc_url'];
$bc_md5 = $_POST['bc_md5'];
$bc_lnk = $_POST['bc_lnk'];
$wp_upload_dir = wp_upload_dir();
$cache_dir = $wp_upload_dir['basedir'] . DSEP . 'luxe-blogcard' . DSEP;
$cache_file = $cache_dir . $bc_md5[0] . DSEP . $bc_md5;
$blogcard->thk_create_blogcard( $bc_url, $bc_md5 );
$caches = $blogcard->thk_get_blogcard_cache( $cache_file, $bc_lnk, $bc_md5, true );
if( isset( $caches[1] ) ) {
echo json_encode( [ $bc_md5, $caches[1] ] );
}
exit;
}
}
endif;
/*---------------------------------------------------------------------------
* ブログカードキャッシュ削除処理
*---------------------------------------------------------------------------*/
/* 削除処理 */
if( function_exists( 'blogcard_cache_cleanup' ) === false ):
function blogcard_cache_cleanup( $rm_dir = false, $del_transient_only = false ) {
global $_is, $wp_filesystem, $wpdb;
if( $del_transient_only === false ) {
thk_filesystem_init();
$wp_upload_dir = wp_upload_dir();
$cache_dir = $wp_upload_dir['basedir'] . '/luxe-blogcard/';
if( $wp_filesystem->is_dir( $cache_dir ) === true ) {
if( $rm_dir === true ) {
// ディレクトリごと消す場合
if( $wp_filesystem->delete( $cache_dir, true ) === false ) {
if( $_is['admin'] === true ) {
add_settings_error( 'luxe-custom', '', __( 'Could not delete cache directory.', 'luxeritas' ) . '
' . $cache_dir );
}
elseif( defined( 'WP_DEBUG' ) === true && WP_DEBUG == true ) {
$result = new WP_Error( 'rmdir failed', __( 'Could not delete cache directory.', 'luxeritas' ), $cache_dir );
thk_error_msg( $result );
}
}
}
else {
// ファイルだけ消す場合
$dirlist = $wp_filesystem->dirlist( $cache_dir );
foreach( (array)$dirlist as $filename => $fileinfo ) {
$wp_filesystem->delete( $cache_dir . $filename, true );
}
}
}
}
// transient を消す
$wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_luxe-bc-%')" );
$wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_timeout_luxe-bc-%')" );
}
endif;
/*---------------------------------------------------------------------------
* 1週間に1度 SNS のカウントキャッシュを全クリア ( transient に登録)
*---------------------------------------------------------------------------*/
if( function_exists( 'set_transient_sns_count_cache_weekly_cleanup' ) === false ):
function set_transient_sns_count_cache_weekly_cleanup() {
if( get_transient( 'sns_count_cache_weekly_cleanup' ) === false ) {
global $wpdb;
$wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_luxe-sns-%')" );
$wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_timeout_luxe-sns-%')" );
delete_transient( 'sns_count_cache_weekly_cleanup' );
set_transient( 'sns_count_cache_weekly_cleanup', 1, WEEK_IN_SECONDS );
sns_count_cache_cleanup( false, false, true );
}
}
endif;
/*---------------------------------------------------------------------------
* SNS のカウントキャッシュ削除処理
*---------------------------------------------------------------------------*/
/* 削除処理 */
if( function_exists( 'sns_count_cache_cleanup' ) === false ):
function sns_count_cache_cleanup( $rm_dir = false, $del_transient = false, $weekly = true ) {
thk_filesystem_init();
global $wp_filesystem;
$target = $weekly === true ? get_theme_mod( 'sns_count_weekly_cleanup', 'dust' ) : 'all';
$filesystem = new thk_filesystem();
if( $filesystem->init_filesystem( site_url() ) === false ) return false;
$wp_upload_dir = wp_upload_dir();
$cache_dir = $wp_upload_dir['basedir'] . '/luxe-sns/';
if( $wp_filesystem->is_dir( $cache_dir ) === true ) {
if( $rm_dir === true ) {
// ディレクトリごと消す場合
if( $wp_filesystem->delete( $cache_dir, true ) === false ) {
if( defined( 'WP_DEBUG' ) === true && WP_DEBUG == true ) {
$result = new WP_Error( 'rmdir failed', __( 'Could not delete cache directory.', 'luxeritas' ), $cache_dir );
thk_error_msg( $result );
}
}
}
else {
// ファイルだけ消す場合
$dirlist = $wp_filesystem->dirlist( $cache_dir );
if( $target === 'dust' ) {
// 明らかなゴミだけ削除する場合
foreach( (array)$dirlist as $filename => $fileinfo ) {
$size = filesize( $cache_dir . $filename );
if( ctype_xdigit( $filename ) === false || strlen( $filename ) !== 32 || $size < 14 || $size > 8200 ) {
$wp_filesystem->delete( $cache_dir . $filename );
}
}
}
elseif( $target === 'all' ) {
// 全ファイル削除する場合
foreach( (array)$dirlist as $filename => $fileinfo ) {
$wp_filesystem->delete( $cache_dir . $filename );
}
}
}
}
// transient も全部消す場合
if( $del_transient === true || $target === 'all' ) {
global $wpdb;
$wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_luxe-sns-%')" );
$wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_timeout_luxe-sns-%')" );
delete_transient( 'sns_count_cache_weekly_cleanup' );
}
}
add_action( 'sns_count_cache_weekly_cleanup', 'sns_count_cache_cleanup' );
endif;
/*---------------------------------------------------------------------------
* SNS カウントキャッシュの中身取得 (初回と失敗時は ajax で取得)
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_get_sns_count_cache' ) === false ):
function thk_get_sns_count_cache( $spin = true ) {
global $_is;
$id_cnt = array( 'f' => '', 't' => '', 'h' => '', 'p' => '' );
if( $spin === false ) {
foreach( $id_cnt as $key => $val ) $id_cnt[$key] = 0;
}
else {
foreach( $id_cnt as $key => $val ) {
$id_cnt[$key] = '';
}
}
$url = $_is['front_page'] === true ? esc_url( THK_HOME_URL ) : esc_url( get_permalink() );
$wp_upload_dir = wp_upload_dir();
$cache_dir = $wp_upload_dir['basedir'] . '/luxe-sns/';
$sns_count_cache = $cache_dir . md5( $url );
if( file_exists( $sns_count_cache ) === true ) {
thk_filesystem_init();
global $wp_filesystem;
$cache = $wp_filesystem->get_contents( $sns_count_cache );
if( !empty( $cache ) && strpos( $cache, $url ) !== false ) {
$ids = explode( "\n", $cache );
array_shift( $ids );
foreach( (array)$ids as $value ) {
foreach( (array)$id_cnt as $key => $val ) {
if( strpos( $value, $key . ':' ) !== false ) {
$value = trim( $value, $key . ':' );
if( ctype_digit( $value ) === true ) {
$id_cnt[$key] = $value;
}
}
}
}
}
}
return $id_cnt;
}
endif;
/*---------------------------------------------------------------------------
* Feedly カウントキャッシュの中身取得 (初回と失敗時は ajax で取得)
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_get_feedly_count_cache' ) === false ):
function thk_get_feedly_count_cache( $spin = true ) {
$feedly_count = '';
if( $spin === false ) {
$feedly_count = 0;
}
$url = esc_url( get_bloginfo( 'rss2_url' ) );
$wp_upload_dir = wp_upload_dir();
$cache_dir = $wp_upload_dir['basedir'] . '/luxe-sns/';
$feedly_count_cache = $cache_dir . md5( $url );
if( file_exists( $feedly_count_cache ) === true ) {
thk_filesystem_init();
global $wp_filesystem;
$cache = $wp_filesystem->get_contents( $feedly_count_cache );
if( !empty( $cache ) && strpos( $cache, $url ) !== false ) {
$cnt = explode( "\nr:", $cache );
if( ctype_digit( trim( $cnt[1] ) ) === true ) {
$feedly_count = trim( $cnt[1] );
}
}
}
return $feedly_count;
}
endif;
/*---------------------------------------------------------------------------
* SNS カウントキャッシュの ajax 受け取り(フォーク処理の代替)
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_sns_cache' ) === false ):
function thk_sns_cache() {
global $luxe;
$url = $_POST['url'];
$sns_cache_start = false;
$sns = new sns_cache();
if(
isset( $luxe['sns_count_cache_force'] ) ||
( $url === home_url('/') && isset( $luxe['sns_toppage_view'] ) && isset( $luxe['sns_bottoms_count'] ) ) ||
( isset( $luxe['sns_tops_enable'] ) && isset( $luxe['sns_tops_count'] ) ) ||
( isset( $luxe['sns_bottoms_enable'] ) && isset( $luxe['sns_bottoms_count'] ) )
) {
$sns->touch_sns_count_cache( esc_url( $url ) );
$sns->set_transient_sns_count_cache( esc_url( $url ) );
}
if(
isset( $luxe['sns_count_cache_force'] ) ||
isset( $luxe['feedly_share_tops_button'] ) || isset( $luxe['feedly_share_bottoms_button'] )
) {
$sns->touch_sns_count_cache( esc_url( get_bloginfo( 'rss2_url' ) ) );
$sns->set_transient_sns_count_cache( esc_url( get_bloginfo( 'rss2_url' ) ) );
}
add_filter( 'shutdown', 'set_transient_sns_count_cache_weekly_cleanup', 95 );
exit;
}
endif;
/*---------------------------------------------------------------------------
* SNS カウントのリアルタイムでの取得 (ajax で取得)
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_sns_real' ) === false ):
function thk_sns_real() {
$sns = new sns_real();
$sns->thk_sns_real();
exit;
}
endif;
/*---------------------------------------------------------------------------
* AMP 用のパーマリンク取得
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_get_amp_permalink' ) === false ):
function thk_get_amp_permalink( $post_id ) {
$paged = get_query_var('page');
$structure = get_option( 'permalink_structure' );
if( empty( $paged ) ) $paged = 1;
if( $paged > 1 ) {
$amplink = wp_get_canonical_url( $post_id );
$amplink = add_query_arg( 'amp', 1, $amplink );
}
elseif( $structure != '' && $structure[0] !== '?' && $structure[1] !== '?' ) {
$amplink = trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( 'amp' );
}
else {
$amplink = add_query_arg( 'amp', 1, get_permalink( $post_id ) );
}
return $amplink;
}
endif;
/*---------------------------------------------------------------------------
* AMP の固定フロントページ ENDPOINT (FAKE)
*---------------------------------------------------------------------------*/
if ( function_exists( 'set_fake_root_endpoint_for_amp' ) === false ):
function set_fake_root_endpoint_for_amp() {
$page_on_front = wp_cache_get( 'page_on_front', 'luxe' );
if( $page_on_front === false ) {
$opts = wp_cache_get( 'alloptions', 'options' );
wp_cache_set( 'page_on_front', $opts['page_on_front'], 'luxe' );
$opts['show_on_front'] = 'posts';
$opts['page_on_front'] = 0;
wp_cache_replace( 'alloptions', $opts, 'options' );
}
}
endif;
if ( function_exists( 'remove_fake_root_endpoint_for_amp' ) === false ):
function remove_fake_root_endpoint_for_amp( $page_on_front ) {
$opts = wp_cache_get( 'alloptions', 'options' );
$opts['show_on_front'] = 'page';
$opts['page_on_front'] = $page_on_front;
wp_cache_replace( 'alloptions', $opts, 'options' );
}
endif;
/*---------------------------------------------------------------------------
* 画像の URL から attachemnt_id を取得する
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_get_image_id_from_url' ) === false ):
function thk_get_image_id_from_url( $attachment_url = '' ) {
global $wpdb;
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", $attachment_url ) );
if( empty( $attachment[0] ) ) {
/***
Source: https://wpshout.com/links/get-wordpress-images-attachment-id-url-php-function-pippin-williamson/
***/
$attachment_id = false;
// If there is no url, return.
if( empty( $attachment_url ) ) {
return false;
}
else {
$attachment_url = str_replace( array( 'http:', 'https:'), '', $attachment_url );
}
// Get the upload directory paths
$upload_dir_paths = wp_upload_dir();
$baseurl = str_replace( array( 'http:', 'https:'), '', $upload_dir_paths['baseurl'] );
// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
if( false !== strpos( $attachment_url, $baseurl ) ) {
// If this is the URL of an auto-generated thumbnail, get the URL of the original image
$attachment_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpe|jpeg|png|gif|bmp|webp)$)/i', '', $attachment_url );
// Remove the upload path base directory from the attachment URL
$attachment_url = str_replace( $baseurl . '/', '', $attachment_url );
// Finally, run a custom database query to get the attachment ID from the modified attachment URL
$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url ) );
}
return $attachment_id;
}
return $attachment[0];
}
endif;
/*---------------------------------------------------------------------------
* 画像の URL から画像情報を取得する
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_get_image_size' ) === false ):
function thk_get_image_size( $src ){
$ret = false;
if( stripos( $src, WP_CONTENT_URL ) !== false ) {
$src = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $src );
}
else {
return $ret;
}
$up_dir = wp_upload_dir();
if( is_callable( 'getimagesize' ) === true ) {
if( isset( $up_dir['baseurl'] ) && isset( $up_dir['basedir'] ) ) {
$replace = str_replace( $up_dir['baseurl'], $up_dir['basedir'], $src );
if( file_exists( $replace ) === true ) {
$ret = getimagesize( $replace );
}
}
}
return $ret;
}
endif;
/*---------------------------------------------------------------------------
* 画像の URL から srcset 付きの img タグを生成する
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_create_srcset_img_tag' ) === false ):
function thk_create_srcset_img_tag( $url, $alt = null, $cls = null, $prop_img = false, $prop_logo = false ) {
global $luxe;
$aid = thk_get_image_id_from_url( $url );
$meta = wp_get_attachment_metadata( $aid );
if( $alt === null ) $alt = get_post( $aid )->post_title;
$width = isset( $meta['width'] ) ? $meta['width'] : '';
$height = isset( $meta['height'] ) ? $meta['height'] : '';
if( empty( $width ) || empty( $height ) ) {
if( function_exists( 'getimagesize' ) ) {
$wp_upload_dir = wp_upload_dir();
$image_path = str_replace( $wp_upload_dir['baseurl'], $wp_upload_dir['basedir'], $url );
if( is_callable( 'getimagesize' ) === true && file_exists( $image_path ) === true ) {
$sizes = getimagesize( $image_path );
if( isset( $sizes[0] ) || isset( $sizes[1] ) ) {
$width = $sizes[0];
$height = $sizes[1];
}
}
}
}
$content = '
]+?)\s*\/*>/', '', $ret );
}
else {
$ret = preg_replace( '/
]+?)\s*\/*>/', '', $ret );
}
}
return $ret;
}
endif;
/*---------------------------------------------------------------------------
* 登録されてるサムネイルサイズの width と height の配列取得
*---------------------------------------------------------------------------*/
/***
Source: https://developer.wordpress.org/reference/functions/get_intermediate_image_sizes/
***/
function thk_get_image_sizes( $size = '' ) {
require_once( INC . 'thumbnail-images.php' );
thk_custom_image_sizes::custom_image_sizes( false );
global $_wp_additional_image_sizes;
$sizes = array();
$get_intermediate_image_sizes = get_intermediate_image_sizes();
// Create the full array with sizes and crop info
foreach( $get_intermediate_image_sizes as $_size ) {
if( in_array( $_size, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
$sizes[$_size]['width'] = (int)get_option( $_size . '_size_w' );
$sizes[$_size]['height'] = (int)get_option( $_size . '_size_h' );
}
elseif( isset( $_wp_additional_image_sizes[$_size] ) ) {
$sizes[ $_size ] = array(
'width' => (int)$_wp_additional_image_sizes[$_size]['width'],
'height' => (int)$_wp_additional_image_sizes[$_size]['height'],
);
}
}
// Get only 1 size if found
if( !empty( $size ) ) {
if( isset( $sizes[$size] ) ) {
return $sizes[$size];
}
else {
return false;
}
}
return $sizes;
}
/*---------------------------------------------------------------------------
* サムネイルが存在してるかどうかの判別
* 存在しなかったら ajax 経由で thk_regenerate_thumbnails をコールして自動再作成
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_thumbnail_exists' ) === false ):
function thk_thumbnail_exists( $attachment_id, $thumb = 'thumbnail' ) {
$image_meta = wp_get_attachment_metadata( $attachment_id );
if( !isset( $image_meta['sizes'][$thumb]['file'] ) ) {
$exists = false;
$image_sizes = thk_get_image_sizes();
foreach( $image_sizes as $key => $val ) {
if(
( isset( $image_meta['width'] ) && isset( $val['width'] ) && (int)$image_meta['width'] === (int)$val['width'] ) &&
( isset( $image_meta['height'] ) && isset( $val['height'] ) && (int)$image_meta['height'] === (int)$val['height'] )
) $exists = true;
}
if( $exists === false ) {
global $luxe, $_is;
if( $luxe['jquery_load'] !== 'none' ) {
// jQuery が有効な場合は ajax 経由でバックグラウンド処理
add_filter( 'wp_footer', function() use( $attachment_id ) {
thk_regenthumb_background_script_insert( $attachment_id );
}, 999 );
return false;
}
else {
// jQuery が無効化されてる場合
if( $_is['customize_preview'] === true ) {
thk_regenerate_thumbnails( $attachment_id );
thk_flash();
}
else {
add_action( 'shutdown', function() use( $attachment_id ) {
thk_regenerate_thumbnails( $attachment_id );
thk_flash();
});
}
}
}
}
else {
// attachment_metadata が空っぽの場合
$_wp_upload_dir = wp_upload_dir();
if( isset( $_wp_upload_dir['basedir'] ) && isset( $image_meta['file'] ) ) {
$thumb_file = $_wp_upload_dir['basedir'] . '/' . $image_meta['file'];
$thumb_file = substr( $thumb_file, 0, strrpos( $thumb_file, '/' ) );
$thumb_file .= '/' . $image_meta['sizes'][$thumb]['file'];
if( file_exists( $thumb_file ) === false ) {
global $luxe, $_is;
if( $luxe['jquery_load'] !== 'none' ) {
// jQuery が有効な場合は ajax 経由でバックグラウンド処理
add_filter( 'wp_footer', function() use( $attachment_id ) {
thk_regenthumb_background_script_insert( $attachment_id );
}, 999 );
return false;
}
else {
// jQuery が無効化されてる場合
if( $_is['customize_preview'] === true ) {
thk_regenerate_thumbnails( $attachment_id );
thk_flash();
}
else {
add_action( 'shutdown', function() use( $attachment_id ) {
thk_regenerate_thumbnails( $attachment_id );
thk_flash();
});
}
}
}
}
}
return true;
}
endif;
/*---------------------------------------------------------------------------
* サムネイルが存在しない場合の自動再作成
*---------------------------------------------------------------------------*/
// ajax スクリプトを HTML に埋め込む
if( function_exists( 'thk_regenthumb_background_script_insert' ) === false ):
function thk_regenthumb_background_script_insert( $attachment_id ) {
$attachment_nonce = wp_create_nonce( 'sec_' . $attachment_id );
?>resize( 18, 18, true );
$image->save( $ico_file );
}
}
}
add_action( 'customize_save_after', function() {
// 外観カスタマイズに変更があった場合
$theme_path = TPATH === SPATH ? TPATH : SPATH;
$del_func = 'un' . 'link';
@$del_func( $theme_path . DSEP . 'images' . DSEP . 'fav' . 'icon-min.png' );
thk_create_icon();
}, 70 );
endif;
/*---------------------------------------------------------------------------
* シンタックスハイライターの一覧
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_syntax_highlighter_list' ) === false ):
function thk_syntax_highlighter_list() {
return array(
'highlight_markup' => 'HTML / XHTML',
'highlight_apacheconf' => 'Apache Config',
'highlight_aspnet' => 'ASP.NET',
'highlight_autohotkey' => 'autoHotkey',
'highlight_bash' => 'Bash',
'highlight_basic' => 'Basic',
'highlight_batch' => 'Batch',
'highlight_clike' => 'Clike',
'highlight_c' => 'C',
'highlight_cpp' => 'C++',
'highlight_csharp' => 'C#',
'highlight_css' => 'CSS',
'highlight_diff' => 'Diff',
'highlight_git' => 'Git',
'highlight_java' => 'Java',
'highlight_javascript' => 'Javascript',
'highlight_json' => 'JSON',
'highlight_nginx' => 'nginx',
'highlight_nim' => 'Nim',
'highlight_perl' => 'Perl',
'highlight_php' => 'PHP',
'highlight_plsql' => 'PL/SQL',
'highlight_powershell' => 'PowerShell',
'highlight_python' => 'Python',
'highlight_r' => 'R',
'highlight_ruby' => 'Ruby',
'highlight_rust' => 'Rust',
'highlight_sass' => 'Sass',
'highlight_sql' => 'SQL',
'highlight_vbnet' => 'VB.NET',
'highlight_vim' => 'Vim',
);
}
endif;
/*---------------------------------------------------------------------------
* ブロックエディタのカラーパレット
*---------------------------------------------------------------------------*/
/* 基本色 */
if( function_exists( 'thk_block_editor_color_palette' ) === false ):
function thk_block_editor_color_palette() {
return [
[
'name' => 'White',
'slug' => 'white',
'color' => '#ffffff',
], [
'name' => 'Whitesmoke',
'slug' => 'whitesmoke',
'color' => '#f5f5f5',
], [
'name' => 'Lightgray',
'slug' => 'lightgray',
'color' => '#d3d3d3',
], [
'name' => 'Gray',
'slug' => 'gray',
'color' => '#808080',
], [
'name' => 'Black',
'slug' => 'black',
'color' => '#000000',
], [
'name' => 'Navy',
'slug' => 'navy',
'color' => '#000080',
], [
'name' => 'Blue',
'slug' => 'blue',
'color' => '#0000ff',
], [
'name' => 'Dodgerblue',
'slug' => 'dodgerblue',
'color' => '#1e90ff',
], [
'name' => 'Deepskyblue',
'slug' => 'deepskyblue',
'color' => '#00bfff',
], [
'name' => 'Aqua',
'slug' => 'aqua',
'color' => '#00ffff',
], [
'name' => 'Blueviolet',
'slug' => 'blueviolet',
'color' => '#8a2be2',
], [
'name' => 'Purple',
'slug' => 'purple',
'color' => '#800080',
], [
'name' => 'Magenta',
'slug' => 'magenta',
'color' => '#ff00ff',
], [
'name' => 'Red',
'slug' => 'red',
'color' => '#ff0000',
], [
'name' => 'Crimson',
'slug' => 'crimson',
'color' => '#dc143c',
], [
'name' => 'Saddlebrown',
'slug' => 'saddlebrown',
'color' => '#8b4513',
], [
'name' => 'Coral',
'slug' => 'coral',
'color' => '#ff7f50',
], [
'name' => 'Orange',
'slug' => 'orange',
'color' => '#ffa500',
], [
'name' => 'Pink',
'slug' => 'pink',
'color' => '#ffc0cb',
], [
'name' => 'Green',
'slug' => 'green',
'color' => '#008000',
], [
'name' => 'Springgreen',
'slug' => 'springgreen',
'color' => '#00ff7f',
], [
'name' => 'Greenyellow',
'slug' => 'greenyellow',
'color' => '#adff2f',
], [
'name' => 'Yellow',
'slug' => 'yellow',
'color' => '#ffff00',
], [
'name' => 'Lightyellow',
'slug' => 'lightyellow',
'color' => '#ffffe0',
],
];
}
endif;
/* 変更および追加分 */
if( function_exists( 'thk_get_block_editor_color_palette' ) === false ):
function thk_get_block_editor_color_palette() {
$mods = get_theme_admin_mods();
$color_palette = thk_block_editor_color_palette();
$c = count( $color_palette );
$i = 0;
// 元
foreach( $color_palette as $key => $val ) {
if( !empty( $mods['block_palette_color_' . $i] ) ) {
$color_palette[$key]['color'] = $mods['block_palette_color_' . $i];
if( !empty( $mods['block_palette_slug_' . $i] ) ) {
$color_palette[$key]['slug'] = $mods['block_palette_slug_' . $i];
}
else {
$color_palette[$key]['slug'] = thk_color_code_to_slug( $color_palette[$key]['color'] );
}
}
if( !empty( $mods['block_palette_name_' . $i] ) ) {
$color_palette[$key]['name'] = $mods['block_palette_name_' . $i];
}
++$i;
}
// 追加分
for( $i = $c; $c + 6 > $i; ++$i ) {
$ca = [];
if( !empty( $mods['block_palette_color_' . $i] ) ) {
$ca['color'] = $mods['block_palette_color_' . $i];
if( !empty( $mods['block_palette_name_' . $i] ) ) {
$ca['name'] = $mods['block_palette_name_' . $i];
if( !empty( $mods['block_palette_slug_' . $i] ) ) {
$ca['slug'] = $mods['block_palette_slug_' . $i];
}
else {
$ca['slug'] = thk_color_code_to_slug( $ca['color'] );
}
}
}
if( !empty( $ca ) ) {
$color_palette[$i] = $ca;
}
}
return $color_palette;
}
endif;
/* スラッグが空っぽの場合、無理矢理カラーコードを英字のみのスラッグにしちゃう */
if( function_exists( 'thk_color_code_to_slug' ) === false ):
function thk_color_code_to_slug( $color_code ) {
$slug = str_replace( '#', '', $color_code );
$a = [ '0' => 'g', '1' => 'h', '2' => 'i', '3' => 'j', '4' => 'k', '5' => 'l', '6' => 'm', '7' => 'n', '8' => 'o', '9' => 'p' ];
foreach( $a as $k => $v ) $slug = str_replace( $k, $v, $slug );
return $slug;
}
endif;
/*---------------------------------------------------------------------------
* flash
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_flash' ) === false ):
function thk_flash() {
if( ob_get_level() < 1 || ob_get_length() === false ) ob_start();
if( ob_get_length() !== false ) {
ob_flush();
flush();
ob_end_flush();
}
}
endif;
/*---------------------------------------------------------------------------
* CSSMin を使わない簡易 CSS 圧縮(動的に圧縮する必要がある時に使う)
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_simple_css_minify' ) === false ):
function thk_simple_css_minify( $css ){
if( !empty( $css ) ) {
$css = preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css );
$css = str_replace( array( "\r\n", "\r", "\n" ), '', $css );
$css = str_replace( "\t", ' ', $css );
$cnt = 1;
while( $cnt !== 0 ) {
$css = str_replace( ' ', ' ', $css, $cnt );
}
$css = str_replace( '{ ', '{', str_replace( ' {', '{', str_replace( '} ', '}', str_replace( ' }', '}', str_replace( '; ', ';', str_replace( ';}', '}', str_replace( ': ', ':', str_replace( ', ', ',', $css ) ) ) ) ) ) ) );
}
return trim( $css );
}
endif;
/*---------------------------------------------------------------------------
* random aria-label
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_random_alt_or_aria_label' ) === false ):
function thk_random_alt_or_aria_label( $imgs = array('Image'), $num = true ){
if( is_array( $imgs ) === true ) {
$key = array_rand( $imgs, 1 );
$img_item = $imgs[$key];
}
else {
$img_item = $imgs;
}
$num_item = '';
if( $num === true ) $num_item = ' ' . sprintf( '%03d', rand( 0, 200 ) );
return $img_item . $num_item;
}
endif;
/*---------------------------------------------------------------------------
* add alt to img tag
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_alt_attribute' ) === false ):
function thk_alt_attribute( $tag, $alt ){
if( stripos( $tag, 'alt=""' ) !== false ) {
$tag = str_replace( 'alt=""', 'alt="' . thk_random_alt_or_aria_label( $alt ) . '"', $tag );
}
elseif( stripos( $tag, 'alt="' ) === false ) {
$tag = str_replace( '
$val ) {
if( isset( $luxe['amp'] ) ) {
if( $key === 'wp_inactive_widgets' || strrpos( $key, '-amp') !== strlen( $key ) - 4 ) continue;
}
else {
if(
$key === 'wp_inactive_widgets' || strrpos( $key, '-amp') === strlen( $key ) - 4 ||
( $_is['home'] === false && $_is['front_page'] === false && strpos( $key, 'side-top-' ) === 0 ) ||
( ( $_is['home'] === true || $_is['front_page'] === true ) && strpos( $key, 'side-no-top-' ) === 0 )
) {
continue;
}
}
if( !empty( $val ) ) {
foreach( (array)$val as $k => $v ) {
// 再利用ブロックウィジェット
if( stripos( $v, 'thk_reusable_blocks_widget' ) === 0 ) {
$opt = get_option( 'widget_thk_reusable_blocks_widget' );
$num = (int)str_replace( 'thk_reusable_blocks_widget-', '', $v );
if( isset( $opt[$num]['block_id'] ) ) {
$post_data = get_post( $opt[$num]['block_id'] );
if( !empty( $post_data->post_content ) ) {
$contents .= $post_data->post_content;
}
}
}
// アドセンスウィジェット
elseif( stripos( $v, 'thk_adsense_widget' ) === 0 ) {
$opt = get_option( 'widget_thk_adsense_widget' );
$num = (int)str_replace( 'thk_adsense_widget-', '', $v );
if( !empty( $opt[$num]['text'] ) ) {
$contents .= $opt[$num]['text'];
}
}
// カスタム HTML ウィジェット
elseif( stripos( $v, 'custom_html' ) === 0 ) {
$opt = get_option( 'widget_custom_html' );
$num = (int)str_replace( 'custom_html-', '', $v );
if( !empty( $opt[$num]['content'] ) ) {
$contents .= $opt[$num]['content'];
}
}
/* 以下、$widget_concat に代入しないもの */
// 新着記事ウィジェットで横長サムネイルが使用されてる場合にAMP で style を挿入する用
elseif( stripos( $v, 'thk_recent_posts' ) === 0 ) {
if( isset( $luxe['amp'] ) ) {
$opt = get_option( 'widget_thk_recent_posts' );
$num = (int)str_replace( 'thk_recent_posts-', '', $v );
if( isset( $opt[$num]['thumb_type'] ) && $opt[$num]['thumb_type'] === 'wide' ) {
if( !isset( $luxe['thk_recent_posts_wide_thumb'] ) ) {
$luxe['thk_recent_posts_wide_thumb'] = '';
}
$luxe['thk_recent_posts_wide_thumb'] .= '#' . $v . ' .term {' . thk_recent_posts::term_style() . '}';
$luxe['thk_recent_posts_wide_thumb'] .= '#' . $v . ' .term amp-img {' . thk_recent_posts::img_style() . '}';
}
}
}
// swiper の CSS を preload するかどうかの判定用
elseif( stripos( $v, 'thk_swiper' ) === 0 ) {
if( !isset( $luxe['thk_swiper_preload'] ) ) {
$opt = get_option( 'widget_thk_swiper_widget' );
$num = (int)str_replace( 'thk_swiper_widget-', '', $v );
if(
( ( $_is['home'] === true || $_is['front_page'] === true ) && isset( $opt[$num]['top'] ) ) ||
( $_is['category'] === true && isset( $opt[$num]['category'] ) ) ||
( $_is['archive'] === true && isset( $opt[$num]['archive'] ) ) ||
( $_is['single'] === true && isset( $opt[$num]['single'] ) ) ||
( $_is['page'] === true && isset( $opt[$num]['page'] ) )
) {
$luxe['thk_swiper_preload'] = true;
}
}
}
}
}
}
// カスタムグローバルナビで表示される固定ページのコンテンツ (ver3.13.0 で追加)
$pages = get_pages( array(
//'parent' => 0,
//'hierarchical' => 0,
'post_type' => 'page',
'post_status' => 'publish',
));
foreach( (array)$pages as $value ) {
$page_template = get_post_meta( $value->ID, '_wp_page_template', true );
if( !empty( $page_template ) && stripos( $page_template, 'pages/wrapper-menu.php' ) !== false ) {
$contents .= $value->post_content;
}
}
$widget_concat = $contents;
}
endif;
/*---------------------------------------------------------------------------
* remote request ( wp_remote_request -> wp_filesystem )
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_remote_request' ) === false ):
function thk_remote_request( $url, $sslverify = false, $agent = null ){
// 2段階で取得を試みる ( wp_remote_request -> wp_filesystem )
$ret = false;
$code = 0;
if( $agent === null ) $agent = $_SERVER['HTTP_USER_AGENT'];
$results = wp_remote_request( $url, array(
'timeout' => 30,
'redirection' => 5,
'compress' => true, // 文字化け対策
'sslverify' => $sslverify, // 悩ましい
'user-agent' => $agent
) );
if( is_wp_error( $results ) === false ) {
$code = wp_remote_retrieve_response_code( $results );
if( $code !== 200 ) {
$msg = wp_remote_retrieve_response_message( $results );
$ret = array( $code, $msg );
}
else {
$ret = wp_remote_retrieve_body( $results );
}
}
/* この処理に来ることは、ほぼあり得ないけど、一応 $wp_filesystem->get_contents での取得も入れておく*/
if( $ret === false ) {
thk_filesystem_init();
global $wp_filesystem;
$ret = $wp_filesystem->get_contents( $url );
}
return $ret;
}
endif;
/*---------------------------------------------------------------------------
* thk_get_home_path ( get_home_path だと FULLPATH 取れないので )
*---------------------------------------------------------------------------*/
if( function_exists('thk_get_home_path') === false ):
function thk_get_home_path() {
$ret = str_replace( '/', DSEP, ABSPATH );
$home_url = home_url('/');
$site_url = site_url('/');
if( !empty( $home_url ) && strcasecmp( $home_url, $site_url ) !== 0 ) {
$home_path = str_replace( '/', DSEP, parse_url( $home_url, PHP_URL_PATH ) );
$site_path = str_replace( '/', DSEP, parse_url( $site_url, PHP_URL_PATH ) );
$pos = strripos( $ret, $site_path );
$ret = substr( $ret, 0, $pos ) . $home_path;
}
return $ret;
}
endif;
/*---------------------------------------------------------------------------
* remove URL
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_remove_url' ) === false ):
function thk_remove_url( $value ){
$url_reg = '/(https?|ftp|HTTPS?|FTP)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&;=+\$,%#]+)/';
return preg_replace( $url_reg, '', $value );
}
endif;
/*---------------------------------------------------------------------------
* URL Encode と Convert
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_encode' ) === false ):
function thk_encode( $value ){
return rawurlencode( thk_convert( $value ) );
}
endif;
if( function_exists( 'thk_convert' ) === false ):
function thk_convert( $value ){
if( empty( $value ) ) return;
if( stripos( $value, chr(0x00) ) !== false ) return;
mb_language( 'Japanese' );
$charcode = check_charcode( $value );
if( $charcode !== null && $charcode !== 'UTF-8' ) {
$value = mb_convert_encoding( $value, 'UTF-8', $charcode );
}
$detect = mb_detect_encoding( $value, 'ASCII,JIS,UTF-8,CP51932,SJIS-win', true );
if( $detect !== false ) {
return mb_convert_encoding( $value, 'UTF-8', $detect );
}
return $value;
}
endif;
// mb_detect_encoding でうまくいかない場合用
if( function_exists( 'check_charcode' ) === false ):
function check_charcode( $value ) {
if( empty( $value ) ) return;
$codes = array( 'UTF-8','SJIS-win','eucJP-win','ASCII','JIS','ISO-2022-JP-MS' );
foreach( $codes as $charcode ){
if( mb_convert_encoding( $value, $charcode, $charcode ) === $value ) {
return $charcode;
}
}
return null;
}
endif;
/*---------------------------------------------------------------------------
* URL Decode
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_decode' ) === false ):
function thk_decode( $value ){
while( $value !== rawurldecode( $value ) ) {
$value = rawurldecode( $value );
}
return $value;
}
endif;
/*---------------------------------------------------------------------------
* Punycode Encode
*---------------------------------------------------------------------------*/
if( function_exists( 'puny_encode' ) === false ):
function puny_encode( $value ) {
if( version_compare( PHP_VERSION, '5.4', '<' ) === true ) {
return $value;
}
if( class_exists('Punycode') === true ) {
$Punycode = new Punycode();
if( method_exists( $Punycode, 'encode' ) === true ) {
$parse = parse_url( $value );
if( isset( $parse['host'] ) ) {
$parse['host'] = $Punycode->encode( $parse['host'] );
$value = http_build_url( $value, $parse );
}
else {
$value = $Punycode->encode( $value );
}
}
}
return $value;
}
endif;
/*---------------------------------------------------------------------------
* Punycode Decode
*---------------------------------------------------------------------------*/
if( function_exists( 'puny_decode' ) === false ):
function puny_decode( $value ) {
if( version_compare( PHP_VERSION, '5.4', '<' ) === true ) {
return $value;
}
if( class_exists('Punycode') === true ) {
$Punycode = new Punycode();
if( method_exists( $Punycode, 'decode' ) === true ) {
$parse = parse_url( $value );
if( isset( $parse['host'] ) ) {
$parse['host'] = $Punycode->decode( $parse['host'] );
$value = http_build_url( $value, $parse );
}
else {
$value = $Punycode->decode( $value );
}
}
}
return $value;
}
endif;
/*---------------------------------------------------------------------------
* Error message
*---------------------------------------------------------------------------*/
if( function_exists( 'thk_error_msg' ) === false ):
function thk_error_msg( $result ) {
echo ''
, '
' . $result->get_error_message() . '
'
, '
' . $result->get_error_data() . '
'
, '
';
}
endif;
/*---------------------------------------------------------------------------
* mb_substr_replace
*---------------------------------------------------------------------------*/
if( function_exists( 'mb_substr_replace' ) === false ):
function mb_substr_replace( $str, $replace, $start, $length ) {
return mb_substr( $str, 0, $start ) . $replace . mb_substr( $str, $start + $length );
}
endif;
/*---------------------------------------------------------------------------
* URLを組み立て(PECL の http_build_url 代替版)
*---------------------------------------------------------------------------*/
if( function_exists( 'http_build_url' ) === false || function_exists( 'http_build_str') === false ) {
require( INC . 'http-build-url.php' );
}
/*---------------------------------------------------------------------------
* ブロックエディタが存在しない WP 5.0 未満のバージョン用
*---------------------------------------------------------------------------*/
if( function_exists('has_blocks') === false ) {
function has_blocks( $arg ) { return false; }
}