pmai_is_acf_update_allowed.php 7.13 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
<?php

function pmai_is_acf_update_allowed( $cur_meta_key, $options ){

	if ($options['update_acf_logic'] == 'full_update') return apply_filters('pmai_is_acf_update_allowed', true, $cur_meta_key, $options);	

	global $acf;

	if ($acf and version_compare($acf->settings['version'], '5.0.0') >= 0){

		// Update only these ACF, leave the rest alone
		if ($options['update_all_data'] == 'no' and $options['is_update_acf'] and $options['update_acf_logic'] == 'only'){			

			$is_acf_update_allowed = false;

			if (! empty($options['acf_list']) and is_array($options['acf_list'])){
				foreach ($options['acf_list'] as $key => $acf_field) {				
					$field_name = trim(array_shift(explode(" ", $acf_field)), "[]");												
					if ( $cur_meta_key == $field_name or $cur_meta_key == "_" . $field_name or preg_match('%'.$field_name.'%_[0-9]{1,}_', $cur_meta_key) or strpos($cur_meta_key, '_' . $field_name . '_') === 0 or preg_match('%.*_[0-9]{1,}_'.$field_name.'$%', $cur_meta_key)){
						$is_acf_update_allowed = true;					
						break;
					}				
				}								
			}		
			
			return apply_filters('pmai_is_acf_update_allowed', $is_acf_update_allowed, $cur_meta_key, $options);		

		}

		// Leave these ACF alone, update all other ACF
		if ($options['update_all_data'] == 'no' and $options['is_update_acf'] and $options['update_acf_logic'] == 'all_except'){
			
			if (! empty($options['acf_list']) and is_array($options['acf_list'])){
				foreach ($options['acf_list'] as $key => $acf_field) {
					$field_name = trim(array_shift(explode(" ", $acf_field)), "[]");
					if ( $cur_meta_key == $field_name or $cur_meta_key == "_" . $field_name or preg_match('%'.$field_name.'%_[0-9]{1,}_', $cur_meta_key) or strpos($cur_meta_key, '_' . $field_name . '_') === 0 or preg_match('%.*_[0-9]{1,}_'.$field_name.'$%', $cur_meta_key)){
						return apply_filters('pmai_is_acf_update_allowed', false, $cur_meta_key, $options);
						break;
					}
				}
			}		
		}

		// Update only mapped ACF fields
		if ($options['update_all_data'] == 'no' and $options['is_update_acf'] and $options['update_acf_logic'] == 'mapped'){
				
			$mapped_acf = $options['acf'];

			if ( ! empty($mapped_acf)){			
				foreach ($mapped_acf as $acf_group_id => $is_mapped) {				
					if ( ! $is_mapped ) continue;				
					$acf_fields = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field', 'post_parent' => $acf_group_id, 'post_status' => 'publish'));
					if ( ! empty($acf_fields) ){
						foreach ($acf_fields as $field) {
							if ( $cur_meta_key == $field->post_excerpt or $cur_meta_key == "_" . $field->post_excerpt or strpos($cur_meta_key, $field->post_excerpt . '_') === 0 or strpos($cur_meta_key, '_' . $field->post_excerpt . '_') === 0){
								return apply_filters('pmai_is_acf_update_allowed', true, $cur_meta_key, $options);
								break;
							}
						}				
					}
				}			
			}

			return apply_filters('pmai_is_acf_update_allowed', false, $cur_meta_key, $options);	
		}

		return apply_filters('pmai_is_acf_update_allowed', true, $cur_meta_key, $options);
		
	}
	else{

		// Update only these ACF, leave the rest alone
		if ($options['update_all_data'] == 'no' and $options['is_update_acf'] and $options['update_acf_logic'] == 'only'){
			
			if (! empty($options['acf_list']) and is_array($options['acf_list'])){
				foreach ($options['acf_list'] as $key => $acf_field) {
					$field_parts = explode('---', $acf_field);					
					$parts_temp = explode(" ", $field_parts[0]);
					$field_name = trim(array_shift($parts_temp), "[]");				
					if (!empty($field_parts[1])){
						$sub_field_name = trim($field_parts[1], "[]");
						if (preg_match('%^_{0,1}'.$field_name.'_[0-9]{1,}_'.$sub_field_name.'$%', $cur_meta_key)){
							return apply_filters('pmai_is_acf_update_allowed', true, $cur_meta_key, $options);
							break;
						}
					}
					elseif ( preg_match('%^_{0,1}'.$field_name.'$%', $cur_meta_key) ){
						return apply_filters('pmai_is_acf_update_allowed', true, $cur_meta_key, $options);
						break;
					}				
				}
				return apply_filters('pmai_is_acf_update_allowed', false, $cur_meta_key, $options);
			}					

		}

		// Leave these ACF alone, update all other ACF
		if ($options['update_all_data'] == 'no' and $options['is_update_acf'] and $options['update_acf_logic'] == 'all_except'){
			
			if (! empty($options['acf_list']) and is_array($options['acf_list'])){
				foreach ($options['acf_list'] as $key => $acf_field) {
					$field_parts = explode('---', $acf_field);
					$field_name = trim(array_shift(explode(" ", $field_parts[0])), "[]");
					if (!empty($field_parts[1])){
						$sub_field_name = trim($field_parts[1], "[]");
						if (preg_match('%^_{0,1}'.$field_name.'_[0-9]{1,}_'.$sub_field_name.'$%', $cur_meta_key)){
							return apply_filters('pmai_is_acf_update_allowed', false, $cur_meta_key, $options);
							break;
						}
					}
					elseif ( preg_match('%^_{0,1}'.$field_name.'$%', $cur_meta_key) ){
						return apply_filters('pmai_is_acf_update_allowed', false, $cur_meta_key, $options);
						break;
					}
				}
			}		
		}

		// Update only mapped ACF fields
		if ($options['update_all_data'] == 'no' and $options['is_update_acf'] and $options['update_acf_logic'] == 'mapped'){
				
			$mapped_acf = $options['acf'];

			if ( ! empty($mapped_acf)){			
				
				$all_acf_fields = array();

				foreach ($mapped_acf as $acf_group_id => $is_mapped) {				
					if ( ! $is_mapped ) continue;
					$acf_fields = get_post_meta($acf_group_id, '');
					if (!empty($acf_fields)){
						foreach ($acf_fields as $meta_key => $cur_meta_val){
							
							if (strpos($meta_key, 'field_') !== 0) continue;

							$field = (!empty($cur_meta_val[0])) ? unserialize($cur_meta_val[0]) : array();

							if ( ! in_array($field['name'], $all_acf_fields) ) $all_acf_fields[] = $field['name'];

							if (!empty($field['sub_fields'])){
								foreach ($field['sub_fields'] as $sub_field) {
									if ( ! in_array($sub_field['name'], $all_acf_fields) ) $all_acf_fields[] = $sub_field['name'];		
								}
							}
						}
					}
				}
				if ( in_array($cur_meta_key, $all_acf_fields)){
					foreach ($mapped_acf as $acf_group_id => $is_mapped) {				
						if ( ! $is_mapped ) continue;
						$acf_fields = get_post_meta($acf_group_id, '');
						if (!empty($acf_fields)){
							foreach ($acf_fields as $meta_key => $cur_meta_val){
								
								if (strpos($meta_key, 'field_') !== 0) continue;
								
								$field = (!empty($cur_meta_val[0])) ? unserialize($cur_meta_val[0]) : array();

								if ( preg_match('%^_{0,1}'.$field['name'].'$%', $cur_meta_key) ){
									return apply_filters('pmai_is_acf_update_allowed', true, $cur_meta_key, $options);
									break;
								}

								if (!empty($field['sub_fields'])){
									foreach ($field['sub_fields'] as $sub_field) {
										if (preg_match('%^_{0,1}'.$field['name'].'_[0-9]{1,}_'.$sub_field['name'].'$%', $cur_meta_key)){
											return apply_filters('pmai_is_acf_update_allowed', true, $cur_meta_key, $options);
											break;
										}
									}
								}
							}
						}
					}
					return apply_filters('pmai_is_acf_update_allowed', false, $cur_meta_key, $options);
				}			
			}
		}

		return apply_filters('pmai_is_acf_update_allowed', true, $cur_meta_key, $options);		

	}
	
}

?>