wp_ajax_get_acf.php 4.26 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
<?php

function pmai_wp_ajax_get_acf(){

	if ( ! check_ajax_referer( 'wp_all_import_secure', 'security', false )){
		exit( json_encode(array('html' => __('Security check', 'wp_all_import_acf_add_on'))) );
	}

	if ( ! current_user_can('manage_options') ){
		exit( json_encode(array('html' => __('Security check', 'wp_all_import_acf_add_on'))) );
	}

	global $acf;

	$version = ($acf) ? $acf->settings['version'] : false;	

	ob_start();	

	$acf_groups = PMXI_Plugin::$session->acf_groups;

	$acf_obj = false;

	if (!empty($acf_groups)){
		foreach ($acf_groups as $key => $group) {			
			if ($group['ID'] == $_GET['acf']){
				$acf_obj = $group;
				break;				
			}
		}
	}	
	
	$import = new PMXI_Import_Record();				
	
	if ( ! empty($_GET['id']) )
		$import->getById($_GET['id']);

	$is_loaded_template = (!empty(PMXI_Plugin::$session->is_loaded_template)) ? PMXI_Plugin::$session->is_loaded_template : false;

	if ($is_loaded_template){
		$default = PMAI_Plugin::get_default_import_options();
		$template = new PMXI_Template_Record();
		if ( ! $template->getById($is_loaded_template)->isEmpty()) {	
			$options = (!empty($template->options) ? $template->options : array()) + $default;														
		}

	}
	else
	if ( ! $import->isEmpty() ) {
		$options = $import->options;
	}
	else
	{
		$options = PMXI_Plugin::$session->options;
	}	
	?>
	<div class="postbox  acf_postbox default acf_signle_group rad4" rel="<?php echo $acf_obj['ID']; ?>">
		<h3 class="hndle" style="margin-top:0;"><span><?php echo $acf_obj['title']; ?></span></h3>
		<div class="inside">
		<?php

			if ($version and version_compare($version, '5.0.0') >= 0){								

				if ( is_numeric($acf_obj['ID'])){
					$acf_fields = get_posts(array('posts_per_page' => -1, 'post_type' => 'acf-field', 'post_parent' => $_GET['acf'], 'post_status' => 'publish', 'orderby' => 'menu_order', 'order' => 'ASC'));				

					if ( ! empty($acf_fields) ){

						foreach ($acf_fields as $field) {				

							$fieldData = (!empty($field->post_content)) ? unserialize($field->post_content) : array();			
							
							$fieldData['ID']    = $field->ID;
							$fieldData['id']    = $field->ID;
							$fieldData['label'] = $field->post_title;
							$fieldData['key']   = $field->post_name;					
							if (empty($fieldData['name'])) $fieldData['name'] = $field->post_excerpt;

							echo pmai_render_field($fieldData, ( ! empty($options) ) ? $options : array() );
						}
					}
				}
				else{
					$fields = acf_local()->fields;
					
					if (!empty($fields)){
						foreach ($fields as $key => $field) {
							if ($field['parent'] == $acf_obj['key']){								
								$fieldData = $field;
							
								$fieldData['ID'] = $fieldData['id']    = uniqid();
								$fieldData['label'] = $field['label'];
								$fieldData['key']   = $field['key'];					

								echo pmai_render_field($fieldData, ( ! empty($options) ) ? $options : array() );
							}
						}
					}			
				}
				
			}
			else {

				if (is_numeric($acf_obj['ID'])){

					$fields = array();

					foreach (get_post_meta($acf_obj['ID'], '') as $cur_meta_key => $cur_meta_val)
					{	
						if (strpos($cur_meta_key, 'field_') !== 0) continue;

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

					if (count($fields)){

						$sortArray = array();

						foreach($fields as $field){
						    foreach($field as $key=>$value){
						        if(!isset($sortArray[$key])){
						            $sortArray[$key] = array();
						        }
						        $sortArray[$key][] = $value;
						    }
						}

						$orderby = "order_no"; 

						array_multisort($sortArray[$orderby],SORT_ASC,$fields); 

						foreach ($fields as $field) {
							echo pmai_render_field($field, ( ! empty($options) ) ? $options : array() );
						}
					}

				}
				else{

					global $acf_register_field_group;

					if (!empty($acf_register_field_group)){
						foreach ($acf_register_field_group as $key => $group) {							
							if ($group['id'] == $acf_obj['ID']){
								
								foreach ($group['fields'] as $field) {									
									
									echo pmai_render_field($field, ( ! empty($options) ) ? $options : array() );

								}
							}
						}
					}					

				}											

			}
			
		?>								
		</div>
	</div>
	<?php			

	exit(json_encode(array('html' => ob_get_clean()))); die;

}

?>