PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.2.2
Jetpack – WP Security, Backup, Speed, & Growth v12.2.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / _inc / lib / components.php
components.php
120 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 require_once JETPACK__PLUGIN_DIR . '_inc/lib/plans.php';
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