| @@ -1,8 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | /** |
| 4 | - * A class to import languages and translations information form a WXR file | |
| 4 | + * a class to import languages and translations information form a WXR file | |
| 5 | 5 | * |
| 6 | 6 | * @since 1.2 |
| 7 | 7 | */ |
| 8 | 8 | class PLL_WP_Import extends WP_Import { |
| @@ -8,16 +8,16 @@ | ||
| 8 | 8 | class PLL_WP_Import extends WP_Import { |
| 9 | 9 | public $post_translations = array(); |
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | - * Overrides WP_Import::process_terms to remap terms translations | |
| 12 | + * overrides WP_Import::process_terms to remap terms translations | |
| 13 | 13 | * |
| 14 | 14 | * @since 1.2 |
| 15 | 15 | */ |
| 16 | - public function process_terms() { | |
| 16 | + function process_terms() { | |
| 17 | 17 | $term_translations = array(); |
| 18 | 18 | |
| 19 | - // Store this for future usage as parent function unsets $this->terms | |
| 19 | + // store this for future usage as parent function unsets $this->terms | |
| 20 | 20 | foreach ( $this->terms as $term ) { |
| 21 | 21 | if ( 'post_translations' == $term['term_taxonomy'] ) { |
| 22 | 22 | $this->post_translations[] = $term; |
| 23 | 23 | } |
| @@ -27,15 +27,15 @@ | ||
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | parent::process_terms(); |
| 30 | 30 | |
| 31 | - // Update the languages list if needed | |
| 32 | - // First reset the core terms cache as WordPress Importer calls wp_suspend_cache_invalidation( true ); | |
| 31 | + // update the languages list if needed | |
| 32 | + // first reset the core terms cache as WordPress Importer calls wp_suspend_cache_invalidation( true ); | |
| 33 | 33 | wp_cache_set( 'last_changed', microtime(), 'terms' ); |
| 34 | 34 | PLL()->model->clean_languages_cache(); |
| 35 | 35 | |
| 36 | 36 | if ( ( $options = get_option( 'polylang' ) ) && empty( $options['default_lang'] ) && ( $languages = PLL()->model->get_languages_list() ) ) { |
| 37 | - // Assign the default language if importer created the first language | |
| 37 | + // assign the default language if importer created the first language | |
| 38 | 38 | $default_lang = reset( $languages ); |
| 39 | 39 | $options['default_lang'] = $default_lang->slug; |
| 40 | 40 | update_option( 'polylang', $options ); |
| 41 | 41 | } |
| @@ -44,17 +44,17 @@ | ||
| 44 | 44 | $this->remap_translations( $term_translations, $this->processed_terms ); |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
| 48 | - * Overrides WP_Import::process_post to remap posts translations | |
| 49 | - * Also merges strings translations from the WXR file to the existing ones | |
| 48 | + * overrides WP_Import::process_post to remap posts translations | |
| 49 | + * also merges strings translations from the WXR file to the existing ones | |
| 50 | 50 | * |
| 51 | 51 | * @since 1.2 |
| 52 | 52 | */ |
| 53 | - public function process_posts() { | |
| 53 | + function process_posts() { | |
| 54 | 54 | $menu_items = $mo_posts = array(); |
| 55 | 55 | |
| 56 | - // Store this for future usage as parent function unset $this->posts | |
| 56 | + // store this for future usage as parent function unset $this->posts | |
| 57 | 57 | foreach ( $this->posts as $post ) { |
| 58 | 58 | if ( 'nav_menu_item' == $post['post_type'] ) { |
| 59 | 59 | $menu_items[] = $post; |
| 60 | 60 | } |
| @@ -64,19 +64,19 @@ | ||
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | if ( ! empty( $mo_posts ) ) { |
| 68 | - new PLL_MO(); // Just to register the polylang_mo post type before processing posts | |
| 68 | + new PLL_MO(); // just to register the polylang_mo post type before processing posts | |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | parent::process_posts(); |
| 72 | 72 | |
| 73 | - PLL()->model->clean_languages_cache(); // To update the posts count in ( cached ) languages list | |
| 73 | + PLL()->model->clean_languages_cache(); // to update the posts count in ( cached ) languages list | |
| 74 | 74 | |
| 75 | 75 | $this->remap_translations( $this->post_translations, $this->processed_posts ); |
| 76 | 76 | unset( $this->post_translations ); |
| 77 | 77 | |
| 78 | - // Language switcher menu items | |
| 78 | + // language switcher menu items | |
| 79 | 79 | foreach ( $menu_items as $item ) { |
| 80 | 80 | foreach ( $item['postmeta'] as $meta ) { |
| 81 | 81 | if ( '_pll_menu_item' == $meta['key'] ) { |
| 82 | 82 | update_post_meta( $this->processed_menu_items[ $item['post_id'] ], '_pll_menu_item', maybe_unserialize( $meta['value'] ) ); |
| @@ -83,14 +83,14 @@ | ||
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - // Merge strings translations | |
| 87 | + // merge strings translations | |
| 88 | 88 | foreach ( $mo_posts as $post ) { |
| 89 | 89 | $lang_id = (int) substr( $post['post_title'], 12 ); |
| 90 | 90 | |
| 91 | 91 | if ( ! empty( $this->processed_terms[ $lang_id ] ) ) { |
| 92 | - if ( $strings = maybe_unserialize( $post['post_content'] ) ) { | |
| 92 | + if ( $strings = unserialize( $post['post_content'] ) ) { | |
| 93 | 93 | $mo = new PLL_MO(); |
| 94 | 94 | $mo->import_from_db( $this->processed_terms[ $lang_id ] ); |
| 95 | 95 | foreach ( $strings as $msg ) { |
| 96 | 96 | $mo->add_entry_or_merge( $mo->make_entry( $msg[0], $msg[1] ) ); |
| @@ -97,48 +97,46 @@ | ||
| 97 | 97 | } |
| 98 | 98 | $mo->export_to_db( $this->processed_terms[ $lang_id ] ); |
| 99 | 99 | } |
| 100 | 100 | } |
| 101 | - // Delete the now useless imported post | |
| 101 | + // delete the now useless imported post | |
| 102 | 102 | wp_delete_post( $this->processed_posts[ $post['post_id'] ], true ); |
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | - * Remaps terms languages | |
| 107 | + * remaps terms languages | |
| 108 | 108 | * |
| 109 | 109 | * @since 1.2 |
| 110 | 110 | * |
| 111 | 111 | * @param array $terms array of terms in 'term_translations' taxonomy |
| 112 | 112 | */ |
| 113 | - protected function remap_terms_relations( &$terms ) { | |
| 113 | + function remap_terms_relations( &$terms ) { | |
| 114 | 114 | global $wpdb; |
| 115 | 115 | |
| 116 | - $trs = array(); | |
| 117 | - | |
| 118 | 116 | foreach ( $terms as $term ) { |
| 119 | - $translations = maybe_unserialize( $term['term_description'] ); | |
| 117 | + $translations = unserialize( $term['term_description'] ); | |
| 120 | 118 | foreach ( $translations as $slug => $old_id ) { |
| 121 | 119 | if ( $old_id && ! empty( $this->processed_terms[ $old_id ] ) && $lang = PLL()->model->get_language( $slug ) ) { |
| 122 | - // Language relationship | |
| 120 | + // language relationship | |
| 123 | 121 | $trs[] = $wpdb->prepare( '( %d, %d )', $this->processed_terms[ $old_id ], $lang->tl_term_taxonomy_id ); |
| 124 | 122 | |
| 125 | - // Translation relationship | |
| 123 | + // translation relationship | |
| 126 | 124 | $trs[] = $wpdb->prepare( '( %d, %d )', $this->processed_terms[ $old_id ], get_term( $this->processed_terms[ $term['term_id'] ], 'term_translations' )->term_taxonomy_id ); |
| 127 | 125 | } |
| 128 | 126 | } |
| 129 | 127 | } |
| 130 | 128 | |
| 131 | - // Insert term_relationships | |
| 129 | + // insert term_relationships | |
| 132 | 130 | if ( ! empty( $trs ) ) { |
| 133 | 131 | $trs = array_unique( $trs ); |
| 134 | 132 | |
| 135 | - // Make sure we don't attempt to insert already existing term relationships | |
| 136 | - $existing_trs = $wpdb->get_results( | |
| 137 | - "SELECT tr.object_id, tr.term_taxonomy_id FROM {$wpdb->term_relationships} AS tr | |
| 138 | - INNER JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id | |
| 139 | - WHERE tt.taxonomy IN ( 'term_language', 'term_translations' )" | |
| 140 | - ); | |
| 133 | + // make sure we don't attempt to insert already existing term relationships | |
| 134 | + $existing_trs = $wpdb->get_results( " | |
| 135 | + SELECT tr.object_id, tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr | |
| 136 | + INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id | |
| 137 | + WHERE tt.taxonomy IN ( 'term_language', 'term_translations' ) | |
| 138 | + " ); | |
| 141 | 139 | |
| 142 | 140 | foreach ( $existing_trs as $key => $tr ) { |
| 143 | 141 | $existing_trs[ $key ] = $wpdb->prepare( '( %d, %d )', $tr->object_id, $tr->term_taxonomy_id ); |
| 144 | 142 | } |
| @@ -145,16 +143,15 @@ | ||
| 145 | 143 | |
| 146 | 144 | $trs = array_diff( $trs, $existing_trs ); |
| 147 | 145 | |
| 148 | 146 | if ( ! empty( $trs ) ) { |
| 149 | - // PHPCS:ignore WordPress.DB.PreparedSQL.NotPrepared | |
| 150 | - $wpdb->query( "INSERT INTO {$wpdb->term_relationships} ( object_id, term_taxonomy_id ) VALUES " . implode( ',', $trs ) ); | |
| 147 | + $wpdb->query( "INSERT INTO $wpdb->term_relationships ( object_id, term_taxonomy_id ) VALUES " . implode( ',', $trs ) ); | |
| 151 | 148 | } |
| 152 | 149 | } |
| 153 | 150 | } |
| 154 | 151 | |
| 155 | 152 | /** |
| 156 | - * Remaps translations for both posts and terms | |
| 153 | + * remaps translations for both posts and terms | |
| 157 | 154 | * |
| 158 | 155 | * @since 1.2 |
| 159 | 156 | * |
| 160 | 157 | * @param array $terms array of terms in 'post_translations' or 'term_translations' taxonomies |
| @@ -159,15 +156,13 @@ | ||
| 159 | 156 | * |
| 160 | 157 | * @param array $terms array of terms in 'post_translations' or 'term_translations' taxonomies |
| 161 | 158 | * @param array $processed_objects array of posts or terms processed by WordPress Importer |
| 162 | 159 | */ |
| 163 | - protected function remap_translations( &$terms, &$processed_objects ) { | |
| 160 | + function remap_translations( &$terms, &$processed_objects ) { | |
| 164 | 161 | global $wpdb; |
| 165 | 162 | |
| 166 | - $u = array(); | |
| 167 | - | |
| 168 | 163 | foreach ( $terms as $term ) { |
| 169 | - $translations = maybe_unserialize( $term['term_description'] ); | |
| 164 | + $translations = unserialize( $term['term_description'] ); | |
| 170 | 165 | $new_translations = array(); |
| 171 | 166 | |
| 172 | 167 | foreach ( $translations as $slug => $old_id ) { |
| 173 | 168 | if ( $old_id && ! empty( $processed_objects[ $old_id ] ) ) { |
| @@ -175,20 +170,16 @@ | ||
| 175 | 170 | } |
| 176 | 171 | } |
| 177 | 172 | |
| 178 | 173 | if ( ! empty( $new_translations ) ) { |
| 179 | - $u['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $this->processed_terms[ $term['term_id'] ], maybe_serialize( $new_translations ) ); | |
| 174 | + $u['case'][] = $wpdb->prepare( 'WHEN %d THEN %s', $this->processed_terms[ $term['term_id'] ], serialize( $new_translations ) ); | |
| 180 | 175 | $u['in'][] = (int) $this->processed_terms[ $term['term_id'] ]; |
| 181 | 176 | } |
| 182 | 177 | } |
| 183 | 178 | |
| 184 | 179 | if ( ! empty( $u ) ) { |
| 185 | - // PHPCS:disable WordPress.DB.PreparedSQL.NotPrepared | |
| 186 | - $wpdb->query( | |
| 187 | - "UPDATE {$wpdb->term_taxonomy} | |
| 188 | - SET description = ( CASE term_id " . implode( ' ', $u['case'] ) . ' END ) | |
| 189 | - WHERE term_id IN ( ' . implode( ',', $u['in'] ) . ' )' | |
| 190 | - ); | |
| 191 | - // PHPCS:enable | |
| 180 | + $wpdb->query( "UPDATE $wpdb->term_taxonomy | |
| 181 | + SET description = ( CASE term_id " . implode( ' ', $u['case'] ) . " END ) | |
| 182 | + WHERE term_id IN ( " . implode( ',', $u['in'] ) . " )" ); | |
| 192 | 183 | } |
| 193 | 184 | } |
| 194 | 185 | } |