PluginProbe
Redirection / 4.0
Redirection v4.0
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.0, at database/database-upgrader.php

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