PluginProbe
Redirection / 4.2.2
Redirection v4.2.2
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / database / database-upgrader.php

database-upgrader.php in Redirection 4.2.2, at database/database-upgrader.php

114 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 abstract class Red_Database_Upgrader {
4 private $queries = [];
5 private $live = true;
6
7 /**
8 * Return an array of all the stages for an upgrade
9 *
10 * @return array stage name => reason
11 */
12 abstract public function get_stages();
13
14 public function get_reason( $stage ) {
15 $stages = $this->get_stages();
16
17 if ( isset( $stages[ $stage ] ) ) {
18 return $stages[ $stage ];
19 }
20
21 return 'Unknown';
22 }
23
24 /**
25 * Run a particular stage on the current upgrader
26 *
27 * @return Red_Database_Status
28 */
29 public function perform_stage( Red_Database_Status $status ) {
30 global $wpdb;
31
32 $stage = $status->get_current_stage();
33 if ( $this->has_stage( $stage ) && method_exists( $this, $stage ) ) {
34 try {
35 $this->$stage( $wpdb );
36 $status->set_ok( $this->get_reason( $stage ) );
37 } catch ( Exception $e ) {
38 $status->set_error( $e->getMessage() );
39 }
40 } else {
41 $status->set_error( 'No stage found for upgrade ' . $stage );
42 }
43 }
44
45 public function get_queries_for_stage( $stage ) {
46 global $wpdb;
47
48 $this->queries = [];
49 $this->live = false;
50 $this->$stage( $wpdb );
51 $this->live = true;
52
53 return $this->queries;
54 }
55
56 /**
57 * Returns the current database charset
58 *
59 * @return string Database charset
60 */
61 public function get_charset() {
62 global $wpdb;
63
64 $charset_collate = '';
65 if ( ! empty( $wpdb->charset ) ) {
66 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
67 }
68
69 if ( ! empty( $wpdb->collate ) ) {
70 $charset_collate .= " COLLATE=$wpdb->collate";
71 }
72
73 return $charset_collate;
74 }
75
76 /**
77 * Performs a $wpdb->query, and throws an exception if an error occurs
78 *
79 * @return bool true if query is performed ok, otherwise an exception is thrown
80 */
81 protected function do_query( $wpdb, $sql ) {
82 if ( ! $this->live ) {
83 $this->queries[] = $sql;
84 return true;
85 }
86
87 // These are known queries without user input
88 // phpcs:ignore
89 $result = $wpdb->query( $sql );
90
91 if ( $result === false ) {
92 /* translators: 1: SQL string */
93 throw new Exception( sprintf( __( 'Failed to perform query "%s"' ), $sql ) );
94 }
95
96 return true;
97 }
98
99 /**
100 * Load a database upgrader class
101 *
102 * @return object Database upgrader
103 */
104 public static function get( $version ) {
105 include_once dirname( __FILE__ ) . '/schema/' . str_replace( [ '..', '/' ], '', $version['file'] );
106
107 return new $version['class'];
108 }
109
110 private function has_stage( $stage ) {
111 return in_array( $stage, array_keys( $this->get_stages() ), true );
112 }
113 }
114