Skip to content

Commit 6851477

Browse files
author
Miika Arponen
committed
A new DustPress.js parameter to bypass main WP_Query if it's not needed.
1 parent 08e1515 commit 6851477

File tree

2 files changed

+63
-38
lines changed

2 files changed

+63
-38
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [1.16.1] - 2018-06-20
8+
9+
### Added
10+
- Added a parameter to enable last update's changes as otherwise they would break backwards-compability.
11+
712
## [1.16.0] - 2018-06-14
813

914
### Added

dustpress.php

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Author: Miika Arponen & Ville Siltala / Geniem Oy
77
Author URI: http://www.geniem.com
88
License: GPLv3
9-
Version: 1.16.0
9+
Version: 1.16.1
1010
*/
1111

1212
final class DustPress {
@@ -105,31 +105,38 @@ protected function __construct() {
105105
}
106106
}
107107
else if ( $this->is_dustpress_ajax() ) {
108-
// DustPress.js is never 404.
109-
add_filter( 'status_header', function( $status, $header ) {
110-
return 'status: 200';
111-
}, 10, 2 );
112-
113-
// Make the main query to be as fast as possible within DustPress.js requests.
114-
add_filter( 'posts_request', function( $request, \WP_Query $query ) {
115-
// Target main home query
116-
if ( $query->is_main_query() ) {
117-
$request = str_replace( '1=1', '0=1', $request );
118-
}
108+
$this->parse_request_data();
109+
110+
// Optimize the run if we don't want to run the main WP_Query at all.
111+
if ( ( is_object( $this->request_data ) && ! empty( $this->request_data->bypassMainQuery ) ) ||
112+
( is_array( $this->request_data ) && ! empty( $this->request_data['bypassMainQuery'] ) ) ) {
113+
114+
// DustPress.js request is never 404.
115+
add_filter( 'status_header', function( $status, $header ) {
116+
return 'status: 200';
117+
}, 10, 2 );
118+
119+
// Make the main query to be as fast as possible within DustPress.js requests.
120+
add_filter( 'posts_request', function( $request, \WP_Query $query ) {
121+
// Target main home query
122+
if ( $query->is_main_query() ) {
123+
$request = str_replace( '1=1', '0=1', $request );
124+
}
119125

120-
return $request;
126+
return $request;
121127

122-
}, PHP_INT_MAX, 2 );
128+
}, PHP_INT_MAX, 2 );
123129

124-
// Populate the main query posts variable with a dummy post to prevent errors and notices.
125-
add_filter( 'posts_pre_query', function( $posts, \WP_Query $query ) {
126-
if( $query->is_main_query() ){
127-
$posts = [ new WP_Post( (object) [] ) ];
128-
$query->found_posts = 1;
129-
}
130+
// Populate the main query posts variable with a dummy post to prevent errors and notices.
131+
add_filter( 'posts_pre_query', function( $posts, \WP_Query $query ) {
132+
if( $query->is_main_query() ){
133+
$posts = [ new WP_Post( (object) [] ) ];
134+
$query->found_posts = 1;
135+
}
130136

131-
return $posts;
132-
}, 10, 2 );
137+
return $posts;
138+
}, 10, 2 );
139+
}
133140

134141
add_filter( 'template_include', [ $this, 'create_ajax_instance' ] );
135142
}
@@ -1057,22 +1064,7 @@ public function create_ajax_instance() {
10571064

10581065
$model = false;
10591066

1060-
$request_body = file_get_contents( 'php://input' );
1061-
$json = json_decode( $request_body );
1062-
1063-
if ( isset( $json->dustpress_data ) ) {
1064-
$request_data = $json->dustpress_data;
1065-
}
1066-
elseif ( isset( $_POST['dustpress_data'] ) ) {
1067-
$request_data = (object) $_POST['dustpress_data'];
1068-
1069-
// Parse old data to correct format and assume it to be false if it isn't defined
1070-
$request_data->tidy = ( isset( $request_data->tidy ) && $request_data->tidy === 'true' ) ? true : false;
1071-
$request_data->render = ( isset( $request_data->render ) && $request_data->render === 'true' ) ? true : false;
1072-
}
1073-
else {
1074-
die( json_encode( [ 'error' => 'Something went wrong. There was no dustpress_data present at the request.' ] ) );
1075-
}
1067+
$request_data = $this->request_data;
10761068

10771069
if ( ! empty( $request_data->token ) && ! empty( $_COOKIE[ 'dpjs_token' ] ) ) {
10781070
$token = ( $request_data->token === $_COOKIE[ 'dpjs_token' ] );
@@ -1617,6 +1609,34 @@ public function register_custom_route( $route, $template ) {
16171609
add_rewrite_rule( '(' . $route . ')(\/(.+))?\/?$', 'index.php?dustpress_custom_route=' . $template . '&dustpress_custom_route_route=$matches[1]&dustpress_custom_route_parameters=$matches[3]', 'top' );
16181610
}, 30);
16191611
}
1612+
1613+
/**
1614+
* Parse DustPress.js request data
1615+
*
1616+
* @type function
1617+
* @date 20/06/2018
1618+
* @since 1.16.1
1619+
*
1620+
* @return void
1621+
*/
1622+
private function parse_request_data() {
1623+
$request_body = file_get_contents( 'php://input' );
1624+
$json = json_decode( $request_body );
1625+
1626+
if ( isset( $json->dustpress_data ) ) {
1627+
$this->request_data = $json->dustpress_data;
1628+
}
1629+
elseif ( isset( $_POST['dustpress_data'] ) ) {
1630+
$this->request_data = (object) $_POST['dustpress_data'];
1631+
1632+
// Parse old data to correct format and assume it to be false if it isn't defined
1633+
$this->request_data->tidy = ( isset( $this->request_data->tidy ) && $this->request_data->tidy === 'true' ) ? true : false;
1634+
$this->request_data->render = ( isset( $this->request_data->render ) && $this->request_data->render === 'true' ) ? true : false;
1635+
}
1636+
else {
1637+
die( json_encode( [ 'error' => 'Something went wrong. There was no dustpress_data present at the request.' ] ) );
1638+
}
1639+
}
16201640
}
16211641

16221642
// Global function that returns the DustPress singleton

0 commit comments

Comments
 (0)