2021-03-03 06:27:47 +00:00
< ? php
2023-08-09 00:20:34 +00:00
defined ( 'ABSPATH' ) || exit ;
2021-03-03 06:27:47 +00:00
use Automattic\WooCommerce\Admin\Notes\Note ;
register_woocommerce_admin_test_helper_rest_route (
'/admin-notes/add-note/v1' ,
'admin_notes_add_note'
);
2023-08-09 00:20:34 +00:00
/**
* Adds an admin note .
*
* @ param WP_REST_Request $request Full data about the request .
*/
2021-03-03 06:27:47 +00:00
function admin_notes_add_note ( $request ) {
2023-08-09 00:20:34 +00:00
$note = new Note ();
2021-04-26 12:46:22 +00:00
$mock_note_data = get_mock_note_data ();
2023-08-09 00:20:34 +00:00
$type = $request -> get_param ( 'type' );
$layout = $request -> get_param ( 'layout' );
2021-03-03 06:27:47 +00:00
$note -> set_name ( $request -> get_param ( 'name' ) );
$note -> set_title ( $request -> get_param ( 'title' ) );
2023-08-09 00:20:34 +00:00
$note -> set_content ( $mock_note_data [ 'content' ] );
2021-04-26 12:46:22 +00:00
$note -> set_image ( $mock_note_data [ $type ][ $layout ] );
2021-04-23 19:37:34 +00:00
$note -> set_layout ( $layout );
$note -> set_type ( $type );
possibly_add_action ( $note );
2023-08-09 00:20:34 +00:00
2021-04-23 19:37:34 +00:00
if ( 'email' === $type ) {
add_email_note_params ( $note );
}
2021-03-03 06:27:47 +00:00
$note -> save ();
return true ;
}
2021-04-21 19:50:33 +00:00
2023-08-09 00:20:34 +00:00
/**
* Adds an email note parameter .
*
* @ param Note $note The note to add parameters to .
*/
2021-04-23 19:37:34 +00:00
function add_email_note_params ( $note ) {
2021-04-21 19:50:33 +00:00
$additional_data = array (
'role' => 'administrator' ,
);
2021-04-23 19:37:34 +00:00
$note -> set_content_data ( ( object ) $additional_data );
}
2023-08-09 00:20:34 +00:00
/**
* Possibly adds an action to a note .
*
* @ param Note $note The note to check and add an action to .
*/
2021-04-23 19:37:34 +00:00
function possibly_add_action ( $note ) {
if ( $note -> get_type () === 'info' ) {
return ;
}
2021-04-21 19:50:33 +00:00
$action_name = sprintf (
'test-action-%s' ,
2021-04-23 19:37:34 +00:00
$note -> get_name ()
2021-04-21 19:50:33 +00:00
);
$note -> add_action ( $action_name , 'Test action' , wc_admin_url () );
2021-04-23 19:37:34 +00:00
}
2021-04-21 19:50:33 +00:00
2023-08-09 00:20:34 +00:00
/**
* Gets mock note data .
*/
2021-04-26 12:46:22 +00:00
function get_mock_note_data () {
2021-04-26 12:49:43 +00:00
$plugin_url = site_url () . '/wp-content/plugins/woocommerce-admin-test-helper/' ;
2021-04-23 19:37:34 +00:00
return array (
2023-08-09 00:20:34 +00:00
'content' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud.' ,
'info' => array (
2021-04-27 15:04:10 +00:00
'banner' => $plugin_url . 'images/admin-notes/banner.jpg' ,
'thumbnail' => $plugin_url . 'images/admin-notes/thumbnail.jpg' ,
2023-08-09 00:20:34 +00:00
'plain' => '' ,
),
'email' => array (
'plain' => $plugin_url . 'images/admin-notes/woocommerce-logo-vector.png' ,
2021-04-23 19:37:34 +00:00
),
2023-08-09 00:20:34 +00:00
'update' => array (
'plain' => '' ,
2021-04-23 19:37:34 +00:00
),
);
2021-04-21 19:50:33 +00:00
}