field-group-locations.php 4.27 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
<?php 

// vars
$rule_types = apply_filters('acf/location/rule_types', array(
	__("Post",'acf') => array(
		'post_type'		=>	__("Post Type",'acf'),
		'post_template'	=>	__("Post Template",'acf'),
		'post_status'	=>	__("Post Status",'acf'),
		'post_format'	=>	__("Post Format",'acf'),
		'post_category'	=>	__("Post Category",'acf'),
		'post_taxonomy'	=>	__("Post Taxonomy",'acf'),
		'post'			=>	__("Post",'acf')
	),
	__("Page",'acf') => array(
		'page_template'	=>	__("Page Template",'acf'),
		'page_type'		=>	__("Page Type",'acf'),
		'page_parent'	=>	__("Page Parent",'acf'),
		'page'			=>	__("Page",'acf')
	),
	__("User",'acf') => array(
		'current_user'		=>	__("Current User",'acf'),
		'current_user_role'	=>	__("Current User Role",'acf'),
		'user_form'			=>	__("User Form",'acf'),
		'user_role'			=>	__("User Role",'acf')
	),
	__("Forms",'acf') => array(
		'attachment'	=>	__("Attachment",'acf'),
		'taxonomy'		=>	__("Taxonomy Term",'acf'),
		'comment'		=>	__("Comment",'acf'),
		'widget'		=>	__("Widget",'acf')
	)
));


// WP < 4.7
if( acf_version_compare('wp', '<', '4.7') ) {
	
	unset( $rule_types[ __("Post",'acf') ]['post_template'] );
	
}

$rule_operators = apply_filters( 'acf/location/rule_operators', array(
	'=='	=>	__("is equal to",'acf'),
	'!='	=>	__("is not equal to",'acf'),
));
						
?>
<div class="acf-field">
	<div class="acf-label">
		<label><?php _e("Rules",'acf'); ?></label>
		<p class="description"><?php _e("Create a set of rules to determine which edit screens will use these advanced custom fields",'acf'); ?></p>
	</div>
	<div class="acf-input">
		<div class="rule-groups">
			
			<?php foreach( $field_group['location'] as $group_id => $group ): 
				
				// validate
				if( empty($group) ) {
				
					continue;
					
				}
				
				
				// $group_id must be completely different to $rule_id to avoid JS issues
				$group_id = "group_{$group_id}";
				$h4 = ($group_id == "group_0") ? __("Show this field group if",'acf') : __("or",'acf');
				
				?>
			
				<div class="rule-group" data-id="<?php echo $group_id; ?>">
				
					<h4><?php echo $h4; ?></h4>
					
					<table class="acf-table -clear">
						<tbody>
							<?php foreach( $group as $rule_id => $rule ): 
								
								// valid rule
								$rule = wp_parse_args( $rule, array(
									'field'		=>	'',
									'operator'	=>	'==',
									'value'		=>	'',
								));
								
															
								// $group_id must be completely different to $rule_id to avoid JS issues
								$rule_id = "rule_{$rule_id}";
								
								?>
								<tr data-id="<?php echo $rule_id; ?>">
								<td class="param"><?php 
									
									// create field
									acf_render_field(array(
										'type'		=> 'select',
										'prefix'	=> "acf_field_group[location][{$group_id}][{$rule_id}]",
										'name'		=> 'param',
										'value'		=> $rule['param'],
										'choices'	=> $rule_types,
										'class'		=> 'location-rule-param'
									));
		
								?></td>
								<td class="operator"><?php 	
									
									// create field
									acf_render_field(array(
										'type'		=> 'select',
										'prefix'	=> "acf_field_group[location][{$group_id}][{$rule_id}]",
										'name'		=> 'operator',
										'value'		=> $rule['operator'],
										'choices' 	=> $rule_operators,
										'class'		=> 'location-rule-operator'
									)); 	
									
								?></td>
								<td class="value"><?php 
									
									$this->render_location_value(array(
										'group_id'	=> $group_id,
										'rule_id'	=> $rule_id,
										'value'		=> $rule['value'],
										'param'		=> $rule['param'],
										'class'		=> 'location-rule-value'
									)); 
									
								?></td>
								<td class="add">
									<a href="#" class="button add-location-rule"><?php _e("and",'acf'); ?></a>
								</td>
								<td class="remove">
									<a href="#" class="acf-icon -minus remove-location-rule"></a>
								</td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
					
				</div>
			<?php endforeach; ?>
			
			<h4><?php _e("or",'acf'); ?></h4>
			
			<a href="#" class="button add-location-group"><?php _e("Add rule group",'acf'); ?></a>
			
		</div>
	</div>
</div>
<script type="text/javascript">
if( typeof acf !== 'undefined' ) {
		
	acf.postbox.render({
		'id': 'acf-field-group-locations',
		'label': 'left'
	});	

}
</script>