Completed
Push — add/custom-import-experience ( 23f9ba...c144d7 )
by
unknown
20:47 queued 13:31
created

Jetpack_Unified_Importer_Module::ui_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Unified_Importer_Module {
4
	const UI_ACTION_ARG = 'jetpack_import_ui';
5
6
	static function admin_init() {
7
		global $pagenow;
8
9
		if ( 'import.php' !== $pagenow ) {
10
			return;
11
		}
12
13
		/**
14
		 * This action fires in wp-admin when query argument `action=jetpack_import_ui`
15
		 * @see https://developer.wordpress.org/reference/hooks/admin_action__requestaction/
16
		 */
17
		add_action( 'admin_action_' . self::UI_ACTION_ARG, __CLASS__ . '::import_ui' );
18
19
		// The `import_ui` class will remove this function when our UI shows
20
		add_action( 'admin_notices', __CLASS__ . '::admin_notice' );
21
	}
22
23
	static function import_ui() {
24
		// Don't print the admin notice when already on our UI
25
		remove_action( 'admin_notices', __CLASS__ . '::admin_notice' );
26
27
		/**
28
		 * Pre-hide the core UI so it doesn't jump around
29
		 *
30
		 * @TODO If you can find a better way to reference this markup, that'd be awesome.
31
		 * This is fragile in that a core change to this markup would break our app.
32
		 * See:
33
		 *   * https://github.com/WordPress/WordPress/blob/71cf332e6569f0ac2f263ce9b2168644942f5534/wp-admin/admin-header.php#L251
34
		 *   * https://github.com/WordPress/WordPress/blob/71cf332e6569f0ac2f263ce9b2168644942f5534/wp-admin/import.php#L56-L60
35
		 *
36
		 * The scripts can use the `parentElement` of `table.importers` which is probably less fragile.
37
		 * (wouldn't a parent pseudoselector be nice! ;) )
38
		 */
39
?><style>#wpbody-content .wrap { display: none; }</style><?php
40
41
		wp_enqueue_script( 'jetpack_import_ui', plugin_dir_url( __FILE__ ) . '/index.js', array( 'jquery' /* @TODO react n stuff */ ), JETPACK__VERSION, true );
42
43
		// @TODO is `admin_notices` our best hook for our entry element?
44
		add_action( 'admin_notices', __CLASS__ . '::import_ui_entry_element' );
45
		add_action( 'admin_footer', __CLASS__ . '::import_ui_ensure_core_ui_is_hidden_by_default' );
46
47
		/**
48
		 * @TODO override help text? https://github.com/WordPress/WordPress/blob/e0e99fe82e652a9d16b603ec17b777395fb9783e/wp-admin/import.php#L20-L33
49
		 * 	See:
50
		 * 		https://user-images.githubusercontent.com/1587282/53657998-a8811880-3c25-11e9-9a75-5e8f67b0d67d.png
51
		 * 	...for default look  & feel
52
		 */
53
	}
54
55
	static function import_ui_entry_element() {
56
?>
57
<div class="jetpack-unified-importer">
58
	<h1>Oh hi, hullo, Unified Importer!!!!</h1>
59
	<p>So, this element (<code>.jetpack-unified-importer</code>) is our entry point for our script.</p>
60
	<hr />
61
	<p>
62
		We can clone and mutate the core list (<code>table.importers</code>)<br />
63
		...then append it to the bottom of our UI as desired
64
	</p>
65
	<hr />
66
	<p>And we can have a button which toggles visibility of this & the "regular" UI</p>
67
	<p>e.g.&nbsp;&nbsp;<button class="jetpack-unified-importer__exit">Exit</button></p>
68
	<div style="width: 400px">
69
		<marquee>For now, please enjoy this super-1337 throwback jQuery mockup ;P</marquee>
70
	</div>
71
</div>
72
<?php
73
	}
74
75
	static function import_ui_ensure_core_ui_is_hidden_by_default() {
76
?>
77
<script>
78
try {
79
	document.querySelector( 'table.importers' ).parentElement.style.display = 'none';
80
} catch ( e ) {
81
	console.error( 'Jetpack Importer UI: Unable to locate importers table' );
82
}
83
</script>
84
<?php
85
	}
86
87
	static function ui_url() {
88
		return add_query_arg( 'action', self::UI_ACTION_ARG, admin_url( 'import.php' ) );
89
	}
90
91
	static function admin_notice() {
92
		/**
93
		 * TODO:
94
		 * - [ ] Copy
95
		 * - [ ] Styling
96
		 * - [ ] Persist the opt-out for some period...?
97
		 */
98
?>
99
<div class="notice notice-info is-dismissible">
100
	<h1>Try the Unified Importer</h1>
101
	<p>Jetpack includes an import experience that's easy-to-use and supports....</p>
102
	<a class="button primary" href="<?php echo esc_url( self::ui_url() ) ?>">Try it!</a>
103
</div>
104
<?php
105
	}
106
}
107
108
add_action( 'admin_init', array( 'Jetpack_Unified_Importer_Module', 'admin_init' ) );
109