Add WC Admin inbox note when page is created

This commit is contained in:
roykho 2021-06-30 12:38:39 -07:00
parent 946aeb8d15
commit ade4478912
No known key found for this signature in database
GPG Key ID: 7B36C0EA25795714
4 changed files with 12688 additions and 2 deletions

View File

@ -0,0 +1,71 @@
<?php
/**
* Refund and Returns Policy Page Note Provider.
*
* Adds notes to the merchant's inbox concerning the created page.
*
* @package WooCommerce
*/
defined( 'ABSPATH' ) || exit;
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Admin\Notes\Note;
/**
* WC_Notes_Refund_Returns.
*/
class WC_Notes_Refund_Returns {
/**
* Name of the note for use in the database.
*/
const NOTE_NAME = 'wc-refund-returns-page';
/**
* Maybe add a note to the inbox.
*
* @param int $page_id The ID of the page.
*/
public static function possibly_add_note( $page_id ) {
$data_store = \WC_Data_Store::load( 'admin-note' );
// Do we already have this note?
$note_id = $data_store->get_notes_with_name( self::NOTE_NAME );
if ( ! empty( $note_id ) ) {
$note = new Note( $note_id );
if ( false !== $note || $note::E_WC_ADMIN_NOTE_ACTIONED === $note->get_status() ) {
// note actioned -> don't show it.
return;
}
}
// Add note.
$note = self::get_note( $page_id );
$note->save();
}
/**
* Get the note.
*
* @param int $page_id The ID of the page.
* @return object $note The note object.
*/
public static function get_note( $page_id ) {
$note = new Note();
$note->set_title( __( 'Setup a Refund and Returns Policy page to boost your store\'s creditibility.', 'woocommerce' ) );
$note->set_content( __( 'We have created a sample draft Refund and Returns Policy page for you. Please have a look and update it to fit your store.', 'woocommerce' ) );
$note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL );
$note->set_name( self::NOTE_NAME );
$note->set_content_data( (object) array() );
$note->set_source( 'woocommerce-core' );
$note->add_action(
'notify-refund-returns-page',
__( 'Edit page', 'woocommerce' ),
admin_url( sprintf( 'post.php?post=%d&action=edit', (int) $page_id ) ),
);
return $note;
}
}

View File

@ -126,6 +126,8 @@ function wc_create_page( $slug, $option = '', $page_title = '', $page_content =
'comment_status' => 'closed',
);
$page_id = wp_insert_post( $page_data );
do_action( 'woocommerce_page_created', $page_id, $page_data );
}
if ( $option ) {

View File

@ -176,6 +176,7 @@ class WC_Install {
add_action( 'admin_init', array( __CLASS__, 'wc_admin_db_update_notice' ) );
add_action( 'woocommerce_run_update_callback', array( __CLASS__, 'run_update_callback' ) );
add_action( 'admin_init', array( __CLASS__, 'install_actions' ) );
add_action( 'woocommerce_page_created', array( __CLASS__, 'add_admin_note_after_page_created' ), 10, 2 );
add_filter( 'plugin_action_links_' . WC_PLUGIN_BASENAME, array( __CLASS__, 'plugin_action_links' ) );
add_filter( 'plugin_row_meta', array( __CLASS__, 'plugin_row_meta' ), 10, 2 );
add_filter( 'wpmu_drop_tables', array( __CLASS__, 'wpmu_drop_tables' ) );
@ -583,8 +584,8 @@ class WC_Install {
'title' => _x( 'My account', 'Page title', 'woocommerce' ),
'content' => '<!-- wp:shortcode -->[' . apply_filters( 'woocommerce_my_account_shortcode_tag', 'woocommerce_my_account' ) . ']<!-- /wp:shortcode -->',
),
'refund_return' => array(
'name' => _x( 'refund_return', 'Page slug', 'woocommerce' ),
'refund_returns' => array(
'name' => _x( 'refund_returns', 'Page slug', 'woocommerce' ),
'title' => _x( 'Refund and Returns Policy', 'Page title', 'woocommerce' ),
'content' => self::get_refunds_return_policy_page_content(),
'post_status' => 'draft',
@ -1816,6 +1817,26 @@ CREATE TABLE {$wpdb->prefix}wc_reserved_stock (
<!-- /wp:paragraph -->
EOT;
}
/**
* Adds an admin inbox note after a page has been created to notify
* user. For example to take action to edit the page such as the
* Refund and returns page.
*
* @since 5.6.0
* @param int $page_id ID of the page.
* @param array $page_data The data of the page created.
* @return void
*/
public static function add_admin_note_after_page_created( $page_id, $page_data ) {
if ( ! WC()->is_wc_admin_active() ) {
return;
}
if ( 'refund_returns' === $page_data['post_name'] ) {
WC_Notes_Refund_Returns::possibly_add_note( $page_id );
}
}
}
WC_Install::init();

12592
yarn.lock Normal file

File diff suppressed because it is too large Load Diff