class-upgrade-history.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internal
  6. */
  7. /**
  8. * This class handles storing the current options for future reference.
  9. *
  10. * This should only be used during an upgrade routine.
  11. */
  12. class WPSEO_Upgrade_History {
  13. /**
  14. * Option to use to store/retrieve data from.
  15. *
  16. * @var string
  17. */
  18. protected $option_name = 'wpseo_upgrade_history';
  19. /**
  20. * WPSEO_Upgrade_History constructor.
  21. *
  22. * @param null|string $option_name Optional. Custom option to use to store/retrieve history from.
  23. */
  24. public function __construct( $option_name = null ) {
  25. if ( $option_name !== null ) {
  26. $this->option_name = $option_name;
  27. }
  28. }
  29. /**
  30. * Retrieves the content of the history items currently stored.
  31. *
  32. * @return array The contents of the history option.
  33. */
  34. public function get() {
  35. $data = get_option( $this->get_option_name(), [] );
  36. if ( ! is_array( $data ) ) {
  37. return [];
  38. }
  39. return $data;
  40. }
  41. /**
  42. * Adds a new history entry in the storage.
  43. *
  44. * @param string $old_version The version we are upgrading from.
  45. * @param string $new_version The version we are upgrading to.
  46. * @param array $option_names The options that need to be stored.
  47. */
  48. public function add( $old_version, $new_version, array $option_names ) {
  49. $option_data = [];
  50. if ( [] !== $option_names ) {
  51. $option_data = $this->get_options_data( $option_names );
  52. }
  53. // Retrieve current history.
  54. $data = $this->get();
  55. // Add new entry.
  56. $data[ time() ] = [
  57. 'options' => $option_data,
  58. 'old_version' => $old_version,
  59. 'new_version' => $new_version,
  60. ];
  61. // Store the data.
  62. $this->set( $data );
  63. }
  64. /**
  65. * Retrieves the data for the specified option names from the database.
  66. *
  67. * @param array $option_names The option names to retrieve.
  68. *
  69. * @return array
  70. */
  71. protected function get_options_data( array $option_names ) {
  72. $wpdb = $this->get_wpdb();
  73. $sql = $wpdb->prepare(
  74. '
  75. SELECT option_value, option_name FROM ' . $wpdb->options . ' WHERE
  76. option_name IN ( ' . implode( ',', array_fill( 0, count( $option_names ), '%s' ) ) . ' )
  77. ',
  78. $option_names
  79. );
  80. $results = $wpdb->get_results( $sql, ARRAY_A );
  81. $data = [];
  82. foreach ( $results as $result ) {
  83. $data[ $result['option_name'] ] = maybe_unserialize( $result['option_value'] );
  84. }
  85. return $data;
  86. }
  87. /**
  88. * Stores the new history state.
  89. *
  90. * @param array $data The data to store.
  91. *
  92. * @return void
  93. */
  94. protected function set( array $data ) {
  95. // This should not be autoloaded!
  96. update_option( $this->get_option_name(), $data, false );
  97. }
  98. /**
  99. * Retrieves the WPDB object.
  100. *
  101. * @return wpdb The WPDB object to use.
  102. */
  103. protected function get_wpdb() {
  104. global $wpdb;
  105. return $wpdb;
  106. }
  107. /**
  108. * Retrieves the option name to store the history in.
  109. *
  110. * @return string The option name to store the history in.
  111. */
  112. protected function get_option_name() {
  113. return $this->option_name;
  114. }
  115. }