暂无描述

components.php 2.6KB

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