InstalledPackage.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace YahnisElsts\PluginUpdateChecker\v5p1;
  3. if ( !class_exists(InstalledPackage::class, false) ):
  4. /**
  5. * This class represents a currently installed plugin or theme.
  6. *
  7. * Not to be confused with the "package" field in WP update API responses that contains
  8. * the download URL of a the new version.
  9. */
  10. abstract class InstalledPackage {
  11. /**
  12. * @var UpdateChecker
  13. */
  14. protected $updateChecker;
  15. public function __construct($updateChecker) {
  16. $this->updateChecker = $updateChecker;
  17. }
  18. /**
  19. * Get the currently installed version of the plugin or theme.
  20. *
  21. * @return string|null Version number.
  22. */
  23. abstract public function getInstalledVersion();
  24. /**
  25. * Get the full path of the plugin or theme directory (without a trailing slash).
  26. *
  27. * @return string
  28. */
  29. abstract public function getAbsoluteDirectoryPath();
  30. /**
  31. * Check whether a regular file exists in the package's directory.
  32. *
  33. * @param string $relativeFileName File name relative to the package directory.
  34. * @return bool
  35. */
  36. public function fileExists($relativeFileName) {
  37. return is_file(
  38. $this->getAbsoluteDirectoryPath()
  39. . DIRECTORY_SEPARATOR
  40. . ltrim($relativeFileName, '/\\')
  41. );
  42. }
  43. /* -------------------------------------------------------------------
  44. * File header parsing
  45. * -------------------------------------------------------------------
  46. */
  47. /**
  48. * Parse plugin or theme metadata from the header comment.
  49. *
  50. * This is basically a simplified version of the get_file_data() function from /wp-includes/functions.php.
  51. * It's intended as a utility for subclasses that detect updates by parsing files in a VCS.
  52. *
  53. * @param string|null $content File contents.
  54. * @return string[]
  55. */
  56. public function getFileHeader($content) {
  57. $content = (string)$content;
  58. //WordPress only looks at the first 8 KiB of the file, so we do the same.
  59. $content = substr($content, 0, 8192);
  60. //Normalize line endings.
  61. $content = str_replace("\r", "\n", $content);
  62. $headers = $this->getHeaderNames();
  63. $results = array();
  64. foreach ($headers as $field => $name) {
  65. $success = preg_match('/^[ \t\/*#@]*' . preg_quote($name, '/') . ':(.*)$/mi', $content, $matches);
  66. if ( ($success === 1) && $matches[1] ) {
  67. $value = $matches[1];
  68. if ( function_exists('_cleanup_header_comment') ) {
  69. $value = _cleanup_header_comment($value);
  70. }
  71. $results[$field] = $value;
  72. } else {
  73. $results[$field] = '';
  74. }
  75. }
  76. return $results;
  77. }
  78. /**
  79. * @return array Format: ['HeaderKey' => 'Header Name']
  80. */
  81. abstract protected function getHeaderNames();
  82. /**
  83. * Get the value of a specific plugin or theme header.
  84. *
  85. * @param string $headerName
  86. * @return string Either the value of the header, or an empty string if the header doesn't exist.
  87. */
  88. abstract public function getHeaderValue($headerName);
  89. }
  90. endif;