InstalledPackage.php 2.8 KB

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