2020-03-31 12:59:48 +00:00
< ? php
/**
* WooCommerce Admin WooCommerce Payments Note Provider .
*
* Adds a note to the merchant ' s inbox showing the benefits of the WooCommerce Payments .
*/
namespace Automattic\WooCommerce\Admin\Notes ;
defined ( 'ABSPATH' ) || exit ;
/**
2020-09-28 04:35:10 +00:00
* WooCommerce_Payments
2020-03-31 12:59:48 +00:00
*/
2020-09-28 04:35:10 +00:00
class WooCommerce_Payments {
2020-03-31 12:59:48 +00:00
/**
* Note traits .
*/
use NoteTraits ;
/**
* Name of the note for use in the database .
*/
const NOTE_NAME = 'wc-admin-woocommerce-payments' ;
/**
* Name of the note for use in the database .
*/
const PLUGIN_SLUG = 'woocommerce-payments' ;
/**
* Name of the note for use in the database .
*/
const PLUGIN_FILE = 'woocommerce-payments/woocommerce-payments.php' ;
/**
* Attach hooks .
*/
public function __construct () {
2020-10-21 18:57:16 +00:00
add_action ( 'init' , array ( $this , 'install_on_action' ) );
2020-03-31 12:59:48 +00:00
add_action ( 'wc-admin-woocommerce-payments_add_note' , array ( $this , 'add_note' ) );
}
/**
* Maybe add a note on WooCommerce Payments for US based sites older than a week without the plugin installed .
*/
public static function possibly_add_note () {
if ( ! self :: wc_admin_active_for ( WEEK_IN_SECONDS ) || 'US' !== WC () -> countries -> get_base_country () ) {
return ;
}
$data_store = \WC_Data_Store :: load ( 'admin-note' );
2020-05-22 13:48:40 +00:00
// We already have this note? Then mark the note as actioned.
2020-03-31 12:59:48 +00:00
$note_ids = $data_store -> get_notes_with_name ( self :: NOTE_NAME );
if ( ! empty ( $note_ids ) ) {
$note_id = array_pop ( $note_ids );
2020-09-28 04:35:10 +00:00
$note = Notes :: get_note ( $note_id );
2020-07-20 18:28:00 +00:00
if ( false === $note ) {
return ;
}
2020-03-31 12:59:48 +00:00
// If the WooCommerce Payments plugin was installed after the note was created, make sure it's marked as actioned.
2020-09-28 04:35:10 +00:00
if ( self :: is_installed () && Note :: E_WC_ADMIN_NOTE_ACTIONED !== $note -> get_status () ) {
$note -> set_status ( Note :: E_WC_ADMIN_NOTE_ACTIONED );
2020-03-31 12:59:48 +00:00
$note -> save ();
}
return ;
}
$current_date = new \DateTime ();
$publish_date = new \DateTime ( '2020-04-14' );
if ( $current_date >= $publish_date ) {
2020-05-22 13:48:40 +00:00
$note = self :: get_note ();
if ( self :: can_be_added () ) {
$note -> save ();
}
return ;
2020-03-31 12:59:48 +00:00
} else {
$hook_name = sprintf ( '%s_add_note' , self :: NOTE_NAME );
if ( ! WC () -> queue () -> get_next ( $hook_name ) ) {
WC () -> queue () -> schedule_single ( $publish_date -> getTimestamp (), $hook_name );
}
}
}
/**
* Add a note about WooCommerce Payments .
2020-09-28 04:35:10 +00:00
*
* @ return Note
2020-03-31 12:59:48 +00:00
*/
2020-05-22 13:48:40 +00:00
public static function get_note () {
2020-09-28 04:35:10 +00:00
$note = new Note ();
2020-03-31 12:59:48 +00:00
$note -> set_title ( __ ( 'Try the new way to get paid' , 'woocommerce-admin' ) );
2020-10-21 18:57:16 +00:00
$note -> set_content (
__ ( 'Securely accept credit and debit cards on your site. Manage transactions without leaving your WordPress dashboard. Only with <strong>WooCommerce Payments</strong>.' , 'woocommerce-admin' ) .
'<br><br>' .
sprintf (
/* translators: 1: opening link tag, 2: closing tag */
__ ( 'By clicking "Get started", you agree to our %1$sTerms of Service%2$s' , 'woocommerce-admin' ),
'<a href="https://wordpress.com/tos/" target="_blank">' ,
'</a>'
)
);
2020-03-31 12:59:48 +00:00
$note -> set_content_data ( ( object ) array () );
2020-09-28 04:35:10 +00:00
$note -> set_type ( Note :: E_WC_ADMIN_NOTE_MARKETING );
2020-03-31 12:59:48 +00:00
$note -> set_name ( self :: NOTE_NAME );
$note -> set_source ( 'woocommerce-admin' );
2020-09-28 04:35:10 +00:00
$note -> add_action ( 'learn-more' , __ ( 'Learn more' , 'woocommerce-admin' ), 'https://woocommerce.com/payments/' , Note :: E_WC_ADMIN_NOTE_UNACTIONED );
2020-10-21 18:57:16 +00:00
$note -> add_action ( 'get-started' , __ ( 'Get started' , 'woocommerce-admin' ), wc_admin_url ( '&action=setup-woocommerce-payments' ), Note :: E_WC_ADMIN_NOTE_ACTIONED , true );
2020-03-31 12:59:48 +00:00
// Create the note as "actioned" if the plugin is already installed.
if ( self :: is_installed () ) {
2020-09-28 04:35:10 +00:00
$note -> set_status ( Note :: E_WC_ADMIN_NOTE_ACTIONED );
2020-03-31 12:59:48 +00:00
}
2020-05-22 13:48:40 +00:00
return $note ;
2020-03-31 12:59:48 +00:00
}
/**
* Check if the WooCommerce Payments plugin is active or installed .
*/
protected static function is_installed () {
if ( defined ( 'WC_Payments' ) ) {
return true ;
}
include_once ABSPATH . '/wp-admin/includes/plugin.php' ;
return 0 === validate_plugin ( self :: PLUGIN_FILE );
}
/**
2020-10-21 18:57:16 +00:00
* Install and activate WooCommerce Payments .
2020-03-31 12:59:48 +00:00
*
2020-10-21 18:57:16 +00:00
* @ return boolean Whether the plugin was successfully activated .
2020-03-31 12:59:48 +00:00
*/
2020-10-21 18:57:16 +00:00
private function install_and_activate_wcpay () {
$install_request = array ( 'plugins' => self :: PLUGIN_SLUG );
$installer = new \Automattic\WooCommerce\Admin\API\Plugins ();
$result = $installer -> install_plugins ( $install_request );
if ( is_wp_error ( $result ) ) {
return false ;
}
2020-03-31 12:59:48 +00:00
2020-10-21 18:57:16 +00:00
$activate_request = array ( 'plugins' => self :: PLUGIN_SLUG );
$result = $installer -> activate_plugins ( $activate_request );
if ( is_wp_error ( $result ) ) {
return false ;
}
2020-03-31 12:59:48 +00:00
2020-10-21 18:57:16 +00:00
return true ;
}
/**
* Install & activate WooCommerce Payments plugin , and redirect to setup .
*/
public function install_on_action () {
// TODO: Need to validate this request more strictly since we're taking install actions directly?
/* phpcs:disable WordPress.Security.NonceVerification */
if (
! isset ( $_GET [ 'page' ] ) ||
'wc-admin' !== $_GET [ 'page' ] ||
! isset ( $_GET [ 'action' ] ) ||
'setup-woocommerce-payments' !== $_GET [ 'action' ]
) {
return ;
}
/* phpcs:enable */
if ( ! current_user_can ( 'install_plugins' ) ) {
return ;
2020-03-31 12:59:48 +00:00
}
2020-10-21 18:57:16 +00:00
$this -> install_and_activate_wcpay ();
// WooCommerce Payments is installed at this point, so link straight into the onboarding flow.
$connect_url = add_query_arg (
array (
'wcpay-connect' => '1' ,
'_wpnonce' => wp_create_nonce ( 'wcpay-connect' ),
),
admin_url ()
);
wp_safe_redirect ( $connect_url );
exit ;
2020-03-31 12:59:48 +00:00
}
}