after-kubio-activation.php
3 months ago
allow-kubio-blog-override.php
2 years ago
cache-plugins.php
4 years ago
default-editor-overlay.php
1 year ago
dismissable-notice.php
1 month ago
gutenerg-plugin-check.php
1 year ago
image-size-auto-fix.php
1 year ago
kubio-fresh-site.php
2 years ago
post-insert.php
2 months ago
register-meta-fields.php
3 years ago
requirements-notices.php
1 year ago
site-urls.php
1 year ago
starter-sites-feature.php
1 year ago
svg-kses.php
1 year ago
wp-import.php
1 month ago
wp-import.php
247 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kubio; |
| 4 | |
| 5 | use IlluminateAgnostic\Arr\Support\Arr; |
| 6 | use Kubio\Core\Importer; |
| 7 | use Kubio\Core\Utils; |
| 8 | |
| 9 | class WPImportFilters { |
| 10 | |
| 11 | private $supported_theme = array( 'elevate-wp', 'pathway', 'kubio', 'pixy' ); |
| 12 | private $global_data_processed = false; |
| 13 | private $fresh_site = false; |
| 14 | |
| 15 | private $handled_by_kubio = array(); |
| 16 | |
| 17 | private function __construct() { |
| 18 | $this->fresh_site = kubio_is_fresh_site(); |
| 19 | |
| 20 | add_action( 'import_end', array( $this, 'handleKubioImports' ) ); |
| 21 | add_action( 'wp_get_object_terms', array( $this, 'addExtraTermsToExport' ) ); |
| 22 | add_filter( 'wp_import_posts', array( $this, 'filterImportedPosts' ), 5 ); |
| 23 | } |
| 24 | |
| 25 | public function addExtraTermsToExport( $terms ) { |
| 26 | if ( defined( 'WXR_VERSION' ) ) { |
| 27 | // exclude if active theme is not kubio compatible |
| 28 | if ( ! kubio_theme_has_kubio_block_support() ) { |
| 29 | return $terms; |
| 30 | } |
| 31 | |
| 32 | $stylesheet = \get_stylesheet(); |
| 33 | foreach ( $terms as $term ) { |
| 34 | if ( $term->taxonomy === 'wp_theme' && $term->name === $stylesheet ) { |
| 35 | $terms[] = (object) array( |
| 36 | 'taxonomy' => '_kubio_suports_rename', |
| 37 | 'slug' => $stylesheet, |
| 38 | 'name' => $stylesheet, |
| 39 | ); |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | return $terms; |
| 46 | } |
| 47 | |
| 48 | public function filterImportedPosts( $posts ) { |
| 49 | |
| 50 | if ( defined( 'KUBIO_IS_STARTER_SITES_IMPORT' ) ) { |
| 51 | return $posts; |
| 52 | } |
| 53 | |
| 54 | $template = get_stylesheet(); |
| 55 | $is_supported_theme = kubio_theme_has_kubio_block_support(); |
| 56 | |
| 57 | wp_insert_term( $template, 'wp_theme', array( 'slug' => $template ) ); |
| 58 | |
| 59 | $this->log( "Theme: {$template}" ); |
| 60 | $this->log( $this->fresh_site ? 'Fresh site' : 'Not a fresh site' ); |
| 61 | |
| 62 | $contains_pages = false; |
| 63 | foreach ( $posts as $index => $post ) { |
| 64 | $post_type = Arr::get( $post, 'post_type', null ); |
| 65 | |
| 66 | switch ( $post_type ) { |
| 67 | case 'page': |
| 68 | $contains_pages = true; |
| 69 | break; |
| 70 | case 'wp_template': |
| 71 | case 'wp_template_part': |
| 72 | $posts[ $index ] = $this->processFSETemplate( $post, $template, $is_supported_theme ); |
| 73 | break; |
| 74 | |
| 75 | case kubio_global_data_post_type(): |
| 76 | $posts[ $index ] = $this->processGlobalData( $posts ); |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // remove precreated front and blog page if the site is fresh & the xml file contains pages to reduce pages duplication |
| 82 | if ( count( $this->handled_by_kubio ) > 0 && $contains_pages ) { |
| 83 | $this->log( 'Cleanup Kubio intial pages' ); |
| 84 | wp_cache_flush(); |
| 85 | $page_on_front = intval( get_option( 'page_on_front', 0 ) ); |
| 86 | $page_for_posts = intval( get_option( 'page_for_posts', 0 ) ); |
| 87 | |
| 88 | if ( $page_on_front && intval( get_post_meta( $page_on_front, '_kubio_created_at_activation', true ) ) ) { |
| 89 | update_option( 'show_on_front', 'posts' ); |
| 90 | wp_delete_post( $page_on_front, true ); |
| 91 | } |
| 92 | |
| 93 | if ( $page_for_posts && intval( get_post_meta( $page_for_posts, '_kubio_created_at_activation', true ) ) ) { |
| 94 | wp_delete_post( $page_for_posts, true ); |
| 95 | } |
| 96 | wp_cache_flush(); |
| 97 | } |
| 98 | |
| 99 | $posts = array_filter( $posts ); |
| 100 | |
| 101 | return $posts; |
| 102 | } |
| 103 | |
| 104 | private function processFSETemplate( $post, $template, $is_supported_theme ) { |
| 105 | $terms = Arr::get( $post, 'terms', array() ); |
| 106 | $supports_rename = false; |
| 107 | |
| 108 | // remove _kubio_suports_rename temporrary term if exists |
| 109 | foreach ( $terms as $term_index => $term ) { |
| 110 | $domain = Arr::get( $term, 'domain', null ); |
| 111 | if ( $domain === '_kubio_suports_rename' ) { |
| 112 | $supports_rename = true; |
| 113 | unset( $terms[ $term_index ] ); |
| 114 | break; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if ( ! $is_supported_theme || ! $this->fresh_site ) { |
| 119 | $post['terms'] = $terms; |
| 120 | return $post; |
| 121 | } |
| 122 | |
| 123 | $use_kubio_importer = $supports_rename; |
| 124 | |
| 125 | foreach ( $terms as $term ) { |
| 126 | $domain = Arr::get( $term, 'domain', null ); |
| 127 | if ( $domain === 'wp_theme' ) { |
| 128 | $use_kubio_importer = $use_kubio_importer || in_array( $term['slug'], $this->supported_theme, true ); |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | if ( $use_kubio_importer ) { |
| 134 | $blocks = parse_blocks( $post['post_content'] ); |
| 135 | $blocks = kubio_blocks_update_template_parts_theme( $blocks, $template ); |
| 136 | $content = kubio_serialize_blocks( $blocks ); |
| 137 | |
| 138 | $this->handled_by_kubio[] = array( |
| 139 | 'slug' => $post['post_name'], |
| 140 | 'type' => $post['post_type'], |
| 141 | 'content' => $content, |
| 142 | ); |
| 143 | |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | return $post; |
| 148 | } |
| 149 | |
| 150 | private function processGlobalData( $posts ) { |
| 151 | if ( $this->global_data_processed || ! $this->fresh_site ) { |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | $global_data_post = null; |
| 156 | |
| 157 | foreach ( $posts as $post ) { |
| 158 | $post_type = Arr::get( $post, 'post_type', null ); |
| 159 | if ( $post_type !== kubio_global_data_post_type() ) { |
| 160 | continue; |
| 161 | } |
| 162 | |
| 163 | if ( ! $global_data_post ) { |
| 164 | $global_data_post = $post; |
| 165 | } |
| 166 | |
| 167 | $terms = Arr::get( $post, 'terms', array() ); |
| 168 | |
| 169 | $is_theme_global_data = false; |
| 170 | |
| 171 | foreach ( $terms as $term ) { |
| 172 | $domain = Arr::get( $term, 'domain', null ); |
| 173 | if ( $domain === '_kubio_suports_rename' ) { |
| 174 | $is_theme_global_data = true; |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if ( $is_theme_global_data ) { |
| 180 | $global_data_post = $post; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | $this->handled_by_kubio[] = array( |
| 186 | 'slug' => kubio_global_data_post_type(), |
| 187 | 'type' => kubio_global_data_post_type(), |
| 188 | 'content' => $global_data_post['post_content'], |
| 189 | ); |
| 190 | |
| 191 | $this->global_data_processed = true; |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | public function handleKubioImports() { |
| 196 | |
| 197 | $stylesheet = \get_stylesheet(); |
| 198 | |
| 199 | if ( Utils::isCLI() ) { |
| 200 | add_filter( 'user_has_cap', array( Importer::class, 'allowImportCaps' ), 10, 2 ); |
| 201 | } |
| 202 | |
| 203 | if ( ! count( $this->handled_by_kubio ) ) { |
| 204 | _kubio_remove_fresh_install_flag(); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | foreach ( $this->handled_by_kubio as $import ) { |
| 209 | $type = $import['type']; |
| 210 | |
| 211 | $this->log( "Import entity: {$type} - {$import['slug']}" ); |
| 212 | |
| 213 | switch ( $type ) { |
| 214 | case 'wp_template': |
| 215 | Importer::createTemplate( $import['slug'], $import['content'], true, 'kubio' ); |
| 216 | break; |
| 217 | case 'wp_template_part': |
| 218 | Importer::createTemplatePart( $import['slug'], $import['content'], true, 'kubio' ); |
| 219 | break; |
| 220 | case kubio_global_data_post_type(): |
| 221 | kubio_replace_global_data_content( $import['content'], $stylesheet ); |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | $this->handled_by_kubio = array(); |
| 227 | } |
| 228 | |
| 229 | public static function load() { |
| 230 | new static(); |
| 231 | } |
| 232 | |
| 233 | private function log( $message ) { |
| 234 | if ( defined( 'WP_CLI' ) ) { |
| 235 | \WP_CLI::log( "Kubio Imported -- $message" ); |
| 236 | } else { |
| 237 | if ( defined( 'WP_DEBUG' ) ) { |
| 238 | printf( '<script>console.log(%s)</script>', wp_json_encode( "Kubio Log: {$message}" ) ); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | |
| 245 | |
| 246 | WPImportFilters::load(); |
| 247 |