| 1 |
<?php |
| 2 |
|
| 3 |
namespace Code_Snippets\Admin; |
| 4 |
|
| 5 |
use const Code_Snippets\PLUGIN_FILE; |
| 6 |
use const Code_Snippets\PLUGIN_VERSION; |
| 7 |
|
| 8 |
/** |
| 9 |
* Collects JavaScript errors thrown on a Code Snippets screen, ready to attach to a report. |
| 10 |
* |
| 11 |
* The panel itself is a React application in the page footer and cannot see anything that |
| 12 |
* failed before it mounted, so the listeners are enqueued in the document head instead. |
| 13 |
* Errors thrown by scripts printed above this one are still missed. |
| 14 |
* |
| 15 |
* @package Code_Snippets |
| 16 |
*/ |
| 17 |
class Feedback_Error_Capture { |
| 18 |
|
| 19 |
/** |
| 20 |
* Script handle. |
| 21 |
*/ |
| 22 |
private const SCRIPT_HANDLE = 'code-snippets-feedback-capture'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Panel deciding whether the reporter belongs on this request. |
| 26 |
* |
| 27 |
* @var Feedback_Panel |
| 28 |
*/ |
| 29 |
private Feedback_Panel $panel; |
| 30 |
|
| 31 |
/** |
| 32 |
* Class constructor. |
| 33 |
* |
| 34 |
* @param Feedback_Panel $panel Panel deciding whether the reporter belongs on this request. |
| 35 |
*/ |
| 36 |
public function __construct( Feedback_Panel $panel ) { |
| 37 |
$this->panel = $panel; |
| 38 |
|
| 39 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ], 0 ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Enqueue the error listeners in the document head. |
| 44 |
* |
| 45 |
* @return void |
| 46 |
*/ |
| 47 |
public function enqueue_assets(): void { |
| 48 |
if ( ! $this->panel->should_render() ) { |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
wp_enqueue_script( |
| 53 |
self::SCRIPT_HANDLE, |
| 54 |
plugins_url( 'dist/feedback-capture.js', PLUGIN_FILE ), |
| 55 |
[], |
| 56 |
PLUGIN_VERSION, |
| 57 |
false |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
|