Merge pull request #501 from tainacan/feature/489

Feature/489
This commit is contained in:
Mateus Machado Luna 2021-03-02 14:22:51 -03:00 committed by GitHub
commit 821514db0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 121 additions and 84 deletions

View File

@ -184,36 +184,36 @@ class Theme_Helper {
}
/**
* Filters the permalink for posts to:
*
* * Replace Collection single permalink with the link to the post type archive for items of that collection
*
* @return string new permalink
*/
function permalink_filter($permalink, $post, $leavename) {
$collection_post_type = \Tainacan\Entities\Collection::get_post_type();
if (!is_admin() && $post->post_type == $collection_post_type) {
$collection = new \Tainacan\Entities\Collection($post);
* Filters the permalink for posts to:
*
* * Replace Collection single permalink with the link to the post type archive for items of that collection
*
* @return string new permalink
*/
function permalink_filter($permalink, $post, $leavename) {
$collection_post_type = \Tainacan\Entities\Collection::get_post_type();
if (!is_admin() && $post->post_type == $collection_post_type) {
$collection = new \Tainacan\Entities\Collection($post);
if ( $collection->is_cover_page_enabled() ) {
return $permalink;
}
$items_post_type = $collection->get_db_identifier();
$post_type_object = get_post_type_object($items_post_type);
if (isset($post_type_object->rewrite) && is_array($post_type_object->rewrite) && isset($post_type_object->rewrite['slug']))
return get_post_type_archive_link($items_post_type);
}
return $permalink;
}
$post_type_object = get_post_type_object($items_post_type);
if (isset($post_type_object->rewrite) && is_array($post_type_object->rewrite) && isset($post_type_object->rewrite['slug']))
return get_post_type_archive_link($items_post_type);
}
return $permalink;
}
function tax_archive_pre_get_posts($wp_query) {
@ -489,33 +489,33 @@ class Theme_Helper {
}
function rewrite_rules( &$wp_rewrite ) {
$items_base = $this->get_items_list_slug();
$new_rules = array(
$items_base . "/?$" => "index.php?tainacan_repository_archive=1",
$items_base . "/?$" => "index.php?tainacan_repository_archive=1",
$items_base . "/page/([0-9]+)/?$" => 'index.php?tainacan_repository_archive=1&paged=$matches[1]'
);
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
function rewrite_rules_query_vars( $public_query_vars ) {
$public_query_vars[] = "tainacan_repository_archive";
return $public_query_vars;
}
function rewrite_rules_query_vars( $public_query_vars ) {
$public_query_vars[] = "tainacan_repository_archive";
return $public_query_vars;
}
function rewrite_rule_template_include( $template ) {
global $wp_query;
if ( $wp_query->get( 'tainacan_repository_archive' ) == 1 ) {
function rewrite_rule_template_include( $template ) {
global $wp_query;
if ( $wp_query->get( 'tainacan_repository_archive' ) == 1 ) {
$templates = apply_filters('tainacan_repository_archive_template_hierarchy', ['tainacan/archive-repository.php', 'index.php']);
$templates = apply_filters('tainacan_repository_archive_template_hierarchy', ['tainacan/archive-repository.php', 'index.php']);
return locate_template($templates, false);
}
return $template;
}
}
return $template;
}
function archive_repository_pre_get_posts($wp_query) {
if (!$wp_query->is_main_query() || $wp_query->get( 'tainacan_repository_archive' ) != 1)
@ -608,6 +608,60 @@ class Theme_Helper {
return isset($this->registered_view_modes[$slug]) ? $this->registered_view_modes[$slug] : false;
}
/**
* When visiting a collection archive or single, returns the current collection id
*
* @uses get_post_type() WordPress function, which looks for the global $wp_query variable
*/
function tainacan_get_collection_id() {
if ( is_post_type_archive() || is_single() ) {
return \Tainacan\Repositories\Collections::get_instance()->get_id_by_db_identifier(get_post_type());
} elseif ( false !== $this->visiting_collection_cover ) {
return $this->visiting_collection_cover;
}
return false;
}
/**
* When visiting a collection archive or single, returns the current collection object
*
* @uses tainacan_get_collection_id()
* @return \Tainacan\Entities\Collection | false
*/
function tainacan_get_collection($args = []) {
$collection_id = isset($args['collection_id']) ? $args['collection_id'] : $this->tainacan_get_collection_id();
if ( $collection_id ) {
$TainacanCollections = \Tainacan\Repositories\Collections::get_instance();
$collection = $TainacanCollections->fetch($collection_id);
if ( $collection instanceof \Tainacan\Entities\Collection ) {
return $collection;
}
}
return false;
}
/**
* Gets the Tainacan Item Entity object
*
* If used inside the Loop of items, will get the Item object for the current post
*/
function tainacan_get_item($post_id = 0) {
$post = get_post( $post_id );
if (!$post)
return null;
if (!$this->is_post_an_item($post))
return null;
$item = new \Tainacan\Entities\Item($post);
return $item;
}
/**
* Adds meta tags to the header to improve social sharing
*/

View File

@ -43,11 +43,15 @@ use \Tainacan\Repositories;
* @type string $after_value String to be added after each metadata value
* Default '</p>'
* }
*
* @param int|string $item_id (Optional) The item ID to retrive the metadatum as a HTML string to be used as output. Default is the global $post
*
*
* @return string The HTML output
*/
function tainacan_get_the_metadata($args = array()) {
function tainacan_get_the_metadata($args = array(), $item_id = 0) {
$item = tainacan_get_item();
$item = tainacan_get_item( $item_id );
if ($item instanceof \Tainacan\Entities\Item) {
return $item->get_metadata_as_html($args);
@ -66,10 +70,12 @@ function tainacan_the_metadata($args = array()) {
*
* Return the item document as a HTML string to be used as output.
*
* @param int|string $item_id (Optional) The item ID. Default is the global $post
*
* @return string The HTML output
*/
function tainacan_get_the_document() {
$item = tainacan_get_item();
function tainacan_get_the_document($item_id = 0) {
$item = tainacan_get_item($item_id);
if (!$item)
return;
@ -77,8 +83,8 @@ function tainacan_get_the_document() {
return apply_filters('tainacan-get-the-document', $item->get_document_as_html(), $item);
}
function tainacan_the_item_document_download_link() {
$item = tainacan_get_item();
function tainacan_the_item_document_download_link($item_id = 0) {
$item = tainacan_get_item($item_id);
if (!$item)
return;
@ -109,9 +115,9 @@ function tainacan_the_document() {
/**
* Return HTML display-ready version of an attachment
*/
function tainacan_get_single_attachment_as_html($attachment_id) {
function tainacan_get_single_attachment_as_html($attachment_id, $item_id = 0) {
$item = tainacan_get_item();
$item = tainacan_get_item($item_id);
if (!$attachment_id) {
return '';
@ -138,15 +144,10 @@ function tainacan_has_document() {
/**
* When visiting a collection archive or single, returns the current collection id
*
* @uses get_post_type() WordPress function, which looks for the global $wp_query variable
* @uses get_post_type() WordPress function via Theme Helper, which looks for the global $wp_query variable
*/
function tainacan_get_collection_id() {
if ( is_post_type_archive() || is_single() ) {
return Repositories\Collections::get_instance()->get_id_by_db_identifier(get_post_type());
} elseif ( false !== \Tainacan\Theme_Helper::get_instance()->visiting_collection_cover ) {
return \Tainacan\Theme_Helper::get_instance()->visiting_collection_cover;
}
return false;
return \Tainacan\Theme_Helper::get_instance()->tainacan_get_collection_id();
}
/**
@ -156,15 +157,7 @@ function tainacan_get_collection_id() {
* @return \Tainacan\Entities\Collection | false
*/
function tainacan_get_collection($args = []) {
$collection_id = isset($args['collection_id']) ? $args['collection_id'] : tainacan_get_collection_id();
if ( $collection_id ) {
$TainacanCollections = Repositories\Collections::get_instance();
$collection = $TainacanCollections->fetch($collection_id);
if ( $collection instanceof Entities\Collection ) {
return $collection;
}
}
return false;
return \Tainacan\Theme_Helper::get_instance()->tainacan_get_collection($args);
}
/**
@ -414,10 +407,11 @@ function tainacan_the_term_description() {
* Return the list of attachments of the current item (by default, excluding the document and the thumbnail)
*
* @param string|array IDs of attachments to be excluded (by default this function already excludes the document and the thumbnail)
* @param int|string $item_id (Optional) The item ID to retrive attachments. Default is the global $post
* @return array Array of WP_Post objects. @see https://developer.wordpress.org/reference/functions/get_children/
*/
function tainacan_get_the_attachments($exclude = null) {
$item = tainacan_get_item();
function tainacan_get_the_attachments($exclude = null, $item_id = 0) {
$item = tainacan_get_item($item_id);
if (!$item)
return [];
@ -443,20 +437,7 @@ function tainacan_register_view_mode($slug, $args = []) {
* If used inside the Loop of items, will get the Item object for the current post
*/
function tainacan_get_item($post_id = 0) {
$post = get_post( $post_id );
if (!$post)
return null;
$theme_helper = \Tainacan\Theme_Helper::get_instance();
if (!$theme_helper->is_post_an_item($post))
return null;
$item = new Entities\Item($post);
return $item;
return \Tainacan\Theme_Helper::get_instance()->tainacan_get_item($post_id);
}
/**
@ -468,17 +449,19 @@ function tainacan_get_item($post_id = 0) {
* @param string|integer The property to be checked. If a string is passed, it will check against
* one of the native property of the item, such as title, description and creation_date.
* If an integer is passed, it will check against the IDs of the metadata.
*
* @param int|string $item_id (Optional) The item ID. Default is the global $post
*
* @return bool
*/
function tainacan_current_view_displays($property) {
function tainacan_current_view_displays($property, $item_id = 0) {
global $view_mode_displayed_metadata;
// Core metadata appear in fetch_only as metadata
if ($property == 'title' || $property == 'description') {
$item = tainacan_get_item();
$item = tainacan_get_item($item_id);
$core_getter_method = "get_core_{$property}_metadatum";
$property = $item->get_collection()->$core_getter_method()->get_id();
$property = $item->get_collection()->$core_getter_method()->get_id();
}
if (is_string($property)) {