PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.0
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 18.0, at inc/class-upgrade-history.php

134 lines 2.9 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 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 $option_names The options that need to be stored.
53 */
54 public function add( $old_version, $new_version, array $option_names ) {
55 $option_data = [];
56 if ( $option_names !== [] ) {
57 $option_data = $this->get_options_data( $option_names );
58 }
59
60 // Retrieve current history.
61 $data = $this->get();
62
63 // Add new entry.
64 $data[ time() ] = [
65 'options' => $option_data,
66 'old_version' => $old_version,
67 'new_version' => $new_version,
68 ];
69
70 // Store the data.
71 $this->set( $data );
72 }
73
74 /**
75 * Retrieves the data for the specified option names from the database.
76 *
77 * @param array $option_names The option names to retrieve.
78 *
79 * @return array
80 */
81 protected function get_options_data( array $option_names ) {
82 $wpdb = $this->get_wpdb();
83
84 $sql = $wpdb->prepare(
85 '
86 SELECT option_value, option_name FROM ' . $wpdb->options . ' WHERE
87 option_name IN ( ' . implode( ',', array_fill( 0, count( $option_names ), '%s' ) ) . ' )
88 ',
89 $option_names
90 );
91
92 $results = $wpdb->get_results( $sql, ARRAY_A );
93
94 $data = [];
95 foreach ( $results as $result ) {
96 $data[ $result['option_name'] ] = maybe_unserialize( $result['option_value'] );
97 }
98
99 return $data;
100 }
101
102 /**
103 * Stores the new history state.
104 *
105 * @param array $data The data to store.
106 *
107 * @return void
108 */
109 protected function set( array $data ) {
110 // This should not be autoloaded!
111 update_option( $this->get_option_name(), $data, false );
112 }
113
114 /**
115 * Retrieves the WPDB object.
116 *
117 * @return wpdb The WPDB object to use.
118 */
119 protected function get_wpdb() {
120 global $wpdb;
121
122 return $wpdb;
123 }
124
125 /**
126 * Retrieves the option name to store the history in.
127 *
128 * @return string The option name to store the history in.
129 */
130 protected function get_option_name() {
131 return $this->option_name;
132 }
133 }
134