Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.12% covered (success)
94.12%
16 / 17
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Templates
94.12% covered (success)
94.12%
16 / 17
50.00% covered (danger)
50.00%
1 / 2
6.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 load_bitcoin_templates
93.75% covered (success)
93.75%
15 / 16
0.00% covered (danger)
0.00%
0 / 1
5.01
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\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     * Used to get the plugin directory URL.
26     */
27    protected Settings_Interface $settings;
28
29    /**
30     * Constructor.
31     *
32     * @param Settings_Interface $settings The plugin settings.
33     */
34    public function __construct( Settings_Interface $settings ) {
35        $this->settings = $settings;
36    }
37
38    /**
39     * Returns the full template path for templates defined within this plugin.
40     * If a template has already been specified on this filter, that is returned.
41     * If the template exists within the current theme, that is returned.
42     *
43     * `wc_get_template( 'bitcoin-paid.php', $formatted_order_details_array );`.
44     *
45     * @see woocommerce_locate_template
46     * @see https://wphave.com/include-woocommerce-templates-from-plugin/
47     *
48     * @hooked wc_get_template
49     *
50     * @param string       $template The full path to the template. Usually an incorrect (!file_exists()) path before this function runs.
51     * @param string       $template_name The template name, i.e. the relative filename from the theme or theme/woocommerce directory.
52     * @param array<mixed> $args Array of values to be exploded and made available to the included template.
53     * @param string       $template_path I'm not sure is there a difference between `$template` and `$template_path`.
54     * @param string       $default_path Optional default path, which seems to be empty in WooCommerce core.
55     *
56     * @return string
57     */
58    public function load_bitcoin_templates( string $template, string $template_name, array $args, string $template_path, string $default_path ): string {
59
60        $templates = array(
61            self::BITCOIN_UNPAID_TEMPLATE_NAME,
62            self::BITCOIN_PAID_TEMPLATE_NAME,
63            Email::TEMPLATE_NAME,
64            My_Account_View_Order::TEMPLATE_NAME,
65            Thank_You::TEMPLATE_NAME,
66            Admin_Order_UI::TEMPLATE_NAME,
67        );
68
69        // Unrelated to us, leave early.
70        if ( ! in_array( $template_name, $templates, true ) ) {
71            return $template;
72        }
73
74        // It will default to a string suggesting the template exists under the WooCommerce plugin directory, which will not exist.
75        // Or could be already set by an earlier filter, where it probably will exist.
76        if ( ! empty( $template ) && file_exists( $template ) ) {
77            return $template;
78        }
79
80        // Check does it exist inside the theme.
81        $theme_template = locate_template( array( '/woocommerce/' . $template_name, $template_name ) );
82
83        if ( $theme_template ) {
84            return $theme_template;
85        }
86
87        return $this->settings->get_plugin_dir() . 'templates/' . $template_name;
88    }
89}