/**
  * @param  object $ad          the Ad being created/updated
  * @param  object $transaction only available when handling awpcp-place-ad
  */
 public function save_extra_fields($ad, $transaction = null)
 {
     global $wpdb;
     $fields = array();
     foreach (awpcp_get_extra_fields() as $field) {
         if (!isset($_POST["awpcp-{$field->field_name}"])) {
             continue;
         }
         $value = $_POST["awpcp-{$field->field_name}"];
         $fields[$field->field_name] = awpcp_extra_fields_field_value($field, $value);
     }
     $non_empty_fields = array_filter($fields, 'strlen');
     if (!empty($non_empty_fields)) {
         $wpdb->update(AWPCP_TABLE_ADS, $non_empty_fields, array('ad_id' => $ad->ad_id));
     }
     $empty_fields = array_diff($fields, $non_empty_fields);
     if (!empty($empty_fields)) {
         foreach (array_keys($empty_fields) as $field) {
             $assignments[] = sprintf("`%s` = NULL", $field);
         }
         $query = 'UPDATE ' . AWPCP_TABLE_ADS . ' SET %s WHERE ad_id = %%d';
         $query = sprintf($query, join(',', $assignments));
         $wpdb->query($wpdb->prepare($query, $ad->ad_id));
     }
 }
예제 #2
0
 private function upgrade_to_1_0_1($version)
 {
     global $wpdb;
     // serialize extra field options both in Ads table and Extra fields table
     $fields = awpcp_get_extra_fields();
     foreach ($fields as &$field) {
         $name = $field->field_name;
         $options = $field->field_options;
         if (!is_array($options) && !is_serialized($options) && !empty($options)) {
             $options = array_map('trim', explode(',', $options));
             $options = maybe_serialize($options);
             $query = 'UPDATE ' . AWPCP_TABLE_EXTRA_FIELDS . ' SET ';
             $query .= 'field_options = %s WHERE field_id = %d';
             $wpdb->query($wpdb->prepare($query, $options, $field->field_id));
         }
         $query = 'SELECT ad_id, ' . $name . ' FROM ' . AWPCP_TABLE_ADS;
         $results = $wpdb->get_results($query);
         foreach ($results as $row) {
             $value = awpcp_extra_fields_field_value($field, $row->{$name});
             $wpdb->update(AWPCP_TABLE_ADS, array($name => $value), array('ad_id' => $row->ad_id));
         }
     }
 }