Skip to content
This repository has been archived by the owner on Feb 23, 2019. It is now read-only.

Commit

Permalink
Google PageSpeed Widget - Added Referrer Key Restriction Field #318
Browse files Browse the repository at this point in the history
  • Loading branch information
Furniel authored and szepeviktor committed May 5, 2017
1 parent 2c0fe24 commit 413b368
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
4 changes: 4 additions & 0 deletions ConfigKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,10 @@
'type' => 'string',
'default' => ''
),
'widget.pagespeed.key.restrict.referrer' => array(
'type' => 'string',
'default' => ''
),
'widget.pagespeed.show_in_admin_bar' => array(
'type' => 'boolean',
'default' => false
Expand Down
4 changes: 3 additions & 1 deletion Generic_AdminActions_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ function w3tc_test_pagespeed_results() {

$config = Dispatcher::config();
$key = $config->get_string( 'widget.pagespeed.key' );
$w3_pagespeed = new PageSpeed_Api( $key );
$ref = $config->get_string( 'widget.pagespeed.key.restrict.referrer' );

$w3_pagespeed = new PageSpeed_Api( $key, $ref );

$results = $w3_pagespeed->analyze( get_home_url() );
include W3TC_INC_POPUP_DIR . '/pagespeed_results.php';
Expand Down
1 change: 1 addition & 0 deletions Generic_ConfigLabels.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public function config_labels( $config_labels ) {
'cluster.messagebus.sns.topic_arn' => __( 'Topic <acronym title="Identification">ID</acronym>:', 'w3-total-cache' ),
'cluster.messagebus.debug' => __( 'Message Bus', 'w3-total-cache' ),
'widget.pagespeed.key' => __( 'Page Speed <acronym title="Application Programming Interface">API</acronym> Key:', 'w3-total-cache' ),
'widget.pagespeed.key.restrict.referrer' => __( 'Key Restriction (Referrer):', 'w3-total-cache' ),
'common.force_master' => __( 'Use single network configuration file for all sites.', 'w3-total-cache' ),
'config.path' => __( 'Nginx server configuration file path', 'w3-total-cache' ),
'config.check' => __( 'Verify rewrite rules', 'w3-total-cache' ),
Expand Down
14 changes: 12 additions & 2 deletions PageSpeed_Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ class PageSpeed_Api {
*/
var $key = '';

/**
* Referrer for key restricting
*
* @var string
*/
var $key_restrict_referrer = '';

/**
* PHP5-style constructor
*/
function __construct( $api_key ) {
function __construct( $api_key, $api_ref ) {
$this->key = $api_key;
$this->key_restrict_referrer = $api_ref;
}

/**
Expand Down Expand Up @@ -51,7 +59,9 @@ function _request( $url ) {
'key' => $this->key,
) );

$response = Util_Http::get( $request_url, array( 'timeout' => 120 ) );
$response = Util_Http::get( $request_url, array( 'timeout' => 120,
'headers' => array( "Referer" => $this->key_restrict_referrer )
) );
if ( !is_wp_error( $response ) && $response['response']['code'] == 200 ) {
return $response['body'];
}
Expand Down
8 changes: 5 additions & 3 deletions PageSpeed_Plugin_Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ public function w3tc_ajax_pagespeed_widgetdata() {

$config = Dispatcher::config();
$key = $config->get_string( 'widget.pagespeed.key' );

$w3_pagespeed = new PageSpeed_Api( $key );
$ref = $config->get_string( 'widget.pagespeed.key.restrict.referrer' );

$w3_pagespeed = new PageSpeed_Api( $key, $ref );
$r = $w3_pagespeed->analyze( get_home_url() );

if ( !$r ) {
Expand Down Expand Up @@ -132,7 +133,8 @@ public function w3tc_monitoring_score( $score ) {

$config = Dispatcher::config();
$key = $config->get_string( 'widget.pagespeed.key' );
$w3_pagespeed = new PageSpeed_Api( $key );
$ref = $config->get_string( 'widget.pagespeed.key.restrict.referrer' );
$w3_pagespeed = new PageSpeed_Api( $key, $ref );

$r = $w3_pagespeed->analyze( $url );

Expand Down
7 changes: 7 additions & 0 deletions inc/options/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,13 @@ class="w3tc-ignore-change" type="text"
Then go to the <acronym title="Application Programming Interface">API</acronym> Access tab. The <acronym title="Application Programming Interface">API</acronym> key is in the Simple <acronym title="Application Programming Interface">API</acronym> Access section.', 'w3-total-cache' ); ?></span>
</td>
</tr>
<tr>
<th><label for="widget_pagespeed_key"><?php Util_Ui::e_config_label( 'widget.pagespeed.key.restrict.referrer', 'general' ) ?></label></th>
<td>
<input id="widget_pagespeed_key_restrict_referrer" type="text" name="widget__pagespeed__key__restrict__referrer" value="<?php echo esc_attr( $this->_config->get_string( 'widget.pagespeed.key.restrict.referrer' ) ); ?>" size="60" /><br>
<span class="description">Although not required, to prevent unauthorized use and quota theft, you have the option to restrict your key using a designated HTTP referrer. If you decide to use it, you will need to set this referrer within the API Console's "Http Referrers (web sites)" key restriction area (under Credentials).</span>
</td>
</tr>
<?php
Util_Ui::config_item( array(
'key' => 'widget.pagespeed.show_in_admin_bar',
Expand Down

0 comments on commit 413b368

Please sign in to comment.