/**
 * Get the entity main type for the specified post ID.
 *
 * @see wl_entity_type_taxonomy_update_term for a list of keys in the returned array.
 *
 * @param int $post_id The post ID
 * @return array|null An array of type properties or null if no term is associated
 */
function wl_entity_type_taxonomy_get_type($post_id)
{
    $terms = wp_get_object_terms($post_id, WL_ENTITY_TYPE_TAXONOMY_NAME, array('fields' => 'ids'));
    if (is_wp_error($terms)) {
        // TODO: handle error
        return null;
    }
    // If there are not terms associated, return null.
    if (0 === count($terms)) {
        return null;
    }
    // Return the entity type with the specified id.
    return wl_entity_type_taxonomy_get_term_options($terms[0]);
}
/**
 * Prints inline JavaScript with the entity types configuration removing duplicates.
 */
function wl_print_entity_type_inline_js()
{
    $terms = get_terms(WL_ENTITY_TYPE_TAXONOMY_NAME, array('hide_empty' => false, 'fields' => 'id=>name'));
    echo <<<EOF
    <script type="text/javascript">
        (function() {
        var t = [];

EOF;
    // Cycle in each WordLift term and get its metadata. The metadata will be printed as a global object in JavaScript
    // to be used by the JavaScript client library.
    foreach ($terms as $term_id => $term_name) {
        // Load the type data.
        $type = wl_entity_type_taxonomy_get_term_options($term_id);
        // Skip types that are not defined.
        if (!empty($type['uri'])) {
            // Prepare the JSON output then print it to the browser.
            $json = json_encode(array('label' => $term_name, 'uri' => $type['uri'], 'css' => $type['css_class'], 'sameAs' => $type['same_as'], 'templates' => isset($type['templates']) ? $type['templates'] : array()));
            // Output the type data.
            echo "t.push({$json});\n";
        }
    }
    echo <<<EOF
            if ('undefined' == typeof window.wordlift) {
                window.wordlift = {}
            }
            window.wordlift.types = t;

        })();
    </script>
EOF;
}
/**
 * Retrieve entity type custom fields
 *
 * @param int $entity_id id of the entity, if any
 *
 * @return mixed if $entity_id was specified, return custom_fields for that entity's type. Otherwise returns all custom_fields
 */
function wl_entity_taxonomy_get_custom_fields($entity_id = null)
{
    if (is_null($entity_id)) {
        // Return all custom fields.
        // Get taxonomy terms
        $terms = get_terms(WL_ENTITY_TYPE_TAXONOMY_NAME, array('hide_empty' => 0));
        if (is_wp_error($terms)) {
            return null;
        }
        $custom_fields = array();
        foreach ($terms as $term) {
            // Get custom_fields
            $term_options = wl_entity_type_taxonomy_get_term_options($term->term_id);
            $custom_fields[$term_options['uri']] = $term_options['custom_fields'];
        }
        return $custom_fields;
    } else {
        // Return custom fields for this specific entity's type.
        $type = wl_entity_type_taxonomy_get_type($entity_id);
        return $type['custom_fields'];
    }
}
function wl_entity_type_taxonomy_edit_term_fields($term)
{
    // put the term ID into a variable
    $t_id = $term->term_id;
    $entity_type = wl_entity_type_taxonomy_get_term_options($t_id);
    $css_class = esc_attr($entity_type['css_class']);
    $uri = esc_attr($entity_type['uri']);
    $same_as = is_array($entity_type['same_as']) ? esc_attr(implode("\n", $entity_type['same_as'])) : '';
    // retrieve the existing value(s) for this meta field. This returns an array
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label
                for="term_meta[css_class]"><?php 
    _e('Entity Type CSS Class', 'wordlift');
    ?>
</label></th>
        <td>
            <input type="text" name="term_meta[css_class]" id="term_meta[css_class]" value="<?php 
    echo $css_class;
    ?>
">

            <p class="description"><?php 
    _e('Enter a value for this field', 'wordlift');
    ?>
</p>
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="term_meta[uri]"><?php 
    _e('Entity Type URI', 'wordlift');
    ?>
</label>
        </th>
        <td>
            <input type="text" name="term_meta[uri]" id="term_meta[uri]" value="<?php 
    echo $uri;
    ?>
">

            <p class="description"><?php 
    _e('Enter a value for this field', 'wordlift');
    ?>
</p>
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row" valign="top"><label
                for="term_meta[same_as]"><?php 
    _e('Entity Type Alternative URIs', 'wordlift');
    ?>
</label></th>
        <td>
            <textarea name="term_meta[same_as]" id="term_meta[same_as]"><?php 
    echo $same_as;
    ?>
</textarea>

            <p class="description"><?php 
    _e('Enter a value for this field', 'wordlift');
    ?>
</p>
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row" valign="top"><label
                for="term_meta[additional_properties]"><?php 
    _e('Additional Properties', 'wordlift');
    ?>
</label></th>
        <td>
            <textarea name="term_meta[additional_properties]" id="term_meta[additional_properties]"><?php 
    echo '';
    ?>
</textarea>

            <p class="description"><?php 
    _e('Enter a value for this field', 'wordlift');
    ?>
</p>
        </td>
    </tr>
<?php 
}
Example #5
0
/**
 * Merge the custom_fields and microdata_templates of an entity type with the ones from parents.
 * This function is used by *wl_install_entity_type_data* at installation time.
 *
 * @param $child_term Array Child entity type (expanded as array).
 * @param $parent_term_ids Array containing the ids of the parent types.
 *
 * @return Array $child_term enriched with parents' custom_fields and microdata_template
 */
function wl_entity_type_taxonomy_type_inheritage($child_term, $parent_term_ids)
{
    // If we re at the top of hierarchy ...
    if (empty($parent_term_ids) || $parent_term_ids[0] == 0) {
        // ... return term as it is.
        return $child_term;
    }
    // Loop over parents
    $merged_custom_fields = $child_term['custom_fields'];
    $merged_microdata_template = $child_term['microdata_template'];
    foreach ($parent_term_ids as $parent_term_id) {
        // Get a parent's custom fields
        $parent_term = wl_entity_type_taxonomy_get_term_options($parent_term_id);
        $parent_term_custom_fields = $parent_term['custom_fields'];
        $parent_term_microdata_template = $parent_term['microdata_template'];
        // Merge custom fields (array)
        $merged_custom_fields = array_merge($merged_custom_fields, $parent_term_custom_fields);
        // Merge microdata templates (string)
        $merged_microdata_template = $merged_microdata_template . $parent_term_microdata_template;
    }
    // Ensure there are no duplications in microdata_templates
    $exploded_microdata_template = explode('}}', $merged_microdata_template);
    $unique_microdata_template = array_unique($exploded_microdata_template);
    $merged_microdata_template = implode('}}', $unique_microdata_template);
    // Update child_term with inherited structures
    $child_term['custom_fields'] = $merged_custom_fields;
    $child_term['microdata_template'] = $merged_microdata_template;
    // Return new version of the term
    return $child_term;
}