Пример #1
0
 /**
  * Triggered on save_post
  *
  * @param int $post_id
  * @param \WP_Post $post
  */
 public function save_meta_box($post_id, $post)
 {
     // check if we should save
     if (true !== $this->should_save($post)) {
         return;
     }
     // save
     if (isset($_POST['wpcm-cd']) && count($_POST['wpcm-cd']) > 0) {
         $fields = Vehicle\Data::get_fields();
         foreach ($_POST['wpcm-cd'] as $key => $val) {
             if (isset($fields[$key])) {
                 update_post_meta($post->ID, 'wpcm_' . $key, $val);
             }
         }
     }
 }
 /**
  * Persist Vehicle in WordPress database
  *
  * @param Car $vehicle
  *
  * @throws \Exception
  *
  * @return Vehicle $vehicle
  */
 public function persist($vehicle)
 {
     // check if new or existing
     if (0 == $vehicle->get_id()) {
         // create
         $vehicle_id = wp_insert_post(array('post_title' => $vehicle->get_title(), 'post_content' => $vehicle->get_description(), 'post_excerpt' => $vehicle->get_short_description(), 'post_author' => $vehicle->get_author(), 'post_type' => PostType::VEHICLE, 'post_status' => $vehicle->get_status()));
         if (is_wp_error($vehicle_id)) {
             throw new \Exception('Unable to insert post in WordPress database');
         }
         // set new vehicle ID
         $vehicle->set_id($vehicle_id);
     } else {
         // update
         $vehicle_id = wp_update_post(array('ID' => $vehicle->get_id(), 'post_title' => $vehicle->get_title(), 'post_content' => $vehicle->get_description(), 'post_excerpt' => $vehicle->get_short_description(), 'post_status' => $vehicle->get_status()));
         if (is_wp_error($vehicle_id)) {
             throw new \Exception('Unable to updarte post in WordPress database');
         }
     }
     // get fields
     $fields = Data::get_fields();
     // set expiration date
     if (null != $vehicle->get_expiration()) {
         update_post_meta($vehicle->get_id(), 'wpcm_expiration', $vehicle->get_expiration()->format('Y-m-d'));
     } else {
         delete_post_meta($vehicle->get_id(), 'wpcm_expiration');
     }
     // set sold
     update_post_meta($vehicle->get_id(), 'wpcm_sold', $vehicle->get_sold());
     // set vehicle meta-data
     if (!empty($fields)) {
         foreach ($fields as $field_key => $field) {
             // the method
             $method = 'get_' . $field_key;
             // check if exists in vehicle
             if (method_exists($vehicle, $method)) {
                 // the value
                 $value = $vehicle->{$method}();
                 // turn \DateTime object into formatted string
                 if ($value instanceof \DateTime) {
                     $value = $value->format('Y-m-d');
                 }
                 // update post method
                 update_post_meta($vehicle->get_id(), 'wpcm_' . $field_key, $value);
             }
         }
     }
     // set vehicle features
     $obj_features = $vehicle->get_features();
     if (!empty($obj_features)) {
         // store feature ID's in array
         $terms = array();
         foreach ($vehicle->get_features() as $feature_id => $feature) {
             $terms[] = $feature_id;
         }
         // set object terms
         wp_set_object_terms($vehicle->get_id(), $terms, Taxonomies::FEATURES);
         // re-fetch features from DB so we're sure to have a correctly formatted array
         $vehicle->set_features($this->get_formatted_features($vehicle->get_id()));
     }
     // set images
     if (is_array($vehicle->get_gallery_attachment_ids()) && count($vehicle->get_gallery_attachment_ids()) > 0) {
         update_post_meta($vehicle_id, '_car_gallery', implode(',', $vehicle->get_gallery_attachment_ids()));
     } else {
         update_post_meta($vehicle_id, '_car_gallery', '');
     }
     return $vehicle;
 }