add new template tag to get item edit link (taincan/tainacan-theme#6)

This commit is contained in:
leogermani 2018-12-17 18:28:33 -02:00
parent 7c2edbaf34
commit 8be70352e0
2 changed files with 43 additions and 0 deletions

View File

@ -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");
}
} }

View File

@ -329,6 +329,40 @@ function tainacan_current_view_displays($property) {
return false; 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 = '<a class="' . esc_attr( $class ) . '" href="' . esc_url( $url ) . '">' . $text . '</a>';
echo $before . $link . $after;
}
/** /**
* Gets the initials from a name. * Gets the initials from a name.
* *