Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import QTS slugs options and meta data into QTX #1171

Merged
merged 32 commits into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
31b6554
Import slugs meta in QTX
herrvigg May 21, 2022
d9d8dbd
Add dry-run mode, update new prefix
herrvigg May 21, 2022
7bfaf27
Rename QTS_META_PREFIX to QTX_SLUG_META_PREFIX
herrvigg May 21, 2022
379227d
Import QTS options
herrvigg May 22, 2022
06e747c
Remove QTS prefix and HTML ID for options
herrvigg May 22, 2022
c00df4e
Move import functions to separate file
herrvigg May 22, 2022
4fb5af5
Update admin notice
herrvigg May 22, 2022
a3f29bc
Rename QTX_SLUG_OLD_OPTIONS_NAME
herrvigg May 22, 2022
ee585ac
Rename import functions with `qtranxf_slugs` prefix
herrvigg May 22, 2022
d7d31ff
Cleanup, doc and rename
herrvigg May 22, 2022
3f1b669
Add check on slugs module state, minor fixes
herrvigg May 23, 2022
e913983
add termmeta data removal on qtx uninstall
spleen1981 May 24, 2022
5ed9b2e
add conditions to display/enable slugs import checkboxes
spleen1981 May 25, 2022
9253300
Import new QTS slugs only
herrvigg May 27, 2022
65be394
Add delete option
herrvigg May 27, 2022
8674988
Import options with conservative merge, retrofix keys
herrvigg May 28, 2022
47c64bd
Merge branch 'master' into slugs-import
herrvigg May 28, 2022
246c6c7
Fix residual QTS_META_PREFIX
herrvigg May 28, 2022
4b12303
Merge branch 'master' into slugs-import
herrvigg May 28, 2022
548c016
Merge branch 'master' into slugs-import
herrvigg May 28, 2022
cd7b0db
Rename QTS import file
herrvigg May 28, 2022
9208b41
Refactor import to migration QTS
herrvigg May 28, 2022
1cae835
Add temporary hack to restore QTS options for master dev
herrvigg May 28, 2022
76fe72b
Rename slugs import to migrate, complete descriptions
herrvigg May 28, 2022
7fffe69
Early exit if no QTS options to migrate
herrvigg May 28, 2022
bcf02d4
Pre-check QTS for conditional delete and migrate by table
herrvigg May 29, 2022
1700f0d
Fix sprintf message formatting
herrvigg May 29, 2022
7f44a11
Remove internal QTS filter hooks (#1176)
herrvigg May 29, 2022
63b2ea2
Merge branch 'master' into slugs-import
herrvigg May 29, 2022
c65ac51
Dry-run mode without SQL transaction
herrvigg May 29, 2022
007d683
Update labels for QTS migration
herrvigg May 29, 2022
98cab95
Merge branch 'master' into slugs-import
herrvigg May 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion admin/qtx_activation_hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,6 @@ function qtranxf_activation_hook() {
// Migrate (rename/import) legacy options, temporary transitions during evolutions.
qtranxf_rename_legacy_option( 'qtranslate_modules', QTX_OPTIONS_MODULES_STATE );
qtranxf_import_legacy_option( 'acf_qtranslate', QTX_OPTIONS_MODULE_ACF, false );
qtranxf_import_legacy_option( 'qts_options', QTX_OPTIONS_MODULE_SLUGS, false );

$ts = time();
$next_thanks = get_option( 'qtranslate_next_thanks' );
Expand Down Expand Up @@ -932,6 +931,32 @@ function qtranxf_admin_notices_gutenberg() {

add_action( 'admin_notices', 'qtranxf_admin_notices_gutenberg' );

function qtranxf_admin_notices_slugs_import() {
if ( qtranxf_check_admin_notice( 'slugs-import' ) ) {
return;
}
herrvigg marked this conversation as resolved.
Show resolved Hide resolved
// Very quick check to see if worth going further.
$old_value = get_option( 'qts_options' );
if ( ! $old_value ) {
return '';
}
// 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"><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>';
echo '</p><p><a class="button qtranxs-notice-dismiss" href="javascript:void(0);">' . __( 'I have already done it, dismiss this message.', 'qtranslate' );
echo '</a></p></div>';
}

add_action( 'admin_notices', 'qtranxf_admin_notices_slugs_import' );

function qtranxf_admin_notice_deactivate_plugin( $name, $plugin ) {
deactivate_plugins( $plugin, true );
$d = dirname( $plugin );
Expand Down Expand Up @@ -1028,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
6 changes: 6 additions & 0 deletions admin/qtx_admin_options_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,12 @@ function qtranxf_executeOnUpdate() {
$messages[] = $msg;
}
}

if ( isset( $_POST['qtranslate_import_slugs'] ) && $_POST['qtranslate_import_slugs'] ) {
require_once( QTRANSLATE_DIR . '/modules/slugs/includes/qtranslate-slug-import.php' );
$db_commit = isset( $_POST['qtranslate_import_slugs_confirm'] ) && $_POST['qtranslate_import_slugs_confirm'];
$messages[] = qts_import_slugs( $db_commit );
}
}

function qtranxf_mark_default( $text ) {
Expand Down
15 changes: 15 additions & 0 deletions admin/qtx_import_export.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,21 @@ function qtranxf_admin_section_import_export( $request_uri ) {
'text' => sprintf( __( 'Use plugin %s to import data.', 'qtranslate' ), '<a href="https://wordpress.org/plugins/w2q-wpml-to-qtranslate/" target="_blank">W2Q: WPML to qTranslate</a>' )
) ) ?>
<?php do_action( 'qtranslate_add_row_migrate' ) ?>
<tr id="qtranslate-import-slugs">
<th scope="row"><?php _e( 'Import from slugs', 'qtranslate' ) ?></th>
<td>
<label for="qtranslate_import_slugs"><input type="checkbox" name="qtranslate_import_slugs"
id="qtranslate_import_slugs"
value="1"/> <?php _e( 'Import options, post and term meta from legacy slugs (QTS). Attention, existing slugs will be deleted.', 'qtranslate' ); ?>
spleen1981 marked this conversation as resolved.
Show resolved Hide resolved
</label>
<br/>
<label for="qtranslate_import_slugs_confirm"><input type="checkbox"
name="qtranslate_import_slugs_confirm"
id="qtranslate_import_slugs_confirm"
value="1"/> <?php _e( "Confirm import in database. Leave unchecked for a dry-run mode without change saved in database.", 'qtranslate' ) ?>
</label>
</td>
</tr>
<tr>
<th scope="row"><?php _e( 'Reset qTranslate', 'qtranslate' ) ?></th>
<td>
Expand Down
30 changes: 13 additions & 17 deletions modules/slugs/includes/class-qtranslate-slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ function init() {
return;
}

// TODO: remove temporary import of legacy option for master, rely on plugin activation in next release.
require_once( QTRANSLATE_DIR . '/admin/qtx_admin_options.php' );
qtranxf_import_legacy_option( 'qts_options', QTX_OPTIONS_MODULE_SLUGS, false );

$this->options_buffer = get_option( QTX_OPTIONS_MODULE_SLUGS, array() );
$this->permalink_structure = get_option( 'permalink_structure' );

Expand Down Expand Up @@ -123,7 +119,7 @@ public function qts_body_class( $classes ) {
* @return string the slug or empty if not found
*/
public function get_slug( $id, $lang ) {
$slugArray = get_post_meta( $id, QTS_META_PREFIX . $lang );
$slugArray = get_post_meta( $id, QTX_SLUG_META_PREFIX . $lang );

return ! empty( $slugArray ) ? $slugArray[0] : "";
}
Expand Down Expand Up @@ -166,7 +162,7 @@ public function get_base_slug( $name = false, $lang = false ) {
return false;
}
$qts_options = $this->options_buffer;
$option_name = QTS_PREFIX . $type . '_' . $name;
$option_name = $type . '_' . $name;
if ( ! isset( $qts_options[ $option_name ] ) || empty( $qts_options[ $option_name ] ) ) {
return false;
}
Expand Down Expand Up @@ -610,7 +606,7 @@ public function post_type_link( $link, $post, $leavename, $sample ) {

$post_link = apply_filters( 'qts_permastruct', $wp_rewrite->get_extra_permastruct( $post->post_type ), $post->post_type );

$slug = get_post_meta( $post->ID, QTS_META_PREFIX . $this->get_temp_lang(), true );
$slug = get_post_meta( $post->ID, QTX_SLUG_META_PREFIX . $this->get_temp_lang(), true );
if ( ! $slug ) {
$slug = $post->post_name;
}
Expand Down Expand Up @@ -688,7 +684,7 @@ public function post_link( $link, $post, $leavename ) {
if ( $cats ) {
usort( $cats, '_usort_terms_by_ID' ); // order by ID

$category = get_metadata( 'term', $cats[0]->term_id, QTS_META_PREFIX . $this->get_temp_lang(), true );
$category = get_metadata( 'term', $cats[0]->term_id, QTX_SLUG_META_PREFIX . $this->get_temp_lang(), true );
if ( ! $category ) {
$category = $cats[0]->slug;
}
Expand All @@ -702,7 +698,7 @@ public function post_link( $link, $post, $leavename ) {
if ( empty( $category ) ) {
$default_category = get_category( get_option( 'default_category' ) );

$default_category_slug = get_metadata( 'term', $default_category->term_id, QTS_META_PREFIX . $this->get_temp_lang(), true );
$default_category_slug = get_metadata( 'term', $default_category->term_id, QTX_SLUG_META_PREFIX . $this->get_temp_lang(), true );
if ( ! $default_category_slug ) {
$default_category_slug = $default_category->slug;
}
Expand All @@ -719,7 +715,7 @@ public function post_link( $link, $post, $leavename ) {

$date = explode( " ", date( 'Y m d H i s', $unixtime ) );

$post_slug = get_post_meta( $post->ID, QTS_META_PREFIX . $this->get_temp_lang(), true );
$post_slug = get_post_meta( $post->ID, QTX_SLUG_META_PREFIX . $this->get_temp_lang(), true );
if ( ! $post_slug ) {
$post_slug = $post->post_name;
}
Expand Down Expand Up @@ -829,7 +825,7 @@ public function term_link( $link, $term, $taxonomy ) {

$termlink = apply_filters( 'qts_permastruct', $wp_rewrite->get_extra_permastruct( $taxonomy ), $taxonomy );

$slug = get_metadata( 'term', $term->term_id, QTS_META_PREFIX . $this->get_temp_lang(), true );
$slug = get_metadata( 'term', $term->term_id, QTX_SLUG_META_PREFIX . $this->get_temp_lang(), true );
if ( ! $slug ) {
$slug = $term->slug;
}
Expand All @@ -852,7 +848,7 @@ public function term_link( $link, $term, $taxonomy ) {
foreach ( (array) $ancestors as $ancestor ) {
$ancestor_term = get_term( $ancestor, $taxonomy );

$ancestor_slug = get_metadata( 'term', $ancestor_term->term_id, QTS_META_PREFIX . $this->get_temp_lang(), true );
$ancestor_slug = get_metadata( 'term', $ancestor_term->term_id, QTX_SLUG_META_PREFIX . $this->get_temp_lang(), true );
if ( ! $ancestor_slug ) {
$ancestor_slug = $ancestor_term->slug;
}
Expand Down Expand Up @@ -991,7 +987,7 @@ private function get_page_id_by_path( $page_path, $output = OBJECT, $post_type =
},
$parts );
$in_string = "'" . implode( "','", $parts ) . "'";
$meta_key = QTS_META_PREFIX . $this->get_temp_lang();
$meta_key = QTX_SLUG_META_PREFIX . $this->get_temp_lang();
$post_type_sql = $post_type;
$wpdb->escape_by_ref( $post_type_sql );

Expand Down Expand Up @@ -1106,7 +1102,7 @@ private function get_category_parents( $id, $link = false, $separator = '/', $ni
}

if ( $nicename ) {
$name = get_metadata( 'term', $parent->term_id, QTS_META_PREFIX . $this->get_temp_lang(), true );
$name = get_metadata( 'term', $parent->term_id, QTX_SLUG_META_PREFIX . $this->get_temp_lang(), true );
if ( ! $name ) {
$name = $parent->slug;
}
Expand Down Expand Up @@ -1142,7 +1138,7 @@ private function get_page_uri( $page ) {
$page = get_post( $page );
}

$uri = get_post_meta( $page->ID, QTS_META_PREFIX . $this->get_temp_lang(), true );
$uri = get_post_meta( $page->ID, QTX_SLUG_META_PREFIX . $this->get_temp_lang(), true );
if ( ! $uri ) {
$uri = $page->post_name;
}
Expand All @@ -1155,7 +1151,7 @@ private function get_page_uri( $page ) {
while ( $page->post_parent != 0 ) {
$page = get_post( $page->post_parent );

$page_name = get_post_meta( $page->ID, QTS_META_PREFIX . $this->get_temp_lang(), true );
$page_name = get_post_meta( $page->ID, QTX_SLUG_META_PREFIX . $this->get_temp_lang(), true );
if ( ! $page_name ) {
$page_name = $page->post_name;
}
Expand Down Expand Up @@ -1186,7 +1182,7 @@ private function get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filt
$original_field = $field;

if ( 'slug' == $field ) {
$field = 'm.meta_key = \'' . QTS_META_PREFIX . $this->get_temp_lang() . '\' AND m.meta_value';
$field = 'm.meta_key = \'' . QTX_SLUG_META_PREFIX . $this->get_temp_lang() . '\' AND m.meta_value';
$value = sanitize_title( $value );
if ( empty( $value ) ) {
return false;
Expand Down
28 changes: 14 additions & 14 deletions modules/slugs/includes/qtranslate-slug-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function qts_uninstall() {

$meta_keys = array();
foreach ( $q_config['enabled_languages'] as $lang ) {
$meta_keys[] = QTS_META_PREFIX . $lang;
$meta_keys[] = QTX_SLUG_META_PREFIX . $lang;
}
$meta_keys = "'" . implode( "','", $meta_keys ) . "'";
$wpdb->query( "DELETE from $wpdb->postmeta WHERE meta_key IN ($meta_keys)" );
Expand Down Expand Up @@ -130,7 +130,7 @@ function qts_draw_meta_box( $post ) {
echo '<input type="hidden" name="qts_nonce" id="qts_nonce" value="' . wp_create_nonce( 'qts_nonce' ) . '" />' . PHP_EOL;
$flag_location = qtranxf_flag_location();
foreach ( $q_config['enabled_languages'] as $lang ):
$slug = get_post_meta( $post->ID, QTS_META_PREFIX . $lang, true );
$slug = get_post_meta( $post->ID, QTX_SLUG_META_PREFIX . $lang, true );
$value = ( $slug ) ? htmlspecialchars( $slug, ENT_QUOTES ) : '';
$name = $q_config['language_name'][ $lang ];
$title = sprintf( __( 'Slug' ) . ' (%s)', $name );
Expand All @@ -153,7 +153,7 @@ function qts_draw_meta_box( $post ) {
*/
function qts_validate_post_slug( $slug, $post, $lang ) {
$post_title = trim( qtranxf_use( $lang, $post->post_title ) );
$post_name = get_post_meta( $post->ID, QTS_META_PREFIX . $lang, true );
$post_name = get_post_meta( $post->ID, QTX_SLUG_META_PREFIX . $lang, true );
if ( ! $post_name ) {
$post_name = $post->post_name;
}
Expand Down Expand Up @@ -235,15 +235,15 @@ function qts_wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $po
// TODO: update unique_slug :: missing hieararchical from current wp func ( 4.3.1 )
// Post slugs must be unique across all posts.
$check_sql = "SELECT $wpdb->postmeta.meta_value FROM $wpdb->posts,$wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id AND $wpdb->postmeta.meta_key = '%s' AND $wpdb->postmeta.meta_value = '%s' AND $wpdb->posts.post_type = %s AND ID != %d LIMIT 1";
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, QTS_META_PREFIX . $lang, $slug, $post_type, $post_ID ) );
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, QTX_SLUG_META_PREFIX . $lang, $slug, $post_type, $post_ID ) );

// TODO: update unique_slug :: missing check for conflict with dates archive from current wp func ( 4.3.1 )
if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) {
$suffix = 2;
do {
// TODO: update unique_slug :: same as above: differs from current wp func ( 4.3.1 )
$alt_post_name = substr( $slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, QTS_META_PREFIX . $lang, $alt_post_name, $post_type, $post_ID ) );
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, QTX_SLUG_META_PREFIX . $lang, $alt_post_name, $post_type, $post_ID ) );
$suffix++;
} while ( $post_name_check );
$slug = $alt_post_name;
Expand Down Expand Up @@ -280,8 +280,8 @@ function qts_save_postdata( $post_id, $post = null ) {
// check required because it is not available inside quick edit
if ( isset( $_POST["qts_{$lang}_slug"] ) ) {
$meta_value = apply_filters( 'qts_validate_post_slug', $_POST["qts_{$lang}_slug"], $post, $lang );
delete_post_meta( $post_id, QTS_META_PREFIX . $lang );
update_post_meta( $post_id, QTS_META_PREFIX . $lang, $meta_value );
delete_post_meta( $post_id, QTX_SLUG_META_PREFIX . $lang );
update_post_meta( $post_id, QTX_SLUG_META_PREFIX . $lang, $meta_value );
}
}
}
Expand Down Expand Up @@ -320,15 +320,15 @@ function qts_validate_term_slug( $slug, $term, $lang ) {
function qts_unique_term_slug( $slug, $term, $lang ) {
global $wpdb;

$query = $wpdb->prepare( "SELECT term_id FROM $wpdb->termmeta WHERE meta_key = '%s' AND meta_value = '%s' AND term_id != %d ", QTS_META_PREFIX . $lang, $slug, $term->term_id );
$query = $wpdb->prepare( "SELECT term_id FROM $wpdb->termmeta WHERE meta_key = '%s' AND meta_value = '%s' AND term_id != %d ", QTX_SLUG_META_PREFIX . $lang, $slug, $term->term_id );
$exists_slug = $wpdb->get_results( $query );

if ( empty( $exists_slug ) ) {
return $slug;
}

// If we didn't get a unique slug, try appending a number to make it unique.
$query = $wpdb->prepare( "SELECT meta_value FROM $wpdb->termmeta WHERE meta_key = '%s' AND meta_value = '%s' AND term_id != %d", QTS_META_PREFIX . $lang, $slug, $term->term_id );
$query = $wpdb->prepare( "SELECT meta_value FROM $wpdb->termmeta WHERE meta_key = '%s' AND meta_value = '%s' AND term_id != %d", QTX_SLUG_META_PREFIX . $lang, $slug, $term->term_id );

if ( $wpdb->get_var( $query ) ) {
$num = 2;
Expand All @@ -338,7 +338,7 @@ function qts_unique_term_slug( $slug, $term, $lang ) {
$slug_check = $wpdb->get_var(
$wpdb->prepare(
"SELECT meta_value FROM $wpdb->termmeta WHERE meta_key = '%s' AND meta_value = '%s'",
QTS_META_PREFIX . $lang,
QTX_SLUG_META_PREFIX . $lang,
$alt_slug ) );
} while ( $slug_check );
$slug = $alt_slug;
Expand Down Expand Up @@ -373,8 +373,8 @@ function qts_save_term( $term_id, $tt_id, $taxonomy ) {

$meta_value = apply_filters( 'qts_validate_term_slug', $term_slug, $term, $lang );

delete_metadata( 'term', $term_id, QTS_META_PREFIX . $lang );
update_metadata( 'term', $term_id, QTS_META_PREFIX . $lang, $meta_value );
delete_metadata( 'term', $term_id, QTX_SLUG_META_PREFIX . $lang );
update_metadata( 'term', $term_id, QTX_SLUG_META_PREFIX . $lang, $meta_value );
}
}

Expand All @@ -391,7 +391,7 @@ function qts_show_list_term_fields( $term ) {
$flag_location = qtranxf_flag_location(); ?>
<ul class="qtranxs-slugs-list qtranxs-slugs-terms"><?php
foreach ( $q_config['enabled_languages'] as $lang ) {
$slug = is_object( $term ) ? get_metadata( 'term', $term->term_id, QTS_META_PREFIX . $lang, true ) : '';
$slug = is_object( $term ) ? get_metadata( 'term', $term->term_id, QTX_SLUG_META_PREFIX . $lang, true ) : '';
$value = $slug ? htmlspecialchars( $slug, ENT_QUOTES ) : '';
$flag = $q_config['flag'][ $lang ];
$name = $q_config['language_name'][ $lang ];
Expand Down Expand Up @@ -532,7 +532,7 @@ function qts_taxonomy_custom_column( $str, $column_name, $term_id ) {

switch ( $column_name ) {
case 'qts-slug':
echo get_metadata( 'term', $term_id, QTS_META_PREFIX . $q_config['language'], true );
echo get_metadata( 'term', $term_id, QTX_SLUG_META_PREFIX . $q_config['language'], true );
break;
}

Expand Down
Loading