<?php
/**
 * Plugin Name: Query Monitor Extend
 * Plugin URI: https://github.com/crstauf/query-monitor-extend
 * Description: Additional panels for Query Monitor by John Blackbourn.
 * Version: 1.6.3
 * Author: Caleb Stauffer
 * Author URI: https://develop.calebstauffer.com
 * Update URI: false
 * Requires Plugins: query-monitor
 *
 * QM tested up to: 3.16.4
 */

defined( 'WPINC' ) || die();

if ( ! function_exists( 'get_plugin_data' ) ) {
	require_once ABSPATH . 'wp-admin/includes/plugin.php';
}

defined( 'QMX_DISABLED' ) || define( 'QMX_DISABLED', false );
defined( 'QMX_TESTED_WITH_QM' ) || define( 'QMX_TESTED_WITH_QM', get_plugin_data( __FILE__ )['QM tested up to'] ?? '3.0.0' );

if ( defined( 'QM_DISABLED' ) && constant( 'QM_DISABLED' ) ) {
	return;
}

if ( constant( 'QMX_DISABLED' ) ) {
	return;
}

// Prevent combo mu-plugin and plugin.
if ( defined( 'QMX_LOADED' ) ) {
	trigger_error( sprintf( 'Query Monitor Extend loaded previously: %s', constant( 'QMX_LOADED' ) ), E_USER_NOTICE );
	return;
}

define( 'QMX_LOADED', __FILE__ );

/**
 * Filter: plugin_row_meta
 *
 * - add "Tested up to" notice
 * - add "Requires QM" notice
 *
 * @param array $meta
 * @param string $file
 * @return string[]
 */
add_filter( 'plugin_row_meta', static function ( array $meta, string $file ) : array {
	if ( 'query-monitor-extend/query-monitor-extend.php' !== $file ) {
		return $meta;
	}

	$first = array_shift( $meta );

	array_unshift(
		$meta,
		$first,
		sprintf(
			'Tested up to <a href="%1$s" rel="noopener noreferrer">Query Monitor</a> <a href="%2$s%3$s" rel="noopener noreferrer">%3$s</a>',
			'https://wordpress.org/plugins/query-monitor/',
			'https://github.com/johnbillion/query-monitor/releases/tag/',
			constant( 'QMX_TESTED_WITH_QM' )
		)
	);

	if ( class_exists( 'QueryMonitor' ) ) {
		return $meta;
	}

	$first = array_shift( $meta );

	array_unshift(
		$meta,
		$first,
		sprintf(
			'Requires <a href="%1$s" rel="noopener noreferrer">Query Monitor</a>',
			'https://wordpress.org/plugins/query-monitor/'
		)
	);

	return $meta;
}, 10, 2 );

if ( ! class_exists( 'QueryMonitor' ) || did_action( 'qm/cease' ) ) {
	return;
}

$collector_names = apply_filters( 'qmx/collectors', array(
	'acf',
	'constants',
	'files',
	'globals',
	'heartbeat',
	'image-sizes',
	'paths',
	'time',
) );

$dir = trailingslashit( __DIR__ );

/**
 * If loading as an mu-plugin, add "query-monitor-extend"
 * to directory path.
 */
if ( trailingslashit( constant( 'WPMU_PLUGIN_DIR' ) ) === $dir ) {
	$dir .= 'query-monitor-extend/';
}

# Include all collector and outputters.
foreach ( $collector_names as $collector_name ) {
	$files = array(
		sprintf( '%1$s%2$s/qmx-%2$s-collector.php', $dir, $collector_name ),
		sprintf( '%1$s%2$s/qmx-%2$s-output.php', $dir, $collector_name ),
	);

	$files = array_filter( $files, 'file_exists' );

	if ( count( $files ) !== 2 ) {
		continue;
	}

	foreach ( $files as $file ) {
		require_once $file;
	}
}

# Include hook timing feature.
include_once $dir . 'qmx-time-hooks.php';

# Include additional conditionals.
include_once $dir . 'qmx-conditionals.php';
