Forked from octaedro/woocommerce-notes-testing.php
Created
September 7, 2020 14:10
-
-
Save seb86/13aedc4cc840f4b7c1e79747bd48c3ed to your computer and use it in GitHub Desktop.
Adds 3 new notes with layout: banner, thumbnail, and plain, OR sets all the notes to not deleted.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WooCommerce Prepare Notes Testing | |
* Plugin URI: https://woocommerce.com | |
* Description: Adds 3 new notes with layout: banner, thumbnail, and plain, OR sets all the notes to not deleted. | |
* Author: WooCommerce | |
* Domain Path: /test_notes | |
* Version: 0.1 | |
*/ | |
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_Notes; | |
use \Automattic\WooCommerce\Admin\Notes\WC_Admin_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' ); | |
// 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 = WC_Admin_Notes::get_note( (int) $note->note_id ); | |
if ( $note_to_update ) { | |
WC_Admin_Notes::update_note( $note_to_update, array( 'is_deleted' => 0 ) ); | |
} | |
} | |
} | |
// 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 ) ) { | |
$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(); | |
} | |
} | |
} | |
} | |
function get_test_notes() { | |
$id = rand(1, 5000); | |
$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'; | |
$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 ) | |
), | |
), | |
); | |
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-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 ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment