コード例 #1
0
/**
 * Generates a single taxonomy section for a given post type at the given entry index,
 * and uses the given and optional $meta options to set the values as selected in the fields.
 * 
 * @since 3.1
 * @param int $id The entry ID - the index/position of the section
 * @param string $post_type The post type used to determine the taxonomies
 * @param array $meta The array of meta options used to prepare the selected values
 * @return string The HTML output of the taxonomy sections
 */
function wprss_ftp_taxonomy_section( $id, $post_type, $meta = NULL ) {
	if ( $post_type == '' ) {
		$post_type = 'post';
	}
	if ( $meta === NULL ) {
		$meta = array(
			'taxonomy'	=>	'',
			'terms'		=>	'',
			'auto'		=>	'false'
		);
	}
	ob_start(); ?>

	<tr data-id="<?php echo $id; ?>" class="wprss-tr-hr ftp-taxonomy-section">
		<th>
			<?php
				$post_type_taxonomies = WPRSS_FTP_Utils::get_post_type_taxonomies( $post_type );
				if ( count($post_type_taxonomies) > 0 ) {
					echo WPRSS_FTP_Utils::array_to_select(
						$post_type_taxonomies,
						array(
							'id'		=>	'ftp-taxonomy-'.$id,
							'class'		=>	'ftp-taxonomy',
							'name'		=>	WPRSS_FTP_Meta::META_PREFIX . "post_taxonomy[$id]",
							'selected'	=>	$meta['taxonomy']
						)
					);
					/* The taxonomy 'selected' above might be saved in meta, but not in the list.
					 * Therefore, we need to check the REAL selected taxonomy, which could be either
					 * the taxonomy saved in meta also the one selected in the dropdown, or the first
					 * taxonomy in the dropdown if the saved taxonomy is not the dropdown.
					 */
					$selected_tax = $meta['taxonomy'];
					// If the saved taxonomy (also the one to be 'selected') is not present
					if ( !array_key_exists( $meta['taxonomy'], $post_type_taxonomies ) ) {
						// Use the first taxonomy in the list
						reset( $post_type_taxonomies );
						$selected_tax = key( $post_type_taxonomies );
					}

					// The edit taxonomy link - the 'href' attribute and the <span> child element will be populated via JS ?> 
					<a class="ftp-edit-tax" href="#" target="_blank" data-base-url="<?php echo admin_url('edit-tags.php?taxonomy='); ?>">
						<i class="fa fa-pencil"></i> Edit <span></span>
					</a>
					<?php
				}
			?>
		</th>

		<td>
			<?php
				if ( count($post_type_taxonomies) === 0 ) :
					$all_post_types = WPRSS_FTP_Meta::get_post_types();
					printf(
						__("<p id='ftp-no-taxonomies'>The Post Type <strong>%s</strong>  has no taxonomies available!</p>", WPRSS_TEXT_DOMAIN),
						$all_post_types[$post_type]
					);
				else :
					$tax_terms = WPRSS_FTP_Utils::get_taxonomy_terms( $selected_tax );
					if ( count($tax_terms) == 0 ) {
						echo wprss_ftp_no_terms_msg( $post_type_taxonomies[$selected_tax] );
					} else {
						echo WPRSS_FTP_Utils::array_to_select(
							$tax_terms,
							array(
								'id'			=>	'ftp-terms-'.$id,
								'class'			=>	'ftp-terms',
								'name'			=>	WPRSS_FTP_Meta::META_PREFIX . "post_terms[$id]",
								'selected'		=>	$meta['terms'],
								'multiple'		=>	TRUE
							)
						);
					}
					?>
			
					<p>
						<?php
							echo WPRSS_FTP_Utils::boolean_to_checkbox(
								$meta['auto'],
								array(
									'id'		=>	'ftp-auto-terms-'.$id,
									'class'		=>	'ftp-auto-terms',
									'name'		=>	WPRSS_FTP_Meta::META_PREFIX . "auto_terms[$id]",
									'value'		=>	'true',
								)
							);
						?>
						<label for="ftp-auto-terms-<?php echo $id; ?>"><?php _e('Auto create terms from the feed items for this taxonomy', WPRSS_TEXT_DOMAIN); ?></label>

						<span class="ftp-tax-row-buttons">
							<button title="<?php esc_attr_e("Refresh the terms, if they've changed", WPRSS_TEXT_DOMAIN); ?>" type="button" class="ftp-tax-section-refresh button-secondary" data-id="<?php echo $id; ?>">
								<fa class="fa fa-fw fa-refresh"></fa>
								<span><?php _e('Refresh terms', WPRSS_TEXT_DOMAIN); ?></span>
							</button>
					
							<button title="<?php esc_attr_e("Remove this entry", WPRSS_TEXT_DOMAIN); ?>" type="button" class="ftp-tax-section-remove button-secondary" data-id="<?php echo $id; ?>">
								<fa class="fa fa-fw fa-times"></fa>
								<span><?php _e('Remove row', WPRSS_TEXT_DOMAIN); ?></span>
							</button>
						</span>
					</p>
					<p>
						<?php
							$select = WPRSS_FTP_Utils::array_to_select(
								array(
									'title'		=> __('Feed Title', WPRSS_TEXT_DOMAIN),
									'content'	=> __('Feed Content', WPRSS_TEXT_DOMAIN)
								),
								array(
									'id'		=> 'ftp-tax-filter-subject-'.$id,
									'class'		=> 'ftp-tax-filter-subject',
									'name'		=> WPRSS_FTP_Meta::META_PREFIX . "filter_subject[$id]",
									'selected'	=> isset( $meta['filter_subject'] ) ? $meta['filter_subject'] : null,
									'multiple'	=> TRUE
								)
							);
							_e(
								sprintf(
									'Only apply the preceding terms if the %s contains all of the following keywords: ', 
									$select
								),
								WPRSS_TEXT_DOMAIN
							);
						?>
							<input
								type="text"
								id="ftp-tax-filter-keywords-<?php echo $id ?>"
								name="<?php echo WPRSS_FTP_Meta::META_PREFIX . "filter_keywords[$id]" ?>"
								value="<?php echo isset( $meta['filter_keywords'] ) ? $meta['filter_keywords'] : '' ?>"
								placeholder="<?php _e('Enter comma separated words or phrases', WPRSS_TEXT_DOMAIN) ?>"
								style="width:100%"
							/>
			<?php endif; ?>
		</td>
	</tr>

	<?php return ob_get_clean();
}