Statistics.php 13.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
<?php

/**
 * SearchWP Statistics.
 *
 * @package SearchWP
 * @author  Jon Christopher
 */

namespace SearchWP;

use SearchWP\Query;
use SearchWP\Settings;
use SearchWP\Index\Tables\LogTable;

/**
 * Class Statistics is responsible for logging searches and displaying statistics.
 *
 * @since 4.0
 */
class Statistics {

	/**
	 * Database table.
	 *
	 * @since 4.0
	 * @var   LogTable
	 */
	private $db_table;

	/**
	 * Capability requirement for viewing Statistics.
	 *
	 * @since 4.0
     * @since 4.2.6 Visibility changed from public to private.
     *
	 * @var string
	 */
	private static $capability = 'edit_others_posts';

	/**
	 * Statistics constructor.
	 *
	 * @since 4.0
	 */
	function __construct() {

		add_action( 'searchwp\query\ran', [ $this, 'log' ] );
		add_action( SEARCHWP_PREFIX . 'maintenance', [ $this, 'maintenance' ] );
	}

	/**
	 * Getter for capability tag.
	 *
	 * @since 4.2.6
	 *
	 * @return string
	 */
	public static function get_capability() {

		return (string) apply_filters( 'searchwp\statistics\capability', self::$capability );
	}

	/**
	 * Callback for WP_Cron event that executes once a day.
	 *
	 * @since 4.1
	 * @return void
	 */
	public function maintenance() {
		$trim_after = Settings::get( 'trim_stats_logs_after', 'int' );

		if ( empty( $trim_after ) ) {
			return;
		}

		$this->trim_logs( [
			'before' => $trim_after + 1, // Add a day to the interval.
		] );
	}

	/**
	 * Trims the logs table based on passed arguments.
	 * Note: This method is very immature and supports only trimming before a time.
	 *
	 * @since 4.1
	 * @param array $args Arguments
	 * @return void
	 */
	public function trim_logs( array $args ) {
		global $wpdb;

		$db_table = \SearchWP::$index->get_tables()['log'];
		$defaults = [
			'before' => '',
		];

		$args         = wp_parse_args( $args, $defaults );
		$args['site'] = [ get_current_blog_id() ];

		// Store the values for WPDB prepare call.
		$values = array_merge(
			[ absint( $args['before'] ) ],
			$args['site']
		);

		$wpdb->query( $wpdb->prepare( "
			DELETE FROM {$db_table->table_name}
			WHERE tstamp < DATE_SUB(NOW(), INTERVAL %d DAY)
			AND site IN (" . implode( ', ', array_fill( 0, count( $args['site'] ), '%d' ) ) . ")
		", $values ) );
	}

	/**
	 * Logs searches.
	 *
	 * @since 4.0
	 * @param Query $query
	 * @return false|int
	 */
	public function log( Query $query ) {
		global $wpdb;

		// If it's an ignored query we don't need to clutter the database with it.
		if ( ! apply_filters(
			'searchwp\statistics\log',
			! self::is_query_ignored( $query->get_keywords() ),
			$query
		) ) {
			return false;
		}

		$this->db_table = \SearchWP::$index->get_tables()['log'];

		return $wpdb->insert(
			$this->db_table->table_name,
			[
				'query'  => $query->get_keywords(),
				'tstamp' => current_time( 'mysql' ),
				'hits'   => $query->found_results,
				'engine' => $query->get_engine()->get_name(),
				'site'   => get_current_blog_id(),
			],
			[ '%s', '%s', '%d', '%s', '%d' ]
		);
	}

	/**
	 * Whether the submitted query is ignored.
	 *
	 * @since 4.1
	 * @var string $query The query to check.
	 * @return boolean
	 */
	public static function is_query_ignored( string $query ) {
		$ignored_queries = Settings::get( 'ignored_queries', 'array' );
		$ignored_queries = array_map( 'strtolower', $ignored_queries );

		$ignored = array_filter( $ignored_queries, function( $ignored ) use ( $query, $ignored_queries ) {
			$ignore_this = false;

			// Exact match?
			if ( $ignored === $query ) {
				$ignore_this = true;
			}

			// Partial match?
			if ( false !== strpos( $ignored, '*' ) ) {
				$ignore_this = fnmatch( $ignored, $query );
			}

			// Filtered match?
			$ignore_this = (bool) apply_filters( 'searchwp\statistics\ignored_query', $ignore_this, [
				'query'   => $query,
				'ignored' => $ignored_queries,
			] );

			return $ignore_this;
		} );

		return ! empty( $ignored );
	}

	/**
	 * Resets Statistics data.
	 *
	 * @since 4.0
	 * @return void
	 */
	public static function reset( $all_sites = false ) {
		global $wpdb;

		$db_table = \SearchWP::$index->get_tables()['log'];

		if ( $all_sites ) {
			$db_table->truncate();
		} else {
			$wpdb->query( $wpdb->prepare( "
				DELETE FROM {$db_table->table_name}
				WHERE site = %d",
				get_current_blog_id()
			) );
		}
	}

	/**
	 * Ignore a search query.
	 *
	 * @since 4.0.18
	 * @param string $query The query to ignore.
	 * @return void
	 */
	public static function ignore_query( string $query) {
		$query = strtolower( trim( $query ) );

		if ( empty( $query ) ) {
			return false;
		}

		$ignored = Settings::get( 'ignored_queries' );

		if ( ! is_array( $ignored ) ) {
			$ignored = [];
		}

		$ignored[] = $query;
		$ignored   = array_map( 'strtolower', $ignored );
		$ignored   = array_unique( $ignored );

		Settings::update( 'ignored_queries', $ignored );

		return true;
	}

	/**
	 * Unignore an ignored search query.
	 *
	 * @since 4.0.18
	 * @param string $query The query to ignore.
	 * @return void
	 */
	public static function unignore_query( string $query) {
		$query = strtolower( trim( $query ) );

		if ( empty( $query ) ) {
			return false;
		}

		$ignored = Settings::get( 'ignored_queries' );

		if ( ! is_array( $ignored ) ) {
			$ignored = [];
		}

		$ignored = array_map( 'strtolower', $ignored );
		$ignored = array_values( array_diff( $ignored, [ $query ] ) );

		Settings::update( 'ignored_queries', $ignored );

		return true;
	}

	/**
	 * Retreives all Statistics.
	 *
	 * @since 4.0
	 * @return array
	 */
	public static function get() {
		$ignored = Settings::get( 'ignored_queries', 'array' );
		$ignored = array_map( 'strtolower', $ignored );

		return [
			'ignored' => $ignored,
			'engines' => array_map( function( $engine ) use ( $ignored ) {
				$over_time = self::get_searches_over_time( [
					'days'    => 30,
					'engine'  => $engine->get_name(),
					'exclude' => $ignored,
				] );

				return [
					'engine' => $engine->get_name(),
					'label'  => $engine->get_label(),
					'data'   => [
						'labels'  => wp_list_pluck( $over_time, 'day' ),
						'counts'  => wp_list_pluck( $over_time, 'searches' ),
					],
					'details' => [ [
						'label' => __( 'No Results Searches', 'searchwp' ),
						'data'  => self::get_popular_searches( [
							'days'     => absint( apply_filters( 'searchwp\statistics\no_results\days_30', 30 ) ),
							'engine'   => $engine->get_name(),
							'min_hits' => 0,
							'max_hits' => 0,
							'exclude'  => $ignored,
						] ),
					], [
						'label' => __( 'Today', 'searchwp' ),
						'data'  => self::get_popular_searches( [
							'days'    => absint( apply_filters( 'searchwp\statistics\popular\days_1', 1 ) ),
							'engine'  => $engine->get_name(),
							'exclude' => $ignored,
						] ),
					], [
						'label' => __( 'This Month', 'searchwp' ),
						'data'  => self::get_popular_searches( [
							'days'    => absint( apply_filters( 'searchwp\statistics\popular\days_30', 30 ) ),
							'engine'  => $engine->get_name(),
							'exclude' => $ignored,
						] ),
					], [
						'label' => __( 'This Year', 'searchwp' ),
						'data'  => self::get_popular_searches( [
							'days'    => absint( apply_filters( 'searchwp\statistics\popular\days_365', 365 ) ),
							'engine'  => $engine->get_name(),
							'exclude' => $ignored,
						] ),
					], ],
				];
			}, Settings::get_engines() ),
		];
	}

	/**
	 * Displays the submitted statistics in an HTML table.
	 *
	 * @since 4.0
	 * @param array $statistics Stats as returned by @get_popular_searches.
	 * @param bool  $echo       Whether to echo.
	 * @return string|false|void
	 */
	public static function display( $statistics, $echo = true ) {
		if ( empty( $echo ) ) {
			ob_start();
		}

		if ( empty( $statistics ) ) {
			?>
			<p class="description"><?php esc_html_e( 'No searches for this time period.', 'searchwp' ); ?></p>
			<?php
		} else {
			$classes = apply_filters( 'searchwp\statistics\display\table\class', [] );
			?>
			<table cellpadding="0" cellspacing="0" class="<?php echo esc_attr( implode( ' ', (array) $classes ) ); ?>">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Query', 'searchwp' ); ?></th>
						<th><?php esc_html_e( 'Count', 'searchwp' ); ?></th>
					</tr>
				</thead>
				<tbody>
				<?php foreach ( $statistics as $stat ) : ?>
					<tr>
						<td>
							<div title="<?php echo esc_attr( $stat->query ); ?>">
								<?php echo esc_html( $stat->query ); ?>
							</div>
						</td>
						<td>
							<?php echo absint( $stat->searches ); ?>
						</td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>
			<?php
		}

		if ( empty( $echo ) ) {
			$output = ob_get_contents();
			ob_end_clean();

			return $output;
		}
	}

	/**
	 * Retrieves searches over time.
	 *
	 * @since 4.0
	 * @param array $args Arguments to consider when finding searches.
	 */
	public static function get_searches_over_time( array $args = [] ) {
		global $wpdb;

		$defaults = [
			'days'     => 1,                         // How many days (from now) to go back.
			'engine'   => 'default',                 // Engine used.
			'exclude'  => [],                        // Excluded search queries.
			'site'     => [ get_current_blog_id() ], // Site(s) to consider.
		];

		$args = wp_parse_args( $args, $defaults );

		if ( 'all' === $args['site'] ) {
			$site_in = '1=1';
			$values  = [ $args['days'], $args['engine'] ];
		} else {
			$site_in = 'site IN (' . implode( ', ', array_fill( 0, count( $args['site'] ), '%d' ) ) . ')';
			$values  = array_merge(
				[ $args['days'], $args['engine'] ],
				$args['site']
			);
		}

		$exclude = '';
		if ( is_array( $args['exclude'] ) && ! empty( $args['exclude'] ) ) {
			$exclude = " AND query NOT IN (" . implode( ', ', array_fill( 0, count( $args['exclude'] ), '%s' ) ) . ') ';
			$values  = array_merge( $values, $args['exclude'] );
		}

		$db_table = \SearchWP::$index->get_tables()['log'];

		$searches_per_day = $wpdb->get_results( $wpdb->prepare( "
			SELECT
				MONTH(tstamp) AS month,
				DAY(tstamp) AS day,
				COUNT(tstamp) AS searches
			FROM {$db_table->table_name}
				WHERE tstamp > DATE_SUB(NOW(), INTERVAL %d day)
					AND engine = %s
					AND query <> ''
					AND {$site_in}
				{$exclude}
			GROUP BY TO_DAYS(tstamp)
			ORDER BY tstamp ASC
			", $values ) );

		return array_reverse( array_map( function( $index ) use ( $searches_per_day ) {
			$timestamp = strtotime( '-'. ( $index ) .' days' );
			$month = date_i18n( 'M', $timestamp );
			$day   = date_i18n( 'd', $timestamp );

			$search_day = array_values( array_filter( $searches_per_day,
				function( $search_from_day ) use ( $timestamp ) {
					return date_i18n( 'j', $timestamp ) === $search_from_day->day
						&& date_i18n( 'n', $timestamp ) === $search_from_day->month;
				}
			) );

			return [
				'day'      => $month . ' ' . $day,
				'searches' => empty( $search_day ) ? 0 : $search_day[0]->searches,
			];
		}, range( 0, $args['days'] ) ) );
	}

	/**
	 * Retrieves popular searches based on submitted arguments.
	 *
	 * @since 4.0
	 * @param array $args Arguments to consider when finding popular searches.
	 */
	public static function get_popular_searches( array $args = [] ) {
		global $wpdb;

		$defaults = [
			'days'     => 1,                         // How many days (from now) to go back.
			'engine'   => 'default',                 // Engine used.
			'exclude'  => [],                        // Excluded search queries.
			'limit'    => 10,                        // How many searches to retrieve.
			'min_hits' => 1,                         // Minimum number of results returned for each search.
			'max_hits' => false,                     // Maximum number of results returned for each search.
			'site'     => [ get_current_blog_id() ], // Site(s) to consider.
		];

		$args = wp_parse_args( $args, $defaults );

		if ( 'all' === $args['site'] ) {
			$site_in = '1=1';
			$values  = [ $args['days'], $args['engine'] ];
		} else {
			$site_in = 'site IN (' . implode( ', ', array_fill( 0, count( $args['site'] ), '%d' ) ) . ')';
			$values  = array_merge(
				[ $args['days'], $args['engine'] ],
				$args['site']
			);
		}

		$min_hits = '';
		if ( false !== $args['min_hits'] ) {
			$min_hits = " AND hits >= %d";
			$values   = array_merge( $values, [ absint( $args['min_hits'] ) ] );
		}

		$max_hits = '';
		if ( false !== $args['max_hits'] ) {
			$max_hits = " AND hits <= %d";
			$values   = array_merge( $values, [ absint( $args['max_hits'] ) ] );
		}

		$exclude = '';
		if ( is_array( $args['exclude'] ) && ! empty( $args['exclude'] ) ) {
			// We need to separate exact and partial match ignored queries because they're processed differently.
			$exacts = array_filter( $args['exclude'], function( $ignored ) {
				return false === strpos( $ignored, '*' );
			} );

			$partials = array_filter( $args['exclude'], function( $ignored ) {
				return false !== strpos( $ignored, '*' );
			} );

			if ( ! empty( $exacts ) ) {
				$exclude .= " AND LOWER(query) NOT IN (" . implode( ', ', array_fill( 0, count( $exacts ), '%s' ) ) . ') ';
				$values  = array_merge( $values, array_map( 'strtolower', $exacts ) );
			}

			if ( ! empty( $partials ) ) {
				$exclude .= " AND (" . implode( ' AND ', array_fill( 0, count( $partials ), 'LOWER(query) NOT LIKE %s' ) ) . ') ';
				$values  = array_merge( $values, array_map( 'strtolower', array_map( function( $partial ) use ( $wpdb ) {
					return str_replace( '*', '%', $wpdb->esc_like( $partial ) );
				}, $partials ) ) );
			}
		}

		$values   = array_merge( $values, [ $args['limit'] ] );
		$db_table = \SearchWP::$index->get_tables()['log'];

		return $wpdb->get_results( $wpdb->prepare( "
			SELECT query, count(query) AS searches
			FROM {$db_table->table_name}
			WHERE tstamp > DATE_SUB(NOW(), INTERVAL %d DAY)
			AND engine = %s
			AND {$site_in}
			{$min_hits}
			{$max_hits}
			{$exclude}
			GROUP BY query
			ORDER BY searches DESC, tstamp DESC
			LIMIT %d
		", $values ) );
	}
}