Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 70
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
wpcom_imports_register_imports
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
wpcom_import_update_wordpress_url_on_simple
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
wpcom_imports_enqueue_script
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Register Calypso import entries in WP-Admin/import.php
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8declare(strict_types=1);
9
10/**
11 * Register the imports.
12 *
13 * WPCOM Simple has a custom filter for the url. However, that filter is not available on AT.
14 * To avoid having duplicate implementations, the implementation uses a redirect.
15 *
16 * @return void
17 */
18function wpcom_imports_register_imports() {
19    $page = function () { };
20
21    $squarespace_description = __( 'Import <strong>posts, comments, images and tags</strong> from a Squarespace export file.', 'jetpack-mu-wpcom' );
22    $medium_description      = __( 'Import <strong>posts</strong> from a Medium export file.', 'jetpack-mu-wpcom' );
23    $wix_description         = __( 'Import <strong>posts, pages, and media</strong> from your Wix.com site.', 'jetpack-mu-wpcom' );
24    $substack_description    = __( 'Import <strong>content and subscribers</strong> from your Substack site.', 'jetpack-mu-wpcom' );
25
26    register_importer( 'wpcom-squarespace', __( 'Squarespace', 'jetpack-mu-wpcom' ), $squarespace_description, $page );
27    register_importer( 'wpcom-medium', _x( 'Medium', 'Company name', 'jetpack-mu-wpcom' ), $medium_description, $page );
28    register_importer( 'wpcom-wix', __( 'Wix', 'jetpack-mu-wpcom' ), $wix_description, $page );
29    register_importer( 'wpcom-substack', __( 'Substack', 'jetpack-mu-wpcom' ), $substack_description, $page );
30}
31
32add_action( 'admin_init', 'wpcom_imports_register_imports' );
33
34/**
35 * Redirect to Calypso Stepper Squarespace importer.
36 */
37add_action(
38    'load-importer-wpcom-squarespace',
39    /**
40     * Redirect to the Squarespace importer in the Calypso Stepper.
41     *
42     * @return never-return
43     */
44    function () {
45        $domain = wp_parse_url( home_url(), PHP_URL_HOST );
46        // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
47        wp_redirect( 'https://wordpress.com/setup/site-setup/importerSquarespace?ref=wp-admin-importers-list-direct-importer&siteSlug=' . $domain );
48        exit();
49    }
50);
51
52/**
53 * Redirect to Calypso Stepper Medium importer.
54 */
55add_action(
56    'load-importer-wpcom-medium',
57    /**
58     * Redirect to the Medium importer in the Calypso Stepper.
59     *
60     * @return never-return
61     */
62    function () {
63        $domain = wp_parse_url( home_url(), PHP_URL_HOST );
64        // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
65        wp_redirect( 'https://wordpress.com/setup/site-setup/importerMedium?ref=wp-admin-importers-list-direct-importer&siteSlug=' . $domain );
66        exit();
67    }
68);
69
70/**
71 * Redirect to Calypso Stepper Wix importer
72 */
73add_action(
74    'load-importer-wpcom-wix',
75    /**
76     * Redirect to the Wix importer in the Calypso Stepper.
77     *
78     * @return never-return
79     */
80    function () {
81        $domain  = wp_parse_url( home_url(), PHP_URL_HOST );
82        $site_id = get_wpcom_blog_id();
83        // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
84        wp_redirect( 'https://wordpress.com/setup/site-migration/site-migration-identify?platform=wix&action=skip_platform_identification&hide_importer_link=true&siteSlug=' . $domain . '&siteId=' . $site_id );
85        exit();
86    }
87);
88
89/**
90 * Redirect to Calypso Stepper Substack importer.
91 */
92add_action(
93    'load-importer-wpcom-substack',
94    /**
95     * Redirect to the Substack importer in the Calypso Stepper.
96     *
97     * @return never-return
98     */
99    function () {
100        $domain = wp_parse_url( home_url(), PHP_URL_HOST );
101        // phpcs:ignore WordPress.Security.SafeRedirect.wp_redirect_wp_redirect
102        wp_redirect( 'https://wordpress.com/setup/site-setup/importerSubstack?ref=wp-admin-importers-list-direct-importer&siteSlug=' . $domain );
103        exit();
104    }
105);
106
107/**
108 * Update the WordPress importer URL to point to the Calypso Stepper importer.
109 *
110 * @param string $url the Importer URL.
111 * @param string $importer_type The type of the importer (e.g. WordPress).
112 *
113 * @return string
114 */
115function wpcom_import_update_wordpress_url_on_simple( $url, $importer_type ) {
116    // @phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText
117    if ( 'wordpress' !== $importer_type ) {
118        return $url;
119    }
120
121    $domain = wp_parse_url( home_url(), PHP_URL_HOST );
122
123    return 'https://wordpress.com/setup/site-setup/importerWordpress?ref=wp-admin-importers-list-direct-importer&siteSlug=' . $domain;
124}
125
126/**
127 * Although the hook is prefixed with wp_*, it's actually a custom WPCOM one that's only executed on Simple Sites.
128 */
129add_action( 'wp_import_run_import_url_filter', 'wpcom_import_update_wordpress_url_on_simple', 11, 2 );
130
131/**
132 * Enqueue the wpcom importer entry script.
133 */
134function wpcom_imports_enqueue_script() {
135    wp_enqueue_script(
136        'wpcom-importer-entry',
137        plugins_url( 'wpcom-importer-entry.js', __FILE__ ),
138        array( 'wp-i18n', 'wp-dom-ready' ),
139        '1.0.1',
140        true
141    );
142
143    wp_set_script_translations( 'wpcom-importer-entry', 'jetpack-mu-wpcom' );
144
145    $domain  = wp_parse_url( home_url(), PHP_URL_HOST );
146    $site_id = get_wpcom_blog_id();
147
148    $url = 'https://wordpress.com/setup/site-migration/site-migration-identify?hide_importer_link=true&siteSlug=' . $domain . '&siteId=' . $site_id;
149
150    wp_add_inline_script(
151        'wpcom-importer-entry',
152        'const wpcomImporterData = ' . wp_json_encode(
153            array(
154                'wpcomImporterUrl' => $url,
155            ),
156            JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP
157        ) . ';',
158        'before'
159    );
160}
161
162add_action( 'admin_print_styles-import.php', 'wpcom_imports_enqueue_script' );