/** * Save and or update the objects meta data * based on the action being performed to the object. * * @access private * @since 0.8 * @param string $action The action being performed. * @param int $id The object ID. * @param array $fields An array of the registered fields to save and or update. * * @return void */ private function save($action, $id, $fields) { foreach ($fields as $field) { if (!($id = absint($id))) { return FALSE; } // Quick and dirty hack to prevent the bio and notes fields from being saved in the meta table. // @todo Think of something better to do here. // There should be some type of flag to check before saving as meta. if ($field['id'] === 'bio' || $field['id'] === 'notes') { continue; } $value = $this->sanitize($field['type'], isset($_POST[$field['id']]) ? $_POST[$field['id']] : NULL, isset($field['options']) ? $field['options'] : array(), isset($field['default']) ? $field['default'] : NULL); switch ($action) { case 'add': cnMeta::add('entry', $id, $field['id'], $value); break; case 'copy': cnMeta::add('entry', $id, $field['id'], $value); break; case 'update': cnMeta::update('entry', $id, $field['id'], $value); break; } } }
/** * Add, update or delete the meta of the specified entry ID. * * @access public * @since 0.8 * @param string $action The action to be performed. * @param int $id The entry ID. * @param array $meta [optional] An array of meta data the action is to be performed on. * * @return array The meta IDs of the meta data the action was performed on. */ public static function meta($action, $id, $meta = array()) { $metaIDs = array(); switch ($action) { case 'add': foreach ($meta as $row) { $metaIDs[] = cnMeta::add('entry', $id, $row['key'], $row['value']); } break; case 'update': foreach ($meta as $metaID => $row) { cnMeta::update('entry', $id, $row['key'], $row['value']); $metaIDs[] = $metaID; } break; case 'delete': if (empty($meta)) { $meta = cnMeta::get('entry', $id); } if ($meta) { foreach ($meta as $key => $value) { cnMeta::delete('entry', $id, $key); $metaIDs[] = $key; } } break; } return $metaIDs; }
/** * Add `meta_key` to term when updating * * @access public * @since 8.5.2 * * @param int $term_id Term ID. * @param int $tt_id Taxonomy Term ID. */ public function save($term_id, $tt_id) { // Get the term being posted $term_key = 'term-' . $this->meta_key; // Bail if not updating meta_key $value = !empty($_POST[$term_key]) ? $_POST[$term_key] : ''; if (empty($value)) { cnMeta::delete('term', $term_id, $this->meta_key); // Update meta_key value } else { cnMeta::update('term', $term_id, $this->meta_key, $value); } }
/** * Save and or update the objects meta data * based on the action being performed to the object. * * @access private * @since 0.8 * * @param string $action The action being performed. * @param int $id The object ID. * @param array $fields An array of the registered fields to save and or update. * * @return bool */ private function save($action, $id, $fields) { foreach ($fields as $field) { /** * Filter field meta before it is inserted into the database. * * @since 8.5.14 * * @param array $field An array of the registered field attributes. * @param int $id The object ID. * @param string $action The action being performed. */ $field = apply_filters('cn_pre_save_meta', $field, $id, $action); if (!($id = absint($id))) { return FALSE; } /** * Filter to allow meta to not be saved. * * The dynamic portion of the filter name is so saving meta can be skipped based on the field ID. * * @since 8.5.14 * * @param false $false Return TRUE to not save the field meta. */ if (apply_filters('cn_pre_save_meta_skip', FALSE) || apply_filters('cn_pre_save_meta_skip-' . $field['id'], FALSE)) { continue; } $value = $this->sanitize($field['type'], isset($_POST[$field['id']]) ? $_POST[$field['id']] : NULL, isset($field['options']) ? $field['options'] : array(), isset($field['default']) ? $field['default'] : NULL); switch ($action) { case 'add': cnMeta::add('entry', $id, $field['id'], $value); break; case 'copy': cnMeta::add('entry', $id, $field['id'], $value); break; case 'update': cnMeta::update('entry', $id, $field['id'], $value); break; } } }