/** * Callback to render the "Name" metabox. * * @access private * @since 0.8 * @param object $entry An instance of the cnEntry object. * @param array $metabox The metabox attributes array set in self::register(). * @return void */ public static function name($entry, $metabox, $atts = array()) { // Grab an instance of the Connections object. $instance = Connections_Directory(); // This array will store field group IDs as the fields are registered. // This array will be checked for an existing ID before rendering // a field to prevent multiple field group IDs from being rendered. $groupIDs = array(); // This array will store field IDs as the fields are registered. // This array will be checked for an existing ID before rendering // a field to prevent multiple field IDs from being rendered. $fieldIDs = array(); $defaults = array('type' => array('individual', 'organization', 'family'), 'individual' => array('meta' => array('name' => array('show' => TRUE, 'field' => array('prefix' => array('id' => 'honorific_prefix', 'show' => TRUE, 'label' => __('Prefix', 'connections'), 'required' => FALSE, 'type' => 'text', 'value' => strlen($entry->getHonorificPrefix()) > 0 ? $entry->getHonorificPrefix('edit') : '', 'before' => '<span id="cn-name-prefix">', 'after' => '</span>'), 'first' => array('id' => 'first_name', 'show' => TRUE, 'label' => __('First Name', 'connections'), 'required' => TRUE, 'type' => 'text', 'value' => strlen($entry->getFirstName()) > 0 ? $entry->getFirstName('edit') : '', 'before' => '<span id="cn-name-first">', 'after' => '</span>'), 'middle' => array('id' => 'middle_name', 'show' => TRUE, 'label' => __('Middle Name', 'connections'), 'required' => FALSE, 'type' => 'text', 'value' => strlen($entry->getMiddleName()) > 0 ? $entry->getMiddleName('edit') : '', 'before' => '<span id="cn-name-middle">', 'after' => '</span>'), 'last' => array('id' => 'last_name', 'show' => TRUE, 'label' => __('Last Name', 'connections'), 'required' => TRUE, 'type' => 'text', 'value' => strlen($entry->getLastName()) > 0 ? $entry->getLastName('edit') : '', 'before' => '<span id="cn-name-last">', 'after' => '</span>'), 'suffix' => array('id' => 'honorific_suffix', 'show' => TRUE, 'label' => __('Suffix', 'connections'), 'required' => FALSE, 'type' => 'text', 'value' => strlen($entry->getHonorificSuffix()) > 0 ? $entry->getHonorificSuffix('edit') : '', 'before' => '<span id="cn-name-suffix">', 'after' => '</span>'))), 'title' => array('show' => TRUE, 'field' => array('title' => array('id' => 'title', 'show' => TRUE, 'label' => __('Title', 'connections'), 'required' => FALSE, 'type' => 'text', 'value' => strlen($entry->getTitle()) > 0 ? $entry->getTitle('edit') : ''))), 'organization' => array('show' => TRUE, 'field' => array('organization' => array('id' => 'organization', 'show' => TRUE, 'label' => __('Organization', 'connections'), 'required' => FALSE, 'type' => 'text', 'value' => strlen($entry->getOrganization()) > 0 ? $entry->getOrganization('edit') : ''))), 'department' => array('show' => TRUE, 'field' => array('department' => array('id' => 'department', 'show' => TRUE, 'label' => __('Department', 'connections'), 'required' => FALSE, 'type' => 'text', 'value' => strlen($entry->getDepartment()) > 0 ? $entry->getDepartment('edit') : ''))))), 'organization' => array('meta' => array('organization' => array('show' => TRUE, 'field' => array('organization' => array('id' => 'organization', 'show' => TRUE, 'label' => __('Organization', 'connections'), 'required' => FALSE, 'type' => 'text', 'value' => strlen($entry->getOrganization()) > 0 ? $entry->getOrganization('edit') : ''))), 'department' => array('show' => TRUE, 'field' => array('department' => array('id' => 'department', 'show' => TRUE, 'label' => __('Department', 'connections'), 'required' => FALSE, 'type' => 'text', 'value' => strlen($entry->getDepartment()) > 0 ? $entry->getDepartment('edit') : ''))), 'contact' => array('show' => TRUE, 'field' => array('contact_first_name' => array('id' => 'contact_first_name', 'show' => TRUE, 'label' => __('Contact First Name', 'connections'), 'required' => FALSE, 'type' => 'text', 'value' => strlen($entry->getContactFirstName()) > 0 ? $entry->getContactFirstName('edit') : '', 'before' => '<span class="cn-half-width" id="cn-contact-first-name">', 'after' => '</span>'), 'contact_last_name' => array('id' => 'contact_last_name', 'show' => TRUE, 'label' => __('Contact Last Name', 'connections'), 'required' => FALSE, 'type' => 'text', 'value' => strlen($entry->getContactLastName()) > 0 ? $entry->getContactLastName('edit') : '', 'before' => '<span class="cn-half-width" id="cn-contact-last-name">', 'after' => '</span>'))))), 'family' => array('callback' => array(__CLASS__, 'family'), 'meta' => array())); $atts = wp_parse_args(apply_filters('cn_metabox_name_atts', $atts), $defaults); foreach ((array) $atts['type'] as $entryType) { if (array_key_exists($entryType, $atts)) { if (isset($atts[$entryType]['callback'])) { call_user_func($atts[$entryType]['callback'], $entry, $atts[$entryType]['meta']); continue; } /* * Dump the output in a var that way it can mre more easily broke up and filters added later. */ $out = ''; foreach ($atts[$entryType]['meta'] as $type => $meta) { if (in_array($type, $groupIDs)) { continue; } else { $groupIDs[] = $type; } $out .= '<div class="cn-metabox" id="cn-metabox-section-' . $type . '">' . PHP_EOL; if ($meta['show'] == TRUE) { foreach ($meta['field'] as $field) { if (in_array($field['id'], $fieldIDs)) { continue; } else { $fieldIDs[] = $field['id']; } if ($field['show']) { $defaults = array('type' => '', 'class' => array(), 'id' => '', 'style' => array(), 'options' => array(), 'value' => '', 'required' => FALSE, 'label' => '', 'before' => '', 'after' => '', 'return' => TRUE); $field = wp_parse_args($field, $defaults); $out .= cnHTML::field(array('type' => $field['type'], 'class' => $field['class'], 'id' => $field['id'], 'style' => $field['style'], 'options' => $field['options'], 'required' => $field['required'], 'label' => $field['label'], 'before' => $field['before'], 'after' => $field['after'], 'return' => TRUE), $field['value']); } } } $out .= '</div>' . PHP_EOL; } echo $out; } } }
/** * 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 * @uses wp_parse_args() * @param array $atts Shortcode $atts passed by the `cn_action_entry_after` action hook. * @param object $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); }