|
6 | 6 | Author: Miika Arponen & Ville Siltala / Geniem Oy
|
7 | 7 | Author URI: http://www.geniem.com
|
8 | 8 | License: GPLv3
|
9 |
| -Version: 1.16.0 |
| 9 | +Version: 1.16.1 |
10 | 10 | */
|
11 | 11 |
|
12 | 12 | final class DustPress {
|
@@ -105,31 +105,38 @@ protected function __construct() {
|
105 | 105 | }
|
106 | 106 | }
|
107 | 107 | 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 | + } |
119 | 125 |
|
120 |
| - return $request; |
| 126 | + return $request; |
121 | 127 |
|
122 |
| - }, PHP_INT_MAX, 2 ); |
| 128 | + }, PHP_INT_MAX, 2 ); |
123 | 129 |
|
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 | + } |
130 | 136 |
|
131 |
| - return $posts; |
132 |
| - }, 10, 2 ); |
| 137 | + return $posts; |
| 138 | + }, 10, 2 ); |
| 139 | + } |
133 | 140 |
|
134 | 141 | add_filter( 'template_include', [ $this, 'create_ajax_instance' ] );
|
135 | 142 | }
|
@@ -1057,22 +1064,7 @@ public function create_ajax_instance() {
|
1057 | 1064 |
|
1058 | 1065 | $model = false;
|
1059 | 1066 |
|
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; |
1076 | 1068 |
|
1077 | 1069 | if ( ! empty( $request_data->token ) && ! empty( $_COOKIE[ 'dpjs_token' ] ) ) {
|
1078 | 1070 | $token = ( $request_data->token === $_COOKIE[ 'dpjs_token' ] );
|
@@ -1617,6 +1609,34 @@ public function register_custom_route( $route, $template ) {
|
1617 | 1609 | add_rewrite_rule( '(' . $route . ')(\/(.+))?\/?$', 'index.php?dustpress_custom_route=' . $template . '&dustpress_custom_route_route=$matches[1]&dustpress_custom_route_parameters=$matches[3]', 'top' );
|
1618 | 1610 | }, 30);
|
1619 | 1611 | }
|
| 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 | + } |
1620 | 1640 | }
|
1621 | 1641 |
|
1622 | 1642 | // Global function that returns the DustPress singleton
|
|
0 commit comments