1: <?php
2: /**
3: * Hook for Energy certificate on Property Templates
4: *
5: * @package EPL
6: * @subpackage Hooks/EnergyCertificate
7: * @copyright Copyright (c) 2020, Merv Barrett
8: * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9: * @since 3.2.0
10: */
11:
12: // Exit if accessed directly.
13: if ( ! defined( 'ABSPATH' ) ) {
14: exit;
15: }
16:
17: /**
18: * Outputs energy certificate button on the property templates
19: *
20: * When the hook epl_buttons_single_property is used and the property
21: * has an energy certificate they will be output on the template
22: *
23: * @since 3.2.0
24: * @since 3.4.25 filter epl_show_{key} e.g. epl_show_property_energy_certificate to disable button rendering.
25: * @since 3.4.38 Added filter epl_energy_certificate_keys to support additional energy certificates.
26: * @since 3.5 Fix for label to use esc_html instead of esc_attr.
27: */
28: function epl_button_energy_certificate() {
29:
30: $keys = apply_filters( 'epl_energy_certificate_keys', array( 'property_energy_certificate' ) );
31:
32: foreach ( $keys as $key ) {
33: $link = get_post_meta( get_the_ID(), $key, true );
34: $count = 'property_energy_certificate' === $key ? '' : substr( $key, - 1 );
35: $default = __( 'Energy Certificate ', 'easy-property-listings' ) . $count;
36: $meta_label = get_post_meta( get_the_ID(), $key . '_label', true );
37: $meta_label = empty( $meta_label ) ? $default : $meta_label;
38:
39: if ( is_array( $link ) ) { // Fallback if metadata is saved as an array.
40:
41: if ( ! empty( $link['image_url_or_path'] ) ) {
42: $link = $link['image_url_or_path'];
43: } else {
44: $link = '';
45: }
46: }
47:
48: if ( ! empty( $link ) && apply_filters( 'epl_show_' . $key, true ) ) { ?>
49: <button type="button" class="epl-button epl-energy-certificate"
50: onclick="window.open('<?php echo esc_url( $link ); ?>')">
51: <?php
52: if ( has_filter( 'epl_button_label_' . $key ) ) {
53: $label = apply_filters( 'epl_button_label_' . $key, $meta_label );
54: } else {
55: $label = apply_filters( 'epl_button_label_energy_certificate', $meta_label );
56: }
57: ?>
58: <?php echo esc_html( $label ); ?>
59: </button>
60: <?php
61:
62: }
63: }
64: }
65:
66: add_action( 'epl_buttons_single_property', 'epl_button_energy_certificate' );
67: