コード例 #1
0
 /**
  * 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);
 }
コード例 #2
0
 /**
  * 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;
 }
 /**
  * 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)
 {
     //$entry->directoryHome(
     //	array(
     //		'page_id'    => $homeID,
     //		'force_home' => $forceHome,
     //	)
     //);
     $addresses = $entry->getAddresses();
     /**
      * NOTES:
      *
      *  - The `coordinates` index array value must not have indexes otherwise it'll be converted to an object
      * which is invalid geoJSON.
      *  - The `coordinates` must but cast as floats otherwise they'll be converted to strings.
      *  - The `coordinates` must be longitude, latitude order per the geoJSON spec.
      *
      * @todo Loop thu each address within an entry so a geoJSON `feature` is added for each address the entry may have.
      * @todo The entry only needs to be added to $entries if it has at least one address and those address has both a latitude and longitude.
      *
      * @link http://connections-pro.com/support/topic/map-view/#post-319981
      */
     if ((!isset($addresses[0]->latitude) || empty($addresses[0]->latitude)) && (!isset($addresses[0]->longitude) || empty($addresses[0]->longitude))) {
         //return;
     }
     switch ($entry->getEntryType()) {
         case 'individual':
             $type = 'Person';
             break;
         case 'organization':
             $type = 'Organization';
             break;
         case 'family':
             $type = 'Family';
             break;
         default:
             $type = NULL;
     }
     $data = array('type' => 'Feature', 'geometry' => array('type' => 'Point', 'coordinates' => array((double) $addresses[0]->longitude, (double) $addresses[0]->latitude)), 'properties' => array('id' => $entry->getId(), 'type' => $type, 'slug' => $entry->getSlug(), 'permalink' => $entry->getPermalink(), 'name' => $entry->getName(), 'title' => $entry->getTitle(), 'department' => $entry->getDepartment() ? array('@type' => 'Organization', 'name' => $entry->getDepartment()) : NULL, 'organization' => $entry->getOrganization() ? array('@type' => 'Organization', 'name' => $entry->getOrganization()) : NULL, 'bio' => $entry->getBio(), 'notes' => $entry->getNotes()));
     // 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;
 }
コード例 #5
0
 /**
  * The core method that processes the content according to the
  * entry part that the shortcode should add to the content.
  *
  * @access private
  * @since 0.8
  * @param  array  $atts    The shortcode attributes array.
  * @param  string $content The content captured between an open/close shortcode.
  * @param  string $tag     The shortcode tag.
  *
  * @return string          The processed content.
  */
 public function shortcode($atts, $content = '', $tag = 'cn_entry')
 {
     // Bail if self::$entry is not set because an instance of the cnEntry object is required.
     if (is_null($this->entry)) {
         return '';
     }
     $defaults = array('part' => '');
     // Normally we'd use shortcode_atts, but that strips keys from $atts that do not exist in $defaults.
     // Since $atts can contain various option for the different callback methods, we'll use wp_parse_args()
     // which leaves keys that do not exist in $atts.
     $atts = wp_parse_args($atts, $defaults);
     // All the core methods in the cnEntry_HTML class echo by default, make sure to return instead.
     $atts['return'] = TRUE;
     switch ($atts['part']) {
         case 'name':
             $out = $this->entry->getName($atts);
             break;
         case 'title':
             $out = $this->entry->getTitle();
             break;
         case 'organization':
             $out = $this->entry->getOrganization();
             break;
         case 'department':
             $out = $this->entry->getDepartment();
             break;
         case 'contact':
             $out = $this->entry->getContactName($atts);
             break;
         case 'family_relationships':
             $out = $this->entry->getFamilyMembers();
             break;
         case 'addresses':
             add_shortcode('cn_address', array($this, 'address'));
             $out = has_shortcode($content, 'cn_address') ? do_shortcode($content) : '';
             remove_shortcode('cn_address');
             break;
         case 'phone_numbers':
             add_shortcode('cn_phone', array($this, 'phone'));
             $out = has_shortcode($content, 'cn_phone') ? do_shortcode($content) : '';
             remove_shortcode('cn_phone');
             break;
         case 'email':
             add_shortcode('cn_email', array($this, 'email'));
             $out = has_shortcode($content, 'cn_email') ? do_shortcode($content) : '';
             remove_shortcode('cn_email');
             break;
         case 'im':
             add_shortcode('cn_im', array($this, 'im'));
             $out = has_shortcode($content, 'cn_im') ? do_shortcode($content) : '';
             remove_shortcode('cn_im');
             break;
         case 'social_networks':
             add_shortcode('cn_social_network', array($this, 'socialNetwork'));
             $out = has_shortcode($content, 'cn_social_network') ? do_shortcode($content) : '';
             remove_shortcode('cn_social_network');
             break;
         case 'links':
             add_shortcode('cn_link', array($this, 'link'));
             $out = has_shortcode($content, 'cn_link') ? do_shortcode($content) : '';
             remove_shortcode('cn_link');
             break;
         case 'dates':
             add_shortcode('cn_date', array($this, 'date'));
             $out = has_shortcode($content, 'cn_date') ? do_shortcode($content) : '';
             remove_shortcode('cn_date');
             break;
         case 'bio':
             $out = $this->entry->getBio();
             break;
         case 'notes':
             $out = $this->entry->getNotes();
             break;
         default:
             // Custom shortcodes can be applied to the content using this filter.
             $out = apply_filters('cn_entry_part-' . $atts['part'], $content, $atts, $this->entry);
             break;
     }
     return $out;
 }