| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Outdated_Template_Helper |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @since 3.0.0 |
| 7 |
*/ |
| 8 |
class LP_Outdated_Template_Helper { |
| 9 |
/** |
| 10 |
* Template counter |
| 11 |
* |
| 12 |
* @var array |
| 13 |
*/ |
| 14 |
public static $counts = array( |
| 15 |
'all' => 0, |
| 16 |
'outdated' => 0, |
| 17 |
'unversioned' => 0, |
| 18 |
'up-to-date' => 0, |
| 19 |
); |
| 20 |
|
| 21 |
/** |
| 22 |
* Get list of template files are overwritten in the theme |
| 23 |
* |
| 24 |
* @param bool $check |
| 25 |
* |
| 26 |
* @return array|bool |
| 27 |
*/ |
| 28 |
public static function get_theme_templates( $check = false ) { |
| 29 |
$template_folder = learn_press_template_path(); |
| 30 |
$template_path = LP_PLUGIN_PATH . '/templates/'; |
| 31 |
$template_dir = get_template_directory(); |
| 32 |
$stylesheet_dir = get_stylesheet_directory(); |
| 33 |
$t_folder = basename( $template_dir ); |
| 34 |
$s_folder = basename( $stylesheet_dir ); |
| 35 |
|
| 36 |
$found_files = array( |
| 37 |
$t_folder => array(), |
| 38 |
$s_folder => array(), |
| 39 |
); |
| 40 |
$outdated_templates = false; |
| 41 |
|
| 42 |
$scanned_files = self::scan_template_files( $template_path ); |
| 43 |
foreach ( $scanned_files as $file ) { |
| 44 |
$theme_folder = ''; |
| 45 |
|
| 46 |
if ( file_exists( $stylesheet_dir . '/' . $file ) ) { |
| 47 |
$theme_file = $stylesheet_dir . '/' . $file; |
| 48 |
$theme_folder = $s_folder; |
| 49 |
} elseif ( file_exists( $stylesheet_dir . '/' . $template_folder . '/' . $file ) ) { |
| 50 |
$theme_file = $stylesheet_dir . '/' . $template_folder . '/' . $file; |
| 51 |
$theme_folder = $s_folder; |
| 52 |
} elseif ( file_exists( $template_dir . '/' . $file ) ) { |
| 53 |
$theme_file = $template_dir . '/' . $file; |
| 54 |
$theme_folder = $t_folder; |
| 55 |
} elseif ( file_exists( $template_dir . '/' . $template_folder . '/' . $file ) ) { |
| 56 |
$theme_file = $template_dir . '/' . $template_folder . '/' . $file; |
| 57 |
$theme_folder = $t_folder; |
| 58 |
} else { |
| 59 |
$theme_file = false; |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! empty( $theme_file ) ) { |
| 63 |
self::$counts['all'] ++; |
| 64 |
$core_version = self::get_file_version( $template_path . $file ); |
| 65 |
$theme_version = self::get_file_version( $theme_file ); |
| 66 |
|
| 67 |
if ( $core_version && ( empty( $theme_version ) || version_compare( $theme_version, $core_version, '<' ) ) ) { |
| 68 |
if ( ! $outdated_templates ) { |
| 69 |
$outdated_templates = true; |
| 70 |
} |
| 71 |
$found_files[ $theme_folder ][] = array( |
| 72 |
str_replace( WP_CONTENT_DIR . '/themes/', '', $theme_file ), |
| 73 |
$theme_version ? $theme_version : '-', |
| 74 |
$core_version, |
| 75 |
true, |
| 76 |
); |
| 77 |
if ( empty( $theme_version ) ) { |
| 78 |
self::$counts['unversioned'] ++; |
| 79 |
} |
| 80 |
self::$counts['outdated'] ++; |
| 81 |
} else { |
| 82 |
$found_files[ $theme_folder ][] = array( |
| 83 |
str_replace( WP_CONTENT_DIR . '/themes/', '', $theme_file ), |
| 84 |
$theme_version ? $theme_version : '?', |
| 85 |
$core_version ? $core_version : '?', |
| 86 |
null, |
| 87 |
); |
| 88 |
} |
| 89 |
} |
| 90 |
if ( $check && $outdated_templates ) { |
| 91 |
return $outdated_templates; |
| 92 |
} |
| 93 |
} |
| 94 |
if ( sizeof( $found_files ) > 1 ) { |
| 95 |
$found_files = array_merge( $found_files[ $t_folder ], $found_files[ $s_folder ] ); |
| 96 |
} else { |
| 97 |
$found_files = reset( $found_files ); |
| 98 |
} |
| 99 |
|
| 100 |
usort( $found_files, array( __CLASS__, '_sort_templates' ) ); |
| 101 |
|
| 102 |
return $check ? $outdated_templates : $found_files; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Check if there is any outdated template files in current theme. |
| 107 |
* |
| 108 |
* @return array|bool |
| 109 |
*/ |
| 110 |
public static function detect_outdated_template() { |
| 111 |
$template_folder = learn_press_template_path(); |
| 112 |
$template_path = LP_PLUGIN_PATH . '/templates/'; |
| 113 |
$template_dir = get_template_directory(); |
| 114 |
$stylesheet_dir = get_stylesheet_directory(); |
| 115 |
$scanned_files = self::scan_template_files( $template_path ); |
| 116 |
$parent_item = 0; |
| 117 |
$child_item = 0; |
| 118 |
|
| 119 |
foreach ( $scanned_files as $file ) { |
| 120 |
$theme_file = false; |
| 121 |
$cradle = ''; |
| 122 |
|
| 123 |
if ( $stylesheet_dir == $template_dir ) { // Parent theme |
| 124 |
if ( file_exists( $template_dir . '/' . $file ) ) { |
| 125 |
$theme_file = $template_dir . '/' . $file; |
| 126 |
$cradle = 'parent'; |
| 127 |
} elseif ( file_exists( $template_dir . '/' . $template_folder . '/' . $file ) ) { |
| 128 |
$theme_file = $template_dir . '/' . $template_folder . '/' . $file; |
| 129 |
$cradle = 'parent'; |
| 130 |
} |
| 131 |
} else { // Child Theme |
| 132 |
if ( file_exists( $stylesheet_dir . '/' . $file ) ) { |
| 133 |
$theme_file = $stylesheet_dir . '/' . $file; |
| 134 |
$cradle = 'child'; |
| 135 |
} elseif ( file_exists( $stylesheet_dir . '/' . $template_folder . '/' . $file ) ) { |
| 136 |
$theme_file = $stylesheet_dir . '/' . $template_folder . '/' . $file; |
| 137 |
$cradle = 'child'; |
| 138 |
} elseif ( file_exists( $template_dir . '/' . $file ) ) { |
| 139 |
$theme_file = $template_dir . '/' . $file; |
| 140 |
$cradle = 'parent'; |
| 141 |
} elseif ( file_exists( $template_dir . '/' . $template_folder . '/' . $file ) ) { |
| 142 |
$theme_file = $template_dir . '/' . $template_folder . '/' . $file; |
| 143 |
$cradle = 'parent'; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if ( ! empty( $theme_file ) ) { |
| 148 |
$core_version = self::get_file_version( $template_path . $file ); |
| 149 |
$theme_version = self::get_file_version( $theme_file ); |
| 150 |
|
| 151 |
if ( $core_version && ( empty( $theme_version ) || version_compare( $theme_version, $core_version, '<' ) ) ) { |
| 152 |
if ( $cradle == 'parent' ) { |
| 153 |
$parent_item ++; |
| 154 |
} else { |
| 155 |
$child_item ++; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if ( ! empty( $child_item ) || ! empty( $parent_item ) ) { |
| 162 |
return array( |
| 163 |
'parent_item' => $parent_item, |
| 164 |
'child_item' => $child_item, |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Scan all template files in a folder (and sub-folders). |
| 173 |
* |
| 174 |
* @param string $template_path |
| 175 |
* |
| 176 |
* @return array |
| 177 |
*/ |
| 178 |
public static function scan_template_files( $template_path ) { |
| 179 |
$files = @scandir( $template_path ); |
| 180 |
$result = array(); |
| 181 |
|
| 182 |
if ( ! empty( $files ) ) { |
| 183 |
foreach ( $files as $key => $value ) { |
| 184 |
if ( ! in_array( $value, array( '.', '..', 'index.php', 'index.html' ) ) ) { |
| 185 |
if ( is_dir( $template_path . '/' . $value ) ) { |
| 186 |
$sub_files = self::scan_template_files( $template_path . '/' . $value ); |
| 187 |
|
| 188 |
foreach ( $sub_files as $sub_file ) { |
| 189 |
$result[] = $value . '/' . $sub_file; |
| 190 |
} |
| 191 |
} else { |
| 192 |
$result[] = $value; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
return $result; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Get number version of a template file. |
| 203 |
* |
| 204 |
* @param string $file |
| 205 |
* |
| 206 |
* @return string |
| 207 |
*/ |
| 208 |
public static function get_file_version( $file ) { |
| 209 |
if ( ! file_exists( $file ) ) { |
| 210 |
return ''; |
| 211 |
} |
| 212 |
|
| 213 |
$fp = fopen( $file, 'r' ); |
| 214 |
$file_data = fread( $fp, 8192 ); |
| 215 |
fclose( $fp ); |
| 216 |
$file_data = str_replace( "\r", "\n", $file_data ); |
| 217 |
$version = ''; |
| 218 |
|
| 219 |
if ( preg_match( '/^[ \t\/*#@]*' . preg_quote( '@version', '/' ) . '(.*)$/mi', $file_data, $match ) && $match[1] ) { |
| 220 |
$version = _cleanup_header_comment( $match[1] ); |
| 221 |
} |
| 222 |
|
| 223 |
return $version; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Sort overrides templates are outdated first |
| 228 |
* |
| 229 |
* @param array $a |
| 230 |
* @param array $b |
| 231 |
* |
| 232 |
* @return int |
| 233 |
*/ |
| 234 |
public static function _sort_templates( $a, $b ) { |
| 235 |
if ( $a[3] && $b[3] ) { |
| 236 |
return 0; |
| 237 |
} |
| 238 |
|
| 239 |
if ( $a[3] ) { |
| 240 |
return - 1; |
| 241 |
} |
| 242 |
|
| 243 |
if ( $b[3] ) { |
| 244 |
return 1; |
| 245 |
} |
| 246 |
|
| 247 |
return 0; |
| 248 |
} |
| 249 |
} |
| 250 |
|