meta_save($options, $id, $post_type, $POST); $this->_active_ex = $active; }//end __construct method private function meta_save($options, $id, $post_type, $POST){ foreach ( $options as $key => $option) { if($post_type === 'athenaprofile'){ $this->athena_post_type($option['id'], $id, $option['type'], $POST, count($options)); }else{ $break = $this->other_post_type($option['id'], $id, $POST); if($break){ break; } }//end post_type conditional }//end foreach loop }//end meta_save method private function athena_post_type($option, $id, $type, $POST, $count){ static $email_enabled, $timestamp, $current = 1; if ( isset( $option ) && ! empty( $option ) ) { $value = isset($POST[$option])? $POST[$option] : ''; if ( !empty($value) ) { update_post_meta( $id, $option, $value ); } else { delete_post_meta( $id, $option, get_post_meta( $id, $option, true ) ); } if($option === 'enable_email'){ $email_enabled = isset( $_POST[$option] ) ? true : false; } if($option === 'email_time' && $email_enabled){ $this->schedule_email_cron($id, $timestamp, $_POST[$option]); } if($type === 'date_box'){ $value = $timestamp = isset($POST[$option.'_timestamp'])? intval($POST[$option.'_timestamp']) : ''; $this->schedule_profile_cron($id, $timestamp); if ( !empty($value) ) { update_post_meta( $id, $option .'_timestamp', $value ); } else { delete_post_meta( $id, $option .'_timestamp', get_post_meta( $id, $option .'_timestamp', true ) ); }//end value empty conditional }//end type date_box conditional; }//end option type conditional if($current === $count){ if( ( $key = in_athena_array($this->_active_ex, $id) ) !== false ){ if($email_enabled){ if( ! array_key_exists('email', $this->_active_ex[$key]) ){ array_push($this->_active_ex[$key], array('email' => true)); update_option('athena_active_profiles', $this->_active_ex); } }else{ if( array_key_exists('email', $this->_active_ex[$key]) ){ unset( $this->_active_ex[$key]['email'] ); update_option('athena_active_profiles', $this->_active_ex); $this->cancel_email_cron( $id ); } } }else{ if($email_enabled){ array_push($this->_active_ex, array('id' => $id, 'email' => true)); }else{ array_push($this->_active_ex, array( 'id' => $id )); } update_option('athena_active_profiles', $this->_active_ex); } } $current++; }//end athena_post_type method private function other_post_type($option, $id, $POST){ if ( isset( $option ) && ! empty( $option ) ) { if($option === '_athena_options' && isset($POST[$option]['enabled']) && $POST[$option]['enabled'] == true){ $profile_data = isset($POST[$option]['profile'])?get_option('athena_profile_'.$POST[$option]['profile']) : false; if(isset($POST[$option]['profile']) && !empty($POST[$option]['profile'])){ $value['enabled'] = $POST[$option]['enabled']; $value['profile'] = $POST[$option]['profile']; if(is_array($profile_data) && !in_array($id, $profile_data)){ array_push($profile_data, $id); update_option('athena_profile_'.$value['profile'], $profile_data); }else if(!is_array($profile_data)){ $profile_data = array($id); update_option('athena_profile_'.$value['profile'], $profile_data); } $break = true; }else{ if(is_array($profile_data) && ($key = array_search($id, $profile_data)) !== false){ unset($profile_data[$key]); update_option('athena_profile_'. $POST[$option]['profile'], $profile_data); } $value = isset($POST[$option])? $POST[$option] : ''; $post_status = get_post_status($id); if( $post_status !== 'auto-draft' && $post_status !== 'inherit'){ $this->schedule_post_cron($id, $POST[$option]['timestamp']); if(is_array($this->_active_ex)){ array_push($this->_active_ex, array($id)); }else{ $this->_active_ex = array($id); } update_option('athena_active_posts', $this->_active_ex, true); } } if ( !empty($value) ) { update_post_meta( $id, $option, $value ); } else { delete_post_meta( $id, $option, get_post_meta( $id, $option, true ) ); } return isset($break)? $break : false; }else if($option !== '_athena_options'){ $value = isset($POST[$option])? $POST[$option] : ''; if ( !empty($value) ) { update_post_meta( $id, $option, $value ); } else { delete_post_meta( $id, $option, get_post_meta( $id, $option, true ) ); } }else{ $value = isset($POST[$option]['enabled'])? $POST[$option]['enabled'] : ''; $this->cancel_post_cron( $id ); if(is_array($this->_active_ex) && ($key = array_search($id, $this->_active_ex)) !== false){ unset($this->_active_ex[$key]); } update_option('athena_active_posts', $this->_active_ex, true); if ( !empty($value) ) { update_post_meta( $id, $option, $value ); } else { delete_post_meta( $id, $option, get_post_meta( $id, $option, true ) ); } return true; }//end post expiration enabled conditional }//end option isset empty conditional }//end other_post_type method private function schedule_profile_cron( $post_id, $time){ if( $time > current_time('timestamp') ){ if( ($timestamp = wp_next_scheduled( 'athena_profile_ex_'.$post_id, array($post_id) )) === false){ wp_schedule_single_event( $time, 'athena_profile_ex_'.$post_id, array($post_id)); }else{ if($timestamp !== $time){ wp_clear_scheduled_hook( 'athena_profile_ex_'.$post_id, array($post_id) ); wp_schedule_single_event( $time, 'athena_profile_ex_'.$post_id, array($post_id)); } } } }//end schedule_cron method private function schedule_email_cron( $post_id, $time, $type ){ if($type['type'] == 'h'){ $time = $time - (3600 * $type['count']); }elseif($type['type'] == 'd'){ $time_frame = athena_date_time( $type['count'] ); $time = strtotime('-'. $type['count'] . ' ' . $time_frame[$type['type']], $time); }else{ return; } if($time >= current_time('timestamp', 1) + 3600){ if( ($timestamp = wp_next_scheduled('athena_profile_email_' . $post_id, array($post_id) ) ) === false){ wp_schedule_single_event($time, 'athena_profile_email_' . $post_id, array($post_id)); }else{ if($timestamp !== $time){ wp_clear_scheduled_hook( 'athena_profile_email_' . $post_id, array($post_id) ); wp_schedule_single_event( $time, 'athena_profile_email_' . $post_id, array($post_id)); } } } }//end schedule_email_cron method private function schedule_post_cron( $post_id, $time ){ if( $time > current_time('timestamp') ){ if( ($timestamp = wp_next_scheduled( 'athena_post_ex_'.$post_id, array($post_id) )) === false){ wp_schedule_single_event( $time, 'athena_post_ex_'.$post_id, array($post_id)); }else{ if($timestamp !== $time){ wp_clear_scheduled_hook( 'athena_post_ex_'.$post_id, array($post_id) ); wp_schedule_single_event( $time, 'athena_post_ex_'.$post_id, array($post_id)); } } } }//end schedule_post_cron method private function cancel_post_cron( $post_id ){ if( ( $timestamp = wp_next_scheduled('athena_post_ex_'.$post_id, array($post_id) ) ) !== false){ wp_clear_scheduled_hook( 'athena_post_ex_'.$post_id, array($post_id) ); } } private function cancel_email_cron( $post_id ){ if( ( $timestamp = wp_next_scheduled('athena_profile_email_'.$post_id, array($post_id) ) ) !== false){ wp_clear_scheduled_hook( 'athena_profile_email_'.$post_id, array($post_id) ); } } }//end Athena_Save class }//end class_exists conditional