/**
  * Prepare a single post output for response.
  *
  * @param cnEntry         $entry   Post object.
  * @param WP_REST_Request $request Request object.
  *
  * @return WP_REST_Response $data
  */
 public function prepare_item_for_response($entry, $request)
 {
     $data = array();
     //$entry->directoryHome(
     //	array(
     //		'page_id'    => $homeID,
     //		'force_home' => $forceHome,
     //	)
     //);
     switch ($entry->getEntryType()) {
         case 'individual':
             $data['@type'] = 'Person';
             $data['honorificPrefix'] = $entry->getHonorificPrefix();
             $data['givenName'] = $entry->getFirstName();
             $data['additionalName'] = $entry->getMiddleName();
             $data['familyName'] = $entry->getLastName();
             $data['honorificSuffix'] = $entry->getHonorificSuffix();
             break;
         case 'organization':
             $data['@type'] = 'Organization';
             $data['name'] = $entry->getName();
             break;
         case 'family':
             /**
              * Unfortunately there is no "Family" type available.
              * Use "Person" as the type with the "additionalType" set as "Family". Valid? Unsure.
              *
              * NOTES: Perhaps if "Family" is merged in from @link http://historical-data.org/ it can be used.
              */
             $data['@type'] = 'Person';
             $data['additionalType'] = 'Family';
             $data['name'] = $entry->getName();
             break;
         default:
             /** @todo add filter for custom entry types */
     }
     /**
      * NOTES:
      *
      *  - Social network links should use the sameAs property as an array.
      */
     // Wrap the data in a response object.
     $response = rest_ensure_response($data);
     return $response;
 }
 /**
  * Outputs entry data JSON encoded in HTML data attribute.
  * This is an action called by the `cn_action_entry_after` hook.
  *
  * @access public
  * @since  0.8
  *
  * @param array   $atts  Shortcode $atts passed by the `cn_action_entry_after` action hook.
  * @param cnEntry $entry An instance the the cnEntry object.
  *
  * @return string
  */
 public static function JSON($atts, $entry)
 {
     $defaults = array('tag' => 'div', 'before' => '', 'after' => '', 'return' => FALSE, 'show_addresses' => TRUE, 'show_phone_numbers' => TRUE, 'show_email' => TRUE, 'show_im' => TRUE, 'show_social_media' => TRUE, 'show_links' => TRUE, 'show_dates' => TRUE, 'show_bio' => TRUE, 'show_notes' => TRUE);
     $atts = wp_parse_args($atts, $defaults);
     $data = array('type' => $entry->getEntryType(), 'id' => $entry->getId(), 'ruid' => $entry->getRuid(), 'slug' => $entry->getSlug(), 'name' => array('full' => $entry->getName($atts), 'prefix' => $entry->getHonorificPrefix(), 'first' => $entry->getFirstName(), 'middle' => $entry->getMiddleName(), 'last' => $entry->getLastName(), 'suffix' => $entry->getHonorificSuffix()), 'title' => $entry->getTitle(), 'organization' => $entry->getOrganization(), 'department' => $entry->getDepartment(), 'contact_name' => array('full' => $entry->getContactName(), 'first' => $entry->getContactFirstName(), 'last' => $entry->getContactLastName()), 'family_name' => $entry->getFamilyName(), 'family_members' => $entry->getFamilyMembers(), 'categories' => $entry->getCategory(), 'meta' => $entry->getMeta($atts));
     if ($atts['show_addresses']) {
         $data['addresses'] = $entry->getAddresses($atts);
     }
     if ($atts['show_phone_numbers']) {
         $data['phone_numbers'] = $entry->getPhoneNumbers($atts);
     }
     if ($atts['show_email']) {
         $data['email_addresses'] = $entry->getEmailAddresses($atts);
     }
     if ($atts['show_im']) {
         $data['im'] = $entry->getIm($atts);
     }
     if ($atts['show_social_media']) {
         $data['social_media'] = $entry->getSocialMedia($atts);
     }
     if ($atts['show_links']) {
         $data['links'] = $entry->getLinks($atts);
     }
     if ($atts['show_dates']) {
         $data['dates'] = $entry->getDates($atts);
     }
     if ($atts['show_bio']) {
         $data['bio'] = $entry->getBio();
     }
     if ($atts['show_notes']) {
         $data['notes'] = $entry->getNotes();
     }
     $out = sprintf('<%1$s class="cn-entry-data-json" data-entry-data-json=\'%2$s\'></%1$s>', $atts['tag'], htmlspecialchars(json_encode($data), ENT_QUOTES, 'UTF-8'));
     $out = (empty($atts['before']) ? '' : $atts['before']) . $out . (empty($atts['after']) ? '' : $atts['after']) . PHP_EOL;
     return self::echoOrReturn($atts['return'], $out);
 }
 /**
  * Prepare a single entry output for response.
  *
  * @param cnEntry         $entry   Post object.
  * @param WP_REST_Request $request Request object.
  *
  * @return WP_REST_Response $data
  */
 public function prepare_item_for_response($entry, $request)
 {
     $data = array();
     //$entry->directoryHome(
     //	array(
     //		'page_id'    => $homeID,
     //		'force_home' => $forceHome,
     //	)
     //);
     $data['id'] = $entry->getId();
     $data['type'] = $entry->getEntryType();
     $data['slug'] = $entry->getSlug();
     $data['name'] = array('raw' => $entry->getName(array(), 'raw'), 'rendered' => $entry->getName());
     $data['honorific_prefix'] = array('raw' => $entry->getHonorificPrefix('raw'), 'rendered' => $entry->getHonorificPrefix());
     $data['given_name'] = array('raw' => $entry->getFirstName('raw'), 'rendered' => $entry->getFirstName());
     $data['additional_name'] = array('raw' => $entry->getMiddleName('raw'), 'rendered' => $entry->getMiddleName());
     $data['family_name'] = array('raw' => $entry->getLastName('raw'), 'rendered' => $entry->getLastName());
     $data['honorific_suffix'] = array('raw' => $entry->getHonorificSuffix('raw'), 'rendered' => $entry->getHonorificSuffix());
     $data['job_title'] = array('raw' => $entry->getTitle('raw'), 'rendered' => $entry->getTitle());
     $data['org'] = array('organization_name' => array('raw' => $entry->getDepartment('raw'), 'rendered' => $entry->getDepartment()), 'organization_unit' => array('raw' => $entry->getOrganization('raw'), 'rendered' => $entry->getOrganization()));
     $data['contact'] = array('given_name' => array('raw' => $entry->getContactFirstName('raw'), 'rendered' => $entry->getContactFirstName()), 'family_name' => array('raw' => $entry->getContactLastName('raw'), 'rendered' => $entry->getContactLastName()));
     $data = $this->prepare_address_for_response($entry, $request, $data);
     // Wrap the data in a response object.
     $response = rest_ensure_response($data);
     return $response;
 }
Пример #4
0
 /**
  * Sort the entries by the user set attributes.
  *
  * $object -- syntax is field|SORT_ASC(SORT_DESC)|SORT_REGULAR(SORT_NUMERIC)(SORT_STRING)
  *
  * example  -- 'state|SORT_ASC|SORT_STRING, last_name|SORT_DESC|SORT_REGULAR
  *
  *
  * Available order_by fields:
  *  id
  *  date_added
  *  date_modified
  *  first_name
  *  last_name
  *  organization
  *  department
  *  city
  *  state
  *  zipcode
  *  country
  *  birthday
  *  anniversary
  *
  * Order Flags:
  *  SORT_ACS
  *  SORT_DESC
  *  SPECIFIED**
  *  RANDOM**
  *
  * Sort Types:
  *  SORT_REGULAR
  *  SORT_NUMERIC
  *  SORT_STRING
  *
  * **NOTE: The SPECIFIED and RANDOM Order Flags can only be used
  * with the id field. The SPECIFIED flag must be used in conjunction
  * with $suppliedIDs which can be either a comma delimited sting or
  * an indexed array of entry IDs. If this is set, other sort fields/flags
  * are ignored.
  *
  * @access private
  * @since  unknown
  * @deprecated since unknown
  *
  * @param array   $entries A reference to an array of object $entries
  * @param string  $orderBy
  * @param mixed   array|string|NULL [optional]
  *
  * @return array of objects
  */
 private function orderBy(&$entries, $orderBy, $suppliedIDs = NULL)
 {
     if (empty($entries) || empty($orderBy)) {
         return $entries;
     }
     $orderFields = array('id', 'date_added', 'date_modified', 'first_name', 'last_name', 'title', 'organization', 'department', 'city', 'state', 'zipcode', 'country', 'birthday', 'anniversary');
     $sortFlags = array('SPECIFIED' => 'SPECIFIED', 'RANDOM' => 'RANDOM', 'SORT_ASC' => SORT_ASC, 'SORT_DESC' => SORT_DESC, 'SORT_REGULAR' => SORT_REGULAR, 'SORT_NUMERIC' => SORT_NUMERIC, 'SORT_STRING' => SORT_STRING);
     $specifiedIDOrder = FALSE;
     // Build an array of each field to sort by and attributes.
     $sortFields = explode(',', $orderBy);
     // For each field the sort order can be defined as well as the sort type
     foreach ($sortFields as $sortField) {
         $sortAtts[] = explode('|', $sortField);
     }
     /*
      * Dynamically build the variables that will be used for the array_multisort.
      *
      * The field type should be the first item in the array if the user
      * constructed the shortcode attribute correctly.
      */
     foreach ($sortAtts as $field) {
         // Trim any spaces the user might have added to the shortcode attribute.
         $field[0] = strtolower(trim($field[0]));
         // If a user included a sort field that is invalid/mis-spelled it is skipped since it can not be used.
         if (!in_array($field[0], $orderFields)) {
             continue;
         }
         // The dynamic variable are being created and populated.
         foreach ($entries as $key => $row) {
             $entry = new cnEntry($row);
             switch ($field[0]) {
                 case 'id':
                     ${$field[0]}[$key] = $entry->getId();
                     break;
                 case 'date_added':
                     ${$field[0]}[$key] = $entry->getDateAdded('U');
                     break;
                 case 'date_modified':
                     ${$field[0]}[$key] = $entry->getUnixTimeStamp();
                     break;
                 case 'first_name':
                     ${$field[0]}[$key] = $entry->getFirstName();
                     break;
                 case 'last_name':
                     ${$field[0]}[$key] = $entry->getLastName();
                     break;
                 case 'title':
                     ${$field[0]}[$key] = $entry->getTitle();
                     break;
                 case 'organization':
                     ${$field[0]}[$key] = $entry->getOrganization();
                     break;
                 case 'department':
                     ${$field[0]}[$key] = $entry->getDepartment();
                     break;
                 case $field[0] === 'city' || $field[0] === 'state' || $field[0] === 'zipcode' || $field[0] === 'country':
                     if ($entry->getAddresses()) {
                         $addresses = $entry->getAddresses();
                         foreach ($addresses as $address) {
                             //${$field[0]}[$key] = $address[$field[0]];
                             ${$field[0]}[$key] = $address->{$field}[0];
                             // Only set the data from the first address.
                             break;
                         }
                     } else {
                         ${$field[0]}[$key] = NULL;
                     }
                     break;
                 case 'birthday':
                     ${$field[0]}[$key] = strtotime($entry->getBirthday());
                     break;
                 case 'anniversary':
                     ${$field[0]}[$key] = strtotime($entry->getAnniversary());
                     break;
             }
         }
         // The sorting order to be determined by a lowercase copy of the original array.
         ${$field}[0] = array_map('strtolower', ${$field}[0]);
         // The arrays to be sorted must be passed by reference or it won't work.
         $sortParams[] =& ${$field}[0];
         // Add the flag and sort type to the sort parameters if they were supplied in the shortcode attribute.
         foreach ($field as $key => $flag) {
             // Trim any spaces the user might have added and change the string to uppercase..
             $flag = strtoupper(trim($flag));
             // If a user included a sort tag that is invalid/mis-spelled it is skipped since it can not be used.
             if (!array_key_exists($flag, $sortFlags)) {
                 continue;
             }
             /*
              * If the order is specified set the variable to true and continue
              * because SPECIFIED should not be added to the $sortParams array
              * as that would be an invalid argument for the array multisort.
              */
             if ($flag === 'SPECIFIED' || $flag === 'RANDOM') {
                 $idOrder = $flag;
                 continue;
             }
             // Must be pass as reference or the multisort will fail.
             $sortParams[] =& $sortFlags[$flag];
             unset($flag);
         }
     }
     /*
      *
      */
     if (isset($id) && isset($idOrder)) {
         switch ($idOrder) {
             case 'SPECIFIED':
                 $sortedEntries = array();
                 /*
                  * Convert the supplied IDs value to an array if it is not.
                  */
                 if (!is_array($suppliedIDs) && !empty($suppliedIDs)) {
                     // Trim the space characters if present.
                     $suppliedIDs = str_replace(' ', '', $suppliedIDs);
                     // Convert to array.
                     $suppliedIDs = explode(',', $suppliedIDs);
                 }
                 foreach ($suppliedIDs as $entryID) {
                     $sortedEntries[] = $entries[array_search($entryID, $id)];
                 }
                 $entries = $sortedEntries;
                 return $entries;
                 break;
             case 'RANDOM':
                 shuffle($entries);
                 return $entries;
                 break;
         }
     }
     /*print_r($sortParams);
     		print_r($first_name);
     		print_r($last_name);
     		print_r($state);
     		print_r($zipcode);
     		print_r($organization);
     		print_r($department);
     		print_r($birthday);
     		print_r($anniversary);*/
     // Must be pass as reference or the multisort will fail.
     $sortParams[] =& $entries;
     //$sortParams = array(&$state, SORT_ASC, SORT_REGULAR, &$zipcode, SORT_DESC, SORT_STRING, &$entries);
     call_user_func_array('array_multisort', $sortParams);
     return $entries;
 }