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