From 4bc165d3838204cae977ecb32d633f771a6ab8a3 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Sun, 18 Aug 2024 09:52:33 -0500 Subject: [PATCH] Add more integration for Query Monitor and display Pods constants and the Pods debug log --- classes/PodsInit.php | 21 --- src/Pods/Integration.php | 29 ++- src/Pods/Integrations/Query_Monitor.php | 98 ++++++++++ .../Query_Monitor/Collectors/Constants.php | 118 ++++++++++++ .../Query_Monitor/Collectors/Debug.php | 73 ++++++++ .../Query_Monitor/Outputters/Constants.php | 79 ++++++++ .../Query_Monitor/Outputters/Debug.php | 174 ++++++++++++++++++ src/Pods/Integrations/Service_Provider.php | 10 +- 8 files changed, 573 insertions(+), 29 deletions(-) create mode 100644 src/Pods/Integrations/Query_Monitor.php create mode 100644 src/Pods/Integrations/Query_Monitor/Collectors/Constants.php create mode 100644 src/Pods/Integrations/Query_Monitor/Collectors/Debug.php create mode 100644 src/Pods/Integrations/Query_Monitor/Outputters/Constants.php create mode 100644 src/Pods/Integrations/Query_Monitor/Outputters/Debug.php diff --git a/classes/PodsInit.php b/classes/PodsInit.php index 53ac773fc6..67220427a2 100644 --- a/classes/PodsInit.php +++ b/classes/PodsInit.php @@ -2485,9 +2485,6 @@ public function run() { // Compatibility with WP 5.4 privacy export. add_filter( 'wp_privacy_additional_user_profile_data', array( $this, 'filter_wp_privacy_additional_user_profile_data' ), 10, 3 ); - // Compatibility for Query Monitor conditionals - add_filter( 'query_monitor_conditionals', array( $this, 'filter_query_monitor_conditionals' ) ); - // Support for quick edit in WP 6.4+. add_filter( 'quick_edit_enabled_for_post_type', [ $this, 'quick_edit_enabled_for_post_type' ], 10, 2 ); add_filter( 'quick_edit_enabled_for_taxonomy', [ $this, 'quick_edit_enabled_for_taxonomy' ], 10, 2 ); @@ -2732,22 +2729,4 @@ public function filter_wp_privacy_additional_user_profile_data( $additional_user return $additional_user_profile_data; } - - /** - * Add Pods conditional functions to Query Monitor. - * - * @param array $conditionals - * @return array - */ - public function filter_query_monitor_conditionals( $conditionals ) { - $conditionals[] = 'pods_developer'; - $conditionals[] = 'pods_tableless'; - $conditionals[] = 'pods_light'; - $conditionals[] = 'pods_strict'; - $conditionals[] = 'pods_allow_deprecated'; - $conditionals[] = 'pods_api_cache'; - $conditionals[] = 'pods_shortcode_allow_evaluate_tags'; - $conditionals[] = 'pods_session_auto_start'; - return $conditionals; - } } diff --git a/src/Pods/Integration.php b/src/Pods/Integration.php index 83eb9d857c..828f400aaf 100644 --- a/src/Pods/Integration.php +++ b/src/Pods/Integration.php @@ -14,12 +14,12 @@ abstract class Integration { * * @var array[] { * @type array $action { - * @type callable $callback The callback. + * @type callable $callback The callback or name of the method on current object. * @type int $priority Priority. * @type int $arguments Number of arguments. * } * @type array $filter { - * @type callable $callback The callback. + * @type callable $callback The callbackor name of the method on current object. * @type int $priority Priority. * @type int $arguments Number of arguments. * } @@ -53,15 +53,29 @@ public static function is_active() { public function hook() { foreach ( $this->hooks as $type => $hooks ) { foreach ( $hooks as $hook => $params ) { - if ( is_string( $params[0] ) && is_callable( [ $this, $params[0] ] ) ) { + if ( is_string( $params[0] ) && method_exists( $this, $params[0] ) ) { $params[0] = [ $this, $params[0] ]; } + + if ( ! is_callable( $params[0]) ) { + _doing_it_wrong( 'hook', 'Pods Integration should have a callable for the first hook parameter', 'TBD' ); + } + array_unshift( $params, $hook ); call_user_func_array( 'add_' . $type, $params ); } } + + $this->post_hook(); } + /** + * Do any post-hook related functionality. + * + * @since TBD + */ + public function post_hook() {} + /** * Remove the class hooks. * @@ -77,6 +91,15 @@ public function unhook() { call_user_func_array( 'remove_' . $type, $params ); } } + + $this->post_unhook(); } + /** + * Do any post-unhook related functionality. + * + * @since TBD + */ + public function post_unhook() {} + } diff --git a/src/Pods/Integrations/Query_Monitor.php b/src/Pods/Integrations/Query_Monitor.php new file mode 100644 index 0000000000..bb6f6bb20c --- /dev/null +++ b/src/Pods/Integrations/Query_Monitor.php @@ -0,0 +1,98 @@ + [ + 'pods_debug_data' => [ + [ Collectors\Debug::class, 'track_debug_data' ], + 10, + 4, + ], + ], + 'filter' => [ + 'pods_is_debug_logging_enabled' => [ + '__return_true', + ], + 'qm/outputter/html' => [ + [ __CLASS__, 'register_outputters' ], + ], + 'query_monitor_conditionals' => [ + [ __CLASS__, 'filter_query_monitor_conditionals' ], + ], + ], + ]; + + /** + * @inheritDoc + */ + public function post_hook() { + QM_Collectors::add( new Collectors\Constants() ); + QM_Collectors::add( new Collectors\Debug() ); + } + + /** + * @inheritDoc + */ + public static function is_active(): bool { + return ( + class_exists( 'QM_Activation' ) + && ( ! defined( 'QM_DISABLED' ) || ! QM_DISABLED ) + && ( ! defined( 'QMX_DISABLED' ) || ! QMX_DISABLED ) + ); + } + + /** + * Register the custom Pods outputters. + * + * @since TBD + * + * @param array $outputters The array of outputter instances. + * + * @return array The updated array of outputters. + */ + public static function register_outputters( array $outputters ): array { + $outputters['pods-constants'] = new Outputters\Constants( QM_Collectors::get( 'pods-constants' ) ); + $outputters['pods-debug'] = new Outputters\Debug( QM_Collectors::get( 'pods-debug' ) ); + + return $outputters; + } + + /** + * Add Pods conditional functions to Query Monitor. + * + * @since TBD + * + * @param array $conditionals The conditional functions for Query Monitor. + * + * @return array The updated conditional functions. + */ + public static function filter_query_monitor_conditionals( array $conditionals ): array { + $conditionals[] = 'pods_developer'; + $conditionals[] = 'pods_tableless'; + $conditionals[] = 'pods_light'; + $conditionals[] = 'pods_strict'; + $conditionals[] = 'pods_allow_deprecated'; + $conditionals[] = 'pods_api_cache'; + $conditionals[] = 'pods_shortcode_allow_evaluate_tags'; + $conditionals[] = 'pods_session_auto_start'; + + return $conditionals; + } + +} diff --git a/src/Pods/Integrations/Query_Monitor/Collectors/Constants.php b/src/Pods/Integrations/Query_Monitor/Collectors/Constants.php new file mode 100644 index 0000000000..02af83e375 --- /dev/null +++ b/src/Pods/Integrations/Query_Monitor/Collectors/Constants.php @@ -0,0 +1,118 @@ +data['constants'] = $data; + } +} diff --git a/src/Pods/Integrations/Query_Monitor/Collectors/Debug.php b/src/Pods/Integrations/Query_Monitor/Collectors/Debug.php new file mode 100644 index 0000000000..7967031793 --- /dev/null +++ b/src/Pods/Integrations/Query_Monitor/Collectors/Debug.php @@ -0,0 +1,73 @@ +data['debug_data'] = self::get_debug_data(); + } + + /** + * Track debug data. + * + * @since TBD + * + * @param mixed $debug The debug data to track. + * @param string $context The context where the debug came from. + * @param string $function The function/method name where the debug was called. + * @param int $line The line number where the debug was called. + */ + public static function track_debug_data( $debug, string $context, string $function, int $line ): void { + self::$custom_data[] = compact( 'debug', 'context', 'function', 'line' ); + } + + /** + * Get all of the debug data tracked. + * + * @since TBD + * + * @return array All of the debug data tracked. + */ + public static function get_debug_data(): array { + return self::$custom_data; + } + + /** + * Reset all of the debug data tracked. + * + * @since TBD + */ + public static function reset_debug_data(): void { + self::$custom_data = []; + } +} diff --git a/src/Pods/Integrations/Query_Monitor/Outputters/Constants.php b/src/Pods/Integrations/Query_Monitor/Outputters/Constants.php new file mode 100644 index 0000000000..79b83e3962 --- /dev/null +++ b/src/Pods/Integrations/Query_Monitor/Outputters/Constants.php @@ -0,0 +1,79 @@ +collector->get_data(); + + $this->before_tabular_output(); + ?> + + + + + + + + $value ) { + ?> + + + + + + + none + + + + + + + + + + after_tabular_output(); + } +} diff --git a/src/Pods/Integrations/Query_Monitor/Outputters/Debug.php b/src/Pods/Integrations/Query_Monitor/Outputters/Debug.php new file mode 100644 index 0000000000..4c9319e29a --- /dev/null +++ b/src/Pods/Integrations/Query_Monitor/Outputters/Debug.php @@ -0,0 +1,174 @@ +collector->get_data(); + + $this->before_tabular_output(); + + $functions = array_unique( array_column( $data['debug_data'], 'function', 'function' ) ); + $contexts = array_unique( array_column( $data['debug_data'], 'context', 'context' ) ); + + sort( $functions ); + sort( $contexts ); + + $debug_log_types = [ + 'yes' => __( 'Log is JSON', 'pods' ), + 'no' => __( 'Log is not JSON', 'pods' ), + ]; + ?> + + + + build_filter( 'function', $functions, __( 'Function/Method', 'pods' ) ); ?> + + + + + + build_filter( 'context', $contexts, __( 'Context', 'pods' ) ); ?> + + + build_filter( 'debug-log-is-json', $debug_log_types, __( 'Debug Log', 'pods' ) ); ?> + + + + + $debug['function'], + 'data-qm-context' => $debug['context'], + 'data-qm-debug-log-is-json' => $has_json ? 'yes' : 'no', + ]; + + $attr = ''; + + foreach ( $row_attr as $a => $v ) { + $attr .= ' ' . $a . '="' . esc_attr( $v ) . '"'; + } + ?> + > + + + + + + + + + + +
';
+							echo esc_html( $excerpt ) . ' …';
+							echo '
'; + echo '
';
+							echo esc_html( $debug_output );
+							echo '
'; + } else { + echo '
';
+							echo esc_html( $debug_output );
+							echo '
'; + } + ?> + + + + + none + + + + + + + + + + + + after_tabular_output(); + } +} diff --git a/src/Pods/Integrations/Service_Provider.php b/src/Pods/Integrations/Service_Provider.php index 9c2aeb53ac..ae2e7201e0 100644 --- a/src/Pods/Integrations/Service_Provider.php +++ b/src/Pods/Integrations/Service_Provider.php @@ -24,9 +24,10 @@ class Service_Provider extends \Pods\Service_Provider_Base { public function register() { $this->integrations = [ - 'polylang' => Polylang::class, - 'wpml' => WPML::class, - 'enfold' => Enfold::class, + 'polylang' => Polylang::class, + 'wpml' => WPML::class, + 'enfold' => Enfold::class, + 'query-monitor' => Query_Monitor::class, ]; foreach ( $this->integrations as $integration ) { @@ -46,7 +47,6 @@ public function register() { * @since 2.8.0 */ protected function hooks() { - add_filter( 'pods_admin_config_pod_fields_post_type_supported_features', $this->container->callback( 'pods.integration.genesis', 'add_post_type_supports' ) ); add_filter( 'pods_admin_config_pod_fields_post_type_supported_features', $this->container->callback( 'pods.integration.yarpp', 'add_post_type_supports' ) ); add_filter( 'pods_admin_config_pod_fields_post_type_supported_features', $this->container->callback( 'pods.integration.jetpack', 'add_post_type_supports' ) ); @@ -64,7 +64,6 @@ protected function hooks() { * @since 2.8.0 */ public function plugins_loaded() { - /** * Filter what integration classes should run on the plugins_loaded hook. * @@ -73,6 +72,7 @@ public function plugins_loaded() { * @param \Pods\Integration[] $integrations The list of integrations to run on plugins_loaded hook. */ $integrations = apply_filters( 'pods_integrations_on_plugins_loaded', $this->integrations ); + foreach ( $integrations as $class ) { if ( is_string( $class ) ) { $class = $this->container->make( $class );