Completed
Branch FET/11450/caching-unit-tests (fd4ae9)
by
unknown
51:38 queued 38:35
created

EEH_Debug_Tools::printv()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 28
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 64
nop 7
dl 0
loc 28
rs 6.7272
c 0
b 0
f 0
1
<?php use EventEspresso\core\services\Benchmark;
2
3
if (! defined('EVENT_ESPRESSO_VERSION')) {
4
    exit('No direct script access allowed');
5
}
6
7
8
9
/**
10
 * Class EEH_Debug_Tools
11
 *
12
 * @package               Event Espresso
13
 * @subpackage            core
14
 * @author                Brent Christensen, Michael Nelson
15
 * @since                 4.0
16
 */
17
class EEH_Debug_Tools
18
{
19
20
    /**
21
     *    instance of the EEH_Autoloader object
22
     *
23
     * @var    $_instance
24
     * @access    private
25
     */
26
    private static $_instance;
27
28
    /**
29
     * @var array
30
     */
31
    protected $_memory_usage_points = array();
32
33
34
35
    /**
36
     * @singleton method used to instantiate class object
37
     * @access    public
38
     * @return EEH_Debug_Tools
39
     */
40
    public static function instance()
41
    {
42
        // check if class object is instantiated, and instantiated properly
43
        if (! self::$_instance instanceof EEH_Debug_Tools) {
44
            self::$_instance = new self();
45
        }
46
        return self::$_instance;
47
    }
48
49
50
51
    /**
52
     * private class constructor
53
     */
54
    private function __construct()
55
    {
56
        // load Kint PHP debugging library
57
        if (! class_exists('Kint') && file_exists(EE_PLUGIN_DIR_PATH . 'tests' . DS . 'kint' . DS . 'Kint.class.php')) {
58
            // despite EE4 having a check for an existing copy of the Kint debugging class,
59
            // if another plugin was loaded AFTER EE4 and they did NOT perform a similar check,
60
            // then hilarity would ensue as PHP throws a "Cannot redeclare class Kint" error
61
            // so we've moved it to our test folder so that it is not included with production releases
62
            // plz use https://wordpress.org/plugins/kint-debugger/  if testing production versions of EE
63
            require_once(EE_PLUGIN_DIR_PATH . 'tests' . DS . 'kint' . DS . 'Kint.class.php');
64
        }
65
        // if ( ! defined('DOING_AJAX') || $_REQUEST['noheader'] !== 'true' || ! isset( $_REQUEST['noheader'], $_REQUEST['TB_iframe'] ) ) {
66
        //add_action( 'shutdown', array($this,'espresso_session_footer_dump') );
67
        // }
68
        $plugin = basename(EE_PLUGIN_DIR_PATH);
69
        add_action("activate_{$plugin}", array('EEH_Debug_Tools', 'ee_plugin_activation_errors'));
70
        add_action('activated_plugin', array('EEH_Debug_Tools', 'ee_plugin_activation_errors'));
71
        add_action('shutdown', array('EEH_Debug_Tools', 'show_db_name'));
72
    }
73
74
75
76
    /**
77
     *    show_db_name
78
     *
79
     * @return void
80
     */
81
    public static function show_db_name()
82
    {
83
        if (! defined('DOING_AJAX') && (defined('EE_ERROR_EMAILS') && EE_ERROR_EMAILS)) {
84
            echo '<p style="font-size:10px;font-weight:normal;color:#E76700;margin: 1em 2em; text-align: right;">DB_NAME: '
85
                 . DB_NAME
86
                 . '</p>';
87
        }
88
        if (EE_DEBUG) {
89
            Benchmark::displayResults();
90
        }
91
    }
92
93
94
95
    /**
96
     *    dump EE_Session object at bottom of page after everything else has happened
97
     *
98
     * @return void
99
     */
100
    public function espresso_session_footer_dump()
101
    {
102
        if (
103
            (defined('WP_DEBUG') && WP_DEBUG)
104
            && ! defined('DOING_AJAX')
105
            && class_exists('Kint')
106
            && function_exists('wp_get_current_user')
107
            && current_user_can('update_core')
108
            && class_exists('EE_Registry')
109
        ) {
110
            Kint::dump(EE_Registry::instance()->SSN->id());
111
            Kint::dump(EE_Registry::instance()->SSN);
112
            //			Kint::dump( EE_Registry::instance()->SSN->get_session_data('cart')->get_tickets() );
113
            $this->espresso_list_hooked_functions();
114
            Benchmark::displayResults();
115
        }
116
    }
117
118
119
120
    /**
121
     *    List All Hooked Functions
122
     *    to list all functions for a specific hook, add ee_list_hooks={hook-name} to URL
123
     *    http://wp.smashingmagazine.com/2009/08/18/10-useful-wordpress-hook-hacks/
124
     *
125
     * @param string $tag
126
     * @return void
127
     */
128
    public function espresso_list_hooked_functions($tag = '')
129
    {
130
        global $wp_filter;
131
        echo '<br/><br/><br/><h3>Hooked Functions</h3>';
132
        if ($tag) {
133
            $hook[$tag] = $wp_filter[$tag];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$hook was never initialized. Although not strictly required by PHP, it is generally a good practice to add $hook = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
134
            if (! is_array($hook[$tag])) {
135
                trigger_error("Nothing found for '$tag' hook", E_USER_WARNING);
136
                return;
137
            }
138
            echo '<h5>For Tag: ' . $tag . '</h5>';
139
        } else {
140
            $hook = is_array($wp_filter) ? $wp_filter : array($wp_filter);
141
            ksort($hook);
142
        }
143
        foreach ($hook as $tag_name => $priorities) {
144
            echo "<br />&gt;&gt;&gt;&gt;&gt;\t<strong>$tag_name</strong><br />";
145
            ksort($priorities);
146
            foreach ($priorities as $priority => $function) {
147
                echo $priority;
148
                foreach ($function as $name => $properties) {
149
                    echo "\t$name<br />";
150
                }
151
            }
152
        }
153
    }
154
155
156
157
    /**
158
     *    registered_filter_callbacks
159
     *
160
     * @param string $hook_name
161
     * @return array
162
     */
163
    public static function registered_filter_callbacks($hook_name = '')
164
    {
165
        $filters = array();
166
        global $wp_filter;
167
        if (isset($wp_filter[$hook_name])) {
168
            $filters[$hook_name] = array();
169
            foreach ($wp_filter[$hook_name] as $priority => $callbacks) {
170
                $filters[$hook_name][$priority] = array();
171
                foreach ($callbacks as $callback) {
172
                    $filters[$hook_name][$priority][] = $callback['function'];
173
                }
174
            }
175
        }
176
        return $filters;
177
    }
178
179
180
181
    /**
182
     *    captures plugin activation errors for debugging
183
     *
184
     * @return void
185
     * @throws EE_Error
186
     */
187
    public static function ee_plugin_activation_errors()
188
    {
189
        if (WP_DEBUG) {
190
            $activation_errors = ob_get_contents();
191
            if (! empty($activation_errors)) {
192
                $activation_errors = date('Y-m-d H:i:s') . "\n" . $activation_errors;
193
            }
194
            espresso_load_required('EEH_File', EE_HELPERS . 'EEH_File.helper.php');
195
            if (class_exists('EEH_File')) {
196
                try {
197
                    EEH_File::ensure_file_exists_and_is_writable(
198
                        EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . 'espresso_plugin_activation_errors.html'
199
                    );
200
                    EEH_File::write_to_file(
201
                        EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . 'espresso_plugin_activation_errors.html',
202
                        $activation_errors
203
                    );
204
                } catch (EE_Error $e) {
205
                    EE_Error::add_error(
206
                        sprintf(
207
                            __(
208
                                'The Event Espresso activation errors file could not be setup because: %s',
209
                                'event_espresso'
210
                            ),
211
                            $e->getMessage()
212
                        ),
213
                        __FILE__, __FUNCTION__, __LINE__
214
                    );
215
                }
216
            } else {
217
                // old school attempt
218
                file_put_contents(
219
                    EVENT_ESPRESSO_UPLOAD_DIR . 'logs' . DS . 'espresso_plugin_activation_errors.html',
220
                    $activation_errors
221
                );
222
            }
223
            $activation_errors = get_option('ee_plugin_activation_errors', '') . $activation_errors;
224
            update_option('ee_plugin_activation_errors', $activation_errors);
225
        }
226
    }
227
228
229
230
    /**
231
     * This basically mimics the WordPress _doing_it_wrong() function except adds our own messaging etc.
232
     * Very useful for providing helpful messages to developers when the method of doing something has been deprecated,
233
     * or we want to make sure they use something the right way.
234
     *
235
     * @access public
236
     * @param string $function      The function that was called
237
     * @param string $message       A message explaining what has been done incorrectly
238
     * @param string $version       The version of Event Espresso where the error was added
239
     * @param string $applies_when  a version string for when you want the doing_it_wrong notice to begin appearing
240
     *                              for a deprecated function. This allows deprecation to occur during one version,
241
     *                              but not have any notices appear until a later version. This allows developers
242
     *                              extra time to update their code before notices appear.
243
     * @param int    $error_type
244
     * @uses   trigger_error()
245
     */
246
    public function doing_it_wrong(
247
        $function,
248
        $message,
249
        $version,
250
        $applies_when = '',
251
        $error_type = null
252
    ) {
253
        $applies_when = ! empty($applies_when) ? $applies_when : espresso_version();
254
        $error_type = $error_type !== null ? $error_type : E_USER_NOTICE;
255
        // because we swapped the parameter order around for the last two params,
256
        // let's verify that some third party isn't still passing an error type value for the third param
257
        if (is_int($applies_when)) {
258
            $error_type = $applies_when;
259
            $applies_when = espresso_version();
260
        }
261
        // if not displaying notices yet, then just leave
262
        if (version_compare(espresso_version(), $applies_when, '<')) {
263
            return;
264
        }
265
        do_action('AHEE__EEH_Debug_Tools__doing_it_wrong_run', $function, $message, $version);
266
        $version = $version === null
267
            ? ''
268
            : sprintf(
269
                __('(This message was added in version %s of Event Espresso)', 'event_espresso'),
270
                $version
271
            );
272
        $error_message = sprintf(
273
            esc_html__('%1$s was called %2$sincorrectly%3$s. %4$s %5$s', 'event_espresso'),
274
            $function,
275
            '<strong>',
276
            '</strong>',
277
            $message,
278
            $version
279
        );
280
        // don't trigger error if doing ajax,
281
        // instead we'll add a transient EE_Error notice that in theory should show on the next request.
282
        if (defined('DOING_AJAX') && DOING_AJAX) {
283
            $error_message .= ' ' . esc_html__(
284
                    'This is a doing_it_wrong message that was triggered during an ajax request.  The request params on this request were: ',
285
                    'event_espresso'
286
                );
287
            $error_message .= '<ul><li>';
288
            $error_message .= implode('</li><li>', EE_Registry::instance()->REQ->params());
289
            $error_message .= '</ul>';
290
            EE_Error::add_error($error_message, 'debug::doing_it_wrong', $function, '42');
291
            //now we set this on the transient so it shows up on the next request.
292
            EE_Error::get_notices(false, true);
293
        } else {
294
            trigger_error($error_message, $error_type);
295
        }
296
    }
297
298
299
300
301
    /**
302
     * Logger helpers
303
     */
304
    /**
305
     * debug
306
     *
307
     * @param string $class
308
     * @param string $func
309
     * @param string $line
310
     * @param array  $info
311
     * @param bool   $display_request
312
     * @param string $debug_index
313
     * @param string $debug_key
314
     * @throws EE_Error
315
     * @throws \EventEspresso\core\exceptions\InvalidSessionDataException
316
     */
317
    public static function log(
318
        $class = '',
319
        $func = '',
320
        $line = '',
321
        $info = array(),
322
        $display_request = false,
323
        $debug_index = '',
324
        $debug_key = 'EE_DEBUG_SPCO'
325
    ) {
326
        if (WP_DEBUG) {
327
            $debug_key = $debug_key . '_' . EE_Session::instance()->id();
328
            $debug_data = get_option($debug_key, array());
329
            $default_data = array(
330
                $class => $func . '() : ' . $line,
331
                'REQ'  => $display_request ? $_REQUEST : '',
332
            );
333
            // don't serialize objects
334
            $info = self::strip_objects($info);
335
            $index = ! empty($debug_index) ? $debug_index : 0;
336
            if (! isset($debug_data[$index])) {
337
                $debug_data[$index] = array();
338
            }
339
            $debug_data[$index][microtime()] = array_merge($default_data, $info);
340
            update_option($debug_key, $debug_data);
341
        }
342
    }
343
344
345
346
    /**
347
     * strip_objects
348
     *
349
     * @param array $info
350
     * @return array
351
     */
352
    public static function strip_objects($info = array())
353
    {
354
        foreach ($info as $key => $value) {
355
            if (is_array($value)) {
356
                $info[$key] = self::strip_objects($value);
357 View Code Duplication
            } else if (is_object($value)) {
358
                $object_class = get_class($value);
359
                $info[$object_class] = array();
360
                $info[$object_class]['ID'] = method_exists($value, 'ID') ? $value->ID() : spl_object_hash($value);
361
                if (method_exists($value, 'ID')) {
362
                    $info[$object_class]['ID'] = $value->ID();
363
                }
364
                if (method_exists($value, 'status')) {
365
                    $info[$object_class]['status'] = $value->status();
366
                } else if (method_exists($value, 'status_ID')) {
367
                    $info[$object_class]['status'] = $value->status_ID();
368
                }
369
                unset($info[$key]);
370
            }
371
        }
372
        return (array)$info;
373
    }
374
375
376
377
    /**
378
     * @param mixed      $var
379
     * @param string     $var_name
380
     * @param string     $file
381
     * @param int|string $line
382
     * @param int        $heading_tag
383
     * @param bool       $die
384
     * @param string     $margin
385
     */
386
    public static function printv(
387
        $var,
388
        $var_name = '',
389
        $file = '',
390
        $line = '',
391
        $heading_tag = 5,
392
        $die = false,
393
        $margin = ''
394
    ) {
395
        $var_name = ! $var_name ? 'string' : $var_name;
396
        $var_name = ucwords(str_replace('$', '', $var_name));
397
        $is_method = method_exists($var_name, $var);
398
        $var_name = ucwords(str_replace('_', ' ', $var_name));
399
        $result = $heading_tag > 3 && defined('EE_TESTS_DIR')
400
            ? "\n"
401
            :'';
402
        $heading_tag = is_int($heading_tag) ? "h{$heading_tag}" : 'h5';
403
        $result .= EEH_Debug_Tools::heading($var_name, $heading_tag, $margin);
404
        $result .= $is_method
405
            ? EEH_Debug_Tools::grey_span('::') . EEH_Debug_Tools::orange_span($var . '()')
406
            : EEH_Debug_Tools::grey_span(' : ') . EEH_Debug_Tools::orange_span($var);
407
        $result .= EEH_Debug_Tools::file_and_line($file, $line);
408
        $result .= EEH_Debug_Tools::headingX($heading_tag);
409
        if ($die) {
410
            die($result);
411
        }
412
        echo $result;
413
    }
414
415
416
417
    /**
418
     * @param string $var_name
419
     * @param string $heading_tag
420
     * @param string $margin
421
     * @return string
422
     */
423
    protected static function heading($var_name = '', $heading_tag = 'h5', $margin = '')
424
    {
425
        if (defined('EE_TESTS_DIR')) {
426
            return "\n{$var_name}";
427
        }
428
        $margin = "25px 0 0 {$margin}";
429
        return '<' . $heading_tag . ' style="color:#2EA2CC; margin:' . $margin . ';"><b>' . $var_name . '</b>';
430
    }
431
432
433
434
    /**
435
     * @param string $heading_tag
436
     * @return string
437
     */
438
    protected static function headingX($heading_tag = 'h5')
439
    {
440
        if (defined('EE_TESTS_DIR')) {
441
            return '';
442
        }
443
        return '</' . $heading_tag . '>';
444
    }
445
446
447
448
    /**
449
     * @param string $content
450
     * @return string
451
     */
452
    protected static function grey_span($content = '')
453
    {
454
        if (defined('EE_TESTS_DIR')) {
455
            return $content;
456
        }
457
        return '<span style="color:#999">' . $content . '</span>';
458
    }
459
460
461
462
    /**
463
     * @param string $file
464
     * @param int    $line
465
     * @return string
466
     */
467
    protected static function file_and_line($file, $line)
468
    {
469
        if ($file === '' || $line === '' || defined('EE_TESTS_DIR')) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $line (integer) and '' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
470
            return '';
471
        }
472
        return '<br /><span style="font-size:9px;font-weight:normal;color:#666;line-height: 12px;">'
473
               . $file
474
               . '<br />line no: '
475
               . $line
476
               . '</span>';
477
    }
478
479
480
481
    /**
482
     * @param string $content
483
     * @return string
484
     */
485
    protected static function orange_span($content = '')
486
    {
487
        if (defined('EE_TESTS_DIR')) {
488
            return $content;
489
        }
490
        return '<span style="color:#E76700">' . $content . '</span>';
491
    }
492
493
494
495
    /**
496
     * @param mixed $var
497
     * @return string
498
     */
499
    protected static function pre_span($var)
500
    {
501
        ob_start();
502
        var_dump($var);
503
        $var = ob_get_clean();
504
        if (defined('EE_TESTS_DIR')) {
505
            return "\n" . $var;
506
        }
507
        return '<pre style="color:#999; padding:1em; background: #fff">' . $var . '</pre>';
508
    }
509
510
511
512
    /**
513
     * @param mixed      $var
514
     * @param string     $var_name
515
     * @param string     $file
516
     * @param int|string $line
517
     * @param int        $heading_tag
518
     * @param bool       $die
519
     */
520
    public static function printr(
521
        $var,
522
        $var_name = '',
523
        $file = '',
524
        $line = '',
525
        $heading_tag = 5,
526
        $die = false
527
    ) {
528
        // return;
529
        $file = str_replace(rtrim(ABSPATH, '\\/'), '', $file);
530
        $margin = is_admin() ? ' 180px' : '0';
531
        //$print_r = false;
532
        if (is_string($var)) {
533
            EEH_Debug_Tools::printv($var, $var_name, $file, $line, $heading_tag, $die, $margin);
534
            return;
535
        }
536
        if (is_object($var)) {
537
            $var_name = ! $var_name ? 'object' : $var_name;
538
            //$print_r = true;
539
        } else if (is_array($var)) {
540
            $var_name = ! $var_name ? 'array' : $var_name;
541
            //$print_r = true;
542
        } else if (is_numeric($var)) {
543
            $var_name = ! $var_name ? 'numeric' : $var_name;
544
        } else if ($var === null) {
545
            $var_name = ! $var_name ? 'null' : $var_name;
546
        }
547
        $var_name = ucwords(str_replace(array('$', '_'), array('', ' '), $var_name));
548
        $heading_tag = is_int($heading_tag) ? "h{$heading_tag}" : 'h5';
549
        $result = EEH_Debug_Tools::heading($var_name, $heading_tag, $margin);
550
        $result .= EEH_Debug_Tools::grey_span(' : ') . EEH_Debug_Tools::orange_span(
551
                EEH_Debug_Tools::pre_span($var)
552
            );
553
        $result .= EEH_Debug_Tools::file_and_line($file, $line);
554
        $result .= EEH_Debug_Tools::headingX($heading_tag);
555
        if ($die) {
556
            die($result);
557
        }
558
        echo $result;
559
    }
560
561
562
563
    /******************** deprecated ********************/
564
565
566
567
    /**
568
     * @deprecated 4.9.39.rc.034
569
     */
570
    public function reset_times()
571
    {
572
        Benchmark::resetTimes();
573
    }
574
575
576
577
    /**
578
     * @deprecated 4.9.39.rc.034
579
     * @param null $timer_name
580
     */
581
    public function start_timer($timer_name = null)
582
    {
583
        Benchmark::startTimer($timer_name);
584
    }
585
586
587
588
    /**
589
     * @deprecated 4.9.39.rc.034
590
     * @param string $timer_name
591
     */
592
    public function stop_timer($timer_name = '')
593
    {
594
        Benchmark::stopTimer($timer_name);
595
    }
596
597
598
599
    /**
600
     * @deprecated 4.9.39.rc.034
601
     * @param string  $label      The label to show for this time eg "Start of calling Some_Class::some_function"
602
     * @param boolean $output_now whether to echo now, or wait until EEH_Debug_Tools::show_times() is called
603
     * @return void
604
     */
605
    public function measure_memory($label, $output_now = false)
606
    {
607
        Benchmark::measureMemory($label, $output_now);
608
    }
609
610
611
612
    /**
613
     * @deprecated 4.9.39.rc.034
614
     * @param int $size
615
     * @return string
616
     */
617
    public function convert($size)
618
    {
619
        return Benchmark::convert($size);
620
    }
621
622
623
624
    /**
625
     * @deprecated 4.9.39.rc.034
626
     * @param bool $output_now
627
     * @return string
628
     */
629
    public function show_times($output_now = true)
630
    {
631
        return Benchmark::displayResults($output_now);
632
    }
633
634
635
636
    /**
637
     * @deprecated 4.9.39.rc.034
638
     * @param string $timer_name
639
     * @param float  $total_time
640
     * @return string
641
     */
642
    public function format_time($timer_name, $total_time)
643
    {
644
        return Benchmark::formatTime($timer_name, $total_time);
645
    }
646
647
648
649
}
650
651
652
653
/**
654
 * borrowed from Kint Debugger
655
 * Plugin URI: http://upthemes.com/plugins/kint-debugger/
656
 */
657
if (class_exists('Kint') && ! function_exists('dump_wp_query')) {
658
    function dump_wp_query()
659
    {
660
        global $wp_query;
661
        d($wp_query);
662
    }
663
}
664
/**
665
 * borrowed from Kint Debugger
666
 * Plugin URI: http://upthemes.com/plugins/kint-debugger/
667
 */
668
if (class_exists('Kint') && ! function_exists('dump_wp')) {
669
    function dump_wp()
670
    {
671
        global $wp;
672
        d($wp);
673
    }
674
}
675
/**
676
 * borrowed from Kint Debugger
677
 * Plugin URI: http://upthemes.com/plugins/kint-debugger/
678
 */
679
if (class_exists('Kint') && ! function_exists('dump_post')) {
680
    function dump_post()
681
    {
682
        global $post;
683
        d($post);
684
    }
685
}
686