/**
  * 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;
 }
 /**
  * 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;
 }
Example #4
0
 /**
  * Load meta data for the player.
  */
 protected function loadMeta()
 {
     if ($this->meta !== null) {
         return $this->meta;
     }
     $meta = $this->entry->getMeta();
     if (!is_array($meta)) {
         $myMeta = array();
     } else {
         $myMeta = array();
         foreach ($meta as $key => $values) {
             if (count($values) > 1 && count(array_unique($values)) == 1) {
                 cnMeta::delete('entry', $this->entry->getId(), $key);
                 cnMeta::add('entry', $this->entry->getId(), $key, $values[0]);
             }
             $myMeta[$key] = $values[0];
         }
     }
     $this->meta = $myMeta;
     return $myMeta;
 }
 /**
  * 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;
         }
     }
 }