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