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

/* https://wordpress.org/plugins/code-click-to-copy/ */
/* Code Click-to-Copy by WPJohnnyプラグインからコード流用 */
function haup_codecopy_activate(){
	$click_to_copy_str = __('Click to Copy', 'hima-art-utility');
	$copied_str = __('Copied!', 'hima-art-utility');

//背景色
	if(isset( GetOP05_blockoption()['codeblock-copy-button-bg-color'] )){
		$mbg_color = GetOP05_blockoption()['codeblock-copy-button-bg-color'];
	}else{
		$mbg_color = '';
	}

	if(empty( $mbg_color )){
		$message_bg_color = sanitize_hex_color('#333');
	}else{
		$message_bg_color = sanitize_hex_color($mbg_color);
	}

//文字色
	if(isset( GetOP05_blockoption()['codeblock-copy-button-text-color'] )){
		$mt_color = GetOP05_blockoption()['codeblock-copy-button-text-color'];
	}else{
		$mt_color = '';
	}

	if(empty( $mt_color )){
		$message_text_color = sanitize_hex_color('#fff');
	}else{
		$message_text_color = sanitize_hex_color($mt_color);
	}

//枠線色
	if(isset( GetOP05_blockoption()['codeblock-copy-button-border-color'] )){
		$mb_color = GetOP05_blockoption()['codeblock-copy-button-border-color'];
	}else{
		$mb_color = '';
	}

	if(empty( $mb_color )){
		$message_border_color = sanitize_hex_color('#111');
	}else{
		$message_border_color = sanitize_hex_color($mb_color);
	}


echo '<div class="ha-codecopy-tooltip" style="display: inline-block; background:';
echo esc_html($message_bg_color);
echo '; color:';
echo esc_html($message_text_color);
echo '; padding: 0 8px; font-size: 14px; border-radius: 2px; border: 1px solid ';
echo esc_html($message_border_color);
echo '; position:absolute; display: none;">';
echo esc_html($click_to_copy_str);
echo '</div>';


echo '<script type="text/javascript">';
echo "function haup_codecopy_get_element_position(el) {
		var rect = el.getBoundingClientRect(),
		scrollLeft = window.pageXOffset || document.documentElement.scrollLeft,
		scrollTop = window.pageYOffset || document.documentElement.scrollTop;
		return { top: rect.top + scrollTop - 20, left: rect.left + scrollLeft };
	}";
echo "function haup_codecopy_apply(element) {
		element.addEventListener('click', function() {
			event.stopPropagation();
			const el = document.createElement('textarea');
			el.value = element.textContent;
			document.body.appendChild(el);
			el.select();
			document.execCommand('copy');
			document.body.removeChild(el);
			ha_codecopy_tooltip.innerHTML = '";
echo esc_html($copied_str);
echo ";
		});
		element.addEventListener('mouseover', function() {
			event.stopPropagation();
			var position = haup_codecopy_get_element_position(element);
			ha_codecopy_tooltip.innerHTML = '";
echo esc_html($click_to_copy_str);
echo "';
			ha_codecopy_tooltip.style.display = 'inline-block';
			ha_codecopy_tooltip.style.top = position.top + 'px';
			ha_codecopy_tooltip.style.left = position.left + 'px';
		});
		element.addEventListener('mouseout', function() {
			event.stopPropagation();
			var position = haup_codecopy_get_element_position(element);
			ha_codecopy_tooltip.style.display = 'none';
			ha_codecopy_tooltip.style.top ='9999px';
		});
	}

	var ha_codecopy_tooltip = document.querySelector('.ha-codecopy-tooltip');

	document.querySelectorAll('code').forEach(function(element) {
		haup_codecopy_apply(element);
	});";
echo '</script>';

}
add_action('wp_footer', 'haup_codecopy_activate');

/***** 「コピー機能を停止」を選択したものは停止 *****/
function haup_disable_specific_code_block_copy(){
if(isset( GetOP05_blockoption()['disable-spec-codeblock-copy-button'] ) && GetOP05_blockoption()['disable-spec-codeblock-copy-button'] == 1 ){
$custom_css_pre = hau_sanitize_css('
/* HAUP Disable Code Copy Function */
.ha-codecopy-tooltip pre.is-style-no-copy {
    display: none!important;
 }

pre.wp-block-code.is-style-no-copy {
    pointer-events: none;
}
');
}
	
//タブを除去
$custom_css_deltab = preg_replace('/\t+/', '', $custom_css_pre);
//改行を除去
$custom_css = str_replace(PHP_EOL, '', $custom_css_deltab);

//インラインで出力
	wp_add_inline_style( 'haup-front-and-blocks-style', $custom_css );

}
add_action('wp_enqueue_scripts','haup_disable_specific_code_block_copy');


function haup_disable_specific_code_block_copy_editor(){
if(isset( GetOP05_blockoption()['disable-spec-codeblock-copy-button'] ) && GetOP05_blockoption()['disable-spec-codeblock-copy-button'] == 1 ){
$custom_css_pre = hau_sanitize_css('
/*.ha-codecopy-tooltip pre.is-style-no-copy {
    display: none!important;
 }*/

pre.is-style-no-copy:hover {
    background-color: unset!important;
    border: 1px solid #f5f5f5!important;
}

');
}
	
//タブを除去
$custom_css_deltab = preg_replace('/\t+/', '', $custom_css_pre);
//改行を除去
$custom_css = str_replace(PHP_EOL, '', $custom_css_deltab);

//エディター側へインライン出力
	wp_add_inline_style( 'wp-edit-blocks', $custom_css );
}
add_action('init','haup_disable_specific_code_block_copy_editor');