Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Templates
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 load_bitcoin_templates
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
3 * Register the `bitcoin-paid.php` and `bitcoin-unpaid.php` templates so they can be found with `wc_get_template()`.
4 *
5 * These templates are used on the Thank You page, Emails, and the My Account page to display payment instructions
6 * and details.
7 *
8 * @package    brianhenryie/bh-wp-bitcoin-gateway
9 */
10
11namespace BrianHenryIE\WP_Bitcoin_Gateway\Integrations\WooCommerce;
12
13use BrianHenryIE\WP_Bitcoin_Gateway\Settings_Interface;
14
15/**
16 * Hooks into the wc_get_template filter called inside `wc_get_template()` to return templates inside this plugin
17 * if they have not already been provided by the theme or another plugin.
18 */
19class Templates {
20
21    const BITCOIN_UNPAID_TEMPLATE_NAME = 'bitcoin-unpaid.php';
22    const BITCOIN_PAID_TEMPLATE_NAME   = 'bitcoin-paid.php';
23
24    /**
25     * Constructor.
26     *
27     * @param Settings_Interface $settings The plugin settings.
28     */
29    public function __construct( protected Settings_Interface $settings ) {
30    }
31
32    /**
33     * Returns the full template path for templates defined within this plugin.
34     * If a template has already been specified on this filter, that is returned.
35     * If the template exists within the current theme, that is returned.
36     *
37     * `wc_get_template( 'bitcoin-paid.php', $formatted_order_details_array );`.
38     *
39     * @see woocommerce_locate_template
40     * @see https://wphave.com/include-woocommerce-templates-from-plugin/
41     *
42     * @hooked wc_get_template
43     *
44     * @param string       $template The full path to the template. Usually an incorrect (!file_exists()) path before this function runs.
45     * @param string       $template_name The template name, i.e. the relative filename from the theme or theme/woocommerce directory.
46     * @param array<mixed> $_args Array of values to be exploded and made available to the included template.
47     * @param string       $_template_path I'm not sure is there a difference between `$template` and `$template_path`.
48     * @param string       $_default_path Optional default path, which seems to be empty in WooCommerce core.
49     *
50     * @return string
51     */
52    public function load_bitcoin_templates( string $template, string $template_name, array $_args, string $_template_path, string $_default_path ): string {
53
54        $templates = array(
55            self::BITCOIN_UNPAID_TEMPLATE_NAME,
56            self::BITCOIN_PAID_TEMPLATE_NAME,
57            Email::TEMPLATE_NAME,
58            My_Account_View_Order::TEMPLATE_NAME,
59            Thank_You::TEMPLATE_NAME,
60            Admin_Order_UI::TEMPLATE_NAME,
61        );
62
63        // Unrelated to us, leave early.
64        if ( ! in_array( $template_name, $templates, true ) ) {
65            return $template;
66        }
67
68        // It will default to a string suggesting the template exists under the WooCommerce plugin directory, which will not exist.
69        // Or could be already set by an earlier filter, where it probably will exist.
70        if ( ! empty( $template ) && file_exists( $template ) ) {
71            return $template;
72        }
73
74        // Check does it exist inside the theme.
75        $theme_template = locate_template( array( '/woocommerce/' . $template_name, $template_name ) );
76
77        if ( $theme_template ) {
78            return $theme_template;
79        }
80
81        return $this->settings->get_plugin_dir() . 'templates/' . $template_name;
82    }
83}