class-admin-asset-seo-location.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Determines the location of an asset within the SEO plugin.
  9. */
  10. final class WPSEO_Admin_Asset_SEO_Location implements WPSEO_Admin_Asset_Location {
  11. /**
  12. * Path to the plugin file.
  13. *
  14. * @var string
  15. */
  16. protected $plugin_file;
  17. /**
  18. * The plugin file to base the asset location upon.
  19. *
  20. * @param string $plugin_file The plugin file string.
  21. */
  22. public function __construct( $plugin_file ) {
  23. $this->plugin_file = $plugin_file;
  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. $path = $this->get_path( $asset, $type );
  35. if ( empty( $path ) ) {
  36. return '';
  37. }
  38. if ( YOAST_ENVIRONMENT !== 'development' && ! $asset->get_suffix() ) {
  39. $plugin_path = plugin_dir_path( $this->plugin_file );
  40. if ( ! file_exists( $plugin_path . $path ) ) {
  41. // Give a notice to the user in the console (only once).
  42. WPSEO_Utils::javascript_console_notification(
  43. 'Development Files',
  44. sprintf(
  45. /* translators: %1$s resolves to https://github.com/Yoast/wordpress-seo */
  46. __( 'You are trying to load non-minified files. These are only available in our development package. Check out %1$s to see all the source files.', 'wordpress-seo' ),
  47. 'https://github.com/Yoast/wordpress-seo'
  48. ),
  49. true
  50. );
  51. // Just load the .min file.
  52. $path = $this->get_path( $asset, $type, '.min' );
  53. }
  54. }
  55. return plugins_url( $path, $this->plugin_file );
  56. }
  57. /**
  58. * Determines the path relative to the plugin folder of an asset.
  59. *
  60. * @param WPSEO_Admin_Asset $asset The asset to determine the path
  61. * for.
  62. * @param string $type The type of asset.
  63. * @param string|null $force_suffix The suffix to force, or null when
  64. * to use the default suffix.
  65. *
  66. * @return string The path to the asset file.
  67. */
  68. protected function get_path( WPSEO_Admin_Asset $asset, $type, $force_suffix = null ) {
  69. $relative_path = '';
  70. $rtl_suffix = '';
  71. $suffix = ( $force_suffix === null ) ? $asset->get_suffix() : $force_suffix;
  72. switch ( $type ) {
  73. case WPSEO_Admin_Asset::TYPE_JS:
  74. $relative_path = 'js/dist/' . $asset->get_src() . $suffix . '.js';
  75. break;
  76. case WPSEO_Admin_Asset::TYPE_CSS:
  77. // Path and suffix for RTL stylesheets.
  78. if ( function_exists( 'is_rtl' ) && is_rtl() && $asset->has_rtl() ) {
  79. $rtl_suffix = '-rtl';
  80. }
  81. $relative_path = 'css/dist/' . $asset->get_src() . $rtl_suffix . $suffix . '.css';
  82. break;
  83. }
  84. return $relative_path;
  85. }
  86. }