-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwoocommerce-template-hints.php
158 lines (127 loc) · 4.1 KB
/
woocommerce-template-hints.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/*
Plugin Name: WooCommerce Template Hints
Plugin URI: https://github.com/growdev/woocommerce-template-hints/
Description: WooCommerce Template Hints adds a visual border around parts of all WooCommerce pages showing details about the template used.
Version: 1.0.0
Author: Shop Plugins
Author URI: http://shopplugins.com
Text Domain: woocommerce-template-hints
* Copyright Shop Plugins
*
* This file is part of WooCommerce Availability Chart,
* a plugin for WordPress.
*
* WooCommerce Template Hints is free software:
* You can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* WooCommerce Template Hints is distributed in the hope that
* it will be useful, but WITHOUT ANY WARRANTY; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WordPress. If not, see <http://www.gnu.org/licenses/>.
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/**
* WooCommerce Template Hints Main Class
*
* @package WooCommerce Template Hints
*/
class WC_Template_Hints {
protected static $instance = null;
/**
* Constructor
*/
function __construct() {
if ( class_exists( 'WooCommerce' ) ) {
if ( $this::can_see() ) {
add_action( 'wp_head', array( $this, 'css' ) );
add_action( 'woocommerce_before_template_part', array( $this, 'output_before_template' ), 30, 4 );
add_action( 'woocommerce_after_template_part', array( $this, 'output_after_template' ), 30, 4 );
}
} else {
add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
}
}
/**
* Start the Class when called
*
* @return WC_Template_Hints
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Inline CSS
*/
public function css() { ?>
<style type="text/css">
fieldset.wcth {
border: 1px red solid;
padding: 25px 3px 3px;
margin: 5px;
}
fieldset.wcth legend {
padding: 2px 6px;
background: red;
color: white;
font-size: 10px;
}
fieldset.wcth.wcth-theme {
border: 1px blue solid;
}
fieldset.wcth.wcth-theme legend {
background: blue;
}
</style>
<?php }
/**
* Output: Before template
*
* @param string $template_name
* @param string $template_path
* @param string $located Path to the located template
* @return string
*/
public function output_before_template( $template_name, $template_path, $located, $args ) {
// TODO account for 3rd party plugins' templates that could be in the plugin directory
$core = ( strpos( $located, 'themes' ) ) ? 'theme' : 'core';
echo '<fieldset class="wcth wcth-' . $core . '">';
echo '<legend title="' . $located . '">template: ' . $template_name . ' </legend>';
}
/**
* Output: After template
*
* @return string
*/
public function output_after_template( $template_name, $template_path, $located, $args ) {
echo "</fieldset>";
}
/**
* Method: Can See (returns true for those with edit_posts cap but can be filtered through wcth_can_see)
*
* @return bool
*/
private static function can_see() {
$return = current_user_can( 'edit_posts' ) ? true : false;
return apply_filters( 'wcth_can_see', $return );
}
/**
* WooCommerce fallback notice.
*
* @return string
*/
public function woocommerce_missing_notice() {
echo '<div class="error"><p>' . sprintf( __( 'WooCommerce Template Hints requires %s to be installed and active.', 'woocommerce-template-hints' ), '<a href="http://woocommerce.com/" target="_blank">' . __( 'WooCommerce', 'woocommerce-template-hints' ) . '</a>' ) . '</p></div>';
}
}
add_action( 'plugins_loaded', array( 'WC_Template_Hints', 'get_instance' ) );