Completed
Push — update/enable-frontend-upgrade... ( 4d6f9a...8f1e24 )
by
unknown
353:17 queued 345:12
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_frontend_nudge(
80
				array(
81
					'checkoutUrl' => '',
82
				)
83
			);
84
		}
85
86
		// WP.com plan objects have a dedicated `path_slug` field, Jetpack plan objects don't.
87
		$plan_path_slug = wp_startswith( $plan_slug, 'jetpack_' )
88
			? $plan_slug
89
			: $plan->path_slug;
90
91
		$post_id = get_the_ID();
92
93
		$site_slug = ( new Status() )->get_site_suffix();
94
95
		// Post-checkout: redirect back to the editor.
96
		$redirect_to = add_query_arg(
97
			array(
98
				'plan_upgraded' => 1,
99
			),
100
			get_edit_post_link( $post_id )
101
		);
102
103
		$upgrade_url =
104
			$plan_path_slug
105
			? add_query_arg(
106
				'redirect_to',
107
				$redirect_to,
108
				"https://wordpress.com/checkout/${site_slug}/${plan_path_slug}"
109
			) : '';
110
111
		return self::render_frontend_nudge(
112
			array(
113
				'checkoutUrl' => $upgrade_url,
114
			)
115
		);
116
	}
117
}
118