0;
}
/**
* How many items version 3.x left behind.
*
* @return int
*/
public static function count_legacy_posts() {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- The old post type is not registered, so no WordPress API can count it.
return (int) $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = %s",
ACS_Portfolio_Post_Type::LEGACY_POST_TYPE
)
);
}
/**
* Is ACS Portfolio Pro 3.x still switched on?
*
* Importing while it runs would take its items away from it, so the offer
* is held back until it has been deactivated.
*
* @return bool
*/
public static function legacy_plugin_active() {
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
return is_plugin_active( 'acs-portfolio-pro/acs-portfolio-pro.php' );
}
/**
* Offer the import on the plugin's own screens.
*
* @return void
*/
public function maybe_show_notice() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
if ( get_option( self::DISMISSED_OPTION ) || ! self::has_legacy_data() ) {
return;
}
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if ( ! $screen ) {
return;
}
$on_plugin_screen = ( ACS_Portfolio_Post_Type::POST_TYPE === $screen->post_type )
|| ( 'plugins' === $screen->id );
if ( ! $on_plugin_screen ) {
return;
}
$count = self::count_legacy_posts();
$active = self::legacy_plugin_active();
?>
403 ) );
}
if ( self::legacy_plugin_active() ) {
wp_die( esc_html__( 'Deactivate the older ACS Portfolio Pro plugin before importing.', 'acs-portfolio' ), '', array( 'response' => 409 ) );
}
$moved = self::run();
wp_safe_redirect( add_query_arg( 'acs_portfolio_imported', $moved, ACS_Portfolio_Settings::page_url() ) );
exit;
}
/**
* Stop offering the import.
*
* @return void
*/
public function handle_dismiss() {
check_admin_referer( 'acs_portfolio_import_dismiss' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You are not allowed to do that.', 'acs-portfolio' ), '', array( 'response' => 403 ) );
}
update_option( self::DISMISSED_OPTION, 1 );
wp_safe_redirect( ACS_Portfolio_Settings::page_url() );
exit;
}
/**
* Move the old items, terms and meta values across.
*
* @return int How many items were moved.
*/
public static function run() {
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- The old post type is not registered, so no WordPress API can list it.
$ids = $wpdb->get_col(
$wpdb->prepare(
"SELECT ID FROM {$wpdb->posts} WHERE post_type = %s",
ACS_Portfolio_Post_Type::LEGACY_POST_TYPE
)
);
// Keeping term_id and term_taxonomy_id means every assignment and every
// saved setting that names a category keeps pointing at the same term.
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- A taxonomy can only be renamed in place at this level.
$wpdb->update(
$wpdb->term_taxonomy,
array( 'taxonomy' => ACS_Portfolio_Post_Type::TAXONOMY ),
array( 'taxonomy' => ACS_Portfolio_Post_Type::LEGACY_TAXONOMY ),
array( '%s' ),
array( '%s' )
);
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- wp_update_post() cannot move a post to a type the old plugin no longer registers.
$wpdb->update(
$wpdb->posts,
array( 'post_type' => ACS_Portfolio_Post_Type::POST_TYPE ),
array( 'post_type' => ACS_Portfolio_Post_Type::LEGACY_POST_TYPE ),
array( '%s' ),
array( '%s' )
);
$map = ACS_Portfolio_Meta::legacy_map();
foreach ( $ids as $id ) {
$id = absint( $id );
if ( ! $id ) {
continue;
}
foreach ( $map as $new_key => $old_key ) {
$existing = get_post_meta( $id, $new_key, true );
if ( '' !== $existing && null !== $existing && false !== $existing ) {
continue;
}
$value = get_post_meta( $id, $old_key, true );
if ( '' === $value || null === $value || false === $value ) {
continue;
}
update_post_meta( $id, $new_key, $value );
}
clean_post_cache( $id );
}
clean_term_cache( array(), ACS_Portfolio_Post_Type::TAXONOMY );
delete_option( self::DISMISSED_OPTION );
update_option(
self::DONE_OPTION,
array(
'version' => ACS_PORTFOLIO_VERSION,
'time' => time(),
'posts' => count( $ids ),
)
);
ACS_Portfolio_Post_Type::register();
flush_rewrite_rules();
return count( $ids );
}
}