class-admin-asset-dev-server-location.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Changes the asset paths to dev server paths.
  9. */
  10. final class WPSEO_Admin_Asset_Dev_Server_Location implements WPSEO_Admin_Asset_Location {
  11. const DEFAULT_URL = 'http://localhost:8080';
  12. /**
  13. * @var string
  14. */
  15. private $url;
  16. /**
  17. * @param string $url Where the dev server is located.
  18. */
  19. public function __construct( $url = null ) {
  20. if ( $url === null ) {
  21. $url = self::DEFAULT_URL;
  22. }
  23. $this->url = $url;
  24. }
  25. /**
  26. * Determines the URL of the asset on the dev server.
  27. *
  28. * @param WPSEO_Admin_Asset $asset The asset to determine the URL for.
  29. * @param string $type The type of asset. Usually JS or CSS.
  30. *
  31. * @return string The URL of the asset.
  32. */
  33. public function get_url( WPSEO_Admin_Asset $asset, $type ) {
  34. if ( WPSEO_Admin_Asset::TYPE_CSS === $type ) {
  35. return $this->get_default_url( $asset, $type );
  36. }
  37. $asset_manager = new WPSEO_Admin_Asset_Manager();
  38. $flat_version = $asset_manager->flatten_version( WPSEO_VERSION );
  39. $version_less_source = str_replace( '-' . $flat_version, '', $asset->get_src() );
  40. if ( false !== strpos( $version_less_source, 'select2' ) ) {
  41. return $this->get_default_url( $asset, $type );
  42. }
  43. $path = sprintf( '%s%s.js', $asset->get_src(), $asset->get_suffix() );
  44. return trailingslashit( $this->url ) . $path;
  45. }
  46. /**
  47. * Determines the URL of the asset not using the dev server.
  48. *
  49. * @param WPSEO_Admin_Asset $asset The asset to determine the URL for.
  50. * @param string $type The type of asset.
  51. *
  52. * @return string The URL of the asset file.
  53. */
  54. public function get_default_url( WPSEO_Admin_Asset $asset, $type ) {
  55. $default_location = new WPSEO_Admin_Asset_SEO_Location( WPSEO_FILE );
  56. return $default_location->get_url( $asset, $type );
  57. }
  58. }