PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / inc / class-upgrade-history.php

class-upgrade-history.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.2, at inc/class-upgrade-history.php

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