Completed
Push — update/connect-podcast-player-... ( eb8dbb...ca5bd7 )
by
unknown
36:16 queued 25:36
created

Jetpack_Components::render_frontend_nudge()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3
use Automattic\Jetpack\Status;
4
5
/**
6
 * Components Library
7
 *
8
 * Load and display a pre-rendered component
9
 */
10
class Jetpack_Components {
11
	/**
12
	 * Load and display a pre-rendered component
13
	 *
14
	 * @since 7.7.0
15
	 *
16
	 * @param string $name  Component name.
17
	 * @param array  $props Component properties.
18
	 *
19
	 * @return string The component markup
20
	 */
21
	public static function render_component( $name, $props ) {
22
23
		$rtl = is_rtl() ? '.rtl' : '';
24
		wp_enqueue_style( 'jetpack-components', plugins_url( "_inc/blocks/components{$rtl}.css", JETPACK__PLUGIN_FILE ), array( 'wp-components' ), JETPACK__VERSION );
25
26
		ob_start();
27
		// `include` fails gracefully and throws a warning, but doesn't halt execution.
28
		include JETPACK__PLUGIN_DIR . "_inc/blocks/$name.html";
29
		$markup = ob_get_clean();
30
31
		foreach ( $props as $key => $value ) {
32
			$markup = str_replace(
33
				"#$key#",
34
				$value,
35
				$markup
36
			);
37
38
			// Workaround, required to replace strings in `sprintf`-expressions.
39
			// See extensions/i18n-to-php.js for more information.
40
			$markup = str_replace(
41
				"%($key)s",
42
				$value,
43
				$markup
44
			);
45
		}
46
47
		return $markup;
48
	}
49
50
	/**
51
	 * Renders the frontend-nudge with the provided props.
52
	 *
53
	 * @param array $props Component properties.
54
	 *
55
	 * @return string The component markup.
56
	 */
57
	public static function render_frontend_nudge( $props ) {
58
		return self::render_component(
59
			'frontend-nudge',
60
			$props
61
		);
62
	}
63
64
	/**
65
	 * Load and display a pre-rendered component
66
	 *
67
	 * @since 7.7.0
68
	 *
69
	 * @param array $props Component properties.
70
	 *
71
	 * @return string The component markup
72
	 */
73
	public static function render_upgrade_nudge( $props ) {
74
		$plan_slug = $props['plan'];
75
		jetpack_require_lib( 'plans' );
76
		$plan = Jetpack_Plans::get_plan( $plan_slug );
77
78
		if ( ! $plan ) {
79
			return self::render_component(
80
				'upgrade-nudge',
81
				array(
82
					'checkoutUrl' => '',
83
				)
84
			);
85
		}
86
87
		// WP.com plan objects have a dedicated `path_slug` field, Jetpack plan objects don't.
88
		$plan_path_slug = wp_startswith( $plan_slug, 'jetpack_' )
89
			? $plan_slug
90
			: $plan->path_slug;
91
92
		$post_id = get_the_ID();
93
94
		$site_slug = ( new Status() )->get_site_suffix();
95
96
		// Post-checkout: redirect back to the editor.
97
		$redirect_to = add_query_arg(
98
			array(
99
				'plan_upgraded' => 1,
100
			),
101
			get_edit_post_link( $post_id )
102
		);
103
104
		$upgrade_url =
105
			$plan_path_slug
106
			? add_query_arg(
107
				'redirect_to',
108
				$redirect_to,
109
				"https://wordpress.com/checkout/${site_slug}/${plan_path_slug}"
110
			) : '';
111
112
		return self::render_component(
113
			'upgrade-nudge',
114
			array(
115
				'checkoutUrl' => $upgrade_url,
116
			)
117
		);
118
	}
119
}
120