| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The file that defines the core plugin class |
| 5 |
* |
| 6 |
* A class definition that includes attributes and functions used across both the |
| 7 |
* public-facing side of the site and the admin area. |
| 8 |
* |
| 9 |
* @link https://route.com/ |
| 10 |
* @since 1.0.4 |
| 11 |
* |
| 12 |
* @package Routeapp |
| 13 |
* @subpackage Routeapp/admin |
| 14 |
*/ |
| 15 |
|
| 16 |
class Routeapp_Notice { |
| 17 |
|
| 18 |
public function __construct( ) { |
| 19 |
add_action( 'admin_notices', array( $this, 'routeapp_attention_required' ) ); |
| 20 |
add_action('network_admin_notices', array( $this, 'routeapp_attention_required' ) ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Add admin notice messages to Woocommerce |
| 25 |
* @since 1.0.4 |
| 26 |
* @param string $message Message that would be output |
| 27 |
* @param string $notification_type (success, warning, danger, info) |
| 28 |
* @param void |
| 29 |
*/ |
| 30 |
public function add_notice($message, $notification_type) { |
| 31 |
echo sprintf('<div class="notice %s is-dismissible"><p><span class="dashicons-before dashicons-arkcommerce" style="vertical-align:middle;"> </span> %s</p></div>', $this->get_notice_class($notification_type), $message); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Get notice class name |
| 36 |
* @since 1.0.4 |
| 37 |
* @param string $notification_type (success, warning, danger, info) |
| 38 |
* @param string $className |
| 39 |
*/ |
| 40 |
public function get_notice_class($notification_type = 'success') { |
| 41 |
return "notice-{$notification_type}"; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Countdown + redirect markup (paired with routeapp-admin.js .routeapp-redirect-countdown). |
| 46 |
* |
| 47 |
* @param string $redirect_url Full URL. |
| 48 |
* @param string $mode "always" = ignore route_installed cookie (ownership / conflict flows). "once" = legacy cookie so we do not loop every visit. |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
private function redirect_countdown_html( $redirect_url, $mode = 'always' ) { |
| 52 |
$url = esc_url( $redirect_url ); |
| 53 |
$countdown = '<span class="routeapp-redirect-countdown" data-countdown-mode="' . esc_attr( $mode ) . '" data-seconds="5" data-redirect-src="' . $url . '"><span class="routeapp-redirect-countdown__num">5</span> ' . esc_html__( 'seconds', 'routeapp' ) . '</span>'; |
| 54 |
$manual = ' <span class="routeapp-redirect-manual"> — <a href="' . $url . '" class="routeapp-redirect-manual-link" target="_blank" rel="noopener noreferrer">' . esc_html__( 'Open manually', 'routeapp' ) . '</a></span>'; |
| 55 |
|
| 56 |
return $countdown . $manual; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Import all admin notices message |
| 61 |
* @since 1.0.4 |
| 62 |
* @param void |
| 63 |
*/ |
| 64 |
public function routeapp_attention_required() |
| 65 |
{ |
| 66 |
//$this->billing_attention_required(); //MSS-5023: disabling this alert until we get a new FR to check the billing info. |
| 67 |
$this->user_attention_required(); |
| 68 |
$this->merchant_token_recovery_attention_required(); |
| 69 |
$this->merchant_attention_required(); |
| 70 |
$this->redirect_attention_required(); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Import admin notice verification to Billing Info |
| 75 |
* @since 1.0.4 |
| 76 |
* @param void |
| 77 |
*/ |
| 78 |
// public function billing_attention_required() //MSS-5023: commented this alert until we get a new FR to check the billing info. |
| 79 |
// { |
| 80 |
// $billingResponse = Routeapp_API_Client::getInstance()->get_billing(); |
| 81 |
// |
| 82 |
// if (!is_wp_error($billingResponse)) { |
| 83 |
// $body = json_decode($billingResponse['body']); |
| 84 |
// |
| 85 |
// if (isset($body) && is_countable($body)) { |
| 86 |
// if (count($body) === 0) { |
| 87 |
// $this->add_notice("Route: You haven't yet completed the Billing Info step. Click <a href='http://dashboard.route.com/admin/preferences' target='_blank'>here</a> to complete.", "warning"); |
| 88 |
// } |
| 89 |
// } |
| 90 |
// } |
| 91 |
// } |
| 92 |
|
| 93 |
/** |
| 94 |
* Import admin notice if user creation has failed |
| 95 |
* @since 1.0.4 |
| 96 |
* @param void |
| 97 |
*/ |
| 98 |
public function user_attention_required() |
| 99 |
{ |
| 100 |
$routeapp_public = $this->get_route_public_instance(); |
| 101 |
if ($routeapp_public->has_user_creation_conflicted()) { |
| 102 |
$dashboard_url = Route_Setup::get_dashboard_ownership_login_url(); |
| 103 |
$failed_step = get_option( Route_Setup::FAILED_REGISTRATION ); |
| 104 |
$auto_done = get_option( Route_Setup::USER_CONFLICT_AUTO_REDIRECT_DONE_OPTION ); |
| 105 |
|
| 106 |
if ( $auto_done ) { |
| 107 |
$link = sprintf( |
| 108 |
'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>', |
| 109 |
esc_url( $dashboard_url ), |
| 110 |
esc_html__( 'Open the Route Dashboard', 'routeapp' ) |
| 111 |
); |
| 112 |
if ( Route_Setup::FAILED_REGISTRATION_STEP_USER_LOGIN_FAILED === $failed_step ) { |
| 113 |
$message = sprintf( |
| 114 |
/* translators: %s: "Open the Route Dashboard" link HTML */ |
| 115 |
__( 'Route could not sign you in. %s to sign in and link this store.', 'routeapp' ), |
| 116 |
$link |
| 117 |
); |
| 118 |
} else { |
| 119 |
$message = sprintf( |
| 120 |
/* translators: %s: "Open the Route Dashboard" link HTML */ |
| 121 |
__( 'This email is already registered with Route. %s to sign in and link this store.', 'routeapp' ), |
| 122 |
$link |
| 123 |
); |
| 124 |
} |
| 125 |
$this->add_notice( wp_kses_post( $message ), 'warning' ); |
| 126 |
} else { |
| 127 |
update_option( Route_Setup::USER_CONFLICT_AUTO_REDIRECT_DONE_OPTION, '1', false ); |
| 128 |
if ( Route_Setup::FAILED_REGISTRATION_STEP_USER_LOGIN_FAILED === $failed_step ) { |
| 129 |
$intro = esc_html__( 'Route could not sign you in. We\'re redirecting you to the Route Dashboard to sign in and link this store.', 'routeapp' ); |
| 130 |
} else { |
| 131 |
$intro = esc_html__( 'This email is already registered with Route. We\'re redirecting you to the Route Dashboard to sign in and link this store.', 'routeapp' ); |
| 132 |
} |
| 133 |
$message = $intro . ' ' . $this->redirect_countdown_html( $dashboard_url, 'always' ); |
| 134 |
$this->add_notice( $message, 'warning' ); |
| 135 |
} |
| 136 |
} |
| 137 |
if ($routeapp_public->has_user_creation_failed()) { |
| 138 |
$this->add_notice("We couldn't create your account, please contact our support or <a href=\"/wp-json/route/recreate_user\"> try again </a></span>.", "warning"); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* When public/secret tokens were cleared but onboarding state remains — prompt Dashboard OTP or password login to refresh keys. |
| 144 |
*/ |
| 145 |
public function merchant_token_recovery_attention_required() |
| 146 |
{ |
| 147 |
$routeapp_public = $this->get_route_public_instance(); |
| 148 |
if ( ! $routeapp_public || $routeapp_public->has_user_creation_conflicted() ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
if ( ! Route_Setup::should_prompt_merchant_token_recovery() ) { |
| 152 |
return; |
| 153 |
} |
| 154 |
$dashboard_url = Route_Setup::get_dashboard_ownership_login_url(); |
| 155 |
$this->add_notice( |
| 156 |
'Route configurations missing from this site. ' . |
| 157 |
sprintf( |
| 158 |
'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>', |
| 159 |
esc_url( $dashboard_url ), |
| 160 |
esc_html__( 'Open Route Dashboard to reconnect', 'routeapp' ) |
| 161 |
), |
| 162 |
'warning' |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* @return WP_User |
| 168 |
*/ |
| 169 |
private function get_current_user() |
| 170 |
{ |
| 171 |
return wp_get_current_user(); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* @return string |
| 176 |
*/ |
| 177 |
private function get_current_email() |
| 178 |
{ |
| 179 |
return get_bloginfo('admin_email'); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Import admin notice if merchant creation has failed |
| 184 |
* @since 1.0.4 |
| 185 |
* @param void |
| 186 |
*/ |
| 187 |
public function merchant_attention_required() |
| 188 |
{ |
| 189 |
$routeapp_public = $this->get_route_public_instance(); |
| 190 |
if ($routeapp_public->has_merchant_creation_failed()) { |
| 191 |
$this->add_notice("Route merchant creation has failed. Please click <a href=\"/wp-json/route/recreate_merchant\"> here </a> to retry</span>.", "warning"); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Import admin notice verification to Billing Info |
| 197 |
* @since 1.0.4 |
| 198 |
* @param void |
| 199 |
*/ |
| 200 |
public function redirect_attention_required() |
| 201 |
{ |
| 202 |
$routeapp_public = $this->get_route_public_instance(); |
| 203 |
if ($routeapp_public->successful_registration() && !$routeapp_public->was_redirected()) { |
| 204 |
$this->add_notice( |
| 205 |
"You're almost there! We're redirecting you to Route website to finalize your account setup... " . |
| 206 |
$this->redirect_countdown_html( Route_Setup::get_route_redirect(), 'once' ), |
| 207 |
'success' |
| 208 |
); |
| 209 |
$routeapp_public->set_as_redirected(); |
| 210 |
} |
| 211 |
if ($routeapp_public->has_merchant_creation_conflicted()) { |
| 212 |
$dashboard_url = Route_Setup::get_dashboard_ownership_login_url(); |
| 213 |
$link = sprintf( |
| 214 |
'<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>', |
| 215 |
esc_url( $dashboard_url ), |
| 216 |
esc_html__( 'Sign in on the Route Dashboard', 'routeapp' ) |
| 217 |
); |
| 218 |
$message = sprintf( |
| 219 |
/* translators: %s: "Sign in on the Route Dashboard" link HTML */ |
| 220 |
__( 'Your merchant account already exists. %s to link this store.', 'routeapp' ), |
| 221 |
$link |
| 222 |
); |
| 223 |
$this->add_notice( wp_kses_post( $message ), 'warning' ); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
private function get_route_public_instance() |
| 228 |
{ |
| 229 |
global $routeapp_public; |
| 230 |
return $routeapp_public; |
| 231 |
} |
| 232 |
|
| 233 |
} |
| 234 |
|