Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@octaedro
Last active April 9, 2021 03:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save octaedro/864315edaf9c6a2a6de71d297be1ed88 to your computer and use it in GitHub Desktop.
Save octaedro/864315edaf9c6a2a6de71d297be1ed88 to your computer and use it in GitHub Desktop.
Adds 4 new notes with layout: banner, thumbnail, and plain, OR sets all the notes to not deleted. One of those notes is a StoreAlert. Now also adds an email note after pressing `Add Email Notes`.
<?php
/**
* Plugin Name: WooCommerce Prepare Notes Testing
* Plugin URI: https://woocommerce.com
* Description: Adds 4 new notes with layout: banner, thumbnail, and plain, OR sets all the notes to not deleted. One of those notes is a StoreAlert. Now also adds an email note after pressing `Add Email Notes`.
* Author: WooCommerce
* Domain Path: /test_notes
* Version: 0.4
*/
use \Automattic\WooCommerce\Admin\Install;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes;
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Note;
use \Automattic\WooCommerce\Admin\Notes\Notes;
use \Automattic\WooCommerce\Admin\Notes\Note;
function woocommerce_notes_prepare_testing() {
// Confirm the "WC_Data_Store" class and the param "notes_action" are set.
if ( ! class_exists( 'WC_Data_Store' ) || ! isset( $_GET["notes_action"] ) ) {
return;
}
$action = $_GET["notes_action"];
$data_store = \WC_Data_Store::load( 'admin-note' );
$current_wc_admin_version = get_option( Install::VERSION_OPTION );
$uses_new_note_classes = version_compare( '1.6.0', $current_wc_admin_version ) > 0;
// Undelete functionality
if ( $action === 'undelete' ) {
$deleted_notes = $data_store->get_notes( array( '_fields' => array( 'id' ),'is_deleted' => 1, 'per_page' => 50, 'type' => array( 'info', 'marketing','update', 'warning' ) ) );
foreach ( (array) $deleted_notes as $note ) {
$note_to_update = get_note( $uses_new_note_classes, $note->note_id );
if ( $note_to_update ) {
update_note( $uses_new_note_classes, $note_to_update );
}
}
}
// Add notes functionality
if ( $action === 'add' ) {
$notes = get_test_notes();
foreach ( $notes as $note ) {
$note_ids = $data_store->get_notes_with_name( $note[ 'name' ] );
if ( empty( $note_ids ) ) {
if ( $uses_new_note_classes ) {
$new_note = new Note();
} else {
$new_note = new WC_Admin_Note();
}
$new_note->set_title( $note[ 'title' ] );
$new_note->set_content( $note[ 'content' ] );
$new_note->set_type( $note[ 'type' ] );
$new_note->set_name( $note[ 'name' ] );
$new_note->set_layout( $note[ 'layout' ] );
$new_note->set_image( $note[ 'image' ] );
$new_note->set_source( $note[ 'source' ] );
$new_note->add_action( $note[ 'action' ], 'Test action', wc_admin_url() );
$new_note->save();
}
}
}
// Add email notes functionality
if ( $action === 'add_email_notes' ) {
$notes = get_test_email_notes();
foreach ( $notes as $note ) {
$note_ids = $data_store->get_notes_with_name( $note[ 'name' ] );
if ( empty( $note_ids ) ) {
if ( $uses_new_note_classes ) {
$new_note = new Note();
} else {
$new_note = new WC_Admin_Note();
}
$new_note->set_title( $note[ 'title' ] );
$new_note->set_content( $note[ 'content' ] );
$new_note->set_type( $note[ 'type' ] );
$new_note->set_name( $note[ 'name' ] );
$new_note->set_image( $note[ 'image' ] );
$new_note->set_source( $note[ 'source' ] );
$new_note->set_content_data( (object) $note[ 'content_data' ] );
$new_note->add_action( $note[ 'action' ], 'Test action', wc_admin_url() );
$new_note->save();
}
}
}
}
function get_note( $uses_new_note_classes, $note_id ) {
if ( $uses_new_note_classes ) {
return Notes::get_note( (int) $note_id );
} else {
return WC_Admin_Notes::get_note( (int) $note_id );
}
}
function update_note( $uses_new_note_classes, $note_to_update ) {
if ( $uses_new_note_classes ) {
Notes::update_note( $note_to_update, array( 'is_deleted' => 0 ) );
} else {
WC_Admin_Notes::update_note( $note_to_update, array( 'is_deleted' => 0 ) );
}
}
function get_test_notes() {
$id = rand(1, 50000);
$title = 'This is the title %s';
$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.';
$name = 'test-note-name-%s';
$type = 'info';
$store_alert_type = 'update';
$source = 'woocommerce-admin';
$action = 'test-action-%s';
$notes = array(
array(
'title' => sprintf(
$title,
strval ( ++$id )
),
'content' => $content,
'name' => sprintf(
$name,
strval ( $id )
),
'type' => $type,
'source' => $source,
'layout' => 'plain',
'image' => '',
'action' => sprintf(
$action,
strval ( $id )
),
),
array(
'title' => sprintf(
$title,
strval ( ++$id )
),
'content' => $content,
'name' => sprintf(
$name,
strval ( $id )
),
'type' => $type,
'source' => $source,
'layout' => 'thumbnail',
'image' => 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTlm2mX1h6GfMnT9sDD9GsJB7of3MZw3UDzCAQM9EBepiJzyxXH&usqp=CAU',
'action' => sprintf(
$action,
strval ( $id )
),
),
array(
'title' => sprintf(
$title,
strval ( ++$id )
),
'content' => $content,
'name' => sprintf(
$name,
strval ( $id )
),
'type' => $type,
'source' => $source,
'layout' => 'banner',
'image' => 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSAK9Xg4L6FGmDAW5UVtVEv1IXKtGV3-rxYLfAzOBF-fMUdmyWz&usqp=CAU',
'action' => sprintf(
$action,
strval ( $id )
),
),
array(
'title' => sprintf(
$title,
strval ( ++$id )
),
'content' => $content,
'name' => sprintf(
$name,
strval ( $id )
),
'type' => $store_alert_type,
'source' => $source,
'layout' => 'plain',
'image' => '',
'action' => sprintf(
$action,
strval ( $id )
),
),
);
return $notes;
}
function get_test_email_notes() {
$id = rand(1, 50000);
$title = 'This is the email note title %s';
$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.';
$name = 'test-email-note-name-%s';
$type = 'email';
$source = 'woocommerce-admin';
$action = 'test-action-%s';
$additional_data = array(
'role' => 'administrator',
);
$notes = array(
array(
'title' => sprintf(
$title,
strval ( ++$id )
),
'content' => $content,
'name' => sprintf(
$name,
strval ( $id )
),
'type' => $type,
'source' => $source,
'image' => '',
'content_data' => $additional_data,
'action' => sprintf(
$action,
strval ( $id )
),
),
);
return $notes;
}
$plugin_file = plugin_basename( __FILE__ );
add_action( 'admin_notices', 'woocommerce_notes_prepare_testing' );
add_filter( "plugin_action_links_{$plugin_file}", __NAMESPACE__ . '\plugin_action_links', 10, 4 );
function plugin_action_links( $actions ) {
$new = array(
'woocommerce-new-notes' => sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( '?notes_action=add' ) ),
esc_html__( 'Add Notes', 'woocommerce-notes-testing' )
),
'woocommerce-new-email-notes' => sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( '?notes_action=add_email_notes' ) ),
esc_html__( 'Add Email Notes', 'woocommerce-notes-testing' )
),
'woocommerce-undelete' => sprintf(
'<a href="%s">%s</a>',
esc_url( admin_url( '?notes_action=undelete' ) ),
esc_html__( 'Undo Delete', 'woocommerce-notes-testing' )
),
);
return array_merge( $new, $actions );
}
@octaedro
Copy link
Author

Instructions to use the plugin here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment