From 8be70352e0496305b199519b5afee77bab5e5e83 Mon Sep 17 00:00:00 2001 From: leogermani Date: Mon, 17 Dec 2018 18:28:33 -0200 Subject: [PATCH] add new template tag to get item edit link (taincan/tainacan-theme#6) --- src/classes/entities/class-tainacan-item.php | 9 ++++++ src/theme-helper/template-tags.php | 34 ++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/classes/entities/class-tainacan-item.php b/src/classes/entities/class-tainacan-item.php index 5c50220bf..ae99f6ad9 100644 --- a/src/classes/entities/class-tainacan-item.php +++ b/src/classes/entities/class-tainacan-item.php @@ -681,4 +681,13 @@ class Item extends Entity { } + /** + * Gets the url to the edit page for this item + */ + public function get_edit_url() { + $collection_id = $this->get_collection_id(); + $id = $this->get_id(); + return admin_url("?page=tainacan_admin#/collections/$collection_id/items/$id/edit"); + } + } \ No newline at end of file diff --git a/src/theme-helper/template-tags.php b/src/theme-helper/template-tags.php index 4039aa54a..78cf67804 100644 --- a/src/theme-helper/template-tags.php +++ b/src/theme-helper/template-tags.php @@ -329,6 +329,40 @@ function tainacan_current_view_displays($property) { return false; } +/** + * + * Displays the link to the edit page of an item, if current user have permission + * + * Can be used outside The Lopp if an ID is provided. + * + * The same as edit_post_link() (@see https://developer.wordpress.org/reference/functions/edit_post_link/) but for + * Tainacan Items + * + * @param string $text (optional) Anchor text. If null, default is 'Edit this item'. + * @param string $before (optional) Display before edit link + * @param string $afer (optional) Display after edit link + * @param int|WP_Post $id (optional) Post ID or post object. Default is the global $post. + * @param string $class (optional) Add custom class to link + * + */ +function tainacan_the_item_edit_link( $text = null, $before = '', $after = '', $id = 0, $class = 'post-edit-link' ) { + if ( ! $item = tainacan_get_item( $id ) ) { + return; + } + + if ( ! $item->can_edit() || ! $url = $item->get_edit_url() ) { + return; + } + + if ( null === $text ) { + $text = __( 'Edit this item', 'tainacan' ); + } + + $link = '' . $text . ''; + + echo $before . $link . $after; +} + /** * Gets the initials from a name. *