Skip to content

Commit 334a496

Browse files
committed
Enforce PSR-2 PHP formatting.
1 parent 3f26839 commit 334a496

17 files changed

+531
-474
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules
22
.DS_Store
33
npm-debug.log
44
yarn-error.log
5+
.php_cs.cache

better-resource-hints.php

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -20,57 +20,59 @@
2020
require_once 'src/Prefetcher.php';
2121
require_once 'src/Utilities.php';
2222

23-
if ( !defined( 'WPINC' ) ) {
24-
die;
23+
if (!defined('WPINC')) {
24+
die;
2525
}
2626

2727
define('BETTER_RESOURCE_HINTS_OPTIONS_PREFIX', 'better_resource_hints');
2828
define('BETTER_RESOURCE_HINTS_ADMIN_SETTINGS_PAGE_SLUG', 'better_resource_hints');
2929

30-
class App {
30+
class App
31+
{
3132

32-
/**
33-
* Initialize the plugin.
34-
*
35-
* @return object App Instance of class.
36-
*/
37-
public static function go()
38-
{
39-
$GLOBALS[ __CLASS__ ] = new self;
40-
return $GLOBALS[ __CLASS__ ];
41-
}
33+
/**
34+
* Initialize the plugin.
35+
*
36+
* @return object App Instance of class.
37+
*/
38+
public static function go()
39+
{
40+
$GLOBALS[ __CLASS__ ] = new self;
41+
return $GLOBALS[ __CLASS__ ];
42+
}
4243

43-
/**
44-
* Retrive array of plugin data.
45-
*
46-
* @return array
47-
*/
48-
public static function getPluginData()
49-
{
50-
return get_plugin_data(__FILE__);
51-
}
44+
/**
45+
* Retrive array of plugin data.
46+
*
47+
* @return array
48+
*/
49+
public static function getPluginData()
50+
{
51+
return get_plugin_data(__FILE__);
52+
}
5253

53-
public function __construct() {
54-
new Filters;
55-
new Settings;
56-
new Preloader;
57-
new Prefetcher;
58-
new Preconnector;
54+
public function __construct()
55+
{
56+
new Filters;
57+
new Settings;
58+
new Preloader;
59+
new Prefetcher;
60+
new Preconnector;
5961

60-
add_action( 'admin_enqueue_scripts', array($this, 'enqueue_styles_and_scripts' ));
61-
}
62+
add_action('admin_enqueue_scripts', array($this, 'enqueue_styles_and_scripts' ));
63+
}
6264

63-
/**
65+
/**
6466
* Enqueue necessary admin scripts & styles.
6567
*
6668
* @return void
6769
*/
68-
public function enqueue_styles_and_scripts() {
69-
$plugin_data = self::getPluginData();
70-
wp_enqueue_style( 'better-resource-hints', plugin_dir_url( __FILE__ ) . 'src/assets/css/style.css', [], $plugin_data['Version']);
71-
wp_enqueue_script( 'better-resource-hints', plugin_dir_url( __FILE__ ) . 'src/assets/js/scripts.min.js', [], $plugin_data['Version'], true);
72-
}
73-
70+
public function enqueue_styles_and_scripts()
71+
{
72+
$plugin_data = self::getPluginData();
73+
wp_enqueue_style('better-resource-hints', plugin_dir_url(__FILE__) . 'src/assets/css/style.css', [], $plugin_data['Version']);
74+
wp_enqueue_script('better-resource-hints', plugin_dir_url(__FILE__) . 'src/assets/js/scripts.min.js', [], $plugin_data['Version'], true);
75+
}
7476
}
7577

7678
App::go();

src/Filters.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22

33
namespace BetterResourceHints;
44

5-
class Filters {
5+
class Filters
6+
{
7+
public function __construct()
8+
{
9+
add_filter('style_loader_tag', array($this, 'add_id_to_style_tags'), 10, 4);
10+
add_filter('script_loader_tag', array($this, 'add_id_to_script_tags'), 10, 3);
11+
}
612

7-
public function __construct() {
8-
add_filter('style_loader_tag', array($this, 'add_id_to_style_tags'), 10, 4);
9-
add_filter('script_loader_tag', array($this, 'add_id_to_script_tags'), 10, 3);
10-
}
13+
public function add_id_to_style_tags($html, $handle, $href, $media)
14+
{
15+
return str_replace('<link ', '<link data-handle="' . $handle . '" ', $html);
16+
}
1117

12-
public function add_id_to_style_tags($html, $handle, $href, $media) {
13-
return str_replace('<link ', '<link data-handle="' . $handle . '" ', $html);
14-
}
15-
16-
public function add_id_to_script_tags($tag, $handle, $src) {
17-
return str_replace('<script ', '<script data-handle="' . $handle . '" ', $tag);
18-
}
18+
public function add_id_to_script_tags($tag, $handle, $src)
19+
{
20+
return str_replace('<script ', '<script data-handle="' . $handle . '" ', $tag);
21+
}
1922
}

src/Preconnector.php

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,47 @@
22

33
namespace BetterResourceHints;
44

5-
class Preconnector {
6-
7-
private $hostsToPreconnect = array();
8-
9-
public function __construct() {
10-
add_filter('wp_resource_hints', array($this, 'preconnect_hosts'), 11, 2);
11-
}
12-
13-
/**
14-
* If enabled, generate a preconnect tag for each asset that's dns-prefetched.
15-
*
16-
* @param array $urls
17-
* @param string $type
18-
* @return array
19-
*/
20-
public function preconnect_hosts($urls, $type) {
21-
if(Utilities::get_option('preconnect_hosts_option') === 'no_assets') return $urls;
22-
23-
foreach($urls as $url) {
24-
25-
if(Utilities::get_option("preconnect_hosts_enable_server_push")) {
26-
Utilities::construct_server_push_headers('preconnect', $url, null, false);
27-
}
28-
29-
if(gettype($url) !== 'string') continue;
30-
31-
$parsedURL = wp_parse_url($url);
32-
unset($parsedURL["scheme"]);
33-
34-
$reconstructedURL = isset($parsedURL['host'])
35-
? '//' . $parsedURL['host']
36-
: '//' . join("", $parsedURL);
37-
38-
echo apply_filters('better_resource_hints_preconnect_tag', "<link rel='preconnect' href='{$reconstructedURL}'/>\n", $reconstructedURL);
39-
}
40-
41-
return $urls;
42-
}
5+
class Preconnector
6+
{
7+
private $hostsToPreconnect = array();
8+
9+
public function __construct()
10+
{
11+
add_filter('wp_resource_hints', array($this, 'preconnect_hosts'), 11, 2);
12+
}
13+
14+
/**
15+
* If enabled, generate a preconnect tag for each asset that's dns-prefetched.
16+
*
17+
* @param array $urls
18+
* @param string $type
19+
* @return array
20+
*/
21+
public function preconnect_hosts($urls, $type)
22+
{
23+
if (Utilities::get_option('preconnect_hosts_option') === 'no_assets') {
24+
return $urls;
25+
}
26+
27+
foreach ($urls as $url) {
28+
if (Utilities::get_option("preconnect_hosts_enable_server_push")) {
29+
Utilities::construct_server_push_headers('preconnect', $url, null, false);
30+
}
31+
32+
if (gettype($url) !== 'string') {
33+
continue;
34+
}
35+
36+
$parsedURL = wp_parse_url($url);
37+
unset($parsedURL["scheme"]);
38+
39+
$reconstructedURL = isset($parsedURL['host'])
40+
? '//' . $parsedURL['host']
41+
: '//' . join("", $parsedURL);
42+
43+
echo apply_filters('better_resource_hints_preconnect_tag', "<link rel='preconnect' href='{$reconstructedURL}'/>\n", $reconstructedURL);
44+
}
45+
46+
return $urls;
47+
}
4348
}

src/Prefetcher.php

Lines changed: 67 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,71 @@
22

33
namespace BetterResourceHints;
44

5-
class Prefetcher {
6-
7-
public function __construct() {
8-
add_action('wp_head', array($this, 'prefetch_javascript'), 1);
9-
add_action('wp_head', array($this, 'prefetch_styles'), 1);
10-
}
11-
12-
public function prefetch_javascript() {
13-
$this->generate_prefetch_markup('scripts');
14-
}
15-
16-
public function prefetch_styles() {
17-
$this->generate_prefetch_markup('styles');
18-
}
19-
20-
/**
21-
* Only allow users to prefetch by specific handle,
22-
* because we don't want to prefetch all admin scripts
23-
* if that option is selected.
24-
*
25-
* @param string $type
26-
* @return void
27-
*/
28-
private function generate_prefetch_markup($type = 'scripts') {
29-
30-
global ${'wp_' . $type};
31-
$singularType = substr_replace($type, "", -1);
32-
$handlesToPrefetch = [];
33-
34-
$noSpaces = Utilities::strip_spaces(Utilities::get_option('prefetch_'. $type. '_handles'));
35-
$specificHandlesToPrefetch = explode(',', $noSpaces ?: '');
36-
37-
//-- @todo In the future, check if user would like to preload $specificHandlesToPrefetch's dependencies as well.
38-
// foreach($specificHandlesToPrefetch as $handle) {
39-
// $specificHandlesToPrefetch = array_merge($specificHandlesToPrefetch, Utilities::collect_all_deps($handle));
40-
// }
41-
42-
//-- Loop through and print preload tags.
43-
foreach(array_unique($specificHandlesToPrefetch) as $handle) {
44-
$resource = isset(${'wp_' . $type}->registered[$handle])
45-
? ${'wp_' . $type}->registered[$handle]
46-
: false;
47-
48-
if($resource === false) continue;
49-
if(empty($resource->src)) continue;
50-
51-
$methodToCheckIfAssetIsEnqueued = 'wp_' . $singularType . '_is';
52-
if($methodToCheckIfAssetIsEnqueued($handle)) continue;
53-
54-
$source = $resource->src . ($resource->ver ? "?ver={$resource->ver}" : "");
55-
56-
if(Utilities::get_option("prefetch_assets_enable_server_push")) {
57-
Utilities::construct_server_push_headers('prefetch', $source);
58-
}
59-
60-
echo apply_filters('better_resource_hints_prefetch_tag', "<link rel='prefetch' href='{$source}'/>\n", $handle, $type);
61-
}
62-
}
5+
class Prefetcher
6+
{
7+
public function __construct()
8+
{
9+
add_action('wp_head', array($this, 'prefetch_javascript'), 1);
10+
add_action('wp_head', array($this, 'prefetch_styles'), 1);
11+
}
12+
13+
public function prefetch_javascript()
14+
{
15+
$this->generate_prefetch_markup('scripts');
16+
}
17+
18+
public function prefetch_styles()
19+
{
20+
$this->generate_prefetch_markup('styles');
21+
}
22+
23+
/**
24+
* Only allow users to prefetch by specific handle,
25+
* because we don't want to prefetch all admin scripts
26+
* if that option is selected.
27+
*
28+
* @param string $type
29+
* @return void
30+
*/
31+
private function generate_prefetch_markup($type = 'scripts')
32+
{
33+
global ${'wp_' . $type};
34+
$singularType = substr_replace($type, "", -1);
35+
$handlesToPrefetch = [];
36+
37+
$noSpaces = Utilities::strip_spaces(Utilities::get_option('prefetch_'. $type. '_handles'));
38+
$specificHandlesToPrefetch = explode(',', $noSpaces ?: '');
39+
40+
//-- @todo In the future, check if user would like to preload $specificHandlesToPrefetch's dependencies as well.
41+
// foreach($specificHandlesToPrefetch as $handle) {
42+
// $specificHandlesToPrefetch = array_merge($specificHandlesToPrefetch, Utilities::collect_all_deps($handle));
43+
// }
44+
45+
//-- Loop through and print preload tags.
46+
foreach (array_unique($specificHandlesToPrefetch) as $handle) {
47+
$resource = isset(${'wp_' . $type}->registered[$handle])
48+
? ${'wp_' . $type}->registered[$handle]
49+
: false;
50+
51+
if ($resource === false) {
52+
continue;
53+
}
54+
if (empty($resource->src)) {
55+
continue;
56+
}
57+
58+
$methodToCheckIfAssetIsEnqueued = 'wp_' . $singularType . '_is';
59+
if ($methodToCheckIfAssetIsEnqueued($handle)) {
60+
continue;
61+
}
62+
63+
$source = $resource->src . ($resource->ver ? "?ver={$resource->ver}" : "");
64+
65+
if (Utilities::get_option("prefetch_assets_enable_server_push")) {
66+
Utilities::construct_server_push_headers('prefetch', $source);
67+
}
68+
69+
echo apply_filters('better_resource_hints_prefetch_tag', "<link rel='prefetch' href='{$source}'/>\n", $handle, $type);
70+
}
71+
}
6372
}

0 commit comments

Comments
 (0)