Geen omschrijving

legacy-widget.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Server-side rendering of the `core/legacy-widget` block.
  4. *
  5. * @package WordPress
  6. */
  7. /**
  8. * Renders the 'core/legacy-widget' block.
  9. *
  10. * @param array $attributes The block attributes.
  11. *
  12. * @return string Rendered block.
  13. */
  14. function render_block_core_legacy_widget( $attributes ) {
  15. global $wp_widget_factory;
  16. if ( isset( $attributes['id'] ) ) {
  17. $sidebar_id = wp_find_widgets_sidebar( $attributes['id'] );
  18. return wp_render_widget( $attributes['id'], $sidebar_id );
  19. }
  20. if ( ! isset( $attributes['idBase'] ) ) {
  21. return '';
  22. }
  23. $id_base = $attributes['idBase'];
  24. if ( method_exists( $wp_widget_factory, 'get_widget_key' ) && method_exists( $wp_widget_factory, 'get_widget_object' ) ) {
  25. $widget_key = $wp_widget_factory->get_widget_key( $id_base );
  26. $widget_object = $wp_widget_factory->get_widget_object( $id_base );
  27. } else {
  28. /*
  29. * This file is copied from the published @wordpress/widgets package when WordPress
  30. * Core is built. Because the package is a dependency of both WordPress Core and the
  31. * Gutenberg plugin where the block editor is developed, this fallback condition is
  32. * required until the minimum required version of WordPress for the plugin is raised
  33. * to 5.8.
  34. */
  35. $widget_key = gutenberg_get_widget_key( $id_base );
  36. $widget_object = gutenberg_get_widget_object( $id_base );
  37. }
  38. if ( ! $widget_key || ! $widget_object ) {
  39. return '';
  40. }
  41. if ( isset( $attributes['instance']['encoded'], $attributes['instance']['hash'] ) ) {
  42. $serialized_instance = base64_decode( $attributes['instance']['encoded'] );
  43. if ( wp_hash( $serialized_instance ) !== $attributes['instance']['hash'] ) {
  44. return '';
  45. }
  46. $instance = unserialize( $serialized_instance );
  47. } else {
  48. $instance = array();
  49. }
  50. $args = array(
  51. 'widget_id' => $widget_object->id,
  52. 'widget_name' => $widget_object->name,
  53. );
  54. ob_start();
  55. the_widget( $widget_key, $instance, $args );
  56. return ob_get_clean();
  57. }
  58. /**
  59. * Registers the 'core/legacy-widget' block.
  60. */
  61. function register_block_core_legacy_widget() {
  62. register_block_type_from_metadata(
  63. __DIR__ . '/legacy-widget',
  64. array(
  65. 'render_callback' => 'render_block_core_legacy_widget',
  66. )
  67. );
  68. }
  69. add_action( 'init', 'register_block_core_legacy_widget' );
  70. /**
  71. * Intercepts any request with legacy-widget-preview in the query param and, if
  72. * set, renders a page containing a preview of the requested Legacy Widget
  73. * block.
  74. */
  75. function handle_legacy_widget_preview_iframe() {
  76. if ( empty( $_GET['legacy-widget-preview'] ) ) {
  77. return;
  78. }
  79. if ( ! current_user_can( 'edit_theme_options' ) ) {
  80. return;
  81. }
  82. define( 'IFRAME_REQUEST', true );
  83. ?>
  84. <!doctype html>
  85. <html <?php language_attributes(); ?>>
  86. <head>
  87. <meta charset="<?php bloginfo( 'charset' ); ?>" />
  88. <meta name="viewport" content="width=device-width, initial-scale=1" />
  89. <link rel="profile" href="https://gmpg.org/xfn/11" />
  90. <?php wp_head(); ?>
  91. <style>
  92. /* Reset theme styles */
  93. html, body, #page, #content {
  94. padding: 0 !important;
  95. margin: 0 !important;
  96. }
  97. </style>
  98. </head>
  99. <body <?php body_class(); ?>>
  100. <div id="page" class="site">
  101. <div id="content" class="site-content">
  102. <?php
  103. $registry = WP_Block_Type_Registry::get_instance();
  104. $block = $registry->get_registered( 'core/legacy-widget' );
  105. echo $block->render( $_GET['legacy-widget-preview'] );
  106. ?>
  107. </div><!-- #content -->
  108. </div><!-- #page -->
  109. <?php wp_footer(); ?>
  110. </body>
  111. </html>
  112. <?php
  113. exit;
  114. }
  115. // Use admin_init instead of init to ensure get_current_screen function is already available.
  116. // This isn't strictly required, but enables better compatibility with existing plugins.
  117. // See: https://github.com/WordPress/gutenberg/issues/32624.
  118. add_action( 'admin_init', 'handle_legacy_widget_preview_iframe', 20 );