Skip to content

Commit

Permalink
Merge pull request #40 from samperrow/1.7.3
Browse files Browse the repository at this point in the history
1.7.3
  • Loading branch information
samperrow authored Aug 3, 2020
2 parents c5e277a + 8ed2834 commit ae5ba49
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
35 changes: 13 additions & 22 deletions includes/class-pprh-send-hints.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ class Send_Hints {

protected $hints = array();

protected $hint_location = '';
protected $send_hints_in_html = '';

public function __construct() {
add_action( 'wp_loaded', array( $this, 'get_resource_hints' ) );
}

public function get_resource_hints() {
$this->hint_location = get_option('pprh_html_head');
$this->send_hints_in_html = get_option('pprh_html_head');

$this->hints = $this->get_hints();

if ( ( ! is_array( $this->hints ) ) || count( $this->hints ) < 1 ) {
return;
}

( 'false' === $this->hint_location && ! headers_sent() )
( 'false' === $this->send_hints_in_html && ! headers_sent() )
? add_action('send_headers', array($this, 'send_in_http_header'), 1, 0)
: add_action('wp_head', array($this, 'send_to_html_head'), 1, 0);
}
Expand All @@ -42,9 +42,9 @@ public function get_hints() {
public function send_to_html_head() {
foreach ( $this->hints as $key => $val ) {
$attrs = '';
$attrs .= $this->add_html_attr( 'as', $val->as_attr );
$attrs .= $this->add_html_attr( 'type', $val->type_attr );
$attrs .= $this->add_html_attr( 'crossorigin', trim( $val->crossorigin ) );
$attrs .= $this->add_attr( 'as', $val->as_attr );
$attrs .= $this->add_attr( 'type', $val->type_attr );
$attrs .= $this->add_attr( 'crossorigin', trim( $val->crossorigin ) );
echo sprintf( '<link href="%s" rel="%s"%s>', $val->url, $val->hint_type, $attrs );
}
}
Expand All @@ -54,9 +54,9 @@ public function send_in_http_header() {

foreach ( $this->hints as $key => $val ) {
$attrs = '';
$attrs .= $this->add_header_attr( 'as', $val->as_attr );
$attrs .= $this->add_header_attr( 'type', $val->type_attr );
$attrs .= $this->add_header_attr( 'crossorigin', trim( $val->crossorigin ) );
$attrs .= $this->add_attr( 'as', $val->as_attr );
$attrs .= $this->add_attr( 'type', $val->type_attr );
$attrs .= $this->add_attr( 'crossorigin', trim( $val->crossorigin ) );
$str = sprintf( '<%s>; rel=%s;%s', $val->url, $val->hint_type, $attrs );
$str = rtrim( $str, ';' );
$output .= $str . ', ';
Expand All @@ -66,22 +66,13 @@ public function send_in_http_header() {
header( $header_str );
}

private function add_header_attr( $name, $val ) {
private function add_attr( $name, $val ) {
if ( ! empty( $val ) ) {
$attr = Utils::clean_hint_attr( $val );
return " $name=$attr;";
} else {
return '';
}
}

private function add_html_attr( $name, $val ) {
if ( ! empty( $val ) ) {
$attr = Utils::clean_hint_attr( $val );
return " $name=\"$attr\"";
} else {
return '';
$attr = ( 'true' === $this->send_hints_in_html ) ? "\"$attr\"" : "$attr;";
return ' ' . ( ( 'crossorigin' === $name ) ? $name : "$name=" . $attr );
}
return '';
}

}
Expand Down
5 changes: 2 additions & 3 deletions js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jQuery(document).ready(function($) {
return {
url: hint_url,
hint_type: hintType,
crossorigin: elems.crossorigin.attr('checked') ? 'crossorigin' : '',
crossorigin: elems.crossorigin.is(':checked') ? 'crossorigin' : '',
as_attr: elems.as_attr.val(),
type_attr: elems.type_attr.val(),
action: op,
Expand Down Expand Up @@ -144,7 +144,6 @@ jQuery(document).ready(function($) {
}

function createAjaxReq(dataObj) {
var host = document.location.origin;
var xhr = new XMLHttpRequest();
var url = pprh_nonce.admin_url + 'admin-ajax.php';
xhr.open('POST', url, true);
Expand Down Expand Up @@ -260,7 +259,7 @@ jQuery(document).ready(function($) {
var checkboxes = $('table.pprh-post-table tbody th.check-column input:checkbox');

$.each(checkboxes, function() {
if ($(this).attr('checked')) {
if ($(this).is(':checked')) {
return idArr.push($(this).val());
}
});
Expand Down
15 changes: 11 additions & 4 deletions pre-party-browser-hints.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Pre* Party Resource Hints
* Plugin URI: https://wordpress.org/plugins/pre-party-browser-hints/
* Description: Take advantage of the browser resource hints DNS-Prefetch, Prerender, Preconnect, Prefetch, and Preload to improve page load time.
* Version: 1.7.2.2
* Version: 1.7.3
* Requires at least: 4.4
* Requires PHP: 5.6.30
* Author: Sam Perrow
Expand All @@ -13,7 +13,7 @@
* Text Domain: pprh
* Domain Path: /languages
*
* last edited July 7, 2020
* last edited August 2, 2020
*
* Copyright 2016 Sam Perrow (email : sam.perrow399@gmail.com)
*
Expand Down Expand Up @@ -48,6 +48,7 @@ public function initialize() {
include_once PPRH_ABS_DIR . '/includes/class-pprh-ajax-ops.php';
add_action( 'admin_menu', array( $this, 'load_admin_page' ) );
} else {
$this->pprh_disable_wp_hints();
include_once PPRH_ABS_DIR . '/includes/class-pprh-send-hints.php';
}

Expand Down Expand Up @@ -81,7 +82,7 @@ public function create_constants() {
$abs_dir = untrailingslashit( __DIR__ );
$rel_dir = plugins_url() . '/pre-party-browser-hints/';

define( 'PPRH_VERSION', '2.0.2' );
define( 'PPRH_VERSION', '1.7.3' );
define( 'PPRH_DB_TABLE', $table );
define( 'PPRH_ABS_DIR', $abs_dir );
define( 'PPRH_REL_DIR', $rel_dir );
Expand All @@ -95,7 +96,7 @@ public function register_admin_files( $hook ) {
'admin_url' => admin_url()
);

wp_register_script( 'pprh_admin_js', plugin_dir_url( __FILE__ ) . 'js/admin.js', null, PPRH_VERSION, true );
wp_register_script( 'pprh_admin_js', plugin_dir_url( __FILE__ ) . 'js/admin.js', array( 'jquery' ), PPRH_VERSION, true );
wp_localize_script( 'pprh_admin_js', 'pprh_nonce', $ajax_data );
wp_register_style( 'pprh_styles_css', plugin_dir_url( __FILE__ ) . '/css/styles.css', null, PPRH_VERSION, 'all' );

Expand Down Expand Up @@ -189,4 +190,10 @@ private function create_table( $table_name ) {
dbDelta( $sql, true );
}

public function pprh_disable_wp_hints() {
if ( 'true' === get_option( 'pprh_disable_wp_hints' ) ) {
return remove_action( 'wp_head', 'wp_resource_hints', 2 );
}
}

}
13 changes: 9 additions & 4 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Contributors: samperrow
Donate link: https://www.paypal.me/samperrow
Tags: W3C, DNS prefetch, prerender, preconnect, prefetch, preload, web perf, performance, speed, resource hints
Requires at least: 4.4
Tested up to: 5.4.2
Stable tag: 1.7.2.2
Tested up to: 5.5
Stable tag: 1.7.3
Requires PHP: 5.6.30
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -80,8 +80,13 @@ To activate the preloaded resource, you must call that file in HTML as you would

== Changelog ==

1. Most recent update: July 7, 2020.
2. Version 1.7.2.2
1. Most recent update: August 2, 2020.
2. Version 1.7.3

August 2, 2020:
1) tested WP 5.5 compatbility
2) corrected bug with updating hints
3) simplified the sending of and creation of frontend hints

July 7, 2020:
1) Replaced hardcoded "wp-admin" URL's with dynamic ones.
Expand Down

0 comments on commit ae5ba49

Please sign in to comment.