<?php if ( !defined( 'ABSPATH' ) ) exit;//PHPの宣言と直接アクセスの拒否

/***** カスタム投稿タイプを含めた対応 *****/
/*** フロントエンド ***/
/* 投稿一覧へ追加 */
function haup_post_list_include_all_posttype( $query ) {
$args = array('public'   => true);
	$post_types = array_diff( get_post_types( $args), [ 'page','attachment' ]);

//特定のリストのみに作用させるためのおまじない
	if ( is_admin() || ! $query->is_main_query()) {
		return;
	}

//特定の一覧のみに作用させる
	if ( $query->is_home() || $query->is_author() || $query->is_category() || $query->is_tag()) {
		$query->set( 'post_type', $post_types );
		return;
	}
}

if(isset( GetOP08_utility()['cpt-convine-frontend-lists'] )){
	$haup_cpt_convine_flist = GetOP08_utility()['cpt-convine-frontend-lists'];
}else{
	$haup_cpt_convine_flist = '';
}

if($haup_cpt_convine_flist == '1'){
add_action( 'pre_get_posts', 'haup_post_list_include_all_posttype');
}

/*** バックエンド ***/
/* ダッシュボードへ記事数追加 */
function haup_detail_widget_in_all_post_type() {
//投稿タイプを抽出
	$args = array(
		'public' => true,
		'_builtin' => false
	);

	$output = 'object';
	$operator = 'and';
 
	$post_types = get_post_types( $args, $output, $operator );

//投稿タイプ分だけ投稿タイプ名と数を抽出
	foreach ( $post_types as $post_type ) {
		$num_posts = wp_count_posts( $post_type->name );
		$num = number_format_i18n( $num_posts->publish );
		//20240622 特に_n()で出力する必要がないので直接投稿タイプ名を出力するよう改修
		//$text = _n( $post_type->labels->singular_name, $post_type->labels->name, intval( $num_posts->publish ) );
		$text = $post_type->labels->name;

//一覧へ投稿タイプと投稿数の一覧を出力
	if ( current_user_can( 'edit_posts' ) ) {
		$output = '<a href="edit.php?post_type=' . $post_type->name . '">' . $num . '&nbsp;' . $text . '</a>';

	}
	echo '<li class="post-count ' . esc_html($post_type->name) . '-count">' . wp_kses_post($output) . '</li>';

	}
}

if(isset( GetOP08_utility()['cpt-convine-admin-detail'] )){
	$haup_cpt_admin_detail = GetOP08_utility()['cpt-convine-admin-detail'];
}else{
	$haup_cpt_admin_detail = '';
}

if($haup_cpt_admin_detail == '1'){
add_action( 'dashboard_glance_items', 'haup_detail_widget_in_all_post_type' );
}

/* アクティビティへ追加 */
function haup_activity_in_all_post_type( $query_args ) {
$args = array('public' => true);
//固定ページとattachmentを除外してすべての投稿タイプを取得
$post_types = array_diff( get_post_types( $args), [ 'page','attachment' ]);

$query_args['post_type'] = $post_types;
	if ( $query_args['post_status'] == "publish" )
		$query_args['posts_per_page'] = 10;
	return $query_args;
}

if(isset( GetOP08_utility()['cpt-convine-admin-activity'] )){
	$haup_cpt_admin_activity = GetOP08_utility()['cpt-convine-admin-activity'];
}else{
	$haup_cpt_admin_activity = '';
}

if($haup_cpt_admin_activity == '1'){
add_filter( 'dashboard_recent_posts_query_args', 'haup_activity_in_all_post_type', 10, 1 );
}

/*** その他 ***/
/* RSS */
function haup_rss_feed_in_all_post_type($qv) {
//固定ページとattachmentを除外してすべての投稿タイプを取得
	$args = array('public'   => true);
	$posttypes_array = array_diff( get_post_types( $args), [ 'page','attachment' ]);

if (isset($qv['feed']) && !isset($qv['post_type'])){
	$qv['post_type'] = $posttypes_array;
}

	return $qv;
}

if(isset( GetOP08_utility()['cpt-convine-rss-feed'] )){
	$haup_cpt_convine_feed = GetOP08_utility()['cpt-convine-rss-feed'];
}else{
	$haup_cpt_convine_feed = '';
}

if($haup_cpt_convine_feed == '1'){
add_filter('request', 'haup_rss_feed_in_all_post_type');
}

/* ping送信 */
function haup_do_ping_in_all_post_type( $post_id , $new_status='' , $old_status='' ) {
//固定ページとattachmentを除外してすべての投稿タイプを取得
	$args = array('public'   => true);
	$post_types = array_diff( get_post_types( $args), [ 'page','attachment' ]);
//公開へステータスが変わったら、スケジュールをセットする
	if( 'publish' == $new_status && 'publish' != $old_status && $post->post_type = $post_types ) {
	wp_schedule_single_event( strtotime( '+10 min' ), 'do_pings', array( $post_id ) );
	}
}

if(isset( GetOP08_utility()['cpt-convine-ping'] )){
	$haup_cpt_convine_ping = GetOP08_utility()['cpt-convine-ping'];
}else{
	$haup_cpt_convine_ping = '';
}

if($haup_cpt_convine_ping == '1'){
add_action( 'transition_post_status', 'haup_do_ping_in_all_post_type', 10, 1 );
}