| 1 |
<?php |
| 2 |
/** |
| 3 |
* Upgrades |
| 4 |
* |
| 5 |
* @package GamiPress\Admin\Upgrades |
| 6 |
* @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> |
| 7 |
* @since 1.2.7 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
require_once GAMIPRESS_DIR . 'includes/admin/upgrades/1.1.0.php'; |
| 13 |
require_once GAMIPRESS_DIR . 'includes/admin/upgrades/1.2.7.php'; |
| 14 |
require_once GAMIPRESS_DIR . 'includes/admin/upgrades/1.2.8.php'; |
| 15 |
require_once GAMIPRESS_DIR . 'includes/admin/upgrades/1.3.0.php'; |
| 16 |
require_once GAMIPRESS_DIR . 'includes/admin/upgrades/1.3.1.php'; |
| 17 |
require_once GAMIPRESS_DIR . 'includes/admin/upgrades/1.3.7.php'; |
| 18 |
require_once GAMIPRESS_DIR . 'includes/admin/upgrades/1.4.3.php'; |
| 19 |
require_once GAMIPRESS_DIR . 'includes/admin/upgrades/1.4.7.php'; |
| 20 |
require_once GAMIPRESS_DIR . 'includes/admin/upgrades/1.5.1.php'; |
| 21 |
require_once GAMIPRESS_DIR . 'includes/admin/upgrades/1.8.7.php'; |
| 22 |
require_once GAMIPRESS_DIR . 'includes/admin/upgrades/2.3.7.php'; |
| 23 |
require_once GAMIPRESS_DIR . 'includes/admin/upgrades/6.9.4.php'; |
| 24 |
|
| 25 |
/** |
| 26 |
* GamiPress upgrades |
| 27 |
* |
| 28 |
* @since 1.1.0 |
| 29 |
*/ |
| 30 |
function gamipress_process_upgrades() { |
| 31 |
|
| 32 |
if ( ! current_user_can( gamipress_get_manager_capability() ) ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// Get stored version |
| 37 |
$stored_version = get_option( 'gamipress_version', '1.0.0' ); |
| 38 |
|
| 39 |
if( $stored_version === GAMIPRESS_VER ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Before process upgrades action |
| 45 |
* |
| 46 |
* @since 1.1.0 |
| 47 |
* |
| 48 |
* @param string $stored_version Latest upgrade version |
| 49 |
*/ |
| 50 |
do_action( 'gamipress_before_process_upgrades', $stored_version ); |
| 51 |
|
| 52 |
/** |
| 53 |
* Version upgrade filter |
| 54 |
* |
| 55 |
* @since 1.1.0 |
| 56 |
* |
| 57 |
* @param string $stored_version Latest upgrade version |
| 58 |
* |
| 59 |
* @return string |
| 60 |
*/ |
| 61 |
$stored_version = apply_filters( 'gamipress_process_upgrades', $stored_version ); |
| 62 |
|
| 63 |
/** |
| 64 |
* After process upgrades action |
| 65 |
* |
| 66 |
* @since 1.1.0 |
| 67 |
* |
| 68 |
* @param string $stored_version Latest upgrade version |
| 69 |
*/ |
| 70 |
do_action( 'gamipress_after_process_upgrades', $stored_version ); |
| 71 |
|
| 72 |
// Updated stored version |
| 73 |
update_option( 'gamipress_version', $stored_version ); |
| 74 |
|
| 75 |
} |
| 76 |
add_action( 'admin_init', 'gamipress_process_upgrades' ); |
| 77 |
|
| 78 |
/** |
| 79 |
* Get the latest GamiPress version that requires an upgrade |
| 80 |
* |
| 81 |
* @since 1.5.1 |
| 82 |
* |
| 83 |
* @return string Last version that required an upgrade |
| 84 |
*/ |
| 85 |
function gamipress_get_last_required_upgrade() { |
| 86 |
|
| 87 |
$version = '1.1.0'; |
| 88 |
|
| 89 |
/** |
| 90 |
* Get the last required upgrade (useful to meet if version stored and current required is the same) |
| 91 |
* |
| 92 |
* @since 1.5.1 |
| 93 |
* |
| 94 |
* @param string $stored_version Latest upgrade version |
| 95 |
* |
| 96 |
* @return string |
| 97 |
*/ |
| 98 |
return apply_filters( 'gamipress_get_last_required_upgrade', $version ); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Helper function to check if GamiPress has been upgraded successfully |
| 104 |
* |
| 105 |
* @since 1.2.8 |
| 106 |
* |
| 107 |
* @param string $desired_version |
| 108 |
* |
| 109 |
* @return bool |
| 110 |
*/ |
| 111 |
function is_gamipress_upgraded_to( $desired_version = '1.0.0' ) { |
| 112 |
|
| 113 |
// Get stored version |
| 114 |
$stored_version = get_option( 'gamipress_version', '1.0.0' ); |
| 115 |
|
| 116 |
return (bool) version_compare( $stored_version, $desired_version, '>=' ); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Get's the array of completed upgrade actions |
| 122 |
* |
| 123 |
* @since 1.2.8 |
| 124 |
* |
| 125 |
* @return array The array of completed upgrades |
| 126 |
*/ |
| 127 |
function gamipress_get_completed_upgrades() { |
| 128 |
|
| 129 |
$completed_upgrades = get_option( 'gamipress_completed_upgrades' ); |
| 130 |
|
| 131 |
if ( ! is_array( $completed_upgrades ) ) { |
| 132 |
$completed_upgrades = array(); |
| 133 |
} |
| 134 |
|
| 135 |
return $completed_upgrades; |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Check if the upgrade routine has been run for a specific action |
| 141 |
* |
| 142 |
* @since 1.2.8 |
| 143 |
* |
| 144 |
* @param string $upgrade_action The upgrade action to check completion for |
| 145 |
* |
| 146 |
* @return bool If the action has been added to the completed actions array |
| 147 |
*/ |
| 148 |
function is_gamipress_upgrade_completed( $upgrade_action = '' ) { |
| 149 |
|
| 150 |
if ( empty( $upgrade_action ) ) { |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
$completed_upgrades = gamipress_get_completed_upgrades(); |
| 155 |
|
| 156 |
return in_array( $upgrade_action, $completed_upgrades ); |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Adds an upgrade action to the completed upgrades array |
| 162 |
* |
| 163 |
* @since 1.2.8 |
| 164 |
* |
| 165 |
* @param string $upgrade_action The action to add to the completed upgrades array |
| 166 |
* |
| 167 |
* @return bool If the function was successfully added |
| 168 |
*/ |
| 169 |
function gamipress_set_upgrade_complete( $upgrade_action = '' ) { |
| 170 |
|
| 171 |
if ( empty( $upgrade_action ) ) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
$completed_upgrades = gamipress_get_completed_upgrades(); |
| 176 |
$completed_upgrades[] = $upgrade_action; |
| 177 |
|
| 178 |
// Remove any blanks, and only show uniques |
| 179 |
$completed_upgrades = array_unique( array_values( $completed_upgrades ) ); |
| 180 |
|
| 181 |
return update_option( 'gamipress_completed_upgrades', $completed_upgrades ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Utility function to check if a database table exists |
| 186 |
* |
| 187 |
* @since 1.3.5 |
| 188 |
* @updated 1.4.0 Added support for network wide database |
| 189 |
* @updated 1.4.7 Cached return |
| 190 |
* |
| 191 |
* @param string $table_name The desired table name |
| 192 |
* |
| 193 |
* @return bool Whatever if table exists or not |
| 194 |
*/ |
| 195 |
function gamipress_database_table_exists( $table_name ) { |
| 196 |
|
| 197 |
global $wpdb; |
| 198 |
|
| 199 |
$cache = gamipress_get_cache( 'installed_tables', array(), false ); |
| 200 |
|
| 201 |
// If result already cached, return it |
| 202 |
if( isset( $cache[$table_name] ) ) { |
| 203 |
return $cache[$table_name]; |
| 204 |
} |
| 205 |
|
| 206 |
$table_exist = $wpdb->get_var( $wpdb->prepare( |
| 207 |
"SHOW TABLES LIKE %s", |
| 208 |
$wpdb->esc_like( $table_name ) |
| 209 |
) ); |
| 210 |
|
| 211 |
if( empty( $table_exist ) ) { |
| 212 |
$table_exist = $wpdb->get_var( $wpdb->prepare( |
| 213 |
"SHOW TABLES LIKE %s", |
| 214 |
$wpdb->esc_like( $wpdb->prefix . $table_name ) |
| 215 |
) ); |
| 216 |
} |
| 217 |
|
| 218 |
if( empty( $table_exist ) ) { |
| 219 |
$table_exist = $wpdb->get_var( $wpdb->prepare( |
| 220 |
"SHOW TABLES LIKE %s", |
| 221 |
$wpdb->esc_like( $wpdb->base_prefix . $table_name ) |
| 222 |
) ); |
| 223 |
} |
| 224 |
|
| 225 |
// Cache function result |
| 226 |
$cache[$table_name] = ( ! empty( $table_exist ) ); |
| 227 |
|
| 228 |
gamipress_set_cache( 'installed_tables', $cache ); |
| 229 |
|
| 230 |
return ! empty( $table_exist ); |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Utility function to check if a database table has a specific field |
| 236 |
* |
| 237 |
* @since 1.4.7 |
| 238 |
* |
| 239 |
* @param string $table_name The desired table name |
| 240 |
* @param string $column_name The desired column name |
| 241 |
* |
| 242 |
* @return bool Whatever if table exists and has this column or not |
| 243 |
*/ |
| 244 |
function gamipress_database_table_has_column( $table_name, $column_name ) { |
| 245 |
|
| 246 |
global $wpdb; |
| 247 |
|
| 248 |
$cache = gamipress_get_cache( 'installed_table_columns', array(), false ); |
| 249 |
|
| 250 |
// If result already cached, return it |
| 251 |
if( isset( $cache[$table_name] ) && isset( $cache[$table_name][$column_name] ) ) { |
| 252 |
return $cache[$table_name][$column_name]; |
| 253 |
} |
| 254 |
|
| 255 |
if( ! gamipress_database_table_exists( $table_name ) ) { |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
$column_exists = $wpdb->get_var( $wpdb->prepare( |
| 260 |
"SHOW COLUMNS FROM {$table_name} LIKE %s", |
| 261 |
$wpdb->esc_like( $column_name ) |
| 262 |
) ); |
| 263 |
|
| 264 |
// Check if already cached any column from this table, if not, initialize it |
| 265 |
if( ! isset( $cache[$table_name] ) ) { |
| 266 |
$cache[$table_name] = array(); |
| 267 |
} |
| 268 |
|
| 269 |
// Cache function result |
| 270 |
$cache[$table_name][$column_name] = ( ! empty( $column_exists ) ); |
| 271 |
|
| 272 |
gamipress_set_cache( 'installed_table_columns', $cache ); |
| 273 |
|
| 274 |
return ! empty( $column_exists ); |
| 275 |
|
| 276 |
} |