<?php

/**
 * Edit post link.
 *
 * @package Total WordPress theme
 * @subpackage Partials
 * @version 6.7
 */

defined( 'ABSPATH' ) || exit;

wp_enqueue_script( 'wpex-hide-edit-links' );

$class = 'post-edit wpex-my-40 wpex-print-hidden';

if ( 'full-screen' === wpex_content_area_layout() ) {
	$class .= ' container';
}

$extra_links = [];

if ( class_exists( 'Elementor\Plugin' )
	&& isset( Elementor\Plugin::$instance->documents )
	&& is_callable( [ Elementor\Plugin::$instance->documents, 'get' ] )
) {
	$elementor_document = Elementor\Plugin::$instance->documents->get( $post->ID );
	if ( $elementor_document
		&& is_callable( [ $elementor_document, 'get_edit_url' ] )
		&& $edit_url = $elementor_document->get_edit_url()
	) {
		$extra_links[] = '<a class="edit-template" href="' . esc_url( $edit_url ) . '">' . esc_html__( 'Edit with Elementor', 'total' ) . '</a>';
	}
}

if ( totaltheme_call_static( 'Theme_Builder\Post_Template', 'has_template' )
	&& $template_id = (int) totaltheme_call_static( 'Theme_Builder\Post_Template', 'get_template_id' )
) {
	if ( $edit_template_link = get_edit_post_link( $template_id ) ) {
		$extra_links[] = '<a class="edit-template" href="' . esc_url( $edit_template_link ) . '">' . esc_html__( 'Edit Template', 'total' ) . '</a>';
	}
}

$extra_links[] = '<a href="#" class="hide-post-edit wpex-text-lg">' . totaltheme_get_icon( 'material-close', 'wpex-flex' ) . '<span class="screen-reader-text">' . esc_html__( 'Hide Post Edit Links', 'total' ) . '</span></a>';

/**
 * Add inline style as it renders better then using wp_enqueue_style in this file
 * & loading CSS via wp_enqueue_scripts requires multiple conditional checks.
 */
$before = '<style>.post-edit{display:flex;flex-wrap:wrap;gap:5px}.is_customize_preview .post-edit{display:none}.content-full-screen .post-edit{text-align:center}.post-edit a{display:flex;align-items:center;text-decoration:none;padding:.5em .75em;border-radius:4px;color:var(--wpex-on-accent);background:var(--wpex-accent);font-family:var(--wpex-font-sans);font-size:var(--wpex-text-sm);line-height:1}.post-edit a:hover{text-decoration:none;color:var(--wpex-on-accent-alt);background:var(--wpex-accent-alt)}.post-edit .hide-post-edit svg{height:18px;width:18px}</style>';

$before .= '<div class="' . esc_attr( $class ) . '">';

// Display the edit post links
edit_post_link( null, $before, ' ' . implode( ' ', $extra_links ) . '</div>' );
