/** * Add, update or delete the entry meta data. * * @access public * @since 0.8 * @param string $action The action to being performed to an entry. * @param int $id The entry ID. * * @return mixed array | bool An array of meta IDs or FALSE on failure. */ public static function processEntryMeta($action, $id) { /** @var wpdb $wpdb */ global $wpdb; if (!($id = absint($id))) { return FALSE; } $meta = array(); $newmeta = array(); $metaSelect = array(); $metaIDs = array(); switch ($action) { case 'add': if (isset($_POST['newmeta']) || !empty($_POST['newmeta'])) { foreach ($_POST['newmeta'] as $row) { // If the key begins with an underscore, remove it because those are private. if (isset($row['key'][0]) && '_' == $row['key'][0]) { $row['key'] = substr($row['key'], 1); } $newmeta[] = cnMeta::add('entry', $id, $row['key'], $row['value']); } } if (isset($_POST['metakeyselect']) && $_POST['metakeyselect'] !== '-1') { $metaSelect[] = cnMeta::add('entry', $id, $_POST['metakeyselect'], $_POST['newmeta']['99']['value']); } $metaIDs['added'] = array_merge($newmeta, $metaSelect); break; case 'copy': // Copy any meta associated with the source entry to the new entry. if (isset($_POST['meta']) || !empty($_POST['meta'])) { foreach ($_POST['meta'] as $row) { // If the key begins with an underscore, remove it because those are private. if (isset($row['key'][0]) && '_' == $row['key'][0]) { $row['key'] = substr($row['key'], 1); } // Add the meta except for those that the user deleted for this entry. if ($row['value'] !== '::DELETED::') { $meta[] = cnMeta::add('entry', $id, $row['key'], $row['value']); } } } // Lastly, add any new meta the user may have added. if (isset($_POST['newmeta']) || !empty($_POST['newmeta'])) { foreach ($_POST['newmeta'] as $row) { // If the key begins with an underscore, remove it because those are private. if (isset($row['key'][0]) && '_' == $row['key'][0]) { $row['key'] = substr($row['key'], 1); } $metaIDs[] = cnMeta::add('entry', $id, $row['key'], $row['value']); } // $newmeta = cnMeta::add( 'entry', $id, $_POST['newmeta']['0']['key'], $_POST['newmeta']['99']['value'] ); } if (isset($_POST['metakeyselect']) && $_POST['metakeyselect'] !== '-1') { $metaSelect[] = cnMeta::add('entry', $id, $_POST['metakeyselect'], $_POST['newmeta']['99']['value']); } $metaIDs['added'] = array_merge($meta, $newmeta, $metaSelect); break; case 'update': // Query the meta associated to the entry. //$results = cnMeta::get( 'entry', $id ); $results = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value, meta_id, entry_id\n\t\t\t\t\t\t\tFROM " . CN_ENTRY_TABLE_META . " WHERE entry_id = %d\n\t\t\t\t\t\t\tORDER BY meta_key,meta_id", $id), ARRAY_A); if ($results !== FALSE) { // Loop thru $results removing any custom meta fields. Custom meta fields are considered to be private. foreach ($results as $metaID => $row) { if (cnMeta::isPrivate($row['meta_key'])) { unset($results[$row['meta_id']]); } } // Loop thru the associated meta and update any that may have been changed. // If the meta id doesn't exist in the $_POST data, assume the user deleted it. foreach ($results as $metaID => $row) { // Update the entry meta if it differs. if (isset($_POST['meta'][$row['meta_id']]['value']) && $_POST['meta'][$row['meta_id']]['value'] !== $row['meta_value'] || isset($_POST['meta'][$row['meta_id']]['key']) && $_POST['meta'][$row['meta_id']]['key'] !== $row['meta_key'] && $_POST['meta'][$row['meta_id']]['value'] !== '::DELETED::') { // If the key begins with an underscore, remove it because those are private. //if ( isset( $row['key'][0] ) && '_' == $row['key'][0] ) $row['key'] = substr( $row['key'], 1 ); //cnMeta::update( 'entry', $id, $_POST['meta'][ $row['meta_id'] ]['key'], $_POST['meta'][ $row['meta_id'] ]['value'], $row['meta_value'], $row['meta_key'], $row['meta_id'] ); cnMeta::updateByID('entry', $row['meta_id'], $_POST['meta'][$row['meta_id']]['value'], $_POST['meta'][$row['meta_id']]['key']); $metaIDs['updated'] = $row['meta_id']; } if (isset($_POST['meta'][$row['meta_id']]['value']) && $_POST['meta'][$row['meta_id']]['value'] === '::DELETED::') { // Record entry meta to be deleted. cnMeta::deleteByID('entry', $row['meta_id']); $metaIDs['deleted'] = $row['meta_id']; } } } // Lastly, add any new meta the user may have added. if (isset($_POST['newmeta']) || !empty($_POST['newmeta'])) { foreach ($_POST['newmeta'] as $row) { // If the key begins with an underscore, remove it because those are private. if (isset($row['key'][0]) && '_' == $row['key'][0]) { $row['key'] = substr($row['key'], 1); } $metaIDs[] = cnMeta::add('entry', $id, $row['key'], $row['value']); } // $newmeta = cnMeta::add( 'entry', $id, $_POST['newmeta']['0']['key'], $_POST['newmeta']['99']['value'] ); } if (isset($_POST['metakeyselect']) && $_POST['metakeyselect'] !== '-1') { $metaSelect[] = cnMeta::add('entry', $id, $_POST['metakeyselect'], $_POST['newmeta']['99']['value']); } $metaIDs['added'] = array_merge($newmeta, $metaSelect); break; } return $metaIDs; }