/home/techb158/primomovers.ca/wp-content/plugins/wp-rocket/inc/classes/Buffer
Edit: /home/techb158/primomovers.ca/wp-content/plugins/wp-rocket/inc/classes/Buffer/class-tests.php (35051B)
1,
'ssl' => 1,
'uri' => 1,
'rejected_cookie' => 1,
'mandatory_cookie' => 1,
'user_agent' => 1,
'mobile' => 1,
'donotcachepage' => 1,
'wp_404' => 1,
'search' => 1,
'is_html' => 1,
];
/**
* Information about the last "error".
* Here an "error" is a test failure.
*
* @var array {
* @type string $message A message.
* @type array $data Related data.
* }
* @since 3.3
* @author Grégory Viguier
*/
private $last_error = [];
/**
* Constructor.
*
* @since 3.3
* @author Grégory Viguier
*
* @param Config $config Config instance.
* @param array $args {
* An array of arguments.
*
* @type array $cookies Values of $_COOKIE to use for the tests. Default is $_COOKIE.
* @type array $post Values of $_POST to use for the tests. Default is $_POST
* @type array $get Values of $_GET to use for the tests. Default is $_GET.
* @type array $tests List of complementary tests to perform. Optional.
* }
*/
public function __construct( Config $config, array $args = [] ) {
$this->config = $config;
if ( ! empty( $args['tests'] ) ) {
$this->set_tests( (array) $args['tests'] );
}
// Provide fallback values.
if ( ! isset( $args['cookies'] ) && ! empty( $_COOKIE ) && is_array( $_COOKIE ) ) {
$args['cookies'] = $_COOKIE;
}
if ( ! isset( $args['post'] ) && ! empty( $_POST ) && is_array( $_POST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$args['post'] = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing
}
if ( ! isset( $args['get'] ) && ! empty( $_GET ) && is_array( $_GET ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
$args['get'] = $_GET; // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
}
self::$cookies = ! empty( $args['cookies'] ) && is_array( $args['cookies'] ) ? $args['cookies'] : [];
self::$post = ! empty( $args['post'] ) && is_array( $args['post'] ) ? $args['post'] : [];
self::$get = ! empty( $args['get'] ) && is_array( $args['get'] ) ? $args['get'] : [];
if ( self::$post ) {
self::$post = array_intersect_key(
// Limit self::$post to the values we need, to save a bit of memory.
self::$post,
[
'wp_customize' => '',
]
);
}
}
/** ----------------------------------------------------------------------------------------- */
/** TESTS =================================================================================== */
/** ----------------------------------------------------------------------------------------- */
/**
* Tell if the process should be initiated.
*
* @since 3.3
* @author Grégory Viguier
*
* @return bool
*/
public function can_init_process() {
$this->last_error = [];
// Don't process robots.txt && .htaccess files (it has happened sometimes with weird server configuration).
if ( $this->is_rejected_file() ) {
$this->set_error( 'Robots.txt or .htaccess file is excluded.' );
return false;
}
// Don't process disallowed file extensions (like php, xml, xsl).
if ( $this->is_rejected_extension() ) {
$this->set_error( 'PHP, XML, or XSL file is excluded.' );
return false;
}
// Don't cache if in admin or ajax.
if ( $this->is_admin() ) {
$this->set_error( 'Admin or AJAX URL is excluded.' );
return false;
}
// Don't process the customizer preview.
if ( $this->is_customizer_preview() ) {
$this->set_error( 'Customizer preview is excluded.' );
return false;
}
// Don't process without GET method.
if ( ! $this->is_allowed_request_method() ) {
$this->set_error(
'Request method is not allowed. Page cannot be cached.',
[
'request_method' => $this->get_request_method(),
]
);
return false;
}
if ( ! $this->has_test() ) {
$this->last_error = [];
return true;
}
$config = $this->config->get_config_file_path();
// Exit if no config file exists.
if ( ! $config['success'] ) {
$this->set_error(
'No config file found.',
[
'config_path' => $config['path'],
]
);
return false;
}
// Don’t process with query strings parameters, but the processed content is served if the visitor comes from an RSS feed, a Facebook action or Google Adsense tracking.
if ( $this->has_test( 'query_string' ) && ! $this->can_process_query_string() ) {
$this->set_error( 'Query string URL is excluded.' . PHP_EOL . print_r( $_GET, true ) );// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r, WordPress.Security.NonceVerification.Recommended
return false;
}
// Don't process SSL.
if ( $this->has_test( 'ssl' ) && ! $this->can_process_ssl() ) {
$this->set_error( 'SSL cache not applied to page.' );
return false;
}
// Don't process these pages.
if ( $this->has_test( 'uri' ) && ! $this->can_process_uri() ) {
$this->set_error( 'Page is excluded.' );
return false;
}
// Don't process page with these cookies.
if ( $this->has_test( 'rejected_cookie' ) && $this->has_rejected_cookie() ) {
$this->set_error(
'Excluded cookie found.',
[
'excluded_cookies' => $this->has_rejected_cookie(),
]
);
return false;
}
// Don't process page when these cookies don't exist.
if ( $this->has_test( 'mandatory_cookie' ) && ! $this->is_speed_tool() && is_array( $this->has_mandatory_cookie() ) ) {
$this->set_error(
'Missing mandatory cookie: page not processed.',
[
'missing_cookies' => $this->has_mandatory_cookie(),
]
);
return false;
}
// Don't process page with these user agents.
if ( $this->has_test( 'user_agent' ) && ! $this->can_process_user_agent() ) {
$this->set_error(
'User Agent is excluded.',
[
'user_agent' => $this->config->get_server_input( 'HTTP_USER_AGENT' ),
]
);
return false;
}
// Don't process if mobile detection is activated.
if ( $this->has_test( 'mobile' ) && ! $this->can_process_mobile() ) {
$this->set_error(
'Mobile User Agent is excluded.',
[
'user_agent' => $this->config->get_server_input( 'HTTP_USER_AGENT' ),
]
);
return false;
}
$this->last_error = [];
return true;
}
/**
* Tell if a test should be performed.
*
* @since 3.3
* @author Grégory Viguier
*
* @param string $test_name Identifier of the test.
* Possible values are: 'query_string', 'ssl', 'uri', 'rejected_cookie', 'mandatory_cookie', 'user_agent', 'mobile'.
* @return bool
*/
public function has_test( $test_name = '' ) {
if ( empty( $test_name ) ) {
return ! empty( $this->tests );
}
return isset( $this->tests[ $test_name ] );
}
/**
* Set the list of tests to perform.
*
* @since 3.3
* @author Grégory Viguier
*
* @param array $tests An array of test names.
*/
public function set_tests( array $tests ) {
$tests = array_flip( $tests );
array_merge( $this->tests, $tests );
}
/**
* Tell if the buffer should be processed.
*
* @since 3.3
* @author Grégory Viguier
*
* @param string $buffer The buffer content.
* @return bool
*/
public function can_process_buffer( $buffer ) {
$this->last_error = [];
if ( ! function_exists( 'rocket_mkdir_p' ) ) {
// Uh?
$this->set_error( 'WP Rocket not found - page cannot be cached.' );
return false;
}
if ( strlen( $buffer ) <= 255 ) {
// Buffer length must be > 255 (IE does not read pages under 255 c).
$this->set_error( 'Buffer content under 255 caracters.' );
return false;
}
if ( $this->get_http_response_code() !== 200 ) {
// Only cache 200.
$this->set_error( 'Page is not a 200 HTTP response and cannot be cached.' );
return false;
}
if ( $this->has_test( 'donotcachepage' ) && $this->has_donotcachepage() ) {
// Don't process templates that use the DONOTCACHEPAGE constant.
$this->set_error( 'DONOTCACHEPAGE is defined. Page cannot be cached.' );
return false;
}
if ( $this->has_test( 'wp_404' ) && $this->is_404() ) {
// Don't process WP 404 page.
$this->set_error( 'WP 404 page is excluded.' );
return false;
}
if ( $this->has_test( 'search' ) && $this->is_search() ) {
// Don't process search results.
$this->set_error( 'Search page is excluded.' );
return false;
}
if ( $this->has_test( 'is_html' ) ) {
if ( $this->is_feed_uri() || defined( 'REST_REQUEST' ) ) {
unset( $this->tests['is_html'] );
}
}
if (
$this->has_test( 'is_html' )
&&
! $this->is_html( $buffer )
) {
// Don't process if there isn't a closing