Skip to content

Instantly share code, notes, and snippets.

@bekarice
Created February 12, 2019 01:40
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bekarice/7785293fb60d7d5297a245b1c1271272 to your computer and use it in GitHub Desktop.
Save bekarice/7785293fb60d7d5297a245b1c1271272 to your computer and use it in GitHub Desktop.
Uses the "demo store" notice to show a switch back link for WC customer accounts with User Switching: http://cloud.skyver.ge/c50cbcd4c5e7
<?php
/**
* Plugin Name: User Switching Notice for WooCommerce
* Plugin URI: https://gist.github.com/bekarice/7785293fb60d7d5297a245b1c1271272
* Description: Adds a frontend notice to switch back to your user on WooCommerce sites with User Switching.
* Author: SkyVerge
* Author URI: http://www.skyverge.com/
* Version: 1.0.0
* Text Domain: user-switching-notice-for-woocommerce
*
* Copyright: (c) 2019 SkyVerge, Inc. (info@skyverge.com)
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* @package User_Switching_Notice_For_WC
* @author SkyVerge
* @category Admin
* @copyright Copyright (c) 2019, SkyVerge, Inc.
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
*
*/
defined( 'ABSPATH' ) or exit;
/**
* Plugin Description
*
* Uses the storefront demo notice to show a "switch back" link for WooCommerce, as the
* WP admin bar will be disabled.
*/
// fire it up!
add_action( 'plugins_loaded', 'user_switching_notice_for_wc', 15 );
/**
* Class User_Switching_Notice_For_WC
*
* @since 1.0.0
*/
class User_Switching_Notice_For_WC {
/** @var bool|WP_User switched user */
public $old_user = false;
/** @var User_Switching_Notice_For_WC single instance of this plugin */
protected static $instance;
/**
* \User_Switching_Notice_For_WC constructor.
*
* @since 1.0.0
*/
public function __construct() {
// load translations
add_action( 'init', array( $this, 'load_translation' ) );
$this->set_old_user();
if ( $this->old_user ) {
// remove previously set store notice cookies
// the cookie name won't change, WC core doesn't set it correctly by notice ID as of v3.5.4
setcookie( 'store_notice', null, -1, '/' );
add_filter( 'pre_option_woocommerce_demo_store', array( $this, 'filter_notice_option' ) );
add_action( 'woocommerce_demo_store', array( $this, 'render_switch_notice' ) );
}
}
/**
* Load Translations
*
* @since 1.0.0
*/
public function load_translation() {
// localization
load_plugin_textdomain( 'user-switching-notice-for-woocommerce', false, dirname( plugin_basename( __FILE__ ) ) . '/i18n/languages' );
}
/** Plugin methods ***************************************/
/**
* Updates the store demo notice option to show the notice.
*
* @since 1.0.0
*
* @return string option value
*/
public function filter_notice_option() {
return 'yes';
}
/**
* Updates the store demo notice messaging for our switched user.
*
* @since 1.0.0
*
* @param string original message
* @return string new message
*/
public function render_switch_notice( $message ) {
$user_switching = user_switching::get_instance();
$message = sprintf(
__( 'Ready to switch back to %1$s (%2$s)?', 'user-switching' ),
$this->old_user->display_name,
$this->old_user->user_login
);
$url = add_query_arg( array(
'redirect_to' => urlencode( get_admin_url() ),
), $user_switching::switch_back_url( $this->old_user ) );
ob_start();
// notice ID doesn't matter here, but leaving it JIC
?>
<p class="woocommerce-store-notice demo_store" data-notice-id="<?php echo esc_attr( time() ); ?>">
<?php echo esc_html( $message ); ?>
<a href="<?php echo esc_url( $url ); ?>" class="woocommerce-store-notice__dismiss-link">
<?php echo esc_html_e( 'Switch back', 'user-switching' ); ?>
</a>
</p>
<?php
return ob_get_clean();
}
/**
* Set up the old user when switched.
*
* @since 1.0.0
*/
private function set_old_user() {
if ( class_exists( 'user_switching' ) ) {
$user_switching = user_switching::get_instance();
$old_user = $user_switching::get_old_user();
if ( $old_user instanceof WP_User ) {
$this->old_user = $old_user;
}
}
}
/** Helper methods ***************************************/
/**
* Main User_Switching_Notice_For_WC Instance, ensures only one instance is/can be loaded
*
* @since 1.0.0
*
* @see user_switching_notice_for_wc()
* @return User_Switching_Notice_For_WC
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Cloning instances is forbidden due to singleton pattern.
*
* @since 1.0.0
*/
public function __clone() {
/* translators: Placeholders: %s - plugin name */
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'You cannot clone instances of %s.', 'user-switching-notice-for-woocommerce' ), 'User Switching notice for WooCommerce' ), '1.0.0' );
}
/**
* Unserializing instances is forbidden due to singleton pattern.
*
* @since 1.0.0
*/
public function __wakeup() {
/* translators: Placeholders: %s - plugin name */
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'You cannot unserialize instances of %s.', 'user-switching-notice-for-woocommerce' ), 'User Switching notice for WooCommerce' ), '1.0.0' );
}
}
/**
* Returns the One True Instance of User_Switching_Notice_For_WC
*
* @since 1.0.0
* @return User_Switching_Notice_For_WC
*/
function user_switching_notice_for_wc() {
return User_Switching_Notice_For_WC::instance();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment