class-wc-notes-refund-returns.php
125 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Refund and Returns Policy Page Note Provider. |
| 4 | * |
| 5 | * Adds notes to the merchant's inbox concerning the created page. |
| 6 | * |
| 7 | * @package WooCommerce |
| 8 | */ |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\Notes\Note; |
| 13 | |
| 14 | /** |
| 15 | * WC_Notes_Refund_Returns. |
| 16 | */ |
| 17 | class WC_Notes_Refund_Returns { |
| 18 | /** |
| 19 | * Name of the note for use in the database. |
| 20 | */ |
| 21 | const NOTE_NAME = 'wc-refund-returns-page'; |
| 22 | |
| 23 | /** |
| 24 | * Attach hooks. |
| 25 | */ |
| 26 | public static function init() { |
| 27 | add_action( 'woocommerce_newly_installed', array( __CLASS__, 'on_newly_installed' ) ); |
| 28 | add_filter( 'woocommerce_get_note_from_db', array( __CLASS__, 'get_note_from_db' ), 10, 1 ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Add the note when WooCommerce is newly installed. |
| 33 | * |
| 34 | * @since 10.5.0 |
| 35 | * @return void |
| 36 | */ |
| 37 | public static function on_newly_installed() { |
| 38 | $page_id = get_option( 'woocommerce_refund_returns_page_id' ); |
| 39 | if ( $page_id ) { |
| 40 | self::possibly_add_note( $page_id ); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Maybe add a note to the inbox. |
| 46 | * |
| 47 | * @param int $page_id The ID of the page. |
| 48 | */ |
| 49 | public static function possibly_add_note( $page_id ) { |
| 50 | if ( ! WC()->is_wc_admin_active() ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | $data_store = \WC_Data_Store::load( 'admin-note' ); |
| 55 | |
| 56 | // Do we already have this note? |
| 57 | $note_id = $data_store->get_notes_with_name( self::NOTE_NAME ); |
| 58 | |
| 59 | if ( ! empty( $note_id ) ) { |
| 60 | $note = new Note( $note_id ); |
| 61 | |
| 62 | if ( false !== $note || $note::E_WC_ADMIN_NOTE_ACTIONED === $note->get_status() ) { |
| 63 | // note actioned -> don't show it. |
| 64 | return; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Add note. |
| 69 | $note = self::get_note( $page_id ); |
| 70 | $note->save(); |
| 71 | delete_option( 'woocommerce_refund_returns_page_created' ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get the note. |
| 76 | * |
| 77 | * @param int $page_id The ID of the page. |
| 78 | * @return object $note The note object. |
| 79 | */ |
| 80 | public static function get_note( $page_id ) { |
| 81 | $note = new Note(); |
| 82 | $note->set_title( __( 'Setup a Refund and Returns Policy page to boost your store\'s credibility.', 'woocommerce' ) ); |
| 83 | $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' ) ); |
| 84 | $note->set_type( Note::E_WC_ADMIN_NOTE_INFORMATIONAL ); |
| 85 | $note->set_name( self::NOTE_NAME ); |
| 86 | $note->set_content_data( (object) array() ); |
| 87 | $note->set_source( 'woocommerce-core' ); |
| 88 | $note->add_action( |
| 89 | 'notify-refund-returns-page', |
| 90 | __( 'Edit page', 'woocommerce' ), |
| 91 | admin_url( sprintf( 'post.php?post=%d&action=edit', (int) $page_id ) ) |
| 92 | ); |
| 93 | |
| 94 | return $note; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get the note. |
| 99 | * |
| 100 | * @param Note $note_from_db The note object from the database. |
| 101 | * @return Note $note The note object. |
| 102 | */ |
| 103 | public static function get_note_from_db( $note_from_db ) { |
| 104 | if ( ! $note_from_db instanceof Note || get_user_locale() === $note_from_db->get_locale() ) { |
| 105 | return $note_from_db; |
| 106 | } |
| 107 | |
| 108 | if ( self::NOTE_NAME === $note_from_db->get_name() ) { |
| 109 | $note = self::get_note( 0 ); |
| 110 | $note_from_db->set_title( $note->get_title() ); |
| 111 | $note_from_db->set_content( $note->get_content() ); |
| 112 | |
| 113 | $action_from_db = $note_from_db->get_action( 'notify-refund-returns-page' ); |
| 114 | $action_from_class = $note->get_action( 'notify-refund-returns-page' ); |
| 115 | |
| 116 | if ( $action_from_db && $action_from_class ) { |
| 117 | $action_from_db->label = $action_from_class->label; |
| 118 | $note_from_db->set_actions( array( $action_from_db ) ); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | return $note_from_db; |
| 123 | } |
| 124 | } |
| 125 |