consistency-check.php 3.45 KB
Newer Older
imac's avatar
imac committed
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
<?php
if ( !defined('ABSPATH') ) {
	die();
}

/** @var string $pluginFile Should be provided by the including file. */

$log = array();
$log[] = sprintf(
	'[OK] Main plugin file: %s',
	$pluginFile
);

$log[] = sprintf(
	'[Info] WordPress version: %s',
	$GLOBALS['wp_version']
);

$log[] = sprintf(
	'[Info] WP_PLUGIN_DIR: %s',
	WP_PLUGIN_DIR
);

$log[] = sprintf(
	'[Info] WP_PLUGIN_URL: %s',
	WP_PLUGIN_URL
);

$log[] = sprintf(
	'[Info] WPMU_PLUGIN_DIR: %s',
	WPMU_PLUGIN_DIR
);

$log[] = sprintf(
	'[Info] WPMU_PLUGIN_URL: %s',
	WPMU_PLUGIN_URL
);

$expectedPluginRoot = dirname(dirname(__FILE__));
$actualPluginRoot = dirname($pluginFile);

if ( $expectedPluginRoot === $actualPluginRoot ) {
	$log[] = sprintf(
		'[OK] Plugin root directory is "%s"',
		$actualPluginRoot
	);
} else {
	$log[] = sprintf(
		'[Error] Actual plugin directory: "%s", expected: "%s"',
		$actualPluginRoot,
		$expectedPluginRoot
	);
}

$requiredFiles = array(
	'css/menu-editor.css',
	'css/jquery.qtip.min.css',
	'js/menu-editor.js',
	'js/menu-highlight-fix.js',
	'js/jquery.sort.js',
	'js/jquery.qtip.min.js',
	'js/jquery.json.js',
	'images/cut.png',
	'images/delete.png',
	'images/page_white_add.png',
	'images/spinner.gif',
	'includes/editor-page.php',
	'includes/menu-editor-core.php',
	'modules/access-editor/access-editor-template.php',
	'includes/menu-item.php',
	'menu-editor.php',
	'uninstall.php',
);

foreach($requiredFiles as $filename) {
	$fullPath = dirname($pluginFile) . '/' . $filename;
	if ( is_readable($fullPath) ) {
		$log[] = sprintf(
			'[OK] File exists: %s',
			$fullPath
		);
	} else {
		$log[] = sprintf(
			'[Error] File does not exist: %s',
			$fullPath
		);
	}
}

foreach($requiredFiles as $filename) {
	if ( !preg_match('@\.(css|js|png)$@', $filename) ) {
		continue;
	}

	$url = plugins_url($filename, $pluginFile);
	$log[] = ame_test_url_access($url, $filename);
}

echo '<pre>';
$divider = str_repeat('-', 50);
echo "File consistency checks:\n", $divider, "\n";
foreach($log as $message) {
	echo $message, "\n";
}

//Test for buggy plugins_url filters.
echo $divider, "\nTesting for problems with the 'plugins_url' hook...\n";
add_filter('plugins_url', 'ame_plugins_url_test_first', -9999, 3);
add_filter('plugins_url', 'ame_plugins_url_test_last', 9999, 3);

$url = plugins_url('css/menu-editor.css', $pluginFile);

remove_filter('plugins_url', 'ame_plugins_url_test_first', -9999, 3);
remove_filter('plugins_url', 'ame_plugins_url_test_last', 9999, 3);

function ame_plugins_url_test_first($url, $path = '', $plugin = '') {
	printf(
		'[Info] plugins_url() output before plugin hooks: %s' . "\n",
		esc_html($url)
	);
	echo ame_test_url_access($url, 'css/menu-editor.css'), "\n";
	return $url;
}

function ame_plugins_url_test_last($url, $path = '', $plugin = '') {
	printf(
		'[Info] plugins_url() output after plugin hooks: %s' . "\n",
		esc_html($url)
	);
	echo ame_test_url_access($url, 'css/menu-editor.css'), "\n";
	return $url;
}

function ame_test_url_access($url, $filename) {
	$result = wp_remote_get($url);

	if ( is_wp_error($result) ) {
		return sprintf(
			'[Error] Can not load URL: %s (%s)',
			esc_html($url),
			$result->get_error_message()
		);
	} else if ( $result['response']['code'] == 200 ) {
		return sprintf(
			'[OK] URL is accessible: %s',
			esc_html($url)
		);
	} else {
		return sprintf(
			'[Error] Can no load "%s", URL : %s (%d %s)',
			esc_html($filename),
			esc_html($url),
			$result['response']['code'],
			$result['response']['message']
		);
	}
}

echo $divider;
echo '</pre>';