Helper.php
3 months ago
Importer_Alterator.php
3 months ago
Quiet_Skin.php
5 years ago
Quiet_Skin_Legacy.php
5 years ago
Slug_Mapping.php
3 months ago
Importer_Alterator.php
308 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Importer alterator. |
| 4 | * |
| 5 | * Used to alter import content. |
| 6 | * |
| 7 | * @package templates-patterns-collection |
| 8 | */ |
| 9 | |
| 10 | |
| 11 | namespace TIOB\Importers\Helpers; |
| 12 | |
| 13 | /** |
| 14 | * Class Importer_Alterator |
| 15 | */ |
| 16 | class Importer_Alterator { |
| 17 | use Helper; |
| 18 | |
| 19 | /** |
| 20 | * Post map. Holds post type / count. |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | private $post_map = array(); |
| 25 | |
| 26 | /** |
| 27 | * Post types that will be ignored if there are more than 2 already on the site. |
| 28 | * |
| 29 | * @var array |
| 30 | */ |
| 31 | private $filtered_post_types = array( |
| 32 | 'post', |
| 33 | 'product', |
| 34 | ); |
| 35 | |
| 36 | /** |
| 37 | * Data passed from the async request for individual site item. |
| 38 | * |
| 39 | * @var array |
| 40 | */ |
| 41 | private $site_json_data; |
| 42 | |
| 43 | private $processed_terms; |
| 44 | |
| 45 | /** |
| 46 | * Importer_Alterator constructor. |
| 47 | * |
| 48 | * @param array $site_json_data the sites data passed from content import. |
| 49 | */ |
| 50 | public function __construct( $site_json_data ) { |
| 51 | $this->site_json_data = $site_json_data; |
| 52 | $this->count_posts_by_post_type(); |
| 53 | add_filter( 'wp_import_posts', array( $this, 'skip_shop_pages' ), 10 ); |
| 54 | add_filter( 'wp_import_posts', array( $this, 'skip_posts' ), 10 ); |
| 55 | add_filter( 'wp_import_posts', array( $this, 'drop_slug_and_prefix_pages' ), 10 ); |
| 56 | add_filter( 'wp_import_terms', array( $this, 'skip_terms' ), 10 ); |
| 57 | add_filter( 'wp_insert_post_data', array( $this, 'encode_post_content' ), 10, 2 ); |
| 58 | add_filter( 'wp_import_nav_menu_item_args', array( $this, 'change_nav_menu_item_link' ), 10, 2 ); |
| 59 | add_filter( 'intermediate_image_sizes_advanced', array( $this, 'filter_sizes' ), 10, 1 ); |
| 60 | add_filter( 'tpc_post_content_before_insert', array( $this, 'replace_links' ), 10, 2 ); |
| 61 | add_filter( 'tpc_post_content_processed_terms', array( $this, 'content_to_import' ), 10, 2 ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Filter the sizes array on image attachment to only default sizes. |
| 66 | * |
| 67 | * @param array $new_sizes The sizes array to use when resizing. |
| 68 | * |
| 69 | * @return array |
| 70 | */ |
| 71 | public function filter_sizes( $new_sizes ) { |
| 72 | return array_filter( |
| 73 | $new_sizes, |
| 74 | function ( $key ) { |
| 75 | return in_array( $key, array( 'thumbnail', 'medium' ) ); |
| 76 | }, |
| 77 | ARRAY_FILTER_USE_KEY |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Remap method for category terms. |
| 83 | * |
| 84 | * @param mixed $input |
| 85 | * @return array|mixed|string|string[]|null |
| 86 | */ |
| 87 | private function remap_category_recursively( $input ) { |
| 88 | $re = '/replace_cat_(?<categoryID>\d+)/m'; |
| 89 | if ( is_array( $input ) ) { |
| 90 | return ( isset( $this->processed_terms[ $input['categoryID'] ] ) ) ? $this->processed_terms[ $input['categoryID'] ] : $input['categoryID']; |
| 91 | } |
| 92 | return preg_replace_callback( $re, array( $this, 'remap_category_recursively' ), $input ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Process imported content and remap terms. |
| 97 | * |
| 98 | * @param string $content The content. |
| 99 | * @param array $processed_terms The list of processed terms. |
| 100 | * @return string |
| 101 | */ |
| 102 | public function content_to_import( $content, $processed_terms ) { |
| 103 | $this->processed_terms = $processed_terms; |
| 104 | if ( strpos( $content, 'wp:woocommerce' ) !== false ) { |
| 105 | $new_content = $this->remap_category_recursively( $content ); |
| 106 | return $new_content; |
| 107 | } |
| 108 | |
| 109 | return $content; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Prefix Front / Blog page slug. |
| 114 | * |
| 115 | * @param array $posts the posts to import array. |
| 116 | * |
| 117 | * @return array |
| 118 | */ |
| 119 | public function drop_slug_and_prefix_pages( $posts ) { |
| 120 | $slug_map = array(); |
| 121 | $reserved_slugs = array(); |
| 122 | |
| 123 | foreach ( $posts as $index => $post ) { |
| 124 | if ( $post['post_type'] !== 'page' ) { |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | $old_slug = $post['post_name']; |
| 129 | $new_slug = $this->cleanup_page_slug( |
| 130 | $old_slug, |
| 131 | $this->site_json_data['demoSlug'], |
| 132 | true, |
| 133 | $reserved_slugs |
| 134 | ); |
| 135 | |
| 136 | $posts[ $index ]['post_name'] = $new_slug; |
| 137 | $reserved_slugs[] = $new_slug; |
| 138 | if ( ! isset( $slug_map[ $old_slug ] ) ) { |
| 139 | $slug_map[ $old_slug ] = $new_slug; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if ( ! empty( $slug_map ) ) { |
| 144 | Slug_Mapping::set_slug_map( $slug_map ); |
| 145 | } |
| 146 | |
| 147 | return $posts; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Change nav menu items link if needed. |
| 152 | * |
| 153 | * @param array $args menu item args. |
| 154 | * @param string $import_source_url the source url. |
| 155 | * |
| 156 | * @return array |
| 157 | */ |
| 158 | public function change_nav_menu_item_link( $args, $import_source_url ) { |
| 159 | Slug_Mapping::register_source_url( $import_source_url ); |
| 160 | $args['menu-item-url'] = Slug_Mapping::rewrite_url( $args['menu-item-url'] ); |
| 161 | |
| 162 | return $args; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Replace content links. |
| 167 | * |
| 168 | * @hooked tpc_post_content_before_insert - 10 |
| 169 | * |
| 170 | * @param string $content post content. |
| 171 | * @param string $old_base_url old site URL. |
| 172 | * |
| 173 | * @return string |
| 174 | */ |
| 175 | public function replace_links( $content, $old_base_url ) { |
| 176 | Slug_Mapping::register_source_url( $old_base_url ); |
| 177 | $content = $this->replace_image_urls( $content ); |
| 178 | $content = Slug_Mapping::rewrite_value( $content ); |
| 179 | return $content; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Encode post content to UTF8 for possible issues with locales. |
| 184 | * |
| 185 | * @param array $data post data |
| 186 | * @param array $postarr post array |
| 187 | * |
| 188 | * @return array |
| 189 | */ |
| 190 | public function encode_post_content( $data, $postarr ) { |
| 191 | if ( isset( $this->site_json_data['editor'] ) && $this->site_json_data['editor'] === 'gutenberg' ) { |
| 192 | return $data; |
| 193 | } |
| 194 | $data['post_content'] = utf8_encode( $data['post_content'] ); |
| 195 | |
| 196 | return $data; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Skip posts if there are more than 2 already. |
| 201 | * |
| 202 | * @param array $posts post data. |
| 203 | * |
| 204 | * @return array |
| 205 | */ |
| 206 | public function skip_posts( $posts ) { |
| 207 | return array_filter( |
| 208 | $posts, |
| 209 | function ( $post_data ) { |
| 210 | if ( ! array_key_exists( $post_data['post_type'], $this->post_map ) ) { |
| 211 | return true; |
| 212 | } |
| 213 | if ( $this->post_map[ $post_data['post_type'] ] <= 2 ) { |
| 214 | return true; |
| 215 | } |
| 216 | |
| 217 | return false; |
| 218 | } |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Skip shop pages if no WooCommerce. |
| 224 | * |
| 225 | * @param array $posts posts data. |
| 226 | * |
| 227 | * @return array |
| 228 | */ |
| 229 | public function skip_shop_pages( $posts ) { |
| 230 | if ( ! isset( $this->site_json_data['shopPages'] ) || $this->site_json_data['shopPages'] === null || ! is_array( $this->site_json_data['shopPages'] ) ) { |
| 231 | return $posts; |
| 232 | } |
| 233 | |
| 234 | if ( class_exists( 'WooCommerce' ) ) { |
| 235 | return $posts; |
| 236 | } |
| 237 | |
| 238 | return array_filter( |
| 239 | $posts, |
| 240 | function ( $post_data ) { |
| 241 | if ( $post_data['post_type'] === 'product' ) { |
| 242 | return false; |
| 243 | } |
| 244 | if ( |
| 245 | $post_data['post_type'] === 'page' && |
| 246 | in_array( $post_data['post_name'], $this->site_json_data['shopPages'] ) |
| 247 | ) { |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | return true; |
| 252 | } |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Skips terms for post types that were skipped. |
| 258 | * |
| 259 | * @param array $terms terms data. |
| 260 | * |
| 261 | * @return array |
| 262 | */ |
| 263 | public function skip_terms( $terms ) { |
| 264 | foreach ( $this->filtered_post_types as $post_type ) { |
| 265 | if ( ! $this->post_map[ $post_type ] <= 2 ) { |
| 266 | continue; |
| 267 | } |
| 268 | |
| 269 | $terms = array_filter( |
| 270 | $terms, |
| 271 | function ( $term ) use ( $post_type ) { |
| 272 | return $this->is_taxonomy_assigned_to_post_type( $post_type, $term['term_taxonomy'] ); |
| 273 | } |
| 274 | ); |
| 275 | } |
| 276 | |
| 277 | return $terms; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Checks if taxonomy is assigned to post type. |
| 282 | * |
| 283 | * @param string $post_type post type slug. |
| 284 | * @param string $taxonomy taxonomy slug. |
| 285 | * |
| 286 | * @return bool |
| 287 | */ |
| 288 | private function is_taxonomy_assigned_to_post_type( $post_type, $taxonomy ) { |
| 289 | $taxonomies = get_object_taxonomies( $post_type ); |
| 290 | |
| 291 | return in_array( $taxonomy, $taxonomies ); |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Count excluded post types. |
| 296 | */ |
| 297 | private function count_posts_by_post_type() { |
| 298 | foreach ( $this->filtered_post_types as $post_type ) { |
| 299 | $args = array( |
| 300 | 'post_type' => $post_type, |
| 301 | ); |
| 302 | $the_query = new \WP_Query( $args ); |
| 303 | |
| 304 | $this->post_map[ $post_type ] = absint( $the_query->found_posts ); |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 |