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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * @var string
  13. */
  14. protected $plugin_file;
  15. /**
  16. * The plugin file to base the asset location upon.
  17. *
  18. * @param string $plugin_file The plugin file string.
  19. */
  20. public function __construct( $plugin_file ) {
  21. $this->plugin_file = $plugin_file;
  22. }
  23. /**
  24. * Determines the URL of the asset on the dev server.
  25. *
  26. * @param WPSEO_Admin_Asset $asset The asset to determine the URL for.
  27. * @param string $type The type of asset. Usually JS or CSS.
  28. *
  29. * @return string The URL of the asset.
  30. */
  31. public function get_url( WPSEO_Admin_Asset $asset, $type ) {
  32. $path = $this->get_path( $asset, $type );
  33. if ( empty( $path ) ) {
  34. return '';
  35. }
  36. if ( YOAST_ENVIRONMENT !== 'development' && ! $asset->get_suffix() ) {
  37. $plugin_path = plugin_dir_path( $this->plugin_file );
  38. if ( ! file_exists( $plugin_path . $path ) ) {
  39. // Give a notice to the user in the console (only once).
  40. WPSEO_Utils::javascript_console_notification(
  41. 'Development Files',
  42. sprintf(
  43. /* translators: %1$s resolves to https://github.com/Yoast/wordpress-seo */
  44. __( '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' ),
  45. 'https://github.com/Yoast/wordpress-seo'
  46. ),
  47. true
  48. );
  49. // Just load the .min file.
  50. $path = $this->get_path( $asset, $type, '.min' );
  51. }
  52. }
  53. return plugins_url( $path, $this->plugin_file );
  54. }
  55. /**
  56. * Determines the path relative to the plugin folder of an asset.
  57. *
  58. * @param WPSEO_Admin_Asset $asset The asset to determine the path
  59. * for.
  60. * @param string $type The type of asset.
  61. * @param string|null $force_suffix The suffix to force, or null when
  62. * to use the default suffix.
  63. *
  64. * @return string The path to the asset file.
  65. */
  66. protected function get_path( WPSEO_Admin_Asset $asset, $type, $force_suffix = null ) {
  67. $relative_path = '';
  68. $rtl_suffix = '';
  69. $suffix = ( $force_suffix === null ) ? $asset->get_suffix() : $force_suffix;
  70. switch ( $type ) {
  71. case WPSEO_Admin_Asset::TYPE_JS:
  72. $relative_path = 'js/dist/' . $asset->get_src() . $suffix . '.js';
  73. break;
  74. case WPSEO_Admin_Asset::TYPE_CSS:
  75. // Path and suffix for RTL stylesheets.
  76. if ( function_exists( 'is_rtl' ) && is_rtl() && $asset->has_rtl() ) {
  77. $rtl_suffix = '-rtl';
  78. }
  79. $relative_path = 'css/dist/' . $asset->get_src() . $rtl_suffix . $suffix . '.css';
  80. break;
  81. }
  82. return $relative_path;
  83. }
  84. }