Function Reference: geodir_add_edit_price

Summary

This function has not been documented yet.

Actions

‘geodir_before_save_package’ [Line: 2543]

‘geodir_after_save_package’ [Line: 2648]

Source Code

function geodir_add_edit_price()
{
	global $wpdb,$plugin_prefix;
	
	if(current_user_can( 'manage_options' )){
	
		if($_POST['gd_add_price'] == 'addprice' && isset($_REQUEST['package_add_update_nonce']))
		{
			
			if ( !wp_verify_nonce( $_REQUEST['package_add_update_nonce'], 'package_add_update' ) )
				return;

			$id = $_POST['gd_id'];
			
			$title = $_POST['gd_title'];
			
			$amount = $_POST['gd_amount'];
			
			$days = $_POST['gd_days'];
			
			$status = $_POST['gd_status'];
			
			$is_default = $_POST['gd_is_default']; //COMP5
			
			$cat = isset($_POST['gd_cat']) ? $_POST['gd_cat'] : '';
			
			$is_featured = $_POST['gd_is_featured'];
			
			$title_desc = $_POST['gd_title_desc'];
			
			$image_limit = $_POST['gd_image_limit'];
			
			$cat_limit = $_POST['gd_cat_limit'];
			
			$google_analytics = $_POST['google_analytics'];
			
			$sendtofriend = $_POST['geodir_sendtofriend'];
			
			$post_type = $_POST['gd_posting_type'];
			
			$link_business_pkg = isset($_POST['gd_link_business_pkg']) ? $_POST['gd_link_business_pkg'] : '';

			$link_business_cpt = isset($_POST['gd_link_business_cpt']) ? $_POST['gd_link_business_cpt'] : '';
			
			$recurring_pkg = isset($_POST['gd_recurring_pkg']) ? $_POST['gd_recurring_pkg'] : '';
			
			$reg_desc_pkg = isset($_POST['gd_reg_desc_pkg']) ? $_POST['gd_reg_desc_pkg'] : '';
			
			$reg_fees_pkg = isset($_POST['gd_reg_fees_pkg']) ? $_POST['gd_reg_fees_pkg'] : '';
			
			$downgrade_pkg = $_POST['gd_downgrade_pkg'];
			
			$sub_active = isset($_POST['gd_sub_active']) ? $_POST['gd_sub_active'] : '';
			
			$display_order = isset($_POST['gd_display_order']) ? (int)$_POST['gd_display_order'] : '';
			
			$sub_units = $_POST['gd_sub_units'];
			
			$sub_units_num = $_POST['gd_sub_units_num'];
			$sub_num_trial_days = $_POST['sub_num_trial_days'];
			$sub_num_trial_units = !empty( $_POST['gd_sub_num_trial_units'] ) && in_array( $_POST['gd_sub_num_trial_units'], array( 'D', 'W', 'M', 'Y' ) ) ? $_POST['gd_sub_num_trial_units'] : 'D';
			$sub_units_num_times = $_POST['sub_units_num_times'];
			
			$use_desc_limit = $_POST['gd_use_desc_limit'];
			$desc_limit = $_POST['gd_desc_limit'];
			$use_tag_limit = $_POST['gd_use_tag_limit'];
			$tag_limit = $_POST['gd_tag_limit'];
			
			if($sub_active){
				if($sub_units=='D'){$mult = 1;}
				if($sub_units=='W'){$mult = 7;}
				if($sub_units=='M'){$mult = 30;}
				if($sub_units=='Y'){$mult = 365;}
				$days = $mult;
			}
			
			$hide_related_tab = isset($_POST['geodir_hide_related_tab']) ? (int)$_POST['geodir_hide_related_tab'] : 0;
			$has_upgrades = isset($_POST['geodir_has_upgrades']) ? (int)$_POST['geodir_has_upgrades'] : 1;
			$disable_coupon = isset($_POST['geodir_disable_coupon']) ? absint($_POST['geodir_disable_coupon']) : 0;
			
			if ($id != '') {
				$get_oldposttype = $wpdb->get_row($wpdb->prepare("SELECT post_type, is_default FROM ".$plugin_prefix."price WHERE pid=%d", array($id)));
							
				$get_oldpricedata = $wpdb->get_results($wpdb->prepare("SELECT post_type FROM ".$plugin_prefix."price WHERE post_type=%s",array($get_oldposttype->post_type)));
	
				
				if(count($get_oldpricedata) > 1)
				{
					
					if($is_default && $get_oldposttype->post_type == $post_type){
						
						$wpdb->query($wpdb->prepare("UPDATE ".$plugin_prefix."price SET is_default='0' WHERE pid!=%d AND post_type=%s",array($id,$post_type)));
						
						$wpdb->query($wpdb->prepare("UPDATE ".$plugin_prefix."price SET is_default='1' WHERE pid=%d",array($id)));
						
					}elseif(!$get_oldposttype->is_default && $get_oldposttype->post_type != $post_type ){
						
						$wpdb->query($wpdb->prepare("UPDATE ".$plugin_prefix."price SET post_type=%s WHERE pid=%d",array($post_type,$id)));
						
						if($is_default)
						{
							$wpdb->query($wpdb->prepare("UPDATE ".$plugin_prefix."price SET is_default='0' WHERE pid!=%d AND post_type=%s",array($id,$post_type)));
							
							$wpdb->query($wpdb->prepare("UPDATE ".$plugin_prefix."price SET is_default='1' WHERE pid=%d",array($id)));
							
						}
						
					}elseif($get_oldposttype->is_default){
						
						$error = __("You have not change this package because its default package for ".$get_oldposttype->post_type.".", 'geodir_payments');
							
					}
					
				}
				else
				{
					
					if($get_oldposttype->is_default != $is_default || $get_oldposttype->post_type != $post_type)
					{
						$error = __("You have not change this package because its default package for ".$get_oldposttype->post_type.".", 'geodir_payments');
					}
					
				}
				
			}
			
			
			
			if(empty($error))
			{
			
				if($cat)
				{
					$cat = implode(',',$cat);
				}
				
				if(!$title_desc)
				{
					$title_desc = $title.' : number of publish days are '.$days.' ('.$amount.' '.geodir_get_currency_type().')';
				}
				
				//$title_desc = addslashes($title_desc);
				
				do_action('geodir_before_save_package');
				
				if($id)
				{
				
								$wpdb->query(
									$wpdb->prepare(
										"update ".GEODIR_PRICE_TABLE." set 
										title=%s, 
										amount=%f,
										days=%d,
										status=%d,
										cat=%s,
										is_featured=%d,
										title_desc=%s, 
										image_limit=%s, 
										cat_limit=%s, 
										google_analytics = %d, 
										sendtofriend = %d, 
										post_type=%s, 
										link_business_pkg=%s,
										link_business_cpt=%s,
										recurring_pkg=%s, 
										reg_desc_pkg=%s, 
										reg_fees_pkg=%s, 
										downgrade_pkg=%s, 
										sub_active=%s,
										display_order=%d,
										sub_units=%s,
										sub_units_num=%s,
										sub_num_trial_days=%s, 
										sub_num_trial_units=%s, 
										sub_units_num_times=%s,
										use_desc_limit=%d,
										desc_limit=%d,
										use_tag_limit=%d,
										tag_limit=%d,
										hide_related_tab=%d,
										has_upgrades=%d,
										disable_coupon=%s where pid=%d",
										
										array($title,$amount,$days,$status,$cat,$is_featured,$title_desc,$image_limit,$cat_limit,$google_analytics,$sendtofriend,$post_type,$link_business_pkg,$link_business_cpt,$recurring_pkg,$reg_desc_pkg,$reg_fees_pkg,$downgrade_pkg,$sub_active,$display_order,$sub_units,$sub_units_num,$sub_num_trial_days,$sub_num_trial_units,$sub_units_num_times, $use_desc_limit, $desc_limit, $use_tag_limit, $tag_limit, $hide_related_tab, $has_upgrades, $disable_coupon,$id)
									)
							);

										$msg = 'Price updated successfully.';
				}
				else
				{
					
									 
					$wpdb->query(
						$wpdb->prepare(
							"insert into ".GEODIR_PRICE_TABLE." set
							title=%s, 
							amount=%f, 
							days=%d, 
							status=%d,
							is_default=%d,
							cat=%s,
							is_featured=%d,
							title_desc=%s, 
							image_limit=%s, 
							cat_limit=%s, 
							google_analytics = %d,
							sendtofriend = %d,
							post_type=%s, 
							link_business_pkg=%s,
							link_business_cpt=%s,
							recurring_pkg=%s, 
							reg_desc_pkg=%s, 
							reg_fees_pkg=%s, 
							downgrade_pkg=%s, 
							sub_active=%s,
							display_order=%d,
							sub_units=%s,
							sub_units_num=%s,
							sub_num_trial_days=%s,
							sub_num_trial_units=%s,  
							sub_units_num_times= %s,
							use_desc_limit=%d,
							desc_limit=%d,
							use_tag_limit=%d,
							tag_limit=%d,
							hide_related_tab=%d,
							has_upgrades=%d,
							disable_coupon=%s",
							array($title, $amount, $days, $status,$is_default,$cat,$is_featured,$title_desc,$image_limit,$cat_limit,$google_analytics,$sendtofriend,$post_type,$link_business_pkg,$link_business_cpt,$recurring_pkg,$reg_desc_pkg,$reg_fees_pkg,$downgrade_pkg,$sub_active,$display_order,$sub_units,$sub_units_num,$sub_num_trial_days,$sub_num_trial_units,$sub_units_num_times, $use_desc_limit, $desc_limit, $use_tag_limit, $tag_limit, $hide_related_tab, $has_upgrades, $disable_coupon)
						)
					
					);
					
					
					$id = $wpdb->insert_id;
					
					$msg = __('Price created successfully.', 'geodir_payments');
					
					if($is_default)
					{
						$wpdb->query($wpdb->prepare("UPDATE ".$plugin_prefix."price SET is_default='0' WHERE pid!=%d AND post_type=%s",array($id,$post_type)));
						
						$wpdb->query($wpdb->prepare("UPDATE ".$plugin_prefix."price SET is_default='1' WHERE pid=%d",array($id)));
					}
				}
				
				do_action('geodir_after_save_package', $id);
				
				$post_fields = isset($_REQUEST['pay_custom_fields']) ? $_REQUEST['pay_custom_fields'] : '';
				
				$all_packages = $wpdb->get_results($wpdb->prepare("SELECT pid FROM ".$plugin_prefix."price WHERE post_type=%s",array($post_type)));
				
				$packages_default_field = '';
				if($all_packages){
						
						foreach($all_packages as $pkg){
							$packages_default_field .= ','.$pkg->pid;
						}
						
				}
				
				/* --- start posts default fields --- */
				$default_address_field = $wpdb->get_row($wpdb->prepare("select id from ".GEODIR_CUSTOM_FIELDS_TABLE." where is_admin='1' and field_type='address' and post_type=%s", array($post_type)));
				
				$post_fields[] = $default_address_field->id;
				$post_default_fields[] = $default_address_field->id;
				
				
				$default_taxonomy_field =	$wpdb->get_row($wpdb->prepare("select id from ".GEODIR_CUSTOM_FIELDS_TABLE." where is_admin='1' and field_type='taxonomy' and post_type=%s",array($post_type)));
				
				$post_fields[] = $default_taxonomy_field->id;
				$post_default_fields[] = $default_taxonomy_field->id;
				
				/* --- end posts default fields --- */
				
				if(!empty($post_fields))
				{
					$post_fields_main_array = array($id,$post_type);
					
					$post_fields_length = count($post_fields);
					$post_fields_format = array_fill(0, $post_fields_length, '%d');
					$post_fields_format = implode(',', $post_fields_format);
					
					$post_fields_main_array = array_merge($post_fields_main_array,$post_fields);
					
					$post_default_main_array = array($post_type);
					$post_default_fields_length = count($post_default_fields);
					$post_default_format = array_fill(0, $post_default_fields_length, '%d');
					$post_default_format = implode(',', $post_default_format);	
					$post_default_main_array = array_merge($post_default_main_array,$post_default_fields);
					
					$old_package_change = $wpdb->get_results($wpdb->prepare("SELECT id, packages from ".GEODIR_CUSTOM_FIELDS_TABLE." WHERE FIND_IN_SET(%s, packages)",array($id)));
					
					if(!empty($old_package_change))
					{
						
						foreach($old_package_change as $key){
							
							$pck_array = explode(',', $key->packages);
							
							$packages = '';
							$comma = '';
							foreach($pck_array as $pck_key)	
							{
								if($pck_key != $id && $pck_key != '')
								{
									$packages .= $comma.$pck_key;
									$comma = ',';
								}
							}
							
							$wpdb->query($wpdb->prepare("UPDATE ".GEODIR_CUSTOM_FIELDS_TABLE." SET packages = %s WHERE id=%d",array($packages,$key->id)));
						}
						
					}
					
					
					$wpdb->query(
						$wpdb->prepare(
						"UPDATE ".GEODIR_CUSTOM_FIELDS_TABLE." SET packages = CONCAT('',TRIM(BOTH ',' FROM packages),',%d,') WHERE post_type = %s AND id IN ($post_fields_format)",
						$post_fields_main_array	
						)
					);
	
	
	
				$wpdb->query(
					$wpdb->prepare(
						"UPDATE ".GEODIR_CUSTOM_FIELDS_TABLE." SET packages = '".$packages_default_field."' WHERE post_type = %s AND id IN ($post_default_format)",
						$post_default_main_array
					)
				);
			
			}
					
				$msg = urlencode($msg);
				$location = admin_url()."admin.php?page=geodirectory&tab=paymentmanager_fields&subtab=geodir_payment_manager&success_msg=".$msg;
				wp_redirect($location);
				gd_die();
				
			}
			else
			{
				$error = urlencode($error);
				$location = admin_url()."admin.php?page=geodirectory&tab=paymentmanager_fields&subtab=geodir_payment_manager&error_msg=".$error;
				wp_redirect($location);
				gd_die();
			}
		}
	
	}else{
		
		wp_redirect(geodir_login_url());
		gd_die();
	
	}
	
	
}