class-theme.php
6.79 KB
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
235
236
237
238
239
240
241
242
<?php
namespace um\common;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class Theme
*
* @package um\common
*/
class Theme {
public function hooks() {
add_action( 'after_switch_theme', array( &$this, 'flush_transient_templates_data' ) );
}
/**
* Find outdated UM templates and notify Administrator.
*
*/
public function flush_transient_templates_data() {
// Flush transient with the custom templates list.
delete_transient( 'um_custom_templates_list' );
}
/**
* Scan the template files.
*
* @param string $template_path Path to the template directory.
* @return array
*/
public static function scan_template_files( $template_path ) {
$files = @scandir( $template_path ); // @codingStandardsIgnoreLine.
$result = array();
if ( ! empty( $files ) ) {
foreach ( $files as $value ) {
if ( ! in_array( $value, array( '.', '..' ), true ) ) {
if ( is_dir( $template_path . DIRECTORY_SEPARATOR . $value ) ) {
$sub_files = self::scan_template_files( $template_path . DIRECTORY_SEPARATOR . $value );
foreach ( $sub_files as $sub_file ) {
$result[] = $value . DIRECTORY_SEPARATOR . $sub_file;
}
} else {
$result[] = $value;
}
}
}
}
return $result;
}
public static function get_all_templates() {
$scan_files['um'] = self::scan_template_files( UM_PATH . '/templates/' );
/**
* Filters an array of the template files for scanning versions.
*
* @since 2.6.1
* @hook um_override_templates_scan_files
*
* @param {array} $scan_files Template files for scanning versions.
*
* @return {array} Template files for scanning versions.
*/
return apply_filters( 'um_override_templates_scan_files', $scan_files );
}
/**
* @param $file string
*
* @return string
*/
public static function get_file_version( $file ) {
// Avoid notices if file does not exist.
if ( ! file_exists( $file ) ) {
return '';
}
// We don't need to write to the file, so just open for reading.
$fp = fopen( $file, 'r' ); // @codingStandardsIgnoreLine.
// Pull only the first 8kiB of the file in.
$file_data = fread( $fp, 8192 ); // @codingStandardsIgnoreLine.
// PHP will close a file handle, but we are good citizens.
fclose( $fp ); // @codingStandardsIgnoreLine.
// Make sure we catch CR-only line endings.
$file_data = str_replace( "\r", "\n", $file_data );
$version = '';
if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( '@version', '/' ) . '(.*)$/mi', $file_data, $match ) && $match[1] ) {
$version = _cleanup_header_comment( $match[1] );
}
return $version;
}
public function get_custom_templates_list() {
$files_in_theme = array();
$scan_files = self::get_all_templates();
foreach ( $scan_files as $key => $files ) {
foreach ( $files as $file ) {
if ( false === strpos( $file, 'email/' ) ) {
/**
* Filters an array of the template files for scanning versions based on $key.
*
* Note: $key - means um or extension key.
*
* @since 2.6.1
* @hook um_override_templates_get_template_path__{$key}
*
* @param {array} $located Template file paths for scanning versions.
* @param {string} $file Template file name.
*
* @return {array} Template file paths for scanning versions.
*/
$located = apply_filters( "um_override_templates_get_template_path__{$key}", array(), $file );
$exceptions = array(
'members-grid.php',
'members-header.php',
'members-list.php',
'members-pagination.php',
'searchform.php',
'login-to-view.php',
'profile/comments.php',
'profile/comments-single.php',
'profile/posts.php',
'profile/posts-single.php',
'modal/um_upload_single.php',
'modal/um_view_photo.php',
);
$theme_file = false;
if ( ! empty( $located ) ) {
$theme_file = $located['theme'];
} elseif ( in_array( $file, $exceptions, true ) && file_exists( get_stylesheet_directory() . '/ultimate-member/' . $file ) ) {
$theme_file = get_stylesheet_directory() . '/ultimate-member/' . $file;
} elseif ( file_exists( get_stylesheet_directory() . '/ultimate-member/templates/' . $file ) ) {
$theme_file = get_stylesheet_directory() . '/ultimate-member/templates/' . $file;
}
if ( ! empty( $theme_file ) ) {
if ( ! empty( $located ) ) {
$core_path = $located['core'];
} else {
$core_path = UM_PATH . 'templates/' . $file;
}
$files_in_theme[] = array(
'core' => $core_path,
'theme' => $theme_file,
);
}
}
}
}
return $files_in_theme;
}
public function is_outdated_template_exist() {
$outdated_exists = false;
$templates = $this->get_custom_templates_list();
foreach ( $templates as $files ) {
if ( ! array_key_exists( 'core', $files ) || ! array_key_exists( 'theme', $files ) ) {
continue;
}
$core_path = $files['core'];
$theme_file = $files['theme'];
$core_version = self::get_file_version( $core_path );
$theme_version = self::get_file_version( $theme_file );
if ( '' === $theme_version || version_compare( $theme_version, $core_version, '<' ) ) {
$outdated_exists = true;
break;
}
}
return $outdated_exists;
}
public function build_templates_data() {
$templates_data = array();
// Get from cache if isn't empty and request isn't force.
$transient = get_transient( 'um_custom_templates_list' );
if ( false !== $transient && array_key_exists( 'data', $transient ) ) {
return $transient['data'];
}
$templates = $this->get_custom_templates_list();
foreach ( $templates as $files ) {
if ( ! array_key_exists( 'core', $files ) || ! array_key_exists( 'theme', $files ) ) {
continue;
}
$core_path = $files['core'];
$theme_file = $files['theme'];
$core_version = self::get_file_version( $core_path );
$theme_version = self::get_file_version( $theme_file );
$status = esc_html__( 'Theme version up to date', 'ultimate-member' );
$status_code = 1;
if ( '' === $theme_version ) {
$status = esc_html__( 'Theme version is empty', 'ultimate-member' );
$status_code = 0;
} elseif ( version_compare( $theme_version, $core_version, '<' ) ) {
$status = esc_html__( 'Theme version is out of date', 'ultimate-member' );
$status_code = 0;
}
$templates_data[] = array(
'core_version' => $core_version,
'theme_version' => $theme_version,
'core_file' => stristr( $core_path, 'wp-content' ),
'theme_file' => stristr( $theme_file, 'wp-content' ),
'status' => $status,
'status_code' => $status_code,
);
}
// Cache results via transient setting.
$transient = array(
'data' => $templates_data,
'time' => time(),
);
set_transient( 'um_custom_templates_list', $transient, 5 * MINUTE_IN_SECONDS );
return $templates_data;
}
}