/**
 * Get the entity main type for the specified post ID.
 *
 * @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, Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME);
    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 Wordlift_Schema_Service::get_instance()->get_schema($terms[0]->slug);
}
/**
 * Prints inline JavaScript with the entity types configuration removing duplicates.
 */
function wl_print_entity_type_inline_js()
{
    $terms = get_terms(Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, array('hide_empty' => false));
    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) {
        $term_name = $term->name;
        // Load the type data.
        $type = Wordlift_Schema_Service::get_instance()->get_schema($term->slug);
        // 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 array|null 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(Wordlift_Entity_Types_Taxonomy_Service::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 = Wordlift_Schema_Service::get_instance()->get_schema($term->slug);
            $custom_fields[$term_options['uri']] = $term_options['custom_fields'];
        }
        return $custom_fields;
    }
    // Return custom fields for this specific entity's type.
    $type = wl_entity_type_taxonomy_get_type($entity_id);
    return $type['custom_fields'];
}
 /**
  * Wordlift_Schema_Service constructor.
  *
  * @since 3.1.0
  */
 public function __construct()
 {
     $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_Schema_Service');
     // Create a singleton instance of the Schema service, useful to provide static functions to global functions.
     self::$instance = $this;
     // Set the taxonomy data.
     // Note: parent types must be defined before child types.
     $this->schema = array('thing' => $this->get_thing_schema(), 'creative-work' => $this->get_creative_work_schema(), 'event' => $this->get_event_schema(), 'organization' => $this->get_organization_schema(), 'person' => $this->get_person_schema(), 'place' => $this->get_place_schema(), 'localbusiness' => $this->get_local_business_schema());
 }