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