<commit_before><?php

function anno_image_popup_head_js() {
	if (isset($_GET['anno_action']) && $_GET['anno_action'] == 'image_popup') {
?>
	<script type="text/javascript">
	var post_id = <?php echo esc_js(anno_get_post_id()); ?>
	</script>
<?php
	}
}
add_action('admin_print_scripts', 'anno_image_popup_head_js');

function anno_image_popup_request_handler() {
	if (isset($_GET['anno_action']) && $_GET['anno_action'] == 'image_popup') {
		anno_popup_images_iframe_html();
		die();
	}
}
add_action('admin_head', 'anno_image_popup_request_handler');


function anno_image_popup_enqueue_scripts() {
	wp_enqueue_script('img-popup', get_bloginfo('template_directory').'/js/tinymce/plugins/annoimages/popup.js', array('jquery'));	
}
if (isset($_GET['anno_action']) && $_GET['anno_action'] == 'image_popup') {
	add_action('admin_enqueue_scripts', 'anno_image_popup_enqueue_scripts');
}

function anno_popup_images_iframe_html() {
	$errors = array();
	if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
		check_admin_referer('media-form');
		// Upload File button was clicked
		$id = media_handle_upload('async-upload', $_REQUEST['post_id']);
		unset($_FILES);
		if ( is_wp_error($id) ) {
			$errors['upload_error'] = $id;
			$id = false;
		}
	}	
	
	global $tab;

	$post_id = anno_get_post_id();

	$attachments = get_posts(array(
		'post_type' => 'attachment',
		'posts_per_page' => -1,
		'post_parent' => $post_id,
		'post_mime_type' => 'image',
		'order' => 'ASC'
	));	
?>
<body id="anno-popup-images">
<div id="anno-popup-images-inside" class="anno-mce-popup">
	<div class="anno-mce-popup-fields">
<?php 
		if ( !empty($id) ) {
			if ( is_wp_error($id) ) {
				echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div>';
				exit;
			}
		}
?>
		<table class="anno-images">
			<thead>
				<tr>
					<th scope="col" class="img-list-img"></th>
					<th scope="col" class="img-list-title"></th>
					<th scope="col" class="img-list-actions"></th>
				</tr>
			</thead>
			<tbody id="media-items">
<?php
	foreach ($attachments as $attachment_key => $attachment) {
		anno_popup_images_row_display($attachment);
		anno_popup_images_row_edit($attachment);
	}
?>		
			</tbody>
		</table>

		<?php anno_upload_form(); ?>
	</div>
</body>
<?php
}

/**
 * Loads the iframe for tinyMCE popup
 */ 
function anno_popup_images() {
	global $post;
	$query_args = array('anno_action' => 'image_popup', 'post' => $post->ID);
	$url = add_query_arg($query_args, admin_url());
?>
	<div id="anno-popup-images">
		<iframe class="" src="<?php echo $url; ?>" width="480px" height="600px"></iframe>
	</div>
<?php
}

function anno_popup_images_row_edit($attachment) {
		$img_url = wp_get_attachment_image_src($attachment->ID, 'anno_img_edit');
		
		$description = $attachment->post_content;
		$caption = $attachment->post_excerpt;
		
		$alt_text = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
		$display = get_post_meta($attachment->ID, '_anno_attachment_image_display', true);
		if (empty($display)) {
			$display = 'figure';
		}

		//@TODO worth storing all this data in a single meta row as a serialized array
		$label = get_post_meta($attachment->ID, '_anno_attachment_image_label', true);
		$copyright_statement = get_post_meta($attachment->ID, '_anno_attachment_image_copyright_statement', true);
		$copyright_holder = get_post_meta($attachment->ID, '_anno_attachment_image_copyright_holder', true);
		$license = get_post_meta($attachment->ID, '_anno_attachment_image_license', true);
		
		$url = get_post_meta($attachment->ID, '_anno_attachment_image_url', true);
		$file_url = wp_get_attachment_url($attachment->ID); 
		$link = get_attachment_link($attachment->ID);
		
		$img_size = get_post_meta($attachment->ID, '_anno_attachment_image_size', true);
		if (!$img_size) {
			$img_size = 'thumbnail';
		}
?>
			<tr>
				<td class="img-edit-td" colspan="3">
					<form id="<?php echo esc_attr('img-edit-'.$attachment->ID); ?>" class="anno-img-edit">
						<div class="img-edit-details">
							<img src="<?php echo esc_url($img_url[0]); ?>" alt="<?php echo esc_attr($attachment->post_title); ?>" class="img-list-img" />
							<label for="<?php echo esc_attr('img-alttext-'.$attachment->ID); ?>">
								<div><?php _ex('Alt Text', 'input label', 'anno'); ?></div>
								<input name="alt_text" type="text" id="<?php echo esc_attr('img-alttext-'.$attachment->ID); ?>" value="<?php echo esc_attr($alt_text); ?>" />
							</label>
							<label for="<?php echo esc_attr('img-description-'.$attachment->ID); ?>">
								<div><?php _ex('Description', 'input label', 'anno'); ?></div>
								<textarea name="description" id="<?php echo esc_attr('img-description-'.$attachment->ID); ?>"><?php echo esc_textarea($description); ?></textarea>
							</label>
						</div>
						<div class="img-url-input">
							<label for="<?php echo esc_attr('img-url-'.$attachment->ID); ?>">
								<input id="<?php echo esc_attr('img-url-'.$attachment->ID); ?>" type="text" name="url" value="<?php echo esc_attr($url); ?>" /><span><?php _e('URL', 'anno'); ?></span>
							</label>
							<div id="<?php echo esc_attr('img-url-buttons-'.$attachment->ID); ?>" class="img-url-buttons">
								<button type="button" class="button" title=""><?php _e('None', 'anno'); ?></button>
								<button type="button" class="button" title="<?php echo esc_attr($file_url); ?>"> <?php _e('File URL', 'anno'); ?></button>
								<button type="button" class="button" title="<?php echo esc_attr($link); ?>"><?php _e('Attachment Post URL', 'anno'); ?></button>
							</div>
						</div>
						<fieldset class="img-display">
							<legend><?php _ex('Display', 'legend', 'anno'); ?></legend>
							<label for="<?php echo esc_attr('img-display-figure-'.$attachment->ID); ?>" class="radio">
								<input type="radio" value="figure" name="display" class="img-display-selection img-display-figure" id="<?php echo esc_attr('img-display-figure-'.$attachment->ID); ?>"<?php checked($display, 'figure', true); ?> />
								<span><?php _ex('Display as Figure', 'input label', 'anno'); ?></span>
							</label>
							<label for="<?php echo esc_attr('img-display-inline-'.$attachment->ID); ?>" class="radio">
								<input type="radio" value="inline" name="display" class="img-display-selection img-display-inline" id="<?php echo esc_attr('img-display-inline-'.$attachment->ID); ?>"<?php checked($display, 'inline', true); ?> />
								<span><?php _ex('Display Inline', 'input label', 'anno'); ?></span>
							</label>
							<div id="<?php echo esc_attr('img-figure-details-'.$attachment->ID); ?>">
								<label for="<?php echo esc_attr('img-label-'.$attachment->ID); ?>"><commit_msg>removing URL from image wrapping - added as a TODO<commit_after><?php

function anno_image_popup_head_js() {
	if (isset($_GET['anno_action']) && $_GET['anno_action'] == 'image_popup') {
?>
	<script type="text/javascript">
	var post_id = <?php echo esc_js(anno_get_post_id()); ?>
	</script>
<?php
	}
}
add_action('admin_print_scripts', 'anno_image_popup_head_js');

function anno_image_popup_request_handler() {
	if (isset($_GET['anno_action']) && $_GET['anno_action'] == 'image_popup') {
		anno_popup_images_iframe_html();
		die();
	}
}
add_action('admin_head', 'anno_image_popup_request_handler');


function anno_image_popup_enqueue_scripts() {
	wp_enqueue_script('img-popup', get_bloginfo('template_directory').'/js/tinymce/plugins/annoimages/popup.js', array('jquery'));	
}
if (isset($_GET['anno_action']) && $_GET['anno_action'] == 'image_popup') {
	add_action('admin_enqueue_scripts', 'anno_image_popup_enqueue_scripts');
}

function anno_popup_images_iframe_html() {
	$errors = array();
	if ( isset($_POST['html-upload']) && !empty($_FILES) ) {
		check_admin_referer('media-form');
		// Upload File button was clicked
		$id = media_handle_upload('async-upload', $_REQUEST['post_id']);
		unset($_FILES);
		if ( is_wp_error($id) ) {
			$errors['upload_error'] = $id;
			$id = false;
		}
	}	
	
	global $tab;

	$post_id = anno_get_post_id();

	$attachments = get_posts(array(
		'post_type' => 'attachment',
		'posts_per_page' => -1,
		'post_parent' => $post_id,
		'post_mime_type' => 'image',
		'order' => 'ASC'
	));	
?>
<body id="anno-popup-images">
<div id="anno-popup-images-inside" class="anno-mce-popup">
	<div class="anno-mce-popup-fields">
<?php 
		if ( !empty($id) ) {
			if ( is_wp_error($id) ) {
				echo '<div id="media-upload-error">'.esc_html($id->get_error_message()).'</div>';
				exit;
			}
		}
?>
		<table class="anno-images">
			<thead>
				<tr>
					<th scope="col" class="img-list-img"></th>
					<th scope="col" class="img-list-title"></th>
					<th scope="col" class="img-list-actions"></th>
				</tr>
			</thead>
			<tbody id="media-items">
<?php
	foreach ($attachments as $attachment_key => $attachment) {
		anno_popup_images_row_display($attachment);
		anno_popup_images_row_edit($attachment);
	}
?>		
			</tbody>
		</table>

		<?php anno_upload_form(); ?>
	</div>
</body>
<?php
}

/**
 * Loads the iframe for tinyMCE popup
 */ 
function anno_popup_images() {
	global $post;
	$query_args = array('anno_action' => 'image_popup', 'post' => $post->ID);
	$url = add_query_arg($query_args, admin_url());
?>
	<div id="anno-popup-images">
		<iframe class="" src="<?php echo $url; ?>" width="480px" height="600px"></iframe>
	</div>
<?php
}

function anno_popup_images_row_edit($attachment) {
		$img_url = wp_get_attachment_image_src($attachment->ID, 'anno_img_edit');
		
		$description = $attachment->post_content;
		$caption = $attachment->post_excerpt;
		
		$alt_text = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
		$display = get_post_meta($attachment->ID, '_anno_attachment_image_display', true);
		if (empty($display)) {
			$display = 'figure';
		}

		//@TODO worth storing all this data in a single meta row as a serialized array
		$label = get_post_meta($attachment->ID, '_anno_attachment_image_label', true);
		$copyright_statement = get_post_meta($attachment->ID, '_anno_attachment_image_copyright_statement', true);
		$copyright_holder = get_post_meta($attachment->ID, '_anno_attachment_image_copyright_holder', true);
		$license = get_post_meta($attachment->ID, '_anno_attachment_image_license', true);
		
		$url = get_post_meta($attachment->ID, '_anno_attachment_image_url', true);
		$file_url = wp_get_attachment_url($attachment->ID); 
		$link = get_attachment_link($attachment->ID);
		
		$img_size = get_post_meta($attachment->ID, '_anno_attachment_image_size', true);
		if (!$img_size) {
			$img_size = 'thumbnail';
		}
?>
			<tr>
				<td class="img-edit-td" colspan="3">
					<form id="<?php echo esc_attr('img-edit-'.$attachment->ID); ?>" class="anno-img-edit">
						<div class="img-edit-details">
							<img src="<?php echo esc_url($img_url[0]); ?>" alt="<?php echo esc_attr($attachment->post_title); ?>" class="img-list-img" />
							<label for="<?php echo esc_attr('img-alttext-'.$attachment->ID); ?>">
								<div><?php _ex('Alt Text', 'input label', 'anno'); ?></div>
								<input name="alt_text" type="text" id="<?php echo esc_attr('img-alttext-'.$attachment->ID); ?>" value="<?php echo esc_attr($alt_text); ?>" />
							</label>
							<label for="<?php echo esc_attr('img-description-'.$attachment->ID); ?>">
								<div><?php _ex('Description', 'input label', 'anno'); ?></div>
								<textarea name="description" id="<?php echo esc_attr('img-description-'.$attachment->ID); ?>"><?php echo esc_textarea($description); ?></textarea>
							</label>
						</div>
<?php 
/*						@TODO Find a way to maintain wrapping URL data while adhering to DTD
						<div class="img-url-input">
							<label for="<?php echo esc_attr('img-url-'.$attachment->ID); ?>">
								<input id="<?php echo esc_attr('img-url-'.$attachment->ID); ?>" type="text" name="url" value="<?php echo esc_attr($url); ?>" /><span><?php _e('URL', 'anno'); ?></span>
							</label>
							<div id="<?php echo esc_attr('img-url-buttons-'.$attachment->ID); ?>" class="img-url-buttons">
								<button type="button" class="button" title=""><?php _e('None', 'anno'); ?></button>
								<button type="button" class="button" title="<?php echo esc_attr($file_url); ?>"> <?php _e('File URL', 'anno'); ?></button>
								<button type="button" class="button" title="<?php echo esc_attr($link); ?>"><?php _e('Attachment Post URL', 'anno'); ?></button>
							</div>
						</div>
*/
?>
						<fieldset class="img-display">
							<legend><?php _ex('Display', 'legend', 'anno'); ?></legend>
							<label for="<?php echo esc_attr('img-display-figure-'.$attachment->ID); ?>" class="radio">
								<input type="radio" value="figure" name="display" class="img-display-selection img-display-figure" id="<?php echo esc_attr('img-display-figure-'.$attachment->ID); ?>"<?php checked($display, 'figure', true); ?> />
								<span><?php _ex('Display as Figure', 'input label', 'anno'); ?></span>
							</label>
							<label for="<?php echo esc_attr('img-display-inline-'.$attachment->ID); ?>" class="radio">
								<input type="radio" value="inline" name="display" class="img-display-selection img-display-inline" id="<?php echo esc_attr('img-display-inline-'.$attachment->ID); ?>"<?php checked($display, 'inline', true); ?> />
								<span><?php _ex('Display Inline', 'input label', 'anno'); ?></span>
							</label>
							<div id="<?php echo esc_attr('img-figure-details-'.$attachment->ID); ?>">
								<label for="<?php echo esc_attr('img-label-'.$attachment->ID); ?>">
{'fraction_non_alphanumeric': 0.21001855287569574, 'fraction_numerical': 0.0013358070500927644, 'mean_word_length': 4.7688356164383565, 'pattern_counts': {'":': 0, '<': 234, '<?xml version=': 0, '>': 310, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '525893', 'n_tokens_mistral': 5220, 'n_tokens_neox': 4634, 'n_words': 840}