/**
  * Attempts to retrieve an EE_Person any way it can.
  *
  * @param int|WP_Post $PER_ID person ID or WP_Post object (optional) If not included try to obtain it via request.
  *
  * @return EE_Person
  */
 public static function get_person($PER_ID = 0)
 {
     $PER_ID = $PER_ID instanceof WP_Post ? $PER_ID->ID : absint($PER_ID);
     // do we already have the Person  you are looking for?
     if (EEH_People_View::$_person instanceof EE_Person && $PER_ID && EEH_People_View::$_person->ID() === $PER_ID) {
         return EEH_People_View::$_person;
     }
     EEH_People_View::$_person = NULL;
     // international newspaper?
     global $post;
     // if this is being called from an EE_Person post, then we can just grab the attached EE_Person object
     if (isset($post->post_type) && $post->post_type == 'espresso_people' || $PER_ID) {
         // grab the person we're looking for
         if (isset($post->EE_Person) && ($PER_ID == 0 || $PER_ID == $post->ID)) {
             EEH_People_View::$_person = $post->EE_Person;
         }
         // now if we STILL do NOT have an EE_Person model object, BUT we have an Person ID...
         if (!EEH_People_View::$_person instanceof EE_Person && $PER_ID) {
             // sigh... pull it from the db
             EEH_People_View::$_person = EEM_Person::instance()->get_one_by_ID($PER_ID);
         }
     }
     return EEH_People_View::$_person;
 }
 /**
  * Callback for cpt route insert/updates.  Runs on the "save_post" hook.
  *
  * @since  1.0.0
  *
  * @param int      $post_id Post id of item
  * @param WP_Post $post
  *
  * @return void
  */
 protected function _insert_update_cpt_item($post_id, $post)
 {
     $success = true;
     $person = EEM_Person::instance()->get_one_by_ID($post_id);
     //for people updates
     if ($post->post_type != 'espresso_people' || !$person instanceof EE_Person) {
         return;
     }
     $updated_fields = array('PER_fname' => $this->_req_data['PER_fname'], 'PER_lname' => $this->_req_data['PER_lname'], 'PER_full_name' => !empty($this->_req_data['post_title']) ? $this->_req_data['post_title'] : $this->_req_data['PER_fname'] . ' ' . $this->_req_data['PER_lname'], 'PER_address' => isset($this->_req_data['PER_address']) ? $this->_req_data['PER_address'] : '', 'PER_address2' => isset($this->_req_data['PER_address2']) ? $this->_req_data['PER_address2'] : '', 'PER_city' => isset($this->_req_data['PER_city']) ? $this->_req_data['PER_city'] : '', 'STA_ID' => isset($this->_req_data['STA_ID']) ? $this->_req_data['STA_ID'] : '', 'CNT_ISO' => isset($this->_req_data['CNT_ISO']) ? $this->_req_data['CNT_ISO'] : '', 'PER_zip' => isset($this->_req_data['PER_zip']) ? $this->_req_data['PER_zip'] : '', 'PER_email' => isset($this->_req_data['PER_email']) ? $this->_req_data['PER_email'] : '', 'PER_phone' => isset($this->_req_data['PER_phone']) ? $this->_req_data['PER_phone'] : '');
     foreach ($updated_fields as $field => $value) {
         $person->set($field, $value);
     }
     $success = $person->save();
     $people_update_callbacks = apply_filters('FHEE__People_Admin_Page__insert_update_cpt_item__people_update', array());
     foreach ($people_update_callbacks as $a_callback) {
         if (FALSE === call_user_func_array($a_callback, array($person, $this->_req_data))) {
             throw new EE_Error(sprintf(__('The %s callback given for the "FHEE__People_Admin_Page__insert_update_cpt_item__people_update" filter is not a valid callback.  Please check the spelling.', 'event_espresso'), $a_callback));
         }
     }
     if ($success === FALSE) {
         EE_Error::add_error(__('Something went wrong with updating the meta table data for the person.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__);
     }
 }
 /**
  * resets the model and returns it
  * @return EEM_Person
  */
 public static function reset($timezone = NULL)
 {
     self::$_instance = NULL;
     return self::instance($timezone);
 }
 /**
  * Callback for FHEE__EE_Admin_Page___display_admin_list_table_page__before_list_table__template_arg used to add
  * context title for when the event list table is filtered by person and person type.
  * @param $original_content
  * @param $page_slug
  * @param $request_data
  * @param $request_action
  */
 public function filtered_events_list_table_title($original_content, $page_slug, $request_data, $request_action)
 {
     if ($page_slug === 'espresso_events' && $request_action === 'default') {
         $person = $person_type = null;
         if (isset($request_data['PER_ID'])) {
             $person = EEM_Person::instance()->get_one_by_ID($request_data['PER_ID']);
         }
         if (isset($request_data['PT_ID'])) {
             $person_type = EEM_Term_Taxonomy::instance()->get_one_by_ID($request_data['PT_ID']);
             $person_type = $person_type instanceof EE_Term_Taxonomy ? $person_type->get_first_related('Term') : null;
         }
         if ($person instanceof EE_Person && $person_type instanceof EE_Term) {
             $title = sprintf(__('Viewing the events that %s is assigned to as %s', 'event_espresso'), $person->full_name(), $person_type->name());
         } elseif ($person instanceof EE_Person && !$person_type instanceof EE_Term) {
             $title = sprintf(__('Viewing the events that %s is assigned to', 'event_espresso'), $person->full_name());
         } elseif (!$person instanceof EE_Person && $person_type instanceof EE_Term) {
             $title = sprintf(__('Viewing the events that has at least one person assigned as %s.', 'event_espresso'), $person_type->name());
         } else {
             $title = '';
         }
         if ($title) {
             return '<h2>' . $title . '</h2>';
         }
     }
     return $original_content;
 }