Completed
Push — try/track-potential-search-per... ( 7ba7ed )
by
unknown
10:40 queued 01:48
created

begin_log_query()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Search_Performance_Logger {
4
	/**
5
	 * @var Jetpack_Search_Performance_Logger
6
	 **/
7
	private static $instance = null;
8
9
	private $current_query = null;
10
	private $query_started = null;
11
	private $was_jetpack_search = false;
12
	private $stats = null;
13
14
	static function init() {
15
		if ( is_null( self::$instance ) ) {
16
			self::$instance = new Jetpack_Search_Performance_Logger;
17
		}
18
19
		return self::$instance;
20
	}
21
22
	private function __construct() {
23
		$this->stats = array();
24
		// add_filter( 'jetpack_search_should_handle_query', '__return_false' );
25
		add_action( 'pre_get_posts', array( $this, 'begin_log_query' ), 10, 1 );
26
		add_action( 'did_jetpack_search_query', array( $this, 'mark_as_jetpack_search_query' ) );
27
		add_filter( 'found_posts', array( $this, 'end_log_query' ), 10, 2 );
28
		add_action( 'wp_footer', array( $this, 'print_stats' ) );
29
	}
30
31
	public function begin_log_query( $query  ) {
32
		if ( $this->should_log_query( $query ) ) {
33
			$this->query_started = microtime( true );
34
			$this->current_query = $query;
35
		}
36
	}
37
38
	public function end_log_query( $found_posts, $query ) {
39
		if ( $this->current_query === $query ) {
40
			$duration = microtime( true ) - $this->query_started;
41
			$this->record_query_time( $duration );
42
			$this->reset_query_state();
43
		}
44
45
		return $found_posts;
46
	}
47
48
	public function mark_as_jetpack_search_query() {
49
		$this->was_jetpack_search = true;
50
		$duration = microtime( true ) - $this->query_started;
51
		$this->record_query_time( $duration );
52
		$this->reset_query_state();
53
	}
54
55
	private function reset_query_state() {
56
		$this->query_started = null;
57
		$this->current_query = null;
58
		$this->was_jetpack_search = false;
59
	}
60
61
	private function should_log_query( $query ) {
62
		return $query->is_main_query() && $query->is_search();
63
	}
64
65
	private function record_query_time( $duration ) {
66
		$this->stats[] = array( $this->was_jetpack_search, $duration * 1000 );
67
	}
68
69
	public function print_stats() {
70
		$beacons = array();
71
		if ( ! empty( $this->stats ) ) {
72
			foreach( $this->stats as $stat ) {
73
				$search_type = $stat[0] ? 'es' : 'mysql';
74
				$beacons[] = "jetpack.".JETPACK__VERSION.".search.{$search_type}.duration:{$stat[1]}|ms";
75
			}
76
		}
77
		$encoded_json = urlencode( json_encode( array( 'beacons' => $beacons ) ) );
78
		$encoded_site_url = urlencode( site_url() );
79
		$url = "https://pixel.wp.com/boom.gif?v=0.9&u={$encoded_site_url}&json={$encoded_json}";
80
		echo '<img src="' . esc_url( $url ) . '" width="1" height="1" style="display:none;" />';
81
	}
82
}