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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /**
  12. * Holds the dev server's default URL.
  13. *
  14. * @var string
  15. */
  16. const DEFAULT_URL = 'http://localhost:8080';
  17. /**
  18. * Holds the url where the server is located.
  19. *
  20. * @var string
  21. */
  22. private $url;
  23. /**
  24. * Class constructor.
  25. *
  26. * @param string $url Where the dev server is located.
  27. */
  28. public function __construct( $url = null ) {
  29. if ( $url === null ) {
  30. $url = self::DEFAULT_URL;
  31. }
  32. $this->url = $url;
  33. }
  34. /**
  35. * Determines the URL of the asset on the dev server.
  36. *
  37. * @param WPSEO_Admin_Asset $asset The asset to determine the URL for.
  38. * @param string $type The type of asset. Usually JS or CSS.
  39. *
  40. * @return string The URL of the asset.
  41. */
  42. public function get_url( WPSEO_Admin_Asset $asset, $type ) {
  43. if ( WPSEO_Admin_Asset::TYPE_CSS === $type ) {
  44. return $this->get_default_url( $asset, $type );
  45. }
  46. $asset_manager = new WPSEO_Admin_Asset_Manager();
  47. $flat_version = $asset_manager->flatten_version( WPSEO_VERSION );
  48. $version_less_source = str_replace( '-' . $flat_version, '', $asset->get_src() );
  49. if ( false !== strpos( $version_less_source, 'select2' ) ) {
  50. return $this->get_default_url( $asset, $type );
  51. }
  52. $path = sprintf( '%s%s.js', $asset->get_src(), $asset->get_suffix() );
  53. return trailingslashit( $this->url ) . $path;
  54. }
  55. /**
  56. * Determines the URL of the asset not using the dev server.
  57. *
  58. * @param WPSEO_Admin_Asset $asset The asset to determine the URL for.
  59. * @param string $type The type of asset.
  60. *
  61. * @return string The URL of the asset file.
  62. */
  63. public function get_default_url( WPSEO_Admin_Asset $asset, $type ) {
  64. $default_location = new WPSEO_Admin_Asset_SEO_Location( WPSEO_FILE );
  65. return $default_location->get_url( $asset, $type );
  66. }
  67. }