woocommerce/plugins/woocommerce-admin/src-internal/Admin/Loader.php

306 lines
8.9 KiB
PHP

<?php
/**
* Register the scripts, styles, and includes needed for pieces of the WooCommerce Admin experience.
* NOTE: DO NOT edit this file in WooCommerce core, this is generated from woocommerce-admin.
*/
namespace Automattic\WooCommerce\Internal\Admin;
use Automattic\WooCommerce\Admin\Features\Features;
use Automattic\WooCommerce\Admin\PageController;
use Automattic\WooCommerce\Admin\PluginsHelper;
use Automattic\WooCommerce\Internal\Admin\Settings;
/**
* Loader Class.
*/
class Loader {
/**
* Class instance.
*
* @var Loader instance
*/
protected static $instance = null;
/**
* An array of classes to load from the includes folder.
*
* @var array
*/
protected static $classes = array();
/**
* WordPress capability required to use analytics features.
*
* @var string
*/
protected static $required_capability = null;
/**
* An array of dependencies that have been preloaded (to avoid duplicates).
*
* @var array
*/
protected $preloaded_dependencies = array(
'script' => array(),
'style' => array(),
);
/**
* Get class instance.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
* Hooks added here should be removed in `wc_admin_initialize` via the feature plugin.
*/
public function __construct() {
Features::get_instance();
WCAdminSharedSettings::get_instance();
Translations::get_instance();
WCAdminUser::get_instance();
Settings::get_instance();
add_filter( 'admin_body_class', array( __CLASS__, 'add_admin_body_classes' ) );
add_filter( 'admin_title', array( __CLASS__, 'update_admin_title' ) );
add_action( 'in_admin_header', array( __CLASS__, 'embed_page_header' ) );
add_action( 'admin_head', array( __CLASS__, 'remove_notices' ) );
add_action( 'admin_head', array( __CLASS__, 'smart_app_banner' ) );
add_action( 'admin_notices', array( __CLASS__, 'inject_before_notices' ), -9999 );
add_action( 'admin_notices', array( __CLASS__, 'inject_after_notices' ), PHP_INT_MAX );
// Added this hook to delete the field woocommerce_onboarding_homepage_post_id when deleting the homepage.
add_action( 'trashed_post', array( __CLASS__, 'delete_homepage' ) );
/*
* Remove the emoji script as it always defaults to replacing emojis with Twemoji images.
* Gutenberg has also disabled emojis. More on that here -> https://github.com/WordPress/gutenberg/pull/6151
*/
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
add_action( 'admin_init', array( __CLASS__, 'is_using_installed_wc_admin_plugin' ) );
}
/**
* Verifies which plugin version is being used. If WooCommerce Admin is installed and activated but not in use
* it will show a warning.
*/
public static function is_using_installed_wc_admin_plugin() {
if ( PluginsHelper::is_plugin_active( 'woocommerce-admin' ) ) {
$path = PluginsHelper::get_plugin_data( 'woocommerce-admin' );
if ( WC_ADMIN_VERSION_NUMBER !== $path['Version'] ) {
add_action(
'admin_notices',
function() {
echo '<div class="error"><p>';
printf(
/* translators: %s: is referring to the plugin's name. */
esc_html__( 'You have the %s plugin activated but it is not being used.', 'woocommerce-admin' ),
'<code>WooCommerce Admin</code>'
);
echo '</p></div>';
}
);
}
}
}
/**
* Returns breadcrumbs for the current page.
*/
private static function get_embed_breadcrumbs() {
return wc_admin_get_breadcrumbs();
}
/**
* Outputs breadcrumbs via PHP for the initial load of an embedded page.
*
* @param array $section Section to create breadcrumb from.
*/
private static function output_heading( $section ) {
echo esc_html( $section );
}
/**
* Set up a div for the header embed to render into.
* The initial contents here are meant as a place loader for when the PHP page initialy loads.
*/
public static function embed_page_header() {
if ( ! PageController::is_admin_page() && ! PageController::is_embed_page() ) {
return;
}
if ( ! PageController::is_embed_page() ) {
return;
}
$sections = self::get_embed_breadcrumbs();
$sections = is_array( $sections ) ? $sections : array( $sections );
?>
<div id="woocommerce-embedded-root" class="is-embed-loading">
<div class="woocommerce-layout">
<div class="woocommerce-layout__header is-embed-loading">
<h1 class="woocommerce-layout__header-heading">
<?php self::output_heading( end( $sections ) ); ?>
</h1>
</div>
</div>
</div>
<?php
}
/**
* Adds body classes to the main wp-admin wrapper, allowing us to better target elements in specific scenarios.
*
* @param string $admin_body_class Body class to add.
*/
public static function add_admin_body_classes( $admin_body_class = '' ) {
if ( ! PageController::is_admin_or_embed_page() ) {
return $admin_body_class;
}
$classes = explode( ' ', trim( $admin_body_class ) );
$classes[] = 'woocommerce-page';
if ( PageController::is_embed_page() ) {
$classes[] = 'woocommerce-embed-page';
}
/**
* Some routes or features like onboarding hide the wp-admin navigation and masterbar.
* Setting `woocommerce_admin_is_loading` to true allows us to premeptively hide these
* elements while the JS app loads.
* This class needs to be removed by those feature components (like <ProfileWizard />).
*
* @param bool $is_loading If WooCommerce Admin is loading a fullscreen view.
*/
$is_loading = apply_filters( 'woocommerce_admin_is_loading', false );
if ( PageController::is_admin_page() && $is_loading ) {
$classes[] = 'woocommerce-admin-is-loading';
}
$admin_body_class = implode( ' ', array_unique( $classes ) );
return " $admin_body_class ";
}
/**
* Adds an iOS "Smart App Banner" for display on iOS Safari.
* See https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html
*/
public static function smart_app_banner() {
if ( PageController::is_admin_or_embed_page() ) {
echo "
<meta name='apple-itunes-app' content='app-id=1389130815'>
";
}
}
/**
* Removes notices that should not be displayed on WC Admin pages.
*/
public static function remove_notices() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
// Hello Dolly.
if ( function_exists( 'hello_dolly' ) ) {
remove_action( 'admin_notices', 'hello_dolly' );
}
}
/**
* Runs before admin notices action and hides them.
*/
public static function inject_before_notices() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
// Wrap the notices in a hidden div to prevent flickering before
// they are moved elsewhere in the page by WordPress Core.
echo '<div class="woocommerce-layout__notice-list-hide" id="wp__notice-list">';
if ( PageController::is_admin_page() ) {
// Capture all notices and hide them. WordPress Core looks for
// `.wp-header-end` and appends notices after it if found.
// https://github.com/WordPress/WordPress/blob/f6a37e7d39e2534d05b9e542045174498edfe536/wp-admin/js/common.js#L737 .
echo '<div class="wp-header-end" id="woocommerce-layout__notice-catcher"></div>';
}
}
/**
* Runs after admin notices and closes div.
*/
public static function inject_after_notices() {
if ( ! PageController::is_admin_or_embed_page() ) {
return;
}
// Close the hidden div used to prevent notices from flickering before
// they are inserted elsewhere in the page.
echo '</div>';
}
/**
* Edits Admin title based on section of wc-admin.
*
* @param string $admin_title Modifies admin title.
* @todo Can we do some URL rewriting so we can figure out which page they are on server side?
*/
public static function update_admin_title( $admin_title ) {
if (
! did_action( 'current_screen' ) ||
! PageController::is_admin_page()
) {
return $admin_title;
}
$sections = self::get_embed_breadcrumbs();
$pieces = array();
foreach ( $sections as $section ) {
$pieces[] = is_array( $section ) ? $section[1] : $section;
}
$pieces = array_reverse( $pieces );
$title = implode( ' &lsaquo; ', $pieces );
/* translators: %1$s: updated title, %2$s: blog info name */
return sprintf( __( '%1$s &lsaquo; %2$s', 'woocommerce-admin' ), $title, get_bloginfo( 'name' ) );
}
/**
* Set up a div for the app to render into.
*/
public static function page_wrapper() {
?>
<div class="wrap">
<div id="root"></div>
</div>
<?php
}
/**
* Delete woocommerce_onboarding_homepage_post_id field when the homepage is deleted
*
* @param int $post_id The deleted post id.
*/
public static function delete_homepage( $post_id ) {
if ( 'page' !== get_post_type( $post_id ) ) {
return;
}
$homepage_id = intval( get_option( 'woocommerce_onboarding_homepage_post_id', false ) );
if ( $homepage_id === $post_id ) {
delete_option( 'woocommerce_onboarding_homepage_post_id' );
}
}
}