CustomizeStoreWithBlocks.php
2 years ago
CustomizingProductCatalog.php
1 year ago
EUVATNumber.php
2 years ago
EditProductsOnTheMove.php
2 years ago
EmailImprovements.php
1 year ago
FirstProduct.php
1 year ago
GivingFeedbackNotes.php
3 years ago
InstallJPAndWCSPlugins.php
4 weeks ago
LaunchChecklist.php
2 years ago
MagentoMigration.php
2 years ago
ManageOrdersOnTheGo.php
2 years ago
MarketingJetpack.php
4 weeks ago
MigrateFromShopify.php
2 years ago
MobileApp.php
2 years ago
NewSalesRecord.php
3 years ago
NoteActionForbiddenException.php
4 weeks ago
OnboardingPayments.php
2 years ago
OnlineClothingStore.php
2 years ago
OrderMilestones.php
2 years ago
PaymentsMoreInfoNeeded.php
5 months ago
PaymentsRemindMeLater.php
5 months ago
PerformanceOnMobile.php
2 years ago
PersonalizeStore.php
3 years ago
RealTimeOrderAlerts.php
2 years ago
ScheduledUpdatesPromotion.php
5 months ago
SellingOnlineCourses.php
2 years ago
TrackingOptIn.php
1 year ago
UnsecuredReportFiles.php
2 years ago
WooCommercePayments.php
2 years ago
WooCommerceSubscriptions.php
2 years ago
WooSubscriptionsNotes.php
1 year ago
WooCommercePayments.php
225 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WooCommerce Admin WooCommerce Payments Note Provider. |
| 4 | * |
| 5 | * Adds a note to the merchant's inbox showing the benefits of the WooCommerce Payments. |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Admin\Notes; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use Automattic\WooCommerce\Admin\Notes\Note; |
| 13 | use Automattic\WooCommerce\Admin\Notes\Notes; |
| 14 | use Automattic\WooCommerce\Admin\Notes\NoteTraits; |
| 15 | |
| 16 | /** |
| 17 | * WooCommerce_Payments |
| 18 | */ |
| 19 | class WooCommercePayments { |
| 20 | /** |
| 21 | * Note traits. |
| 22 | */ |
| 23 | use NoteTraits; |
| 24 | |
| 25 | /** |
| 26 | * Name of the note for use in the database. |
| 27 | */ |
| 28 | const NOTE_NAME = 'wc-admin-woocommerce-payments'; |
| 29 | |
| 30 | /** |
| 31 | * Name of the note for use in the database. |
| 32 | */ |
| 33 | const PLUGIN_SLUG = 'woocommerce-payments'; |
| 34 | |
| 35 | /** |
| 36 | * Name of the note for use in the database. |
| 37 | */ |
| 38 | const PLUGIN_FILE = 'woocommerce-payments/woocommerce-payments.php'; |
| 39 | |
| 40 | /** |
| 41 | * Attach hooks. |
| 42 | */ |
| 43 | public function __construct() { |
| 44 | add_action( 'init', array( $this, 'install_on_action' ) ); |
| 45 | add_action( 'wc-admin-woocommerce-payments_add_note', array( $this, 'add_note' ) ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Maybe add a note on WooCommerce Payments for US based sites older than a week without the plugin installed. |
| 50 | */ |
| 51 | public static function possibly_add_note() { |
| 52 | if ( ! self::is_wc_admin_active_in_date_range( 'week-1-4' ) || 'US' !== WC()->countries->get_base_country() ) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | $data_store = Notes::load_data_store(); |
| 57 | |
| 58 | // We already have this note? Then mark the note as actioned. |
| 59 | $note_ids = $data_store->get_notes_with_name( self::NOTE_NAME ); |
| 60 | if ( ! empty( $note_ids ) ) { |
| 61 | |
| 62 | $note_id = array_pop( $note_ids ); |
| 63 | $note = Notes::get_note( $note_id ); |
| 64 | if ( false === $note ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | // If the WooCommerce Payments plugin was installed after the note was created, make sure it's marked as actioned. |
| 69 | if ( self::is_installed() && Note::E_WC_ADMIN_NOTE_ACTIONED !== $note->get_status() ) { |
| 70 | $note->set_status( Note::E_WC_ADMIN_NOTE_ACTIONED ); |
| 71 | $note->save(); |
| 72 | } |
| 73 | |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | $current_date = new \DateTime(); |
| 78 | $publish_date = new \DateTime( '2020-04-14' ); |
| 79 | |
| 80 | if ( $current_date >= $publish_date ) { |
| 81 | |
| 82 | $note = self::get_note(); |
| 83 | if ( self::can_be_added() ) { |
| 84 | $note->save(); |
| 85 | } |
| 86 | |
| 87 | return; |
| 88 | |
| 89 | } else { |
| 90 | |
| 91 | $hook_name = sprintf( '%s_add_note', self::NOTE_NAME ); |
| 92 | |
| 93 | if ( ! WC()->queue()->get_next( $hook_name ) ) { |
| 94 | WC()->queue()->schedule_single( $publish_date->getTimestamp(), $hook_name ); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Add a note about WooCommerce Payments. |
| 101 | * |
| 102 | * @return Note |
| 103 | */ |
| 104 | public static function get_note() { |
| 105 | $note = new Note(); |
| 106 | $note->set_title( __( 'Try the new way to get paid', 'woocommerce' ) ); |
| 107 | $note->set_content( |
| 108 | __( 'Securely accept credit and debit cards on your site. Manage transactions without leaving your WordPress dashboard. Only with <strong>WooPayments</strong>.', 'woocommerce' ) . |
| 109 | '<br><br>' . |
| 110 | sprintf( |
| 111 | /* translators: 1: opening link tag, 2: closing tag */ |
| 112 | __( 'By clicking "Get started", you agree to our %1$sTerms of Service%2$s', 'woocommerce' ), |
| 113 | '<a href="https://wordpress.com/tos/" target="_blank">', |
| 114 | '</a>' |
| 115 | ) |
| 116 | ); |
| 117 | $note->set_content_data( (object) array() ); |
| 118 | $note->set_type( Note::E_WC_ADMIN_NOTE_MARKETING ); |
| 119 | $note->set_name( self::NOTE_NAME ); |
| 120 | $note->set_source( 'woocommerce-admin' ); |
| 121 | $note->add_action( 'learn-more', __( 'Learn more', 'woocommerce' ), 'https://woocommerce.com/payments/?utm_medium=product', Note::E_WC_ADMIN_NOTE_UNACTIONED ); |
| 122 | $note->add_action( 'get-started', __( 'Get started', 'woocommerce' ), wc_admin_url( '&action=setup-woocommerce-payments' ), Note::E_WC_ADMIN_NOTE_ACTIONED, true ); |
| 123 | $note->add_nonce_to_action( 'get-started', 'setup-woocommerce-payments', '' ); |
| 124 | |
| 125 | // Create the note as "actioned" if the plugin is already installed. |
| 126 | if ( self::is_installed() ) { |
| 127 | $note->set_status( Note::E_WC_ADMIN_NOTE_ACTIONED ); |
| 128 | } |
| 129 | return $note; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * Check if the WooCommerce Payments plugin is active or installed. |
| 135 | */ |
| 136 | protected static function is_installed() { |
| 137 | if ( defined( 'WC_Payments' ) ) { |
| 138 | return true; |
| 139 | } |
| 140 | include_once ABSPATH . '/wp-admin/includes/plugin.php'; |
| 141 | return 0 === validate_plugin( self::PLUGIN_FILE ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Install and activate WooCommerce Payments. |
| 146 | * |
| 147 | * @return boolean Whether the plugin was successfully activated. |
| 148 | */ |
| 149 | private function install_and_activate_wcpay() { |
| 150 | $install_request = array( 'plugins' => self::PLUGIN_SLUG ); |
| 151 | $installer = new \Automattic\WooCommerce\Admin\API\Plugins(); |
| 152 | $result = $installer->install_plugins( $install_request ); |
| 153 | if ( is_wp_error( $result ) ) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | wc_admin_record_tracks_event( 'woocommerce_payments_install', array( 'context' => 'inbox' ) ); |
| 158 | |
| 159 | $activate_request = array( 'plugins' => self::PLUGIN_SLUG ); |
| 160 | $result = $installer->activate_plugins( $activate_request ); |
| 161 | if ( is_wp_error( $result ) ) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Install & activate WooCommerce Payments plugin, and redirect to setup. |
| 170 | */ |
| 171 | public function install_on_action() { |
| 172 | // TODO: Need to validate this request more strictly since we're taking install actions directly? |
| 173 | if ( |
| 174 | ! isset( $_GET['page'] ) || |
| 175 | 'wc-admin' !== $_GET['page'] || |
| 176 | ! isset( $_GET['action'] ) || |
| 177 | 'setup-woocommerce-payments' !== $_GET['action'] |
| 178 | ) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | $data_store = Notes::load_data_store(); |
| 183 | |
| 184 | // We already have this note? Then mark the note as actioned. |
| 185 | $note_ids = $data_store->get_notes_with_name( self::NOTE_NAME ); |
| 186 | if ( empty( $note_ids ) ) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | $note_id = array_pop( $note_ids ); |
| 191 | $note = Notes::get_note( $note_id ); |
| 192 | if ( false === $note ) { |
| 193 | return; |
| 194 | } |
| 195 | $action = $note->get_action( 'get-started' ); |
| 196 | if ( ! $action || |
| 197 | ( isset( $action->nonce_action ) && |
| 198 | ( |
| 199 | empty( $_GET['_wpnonce'] ) || |
| 200 | ! wp_verify_nonce( wp_unslash( $_GET['_wpnonce'] ), $action->nonce_action ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput |
| 201 | ) |
| 202 | ) |
| 203 | ) { |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | if ( ! current_user_can( 'install_plugins' ) ) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | $this->install_and_activate_wcpay(); |
| 212 | |
| 213 | // WooCommerce Payments is installed at this point, so link straight into the onboarding flow. |
| 214 | $connect_url = add_query_arg( |
| 215 | array( |
| 216 | 'wcpay-connect' => '1', |
| 217 | '_wpnonce' => wp_create_nonce( 'wcpay-connect' ), |
| 218 | ), |
| 219 | admin_url() |
| 220 | ); |
| 221 | wp_safe_redirect( $connect_url ); |
| 222 | exit; |
| 223 | } |
| 224 | } |
| 225 |