class-admin-asset-analysis-worker-location.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Represents a way to determine the analysis worker asset location.
  9. */
  10. final class WPSEO_Admin_Asset_Analysis_Worker_Location implements WPSEO_Admin_Asset_Location {
  11. /**
  12. * Holds the asset's location.
  13. *
  14. * @var WPSEO_Admin_Asset_Location $asset_location.
  15. */
  16. private $asset_location;
  17. /**
  18. * Holds the asset itself.
  19. *
  20. * @var WPSEO_Admin_Asset $asset.
  21. */
  22. private $asset;
  23. /**
  24. * Constructs the location of the analysis worker asset.
  25. *
  26. * @param string $flat_version The flat version of the asset.
  27. * @param string $name The name of the analysis worker asset.
  28. */
  29. public function __construct( $flat_version = '', $name = 'analysis-worker' ) {
  30. if ( $flat_version === '' ) {
  31. $asset_manager = new WPSEO_Admin_Asset_Manager();
  32. $flat_version = $asset_manager->flatten_version( WPSEO_VERSION );
  33. }
  34. $analysis_worker = 'wp-seo-' . $name . '-' . $flat_version;
  35. $this->asset_location = WPSEO_Admin_Asset_Manager::create_default_location();
  36. $this->asset = new WPSEO_Admin_Asset(
  37. [
  38. 'name' => $name,
  39. 'src' => $analysis_worker,
  40. ]
  41. );
  42. }
  43. /**
  44. * Retrieves the analysis worker asset.
  45. *
  46. * @return WPSEO_Admin_Asset The analysis worker asset.
  47. */
  48. public function get_asset() {
  49. return $this->asset;
  50. }
  51. /**
  52. * Determines the URL of the asset on the dev server.
  53. *
  54. * @param WPSEO_Admin_Asset $asset The asset to determine the URL for.
  55. * @param string $type The type of asset. Usually JS or CSS.
  56. *
  57. * @return string The URL of the asset.
  58. */
  59. public function get_url( WPSEO_Admin_Asset $asset, $type ) {
  60. $scheme = wp_parse_url( $asset->get_src(), PHP_URL_SCHEME );
  61. if ( in_array( $scheme, [ 'http', 'https' ], true ) ) {
  62. return $asset->get_src();
  63. }
  64. return $this->asset_location->get_url( $asset, $type );
  65. }
  66. }