-
Notifications
You must be signed in to change notification settings - Fork 4
/
wp-template-preview.php
234 lines (210 loc) · 5.76 KB
/
wp-template-preview.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Plugin Name: WP Template Preview
* Plugin URI: https://obiPlabon.im/wp-template-preview
* Description: Display custom page or post template preview image to improve UX.
* Version: 0.0.8
* Author: obiPlabon
* Author URI: https://obiPlabon.im
* Contributor: oneTarek http://onetarek.com
*
* Text Domain: wp-template-preview
* Domain Path: /lang
*
* @package WP_Template_Preview
* @category Core
* @author obiPlabon
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_Template_Preview' ) ) :
/**
* WP_Template_Preview class defination
*/
class WP_Template_Preview {
/**
* Custom tempaltes image preview header map
*
* @var array
*/
protected $header = array();
/**
* Constructor
*
* Setup initialization hooks
*/
public function __construct() {
add_action( 'load-post.php', array( $this, 'init' ) );
add_action( 'load-post-new.php', array( $this, 'init' ) );
add_filter( 'template_include', array( $this,'set_preview_template' ) );
}
/**
* Initialize all the stuffs
*
* @return void
*/
public function init() {
$this->header = array(
'PreviewImage' => apply_filters( 'wp_template_preview_header', 'Preview Image' ),
);
add_action( 'page_attributes_meta_box_template', array( $this, 'render_frame' ), 10, 2 );
add_action( 'admin_head', array( $this, 'enqueue_style' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_script' ) );
}
/**
* Enqueue styles to admin head
*
* @return void
*/
public function enqueue_style() {
?>
<style>
#wp-template-preview-links {
margin-top: .5em;
margin-bottom: .5em;
}
#wp-template-preview {
margin-top: .5em;
}
#wp-template-preview > img {
max-width: 100%;
height: auto;
}
</style>
<?php
}
/**
* Enqueue scripts to admin footer
*
* @return void
*/
public function enqueue_script() {
wp_enqueue_script(
'wp-template-preview',
plugin_dir_url( __FILE__ ) . 'assets/js/main.js',
array( 'jquery' ),
'0.0.8',
true
);
}
/**
* Render preview image frame
*
* Render already selected template preview image
* and create a frame to load template preivew image.
*
* @param string $template
* @param object $post WP_Post object
* @return void
*/
public function render_frame( $template, $post ) {
$links = $this->get_preview_links( $post );
if ( count( $links ) ) {
$current_template_preview_link = isset( $links[ $template ] ) ? $links[ $template ] : '#';
?>
<div id="wp-template-preview-links" data-template-preview-links="<?php echo esc_js( wp_json_encode( $links ) ); ?>">
<a target="_blank" href="<?php echo esc_url( $current_template_preview_link ); ?>"><?php esc_html_e( 'Template Preview', 'wp-template-preview' ); ?></a>
</div>
<?php
}
$images = $this->get_images( $post );
$images['default'] = ''; // Prevents breaking
if ( empty( $images ) || ! isset( $images[ $template ] ) ) {
return;
}
?>
<div id="wp-template-preview" data-template-images="<?php echo esc_js( wp_json_encode( $images ) ); ?>">
<img src="<?php echo esc_url( $images[ $template ] ); ?>" alt="<?php esc_attr( $template ); ?>">
</div>
<?php
}
/**
* Get the list of all preview images
*
* Get all preivew images of the current post type
*
* @param object $post WP_Post object
* @return array Images URI with template name
*/
protected function get_images( $post ) {
$images = array();
foreach ( get_page_templates( $post ) as $template_name => $template_file ) {
$data = $this->get_header_data( $template_file );
if ( empty( $data['PreviewImage'] ) ) {
continue;
}
if ( file_exists( $this->get_path() . $data['PreviewImage'] ) ) {
$images[ $template_file ] = $this->get_uri() . $data['PreviewImage'];
}
}
return $images;
}
/**
* Get the list of all preview links
*
* Get all preivew links for all templates for current post
*
* @param object $post WP_Post object
* @return array URL
* @author oneTarek
*/
protected function get_preview_links( $post ) {
$links = array();
$templates = get_page_templates( $post );
$preview_link = get_preview_post_link( $post );
foreach ( $templates as $template_name => $template_file ) {
$links[ $template_file ] = add_query_arg( array( 'template' => $template_file ), $preview_link );
}
return $links;
}
/**
* Get template preview image header data
*
* @param string $template_file Template file name
* @return array Header data
*/
protected function get_header_data( $template_file ) {
return get_file_data( get_template_directory() . DIRECTORY_SEPARATOR . $template_file, $this->header );
}
/**
* Returns template directory path
*
* @return string Template directory path
*/
protected function get_uri() {
return get_template_directory_uri() . DIRECTORY_SEPARATOR;
}
/**
* Returns template directory URI
*
* @return string Template URI
*/
protected function get_path() {
return get_template_directory() . DIRECTORY_SEPARATOR;
}
/**
* Filter wp current tempalte.
*
* If current request is a page preview request and custom
* page tempalte file name is given with url then load that new tempalte
*
* @param string $template.
* @return string
* @author oneTarek
**/
public function set_preview_template( $template ) {
if ( ! is_preview() ) {
return $template;
}
if ( isset( $_GET['template'] ) && '' != $_GET['template'] ) {
$new_template = locate_template( $_GET['template'], false, false );
if ( $new_template ) {
return $new_template;
}
}
return $template;
}
}
new WP_Template_Preview();
endif;