Completed
Push — renovate/debug-4.x ( e10916...91e0c8 )
by
unknown
86:28 queued 77:02
created

Jetpack_Components::render_upgrade_nudge()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 5
nop 1
dl 0
loc 49
rs 9.1127
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
	 * Load and display a pre-rendered component
52
	 *
53
	 * @since 7.7.0
54
	 *
55
	 * @param array $props Component properties.
56
	 *
57
	 * @return string The component markup
58
	 */
59
	public static function render_upgrade_nudge( $props ) {
60
		$plan_slug = $props['plan'];
61
		jetpack_require_lib( 'plans' );
62
		$plan = Jetpack_Plans::get_plan( $plan_slug );
63
64
		if ( ! $plan ) {
65
			return self::render_component(
66
				'upgrade-nudge',
67
				array(
68
					'planName'   => __( 'a paid plan', 'jetpack' ),
69
					'upgradeUrl' => '',
70
				)
71
			);
72
		}
73
74
		// WP.com plan objects have a dedicated `path_slug` field, Jetpack plan objects don't
75
		// For Jetpack, we thus use the plan slug with the 'jetpack_' prefix removed.
76
		$plan_path_slug = wp_startswith( $plan_slug, 'jetpack_' )
77
			? substr( $plan_slug, strlen( 'jetpack_' ) )
78
			: $plan->path_slug;
79
80
		$post_id = get_the_ID();
81
82
		$site_slug = ( new Status() )->get_site_suffix();
83
84
		// Post-checkout: redirect back to the editor.
85
		$redirect_to = add_query_arg(
86
			array(
87
				'plan_upgraded' => 1,
88
			),
89
			get_edit_post_link( $post_id )
90
		);
91
92
		$upgrade_url =
93
			$plan_path_slug
94
			? add_query_arg(
95
				'redirect_to',
96
				$redirect_to,
97
				"https://wordpress.com/checkout/${site_slug}/${plan_path_slug}"
98
			) : '';
99
100
		return self::render_component(
101
			'upgrade-nudge',
102
			array(
103
				'planName'   => $plan->product_name,
104
				'upgradeUrl' => $upgrade_url,
105
			)
106
		);
107
	}
108
}
109