123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770 |
- <?php
- /**
- * WPSEO plugin file.
- *
- * @package WPSEO\Admin
- */
- /**
- * This class registers all the necessary styles and scripts.
- *
- * Also has methods for the enqueing of scripts and styles.
- * It automatically adds a prefix to the handle.
- */
- class WPSEO_Admin_Asset_Manager {
- /**
- * Class that manages the assets' location.
- *
- * @var WPSEO_Admin_Asset_Location
- */
- protected $asset_location;
- /**
- * Prefix for naming the assets.
- *
- * @var string
- */
- const PREFIX = 'yoast-seo-';
- /**
- * Prefix for naming the assets.
- *
- * @var string
- */
- private $prefix;
- /**
- * Constructs a manager of assets. Needs a location to know where to register assets at.
- *
- * @param WPSEO_Admin_Asset_Location $asset_location The provider of the asset location.
- * @param string $prefix The prefix for naming assets.
- */
- public function __construct( WPSEO_Admin_Asset_Location $asset_location = null, $prefix = self::PREFIX ) {
- if ( $asset_location === null ) {
- $asset_location = self::create_default_location();
- }
- $this->asset_location = $asset_location;
- $this->prefix = $prefix;
- }
- /**
- * Enqueues scripts.
- *
- * @param string $script The name of the script to enqueue.
- */
- public function enqueue_script( $script ) {
- wp_enqueue_script( $this->prefix . $script );
- }
- /**
- * Enqueues styles.
- *
- * @param string $style The name of the style to enqueue.
- */
- public function enqueue_style( $style ) {
- wp_enqueue_style( $this->prefix . $style );
- }
- /**
- * Registers scripts based on it's parameters.
- *
- * @param WPSEO_Admin_Asset $script The script to register.
- */
- public function register_script( WPSEO_Admin_Asset $script ) {
- wp_register_script(
- $this->prefix . $script->get_name(),
- $this->get_url( $script, WPSEO_Admin_Asset::TYPE_JS ),
- $script->get_deps(),
- $script->get_version(),
- $script->is_in_footer()
- );
- }
- /**
- * Registers styles based on it's parameters.
- *
- * @param WPSEO_Admin_Asset $style The style to register.
- */
- public function register_style( WPSEO_Admin_Asset $style ) {
- wp_register_style(
- $this->prefix . $style->get_name(),
- $this->get_url( $style, WPSEO_Admin_Asset::TYPE_CSS ),
- $style->get_deps(),
- $style->get_version(),
- $style->get_media()
- );
- }
- /**
- * Calls the functions that register scripts and styles with the scripts and styles to be registered as arguments.
- */
- public function register_assets() {
- $this->register_scripts( $this->scripts_to_be_registered() );
- $this->register_styles( $this->styles_to_be_registered() );
- }
- /**
- * Registers all the scripts passed to it.
- *
- * @param array $scripts The scripts passed to it.
- */
- public function register_scripts( $scripts ) {
- foreach ( $scripts as $script ) {
- $script = new WPSEO_Admin_Asset( $script );
- $this->register_script( $script );
- }
- }
- /**
- * Registers all the styles it receives.
- *
- * @param array $styles Styles that need to be registered.
- */
- public function register_styles( $styles ) {
- foreach ( $styles as $style ) {
- $style = new WPSEO_Admin_Asset( $style );
- $this->register_style( $style );
- }
- }
- /**
- * A list of styles that shouldn't be registered but are needed in other locations in the plugin.
- *
- * @return array
- */
- public function special_styles() {
- $flat_version = $this->flatten_version( WPSEO_VERSION );
- $asset_args = [
- 'name' => 'inside-editor',
- 'src' => 'inside-editor-' . $flat_version,
- ];
- return [ 'inside-editor' => new WPSEO_Admin_Asset( $asset_args ) ];
- }
- /**
- * Flattens a version number for use in a filename.
- *
- * @param string $version The original version number.
- *
- * @return string The flattened version number.
- */
- public function flatten_version( $version ) {
- $parts = explode( '.', $version );
- if ( count( $parts ) === 2 && preg_match( '/^\d+$/', $parts[1] ) === 1 ) {
- $parts[] = '0';
- }
- return implode( '', $parts );
- }
- /**
- * Creates a default location object for use in the admin asset manager.
- *
- * @return WPSEO_Admin_Asset_Location The location to use in the asset manager.
- */
- public static function create_default_location() {
- if ( defined( 'YOAST_SEO_DEV_SERVER' ) && YOAST_SEO_DEV_SERVER ) {
- $url = defined( 'YOAST_SEO_DEV_SERVER_URL' ) ? YOAST_SEO_DEV_SERVER_URL : WPSEO_Admin_Asset_Dev_Server_Location::DEFAULT_URL;
- return new WPSEO_Admin_Asset_Dev_Server_Location( $url );
- }
- return new WPSEO_Admin_Asset_SEO_Location( WPSEO_FILE );
- }
- /**
- * Registers the WordPress dependencies that exist in 5.0 in case they are not present.
- *
- * This function can be removed when WordPress 5.1 has been released, because from 5.0 wp-elements will be
- * registered earlier, which means we don't have to reregister things.
- *
- * @return void
- */
- public function register_wp_assets() {
- global $wp_scripts;
- $script = $wp_scripts->query( 'react' );
- // IE11 needs wp-polyfill to be registered before react.
- if ( $script && ! in_array( 'wp-polyfill', $script->deps, true ) ) {
- $script->deps[] = 'wp-polyfill';
- }
- $flat_version = $this->flatten_version( WPSEO_VERSION );
- wp_register_script(
- 'react',
- plugins_url( 'js/vendor/react.min.js', WPSEO_FILE ),
- [],
- 'v16.6.1',
- true
- );
- wp_register_script(
- 'react-dom',
- plugins_url( 'js/vendor/react-dom.min.js', WPSEO_FILE ),
- [ 'react' ],
- 'v16.6.1',
- true
- );
- wp_register_script(
- 'lodash-base',
- plugins_url( 'js/vendor/lodash.min.js', WPSEO_FILE ),
- [],
- '4.17.5',
- true
- );
- wp_register_script(
- 'lodash',
- plugins_url( 'js/vendor/lodash-noconflict.js', WPSEO_FILE ),
- [ 'lodash-base' ],
- WPSEO_VERSION,
- true
- );
- wp_register_script(
- 'wp-polyfill',
- plugins_url( 'js/dist/babel-polyfill-' . $flat_version . '.min.js', WPSEO_FILE ),
- [],
- WPSEO_VERSION,
- true
- );
- wp_register_script(
- 'wp-element',
- plugins_url( 'js/dist/wp-element-' . $flat_version . '.min.js', WPSEO_FILE ),
- [ 'lodash', 'wp-polyfill', 'react', 'react-dom' ],
- WPSEO_VERSION,
- true
- );
- wp_register_script(
- 'wp-api-fetch',
- plugins_url( 'js/dist/wp-apiFetch-' . $flat_version . '.min.js', WPSEO_FILE ),
- [ 'wp-i18n', 'wp-polyfill' ],
- WPSEO_VERSION,
- true
- );
- wp_register_script(
- 'wp-components',
- plugins_url( 'js/dist/wp-components-' . $flat_version . '.min.js', WPSEO_FILE ),
- [ 'lodash', 'wp-api-fetch', 'wp-i18n', 'wp-polyfill', 'wp-compose' ],
- WPSEO_VERSION,
- true
- );
- wp_register_script(
- 'wp-data',
- plugins_url( 'js/dist/wp-data-' . $flat_version . '.min.js', WPSEO_FILE ),
- [ 'lodash', 'wp-element', 'wp-polyfill', 'wp-compose' ],
- WPSEO_VERSION,
- true
- );
- wp_register_script(
- 'wp-i18n',
- plugins_url( 'js/dist/wp-i18n-' . $flat_version . '.min.js', WPSEO_FILE ),
- [ 'wp-polyfill' ],
- WPSEO_VERSION,
- true
- );
- wp_register_script(
- 'wp-rich-text',
- plugins_url( 'js/dist/wp-rich-text-' . $flat_version . '.min.js', WPSEO_FILE ),
- [ 'lodash', 'wp-polyfill', 'wp-data' ],
- WPSEO_VERSION,
- true
- );
- wp_register_script(
- 'wp-compose',
- plugins_url( 'js/dist/wp-compose-' . $flat_version . '.min.js', WPSEO_FILE ),
- [ 'lodash', 'wp-polyfill' ],
- WPSEO_VERSION,
- true
- );
- /*
- * wp-annotations only exists from Gutenberg 4.3 and onwards, so we register a no-op in earlier versions.
- * The no-op achieves that our scripts that depend on this are actually loaded. Because WordPress doesn't
- * load a script if any of the dependencies are missing.
- *
- * @phpcs:disable WordPress.WP.EnqueuedResourceParameters -- The no-op does not require these settings.
- */
- wp_register_script(
- 'wp-annotations',
- null
- );
- // phpcs:enable -- End of disable.
- }
- /**
- * Returns the scripts that need to be registered.
- *
- * @todo Data format is not self-documenting. Needs explanation inline. R.
- *
- * @return array The scripts that need to be registered.
- */
- protected function scripts_to_be_registered() {
- $select2_language = 'en';
- $user_locale = WPSEO_Language_Utils::get_user_locale();
- $language = WPSEO_Language_Utils::get_language( $user_locale );
- if ( file_exists( WPSEO_PATH . "js/dist/select2/i18n/{$user_locale}.js" ) ) {
- $select2_language = $user_locale; // Chinese and some others use full locale.
- }
- elseif ( file_exists( WPSEO_PATH . "js/dist/select2/i18n/{$language}.js" ) ) {
- $select2_language = $language;
- }
- $flat_version = $this->flatten_version( WPSEO_VERSION );
- return [
- [
- 'name' => 'commons',
- // Load webpack-commons for bundle support.
- 'src' => 'commons-' . $flat_version,
- 'in_footer' => false,
- 'deps' => [
- 'wp-polyfill',
- ],
- ],
- [
- 'name' => 'search-appearance',
- 'src' => 'search-appearance-' . $flat_version,
- 'deps' => [
- 'wp-api',
- self::PREFIX . 'components',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'yoast-modal',
- 'src' => 'wp-seo-modal-' . $flat_version,
- 'deps' => [
- 'jquery',
- 'wp-element',
- 'wp-i18n',
- self::PREFIX . 'components',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'admin-script',
- 'src' => 'wp-seo-admin-' . $flat_version,
- 'deps' => [
- 'lodash',
- 'jquery',
- 'jquery-ui-core',
- 'jquery-ui-progressbar',
- self::PREFIX . 'select2',
- self::PREFIX . 'select2-translations',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'admin-media',
- 'src' => 'wp-seo-admin-media-' . $flat_version,
- 'deps' => [
- 'jquery',
- 'jquery-ui-core',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'network-admin-script',
- 'src' => 'wp-seo-network-admin-' . $flat_version,
- 'deps' => [
- 'jquery',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'bulk-editor',
- 'src' => 'wp-seo-bulk-editor-' . $flat_version,
- 'deps' => [
- 'jquery',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'admin-global-script',
- 'src' => 'wp-seo-admin-global-' . $flat_version,
- 'deps' => [
- 'jquery',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'metabox',
- 'src' => 'wp-seo-metabox-' . $flat_version,
- 'deps' => [
- 'jquery',
- 'wp-element',
- 'wp-i18n',
- 'wp-data',
- 'wp-components',
- self::PREFIX . 'select2',
- self::PREFIX . 'select2-translations',
- self::PREFIX . 'commons',
- ],
- 'in_footer' => false,
- ],
- [
- 'name' => 'featured-image',
- 'src' => 'wp-seo-featured-image-' . $flat_version,
- 'deps' => [
- 'jquery',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'admin-gsc',
- 'src' => 'wp-seo-admin-gsc-' . $flat_version,
- 'deps' => [
- 'wp-element',
- 'wp-i18n',
- self::PREFIX . 'styled-components',
- self::PREFIX . 'components',
- self::PREFIX . 'commons',
- ],
- 'in_footer' => false,
- ],
- [
- 'name' => 'post-scraper',
- 'src' => 'wp-seo-post-scraper-' . $flat_version,
- 'deps' => [
- 'wp-util',
- 'wp-api',
- 'wp-sanitize',
- 'wp-element',
- 'wp-i18n',
- 'wp-data',
- 'wp-api-fetch',
- 'wp-annotations',
- 'wp-compose',
- self::PREFIX . 'replacevar-plugin',
- self::PREFIX . 'shortcode-plugin',
- self::PREFIX . 'analysis',
- self::PREFIX . 'components',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'term-scraper',
- 'src' => 'wp-seo-term-scraper-' . $flat_version,
- 'deps' => [
- 'wp-sanitize',
- 'wp-element',
- 'wp-i18n',
- 'wp-data',
- 'wp-api-fetch',
- 'wp-compose',
- self::PREFIX . 'replacevar-plugin',
- self::PREFIX . 'analysis',
- self::PREFIX . 'components',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'replacevar-plugin',
- 'src' => 'wp-seo-replacevar-plugin-' . $flat_version,
- 'deps' => [
- self::PREFIX . 'analysis',
- self::PREFIX . 'components',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'shortcode-plugin',
- 'src' => 'wp-seo-shortcode-plugin-' . $flat_version,
- 'deps' => [
- self::PREFIX . 'analysis',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'recalculate',
- 'src' => 'wp-seo-recalculate-' . $flat_version,
- 'deps' => [
- 'jquery',
- 'jquery-ui-core',
- 'jquery-ui-progressbar',
- self::PREFIX . 'analysis',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'primary-category',
- 'src' => 'wp-seo-metabox-category-' . $flat_version,
- 'deps' => [
- 'jquery',
- 'wp-util',
- 'wp-element',
- 'wp-i18n',
- 'wp-components',
- 'wp-data',
- self::PREFIX . 'analysis',
- self::PREFIX . 'components',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'select2',
- 'src' => 'select2/select2.full',
- 'suffix' => '.min',
- 'deps' => [
- 'jquery',
- ],
- 'version' => '4.0.3',
- ],
- [
- 'name' => 'select2-translations',
- 'src' => 'select2/i18n/' . $select2_language,
- 'deps' => [
- 'jquery',
- self::PREFIX . 'select2',
- ],
- 'version' => '4.0.3',
- 'suffix' => '',
- ],
- [
- 'name' => 'configuration-wizard',
- 'src' => 'configuration-wizard-' . $flat_version,
- 'deps' => [
- 'jquery',
- 'wp-element',
- 'wp-i18n',
- 'wp-api',
- self::PREFIX . 'components',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'reindex-links',
- 'src' => 'wp-seo-reindex-links-' . $flat_version,
- 'deps' => [
- 'jquery',
- 'jquery-ui-core',
- 'jquery-ui-progressbar',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'edit-page-script',
- 'src' => 'wp-seo-edit-page-' . $flat_version,
- 'deps' => [
- 'jquery',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'quick-edit-handler',
- 'src' => 'wp-seo-quick-edit-handler-' . $flat_version,
- 'deps' => [
- 'jquery',
- self::PREFIX . 'commons',
- ],
- 'in_footer' => true,
- ],
- [
- 'name' => 'api',
- 'src' => 'wp-seo-api-' . $flat_version,
- 'deps' => [
- 'wp-api',
- 'jquery',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'dashboard-widget',
- 'src' => 'wp-seo-dashboard-widget-' . $flat_version,
- 'deps' => [
- self::PREFIX . 'api',
- 'jquery',
- 'wp-element',
- 'wp-i18n',
- self::PREFIX . 'components',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'filter-explanation',
- 'src' => 'wp-seo-filter-explanation-' . $flat_version,
- 'deps' => [
- 'jquery',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'analysis',
- 'src' => 'analysis-' . $flat_version,
- 'deps' => [
- 'lodash',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'components',
- 'src' => 'components-' . $flat_version,
- 'deps' => [
- self::PREFIX . 'analysis',
- self::PREFIX . 'styled-components',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'structured-data-blocks',
- 'src' => 'wp-seo-structured-data-blocks-' . $flat_version,
- 'deps' => [
- 'wp-blocks',
- 'wp-i18n',
- 'wp-element',
- self::PREFIX . 'styled-components',
- self::PREFIX . 'commons',
- ],
- ],
- [
- 'name' => 'styled-components',
- 'src' => 'styled-components-' . $flat_version,
- 'deps' => [
- 'wp-element',
- ],
- ],
- [
- 'name' => 'help-scout-beacon',
- 'src' => 'help-scout-beacon-' . $flat_version,
- 'in_footer' => false,
- 'deps' => [
- self::PREFIX . 'styled-components',
- 'wp-element',
- 'wp-i18n',
- ],
- ],
- ];
- }
- /**
- * Returns the styles that need to be registered.
- *
- * @todo Data format is not self-documenting. Needs explanation inline. R.
- *
- * @return array Styles that need to be registered.
- */
- protected function styles_to_be_registered() {
- $flat_version = $this->flatten_version( WPSEO_VERSION );
- return [
- [
- 'name' => 'admin-css',
- 'src' => 'yst_plugin_tools-' . $flat_version,
- 'deps' => [ self::PREFIX . 'toggle-switch' ],
- ],
- [
- 'name' => 'toggle-switch',
- 'src' => 'toggle-switch-' . $flat_version,
- ],
- [
- 'name' => 'dismissible',
- 'src' => 'wpseo-dismissible-' . $flat_version,
- ],
- [
- 'name' => 'alerts',
- 'src' => 'alerts-' . $flat_version,
- ],
- [
- 'name' => 'edit-page',
- 'src' => 'edit-page-' . $flat_version,
- ],
- [
- 'name' => 'featured-image',
- 'src' => 'featured-image-' . $flat_version,
- ],
- [
- 'name' => 'metabox-css',
- 'src' => 'metabox-' . $flat_version,
- 'deps' => [
- self::PREFIX . 'select2',
- self::PREFIX . 'admin-css',
- ],
- ],
- [
- 'name' => 'wp-dashboard',
- 'src' => 'dashboard-' . $flat_version,
- ],
- [
- 'name' => 'scoring',
- 'src' => 'yst_seo_score-' . $flat_version,
- ],
- [
- 'name' => 'adminbar',
- 'src' => 'adminbar-' . $flat_version,
- 'deps' => [
- 'admin-bar',
- ],
- ],
- [
- 'name' => 'primary-category',
- 'src' => 'metabox-primary-category-' . $flat_version,
- ],
- [
- 'name' => 'select2',
- 'src' => 'select2/select2',
- 'suffix' => '.min',
- 'version' => '4.0.1',
- 'rtl' => false,
- ],
- [
- 'name' => 'admin-global',
- 'src' => 'admin-global-' . $flat_version,
- ],
- [
- 'name' => 'yoast-components',
- 'src' => 'yoast-components-' . $flat_version,
- ],
- [
- 'name' => 'extensions',
- 'src' => 'yoast-extensions-' . $flat_version,
- ],
- [
- 'name' => 'filter-explanation',
- 'src' => 'filter-explanation-' . $flat_version,
- ],
- [
- 'name' => 'search-appearance',
- 'src' => 'search-appearance-' . $flat_version,
- ],
- [
- 'name' => 'structured-data-blocks',
- 'src' => 'structured-data-blocks-' . $flat_version,
- 'deps' => [ 'wp-edit-blocks' ],
- ],
- ];
- }
- /**
- * Determines the URL of the asset.
- *
- * @param WPSEO_Admin_Asset $asset The asset to determine the URL for.
- * @param string $type The type of asset. Usually JS or CSS.
- *
- * @return string The URL of the asset.
- */
- protected function get_url( WPSEO_Admin_Asset $asset, $type ) {
- $scheme = wp_parse_url( $asset->get_src(), PHP_URL_SCHEME );
- if ( in_array( $scheme, [ 'http', 'https' ], true ) ) {
- return $asset->get_src();
- }
- return $this->asset_location->get_url( $asset, $type );
- }
- }
|