Function Reference: geodir_save_listing_package_fields

Summary

Save the listing price package fields values after listing saved.

Description

This function will checks & validates the limitations of category count, tags,
post images, category exclusions, description characters length etc.

Global Values

$wpdb
(object) (required) WordPress Database object.

Default: None

Parameters

$post_id
(int) (required) The post id.

Default: None
$request_info
(array) (required) The listing request data.

Default: None

Change Log

Since: 1.0.0

Source Code

function geodir_save_listing_package_fields($post_id='', $request_info) {
	global $wpdb;
	$package_id = (isset($request_info['package_id'])) ? $request_info['package_id'] : '';
	
	if (!$post_id || !$package_id) {
		return;
	}
	
	$package_info = (array)geodir_get_package_info($package_id);
	$post_info = geodir_get_post_info($post_id);
	$post_type = $post_info->post_type;
	$post_category = $post_type.'category';

	// check for excluded cats
	if ($package_info['cat']) {// only run if there are excluded cats
		$cur_cats = array_unique(array_filter(explode(",", $post_info->{$post_category})));
		$ex_cats = array_filter(explode(",", $package_info['cat']));

		foreach($cur_cats as $key => $value) {
		  if(in_array($value, $ex_cats)) {  
			unset($cur_cats[$key]);
		  }
		}

		$cur_cats = array_map('intval',$cur_cats);// this was being treated as a string so we convert to int.
		$cur_cats_str = (!empty($cur_cats)) ? implode(',',$cur_cats) : '';
		$term_taxonomy_ids = wp_set_object_terms($post_id, $cur_cats,$post_category);
		geodir_save_post_meta($post_id, $post_category,$cur_cats_str );
		
		// check if defualt cat is excluded and if so chane it
		$default_cat = $post_info->default_category;
		if($default_cat && in_array($default_cat, $ex_cats)){
		geodir_save_post_meta($post_id, 'default_category', $cur_cats[0]);	
		}
	}

	// check if featured only if not in admin
	if (!is_admin()) {
		if($package_info['is_featured']!=$post_info->is_featured){
			geodir_save_post_meta($post_id, 'is_featured', $package_info['is_featured']);
		}
	}

	// check image limit
	if ($package_info['image_limit']!='') {
		$image_limit = $package_info['image_limit'];
		$post_images  = $wpdb->get_results(
				$wpdb->prepare(
					"SELECT * FROM ".GEODIR_ATTACHMENT_TABLE." WHERE `post_id`=%d order by menu_order asc",
					array($post_id)
				)
			);

		$count_post_images = count($post_images);
		
		if ($count_post_images > $image_limit) {
			if($image_limit>='1'){
				foreach ($post_images as $key=>$perimage) {// move featured image to the end of the arr so it's not removed
					if($post_info->featured_image==$perimage->file){unset($post_images[$key]);$post_images[]=$perimage;}
				}
			}

			$post_images_arr = array_slice($post_images, 0, $count_post_images-$image_limit);

			$upload_dir = wp_upload_dir();
			$upload_basedir = $upload_dir['basedir'];
			
			foreach ($post_images_arr as $perimage) {
				
				if (file_exists($upload_basedir.$perimage->file)) {
					unlink($upload_basedir.$perimage->file);
				}
				
				$wpdb->query(
					$wpdb->prepare(
						"DELETE FROM ".GEODIR_ATTACHMENT_TABLE." WHERE ID=%d",
						array($perimage->ID)
					)
				);
				if($post_info->featured_image==$perimage->file){geodir_save_post_meta($post_id, 'featured_image', '');}
			}
		}
	}

	// check if there is a category limit
	if ( $package_info['cat_limit'] != '') {
		$cur_cats = array_unique(array_filter(explode(",", $post_info->{$post_category})));
		$cat_limit = (int)$package_info['cat_limit'];
		
		if (count($cur_cats) > $cat_limit) {
			$default_category = (int)$post_info->default_category > 0 ? (int)$post_info->default_category : $cur_cats[0];

			$count = 0;
			$new_cur_cats = array();
			foreach ($cur_cats as $cat_id) {
				$new_cur_cats[] = (int)$cat_id;
				
				$count++;
				if ($count >= $cat_limit) {
					break;
				}
			}

			if ($default_category && !in_array($default_category, $new_cur_cats)) {
				$new_cur_cats[$cat_limit-1] = $default_category;
			}
			
			$cur_cats_str = (!empty($new_cur_cats)) ? implode(',',$new_cur_cats) : '';
			$term_taxonomy_ids = wp_set_object_terms($post_id, $new_cur_cats, $post_category);
			
			geodir_save_post_meta($post_id, $post_category, $cur_cats_str);
			
			$post_cat_str = '';
			if (!empty($new_cur_cats)) {
				$post_cat_str = '#'.implode(",y:#", $new_cur_cats) . ',y:';
				$post_cat_str = str_replace('#' . $default_category . ',y', '#' . $default_category . ',y,d', $post_cat_str);
				$post_cat_str = ltrim($post_cat_str, '#');
				
				$post_cat_str = array($post_category => $post_cat_str);
			}
			
			geodir_set_postcat_structure($post_id, $post_category, $default_category, $post_cat_str);
		}
	}

	// check custom fields
	$custom_fields = geodir_post_custom_fields('','all',$post_type);

	if (!empty($custom_fields)) {
		foreach ($custom_fields as $key=>$val) {
			$id =  $val['id'];
			$label =  $val['label'];
			$is_default =  $val['is_default'];
			$is_admin =  $val['is_admin'];
			$field_type =  $val['field_type'];
			$packages = array();
			$packages = array_unique(array_filter(explode(",",$val['packages'])));
			
			if (!($field_type == 'address' && $is_admin == '1') && !($field_type == 'taxonomy' && $is_admin == '1') && $val['for_admin_use']!='1') {
				if (in_array($package_id,$packages)) { // if active for this package then dont change
				} else { // if not active in this package then blank
					geodir_save_post_meta($post_id, $val['name'],'');	
				}
			}
		}
	}
}