Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.44% |
43 / 57 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_Components | |
75.44% |
43 / 57 |
|
25.00% |
1 / 4 |
10.20 | |
0.00% |
0 / 1 |
| get_component_markup | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| render_component | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
3 | |||
| render_frontend_nudge | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| render_upgrade_nudge | |
80.00% |
28 / 35 |
|
0.00% |
0 / 1 |
4.13 | |||
| 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 | * Get the contents of a component file |
| 13 | * |
| 14 | * @since 14.7 |
| 15 | * |
| 16 | * @param string $name Component name. |
| 17 | * @return string The component markup |
| 18 | */ |
| 19 | protected static function get_component_markup( $name ) { |
| 20 | ob_start(); |
| 21 | // `include` fails gracefully and throws a warning, but doesn't halt execution. |
| 22 | include JETPACK__PLUGIN_DIR . "_inc/blocks/$name.html"; |
| 23 | return ob_get_clean(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Load and display a pre-rendered component |
| 28 | * |
| 29 | * @since 7.7.0 |
| 30 | * |
| 31 | * @param string $name Component name. |
| 32 | * @param array $props Component properties. |
| 33 | * |
| 34 | * @return string The component markup |
| 35 | */ |
| 36 | public static function render_component( $name, $props ) { |
| 37 | |
| 38 | $rtl = is_rtl() ? '.rtl' : ''; |
| 39 | wp_enqueue_style( 'jetpack-components', plugins_url( "_inc/blocks/components{$rtl}.css", JETPACK__PLUGIN_FILE ), array( 'wp-components' ), JETPACK__VERSION ); |
| 40 | |
| 41 | $markup = self::get_component_markup( $name ); |
| 42 | |
| 43 | foreach ( $props as $key => $value ) { |
| 44 | $markup = str_replace( |
| 45 | "#$key#", |
| 46 | $value, |
| 47 | $markup |
| 48 | ); |
| 49 | |
| 50 | // Workaround, required to replace strings in `sprintf`-expressions. |
| 51 | // See extensions/i18n-to-php.js for more information. |
| 52 | $markup = str_replace( |
| 53 | "%($key)s", |
| 54 | $value, |
| 55 | $markup |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | return $markup; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Renders the frontend-nudge with the provided props. |
| 64 | * |
| 65 | * @param array $props Component properties. |
| 66 | * |
| 67 | * @return string The component markup. |
| 68 | */ |
| 69 | public static function render_frontend_nudge( $props ) { |
| 70 | return self::render_component( |
| 71 | 'frontend-nudge', |
| 72 | $props |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Load and display a pre-rendered component |
| 78 | * |
| 79 | * @since 7.7.0 |
| 80 | * |
| 81 | * @param array $props Component properties. |
| 82 | * |
| 83 | * @return string The component markup |
| 84 | */ |
| 85 | public static function render_upgrade_nudge( $props ) { |
| 86 | $plan_slug = $props['plan']; |
| 87 | require_once JETPACK__PLUGIN_DIR . '_inc/lib/plans.php'; |
| 88 | $plan = Jetpack_Plans::get_plan( $plan_slug ); |
| 89 | |
| 90 | if ( ! $plan ) { |
| 91 | return self::render_component( |
| 92 | 'upgrade-nudge', |
| 93 | array( |
| 94 | 'checkoutUrl' => '', |
| 95 | ) |
| 96 | ); |
| 97 | } |
| 98 | |
| 99 | // WP.com plan objects have a dedicated `path_slug` field, Jetpack plan objects don't. |
| 100 | $plan_path_slug = wp_startswith( $plan_slug, 'jetpack_' ) |
| 101 | ? $plan_slug |
| 102 | : $plan->path_slug; |
| 103 | |
| 104 | $post_id = get_the_ID(); |
| 105 | |
| 106 | $site_slug = ( new Status() )->get_site_suffix(); |
| 107 | |
| 108 | // Post-checkout: redirect back to the editor. |
| 109 | $redirect_to = add_query_arg( |
| 110 | array( |
| 111 | 'plan_upgraded' => 1, |
| 112 | ), |
| 113 | get_edit_post_link( $post_id ) |
| 114 | ); |
| 115 | |
| 116 | // Decode the URL to avoid double encoding. |
| 117 | $redirect_to = html_entity_decode( wp_unslash( $redirect_to ), ENT_QUOTES ); |
| 118 | |
| 119 | $upgrade_url = |
| 120 | $plan_path_slug |
| 121 | ? add_query_arg( |
| 122 | 'redirect_to', |
| 123 | rawurlencode( $redirect_to ), |
| 124 | "https://wordpress.com/checkout/{$site_slug}/{$plan_path_slug}" |
| 125 | ) : ''; |
| 126 | |
| 127 | return self::render_component( |
| 128 | 'upgrade-nudge', |
| 129 | array( |
| 130 | 'checkoutUrl' => $upgrade_url, |
| 131 | ) |
| 132 | ); |
| 133 | } |
| 134 | } |