Skip to content

Commit

Permalink
Update admin notice
Browse files Browse the repository at this point in the history
  • Loading branch information
herrvigg committed May 22, 2022
1 parent c00df4e commit 4fb5af5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
15 changes: 12 additions & 3 deletions admin/qtx_activation_hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -935,18 +935,19 @@ function qtranxf_admin_notices_slugs_import() {
if ( qtranxf_check_admin_notice( 'slugs-import' ) ) {
return;
}
// Very quick check to see if worth going further.
$old_value = get_option( 'qts_options' );
if ( ! $old_value ) {
return;
return '';
}
// TODO: check stored value after migration done.
// More advanced checks.
require_once( QTRANSLATE_DIR . '/modules/slugs/includes/qtranslate-slug-import.php' );
$msg = qts_check_import_slugs();
if ( empty( $msg ) ) {
return;
}
qtranxf_admin_notice_dismiss_script();
echo '<div class="notice notice-warning qtranxs-notice-ajax is-dismissible" id="qtranxs-slugs-import" action="unset"><p>';
echo '<div class="notice notice-warning qtranxs-notice-ajax is-dismissible" id="qtranxs-slugs-import"><p>';
$options_link = admin_url( 'options-general.php?page=qtranslate-xt#import' );
echo '<p>' . sprintf( __( '%s : found slugs meta that can be imported. Go to the <a href="%s">import settings</a> to import.', 'qtranslate' ), qtranxf_get_plugin_link(), $options_link ) . '</p>';
echo '<p>' . $msg . '</p>';
Expand Down Expand Up @@ -1052,6 +1053,14 @@ function qtranxf_update_option_admin_notices( $messages, $id, $set = true ) {
return $messages;
}

/**
* Update an admin notice to be set (hidden) / unset (shown).
*
* @param string $id
* @param bool $set true to set the message as seen (hide), false to unset (show)
*
* @return array|mixed
*/
function qtranxf_update_admin_notice( $id, $set ) {
$messages = get_option( 'qtranslate_admin_notices', array() );

Expand Down
38 changes: 29 additions & 9 deletions modules/slugs/includes/qtranslate-slug-import.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
<?php

const QTX_SLUG_OLD_META_PREFIX = '_qts_slug_';
const QTX_SLUG_OLD_OPTIONS_PREFIX = '_qts_';
const QTX_SLUG_OLD_OPTIONS = 'qts_options';

/**
* Check if slugs meta can be imported from the legacy postmeta and termmeta.
* Check if slugs meta should be imported from the legacy postmeta and termmeta.
*
* @return string messages giving details, or empty if no meta found.
* @return string messages giving details, empty if new meta found or no legacy meta found.
*/
function qts_check_import_slugs() {
global $wpdb;

$count_slugs = function ( $dbmeta, &$msg ) use ( $wpdb ) {
$results = $wpdb->get_var( "SELECT count(*) FROM $dbmeta WHERE meta_key like '_qts_slug%'" );
$count_slugs = function ( $dbmeta, $prefix, &$msg ) use ( $wpdb ) {
$results = $wpdb->get_var( "SELECT count(*) FROM $dbmeta WHERE meta_key like '$prefix%'" );
if ( $results ) {
$msg[] = sprintf( __( "Found %s slugs from $dbmeta.", 'qtranslate' ), $results );
}
};

$count_slugs( $wpdb->postmeta, $msg );
$count_slugs( $wpdb->termmeta, $msg );
$msg = [];
$count_slugs( $wpdb->postmeta, QTX_SLUG_META_PREFIX, $msg );
$count_slugs( $wpdb->termmeta, QTX_SLUG_META_PREFIX, $msg );
// Found some post/term meta with the new keys. No import to suggest, but it can still be done manually.
if ( ! empty( $msg ) ) {
return '';
}

$msg = [];
$count_slugs( $wpdb->postmeta, QTX_SLUG_OLD_META_PREFIX, $msg );
$count_slugs( $wpdb->termmeta, QTX_SLUG_OLD_META_PREFIX, $msg );

return empty ( $msg ) ? $msg : implode( '<br>', $msg );
}
Expand All @@ -31,9 +45,10 @@ function qts_import_slugs_meta( $db_commit ) {
global $wpdb;

$new_prefix = QTX_SLUG_META_PREFIX;
$old_prefix = '_qts_slug';
$old_prefix = QTX_SLUG_OLD_META_PREFIX;

$import_meta = function ( $dbmeta, $dbmetaid, &$msg ) use ( $wpdb, $old_prefix, $new_prefix ) {

$results = $wpdb->query( "DELETE FROM $dbmeta WHERE meta_key like '$new_prefix%'" );
if ( $results ) {
$msg[] = sprintf( __( "Deleted %s rows from $dbmeta (%s).", 'qtranslate' ), $results, $new_prefix );
Expand Down Expand Up @@ -76,12 +91,12 @@ function qts_import_slugs_options( $db_commit ) {
}
}

$options = get_option( 'qts_options' );
$options = get_option( QTX_SLUG_OLD_OPTIONS );
if ( $options ) {
$new_options = [];
// Drop the legacy prefix.
foreach ( $options as $type => $slugs ) {
$type = str_replace( '_qts_', '', $type );
$type = str_replace( QTX_SLUG_OLD_OPTIONS_PREFIX, '', $type );
$new_options[ $type ] = $slugs;
}
$msg[] = sprintf( __( "Imported %s types from options.", 'qtranslate' ), count( $new_options ) );
Expand All @@ -106,5 +121,10 @@ function qts_import_slugs( $db_commit ) {
$msg[] = qts_import_slugs_meta( $db_commit );
$msg[] = qts_import_slugs_options( $db_commit );

if ( $db_commit ) {
// Hide the admin notice.
qtranxf_update_admin_notice( 'slugs-import', true );
}

return implode( '<br/>', $msg );
}

0 comments on commit 4fb5af5

Please sign in to comment.