column
12 years ago
storage_model
12 years ago
api.php
12 years ago
column.php
12 years ago
deprecated.php
12 years ago
export_import.php
12 years ago
settings.php
12 years ago
storage_model.php
12 years ago
third_party.php
12 years ago
upgrade.php
12 years ago
utility.php
12 years ago
upgrade.php
362 lines
| 1 | <?php |
| 2 | |
| 3 | // @dev_only: set_site_transient( 'update_plugins', null ); |
| 4 | |
| 5 | /** |
| 6 | * Upgrade |
| 7 | * |
| 8 | * Class largely based on code from ACF ( thanks to Elliot Condon ) |
| 9 | * |
| 10 | * @since 2.0.0 |
| 11 | */ |
| 12 | class CPAC_Upgrade { |
| 13 | |
| 14 | /** |
| 15 | * CPAC class |
| 16 | */ |
| 17 | private $cpac; |
| 18 | |
| 19 | /** |
| 20 | * Constructor |
| 21 | * |
| 22 | * @since 2.0.0 |
| 23 | */ |
| 24 | function __construct( $cpac ) { |
| 25 | |
| 26 | $this->cpac = $cpac; |
| 27 | |
| 28 | // run upgrade based on version |
| 29 | add_action( 'admin_init', array( $this, 'init' ) ); |
| 30 | add_action( 'admin_menu', array( $this, 'admin_menu' ), 11 ); |
| 31 | add_action( 'wp_ajax_cpac_upgrade', array( $this, 'ajax_upgrade' ) ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Add submenu page |
| 36 | * |
| 37 | * @since 2.0.0 |
| 38 | */ |
| 39 | public function admin_menu() { |
| 40 | |
| 41 | // Don't run on plugin activate |
| 42 | if ( isset( $_GET['action'] ) && 'activate-plugin' == $_GET['action'] ) return; |
| 43 | |
| 44 | // add settings page |
| 45 | $upgrade_page = add_submenu_page( 'options-general.php', __( 'Upgrade', 'cpac' ), __( 'Upgrade', 'cpac' ), 'manage_options', 'cpac-upgrade', array( $this, 'start_upgrade' ) ); |
| 46 | |
| 47 | // add scripts |
| 48 | add_action( "admin_print_scripts-{$upgrade_page}", array( $this, 'admin_scripts' ) ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * init |
| 53 | * |
| 54 | * @since 2.0.0 |
| 55 | */ |
| 56 | public function init() { |
| 57 | |
| 58 | // @dev_only delete_option( 'cpac_version' ); set_transient( 'cpac_show_welcome', 'display' ); |
| 59 | $version = get_option( 'cpac_version', false ); |
| 60 | |
| 61 | // Maybe version pre 2.0.0 was used |
| 62 | if ( ! $version && get_option( 'cpac_options' ) ) { |
| 63 | $version = '1.0.0'; |
| 64 | } |
| 65 | |
| 66 | // @dev_only if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) echo "--------- CPAC DEBUG: START ---------<br/>\n" . $version . "<br/>\n" . CPAC_VERSION . "<br/>\n" . CPAC_UPGRADE_VERSION . "<br/>\n" . get_transient( 'cpac_show_welcome' ) . "<br/>\n" . "--------- CPAC DEBUG: END ---------<br/>\n"; |
| 67 | |
| 68 | // Maybe upgrade? |
| 69 | if ( $version ) { |
| 70 | |
| 71 | // run every upgrade |
| 72 | if ( $version < CPAC_VERSION ) { |
| 73 | // nothing yet |
| 74 | } |
| 75 | |
| 76 | // run only when updating from v1 to v2 |
| 77 | if ( $version < '2.0.0' ) { |
| 78 | |
| 79 | // show welcome screen |
| 80 | set_transient( 'cpac_show_welcome', 'display' ); |
| 81 | } |
| 82 | |
| 83 | // run only when database upgrade is needed |
| 84 | if ( $version < CPAC_UPGRADE_VERSION ) { |
| 85 | |
| 86 | // display upgrade message on every page except upgrade page itself |
| 87 | if ( ! ( isset( $_REQUEST['page'] ) && 'cpac-upgrade' === $_REQUEST['page'] ) ) { |
| 88 | |
| 89 | $message = __( 'Admin Columns', 'cpac' ) . ' v' . CPAC_VERSION . ' ' . |
| 90 | __( 'requires a database upgrade','cpac' ) . |
| 91 | ' (<a class="thickbox" href="' . admin_url() . |
| 92 | 'plugin-install.php?tab=plugin-information&plugin=codepress-admin-columns§ion=changelog&TB_iframe=true&width=640&height=559">' . |
| 93 | __( 'why?', 'cpac' ) .'</a>). ' . |
| 94 | __( "Please", 'cpac' ) .' <a href="http://codex.wordpress.org/Backing_Up_Your_Database">' . |
| 95 | __( "backup your database", 'cpac' ) .'</a>, '. |
| 96 | __( "then click", 'cpac' ) . ' <a href="' . admin_url() . 'options-general.php?page=cpac-upgrade" class="button">' . |
| 97 | __( "Upgrade Database", 'cpac' ) . '</a>'; |
| 98 | |
| 99 | cpac_admin_message( $message, 'updated' ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // run when NO upgrade is needed |
| 104 | elseif ( $version < CPAC_VERSION ) { |
| 105 | |
| 106 | update_option( 'cpac_version', CPAC_VERSION ); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // Fresh install |
| 111 | else { |
| 112 | |
| 113 | update_option( 'cpac_version', CPAC_VERSION ); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Init Upgrade Process |
| 119 | * |
| 120 | * @since 2.0.0 |
| 121 | */ |
| 122 | public function ajax_upgrade() { |
| 123 | |
| 124 | // vars |
| 125 | $return = array( |
| 126 | 'status' => false, |
| 127 | 'message' => "", |
| 128 | 'next' => false, |
| 129 | ); |
| 130 | |
| 131 | $version = $_POST['version']; |
| 132 | |
| 133 | // versions |
| 134 | switch ( $version ) { |
| 135 | |
| 136 | case '2.0.0' : |
| 137 | |
| 138 | $old_settings = get_option( 'cpac_options' ); |
| 139 | |
| 140 | // old settings |
| 141 | if ( ! empty( $old_settings['columns'] ) ) { |
| 142 | |
| 143 | foreach ( $old_settings['columns'] as $storage_key => $old_columns ){ |
| 144 | |
| 145 | $columns = array(); |
| 146 | |
| 147 | if ( $old_columns ) { |
| 148 | |
| 149 | // used to determine clone ID |
| 150 | $tax_count = null; |
| 151 | $post_count = null; |
| 152 | $meta_count = null; |
| 153 | |
| 154 | foreach ( $old_columns as $old_column_name => $old_column_settings ) { |
| 155 | |
| 156 | // only active columns |
| 157 | if ( isset( $old_column_settings['state'] ) && 'on' !== $old_column_settings['state'] ) |
| 158 | continue; |
| 159 | |
| 160 | // convert old settings to new |
| 161 | $settings = array_merge( $old_column_settings, array( |
| 162 | 'type' => $old_column_name, |
| 163 | 'clone' => '' |
| 164 | ) ); |
| 165 | |
| 166 | // set name |
| 167 | $name = $old_column_name; |
| 168 | |
| 169 | // convert: Users |
| 170 | if ( 'wp-users' == $storage_key ) { |
| 171 | |
| 172 | // is user postcount? |
| 173 | if ( strpos( $old_column_name, 'column-user_postcount-' ) !== false ) { |
| 174 | $settings['type'] = 'column-user_postcount'; |
| 175 | $settings['clone'] = $post_count; |
| 176 | $settings['post_type'] = str_replace( 'column-user_postcount-', '', $old_column_name ); |
| 177 | |
| 178 | $name = $post_count ? $settings['type'] . '-' . $settings['clone'] : $settings['type']; |
| 179 | $post_count++; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // convert: Media |
| 184 | elseif ( 'wp-media' == $storage_key ) { |
| 185 | |
| 186 | if ( 'column-filesize' == $old_column_name ) { |
| 187 | $name = 'column-file_size'; |
| 188 | $settings['type'] = $name; |
| 189 | } |
| 190 | // is EXIF data? |
| 191 | elseif ( strpos( $old_column_name, 'column-image-' ) !== false ) { |
| 192 | $name = 'column-exif_data'; |
| 193 | $settings['type'] = $name; |
| 194 | $settings['exif_datatype'] = str_replace( 'column-image-', '', $old_column_name ); |
| 195 | } |
| 196 | elseif ( 'column-file_paths' == $old_column_name ) { |
| 197 | $name = 'column-available_sizes'; |
| 198 | $settings['type'] = $name; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // convert: Comments |
| 203 | elseif ( 'wp-comments' == $storage_key ) { |
| 204 | |
| 205 | if ( 'column-author_author' == $old_column_name ) { |
| 206 | $name = 'column-author'; |
| 207 | $settings['type'] = $name; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // convert: Posts |
| 212 | else { |
| 213 | |
| 214 | if ( 'column-attachment-count' == $old_column_name ) { |
| 215 | $name = 'column-attachment_count'; |
| 216 | $settings['type'] = $name; |
| 217 | } |
| 218 | elseif ( 'column-author-name' == $old_column_name ) { |
| 219 | $name = 'column-author_name'; |
| 220 | $settings['type'] = $name; |
| 221 | $settings['display_author_as'] = $old_column_settings['display_as']; |
| 222 | } |
| 223 | elseif ( 'column-before-moretag' == $old_column_name ) { |
| 224 | $name = 'column-before_moretag'; |
| 225 | $settings['type'] = $name; |
| 226 | } |
| 227 | elseif ( 'column-comment-count' == $old_column_name ) { |
| 228 | $name = 'column-comment_count'; |
| 229 | $settings['type'] = $name; |
| 230 | $settings['comment_status'] = 'total_comments'; |
| 231 | } |
| 232 | elseif ( 'column-comment-status' == $old_column_name ) { |
| 233 | $name = 'column-comment_status'; |
| 234 | $settings['type'] = $name; |
| 235 | } |
| 236 | elseif ( 'column-ping-status' == $old_column_name ) { |
| 237 | $name = 'column-ping_status'; |
| 238 | $settings['type'] = $name; |
| 239 | } |
| 240 | elseif ( 'column-page-slug' == $old_column_name ) { |
| 241 | $name = 'column-slug'; |
| 242 | $settings['type'] = $name; |
| 243 | } |
| 244 | elseif ( 'column-page-template' == $old_column_name ) { |
| 245 | $name = 'column-page_template'; |
| 246 | $settings['type'] = $name; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // convert: Applies to all storage types |
| 251 | |
| 252 | // is taxonomy? |
| 253 | if ( strpos( $old_column_name, 'column-taxonomy-' ) !== false ) { |
| 254 | $settings['type'] = 'column-taxonomy'; |
| 255 | $settings['clone'] = $tax_count; |
| 256 | $settings['taxonomy'] = str_replace( 'column-taxonomy-', '', $old_column_name ); |
| 257 | |
| 258 | $name = $tax_count ? $settings['type'] . '-' . $settings['clone'] : $settings['type']; |
| 259 | $tax_count++; |
| 260 | } |
| 261 | // is custom field? |
| 262 | elseif ( strpos( $old_column_name, 'column-meta-' ) !== false ) { |
| 263 | |
| 264 | $settings['type'] = 'column-meta'; |
| 265 | //$settings['clone'] = str_replace( 'column-meta-', '', $old_column_name ); |
| 266 | $settings['clone'] = $meta_count; |
| 267 | |
| 268 | $name = $meta_count ? $settings['type'] . '-' . $settings['clone'] : $settings['type']; |
| 269 | $meta_count++; |
| 270 | } |
| 271 | elseif ( 'column-word-count' == $old_column_name ) { |
| 272 | $name = 'column-word_count'; |
| 273 | $settings['type'] = $name; |
| 274 | } |
| 275 | |
| 276 | // add to column set |
| 277 | $columns[ $name ] = $settings; |
| 278 | |
| 279 | // reorder so that active column are at the top of the pile. |
| 280 | $active = $inactive = array(); |
| 281 | foreach ( $columns as $name => $_settings ) { |
| 282 | if ( 'on' === $_settings['state'] ) { |
| 283 | $active[ $name ] = $_settings; |
| 284 | } |
| 285 | else { |
| 286 | $inactive[ $name ] = $_settings; |
| 287 | } |
| 288 | } |
| 289 | $columns = array_merge( $active, $inactive ); |
| 290 | } |
| 291 | |
| 292 | // store column settings |
| 293 | if ( ! get_option( "cpac_options_{$storage_key}" ) ) |
| 294 | update_option( "cpac_options_{$storage_key}", $columns ); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // update version |
| 300 | update_option( 'cpac_version', $version ); |
| 301 | |
| 302 | $return = array( |
| 303 | 'status' => true, |
| 304 | 'message' => __( "Migrating Column Settings", 'cpac' ) . '...', |
| 305 | 'next' => false, |
| 306 | ); |
| 307 | |
| 308 | break; |
| 309 | } |
| 310 | |
| 311 | // return json |
| 312 | echo json_encode( $return ); |
| 313 | die; |
| 314 | } |
| 315 | |
| 316 | /* |
| 317 | * Starting points of the upgrade process |
| 318 | * |
| 319 | * @since 2.0.0 |
| 320 | */ |
| 321 | public function start_upgrade() { |
| 322 | |
| 323 | $version = get_option( 'cpac_version', '1.0.0' ); |
| 324 | $next = false; |
| 325 | |
| 326 | // list of starting points |
| 327 | if( $version < '2.0.0' ) { |
| 328 | $next = '2.0.0'; |
| 329 | } |
| 330 | |
| 331 | // Run upgrade? |
| 332 | if( $next ) : ?> |
| 333 | <script type="text/javascript"> |
| 334 | run_upgrade("<?php echo $next; ?>"); |
| 335 | </script> |
| 336 | <?php |
| 337 | |
| 338 | // No update required |
| 339 | else : ?> |
| 340 | <p><?php _e( 'No Upgrade Required', 'cpac' ); ?></p> |
| 341 | <a href="<?php echo admin_url('options-general.php'); ?>?page=codepress-admin-columns&info"><?php _e( 'Return to welcome screen.', 'cpac' ); ?></a> |
| 342 | <?php |
| 343 | endif; |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * Scripts |
| 348 | * |
| 349 | * @since 2.0.0 |
| 350 | */ |
| 351 | public function admin_scripts() { |
| 352 | wp_enqueue_script( 'cpac-upgrade', CPAC_URL . 'assets/js/upgrade.js', array( 'jquery' ), CPAC_VERSION ); |
| 353 | wp_enqueue_style( 'cpac-admin', CPAC_URL . 'assets/css/admin-column.css', array(), CPAC_VERSION, 'all' ); |
| 354 | |
| 355 | // javascript translations |
| 356 | wp_localize_script( 'cpac-upgrade', 'cpac_upgrade_i18n', array( |
| 357 | 'complete' => __( 'Upgrade Complete!', 'cpac' ) . '</p><p><a href="' . admin_url('options-general.php') . '?page=codepress-admin-columns&info">' . __( 'Return to settings.', 'cpac' ) . "</a>" , |
| 358 | 'error' => __( 'Error', 'cpac' ), |
| 359 | 'major_error' => __( 'Sorry. Something went wrong during the upgrade process. Please report this on the support forum.', 'cpac' ) |
| 360 | )); |
| 361 | } |
| 362 | } |