• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 13316763745

13 Feb 2025 08:45PM UTC coverage: 82.712% (-0.3%) from 83.023%
13316763745

push

github

web-flow
Merge pull request #3307 from wp-graphql/release/v2.0.0

release: v2.0.0

195 of 270 new or added lines in 20 files covered. (72.22%)

180 existing lines in 42 files now uncovered.

13836 of 16728 relevant lines covered (82.71%)

299.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

10.34
/src/Utils/QueryLog.php
1
<?php
2

3
namespace WPGraphQL\Utils;
4

5
/**
6
 * Class QueryLog
7
 *
8
 * @package WPGraphQL\Utils
9
 */
10
class QueryLog {
11

12
        /**
13
         * Whether Query Logs are enabled
14
         *
15
         * @var bool
16
         */
17
        protected $query_logs_enabled;
18

19
        /**
20
         * The user role query logs should be limited to
21
         *
22
         * @var string
23
         */
24
        protected $query_log_user_role;
25

26
        /**
27
         * Initialize Query Logging
28
         *
29
         * @return void
30
         */
31
        public function init() {
754✔
32

33
                // Check whether Query Logs have been enabled from the settings page
34
                $enabled                  = get_graphql_setting( 'query_logs_enabled', 'off' );
754✔
35
                $this->query_logs_enabled = 'on' === $enabled;
754✔
36

37
                $this->query_log_user_role = get_graphql_setting( 'query_log_user_role', 'manage_options' );
754✔
38

39
                if ( ! $this->query_logs_enabled ) {
754✔
40
                        return;
754✔
41
                }
42

43
                add_action( 'init', [ $this, 'init_save_queries' ] );
×
44
                add_filter( 'graphql_request_results', [ $this, 'show_results' ], 10, 5 );
×
45
        }
46

47
        /**
48
         * Tell WordPress to start saving queries.
49
         *
50
         * NOTE: This will affect all requests, not just GraphQL requests.
51
         *
52
         * @return void
53
         */
UNCOV
54
        public function init_save_queries() {
×
55
                if ( is_graphql_http_request() && ! defined( 'SAVEQUERIES' ) ) {
×
56
                        define( 'SAVEQUERIES', true );
×
57
                }
58
        }
59

60
        /**
61
         * Determine if the requesting user can see logs
62
         *
63
         * @return bool
64
         */
UNCOV
65
        public function user_can_see_logs() {
×
66
                $can_see = false;
×
67

68
                // If logs are disabled, user cannot see logs
69
                if ( ! $this->query_logs_enabled ) {
×
70
                        $can_see = false;
×
71
                } elseif ( 'any' === $this->query_log_user_role ) {
×
72
                        // If "any" is the selected role, anyone can see the logs
73
                        $can_see = true;
×
74
                } else {
75
                        // Get the current users roles
76
                        $user = wp_get_current_user();
×
77

78
                        // If the user doesn't have roles or the selected role isn't one the user has, the
79
                        // user cannot see roles;
80
                        if ( in_array( $this->query_log_user_role, $user->roles, true ) ) {
×
81
                                $can_see = true;
×
82
                        }
83
                }
84

85
                /**
86
                 * Filter whether the logs can be seen in the request results or not
87
                 *
88
                 * @param bool $can_see Whether the requester can see the logs or not
89
                 */
90
                return apply_filters( 'graphql_user_can_see_query_logs', $can_see );
×
91
        }
92

93
        /**
94
         * Filter the results of the GraphQL Response to include the Query Log
95
         *
96
         * @param mixed               $response
97
         * @param \WPGraphQL\WPSchema $schema The WPGraphQL Schema
98
         * @param string              $operation_name The operation name being executed
99
         * @param string              $request        The GraphQL Request being made
100
         * @param array<string,mixed> $variables      The variables sent with the request
101
         *
102
         * @return mixed[]
103
         */
UNCOV
104
        public function show_results( $response, $schema, $operation_name, $request, $variables ) {
×
105
                $query_log = $this->get_query_log();
×
106

107
                // If the user cannot see the logs, return the response as-is without the logs
108
                if ( ! $this->user_can_see_logs() ) {
×
109
                        return $response;
×
110
                }
111

112
                if ( ! empty( $response ) ) {
×
113
                        if ( is_array( $response ) ) {
×
114
                                $response['extensions']['queryLog'] = $query_log;
×
115
                        } elseif ( is_object( $response ) ) {
×
116
                                // @phpstan-ignore-next-line
117
                                $response->extensions['queryLog'] = $query_log;
×
118
                        }
119
                }
120

121
                return $response;
×
122
        }
123

124
        /**
125
         * Return the query log produced from the logs stored by WPDB.
126
         *
127
         * @return array<string,mixed>
128
         */
UNCOV
129
        public function get_query_log() {
×
130
                global $wpdb;
×
131

132
                $save_queries_value = defined( 'SAVEQUERIES' ) && true === SAVEQUERIES ? 'true' : 'false';
×
133
                $default_message    = sprintf(
×
134
                        // translators: %s is the value of the SAVEQUERIES constant
135
                        __( 'Query Logging has been disabled. The \'SAVEQUERIES\' Constant is set to \'%s\' on your server.', 'wp-graphql' ),
×
136
                        $save_queries_value
×
137
                );
×
138

139
                // Default message
140
                $trace = [ $default_message ];
×
141

142
                if ( ! empty( $wpdb->queries ) && is_array( $wpdb->queries ) ) {
×
143
                        $queries = array_map(
×
144
                                static function ( $query ) {
×
145
                                        return [
×
146
                                                'sql'   => $query[0],
×
147
                                                'time'  => $query[1],
×
148
                                                'stack' => $query[2],
×
149
                                        ];
×
150
                                },
×
151
                                $wpdb->queries
×
152
                        );
×
153

154
                        $times      = wp_list_pluck( $queries, 'time' );
×
155
                        $total_time = array_sum( $times );
×
156
                        $trace      = [
×
157
                                'queryCount' => count( $queries ),
×
158
                                'totalTime'  => $total_time,
×
159
                                'queries'    => $queries,
×
160
                        ];
×
161
                }
162

163
                /**
164
                 * Filter the trace
165
                 *
166
                 * @param mixed[]                   $trace     The trace to return
167
                 * @param \WPGraphQL\Utils\QueryLog $instance  The QueryLog class instance
168
                 */
169
                return apply_filters( 'graphql_tracing_response', $trace, $this );
×
170
        }
171
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc