class-wp-widget-form-customize-control.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Customize API: WP_Widget_Form_Customize_Control class
  4. *
  5. * @package WordPress
  6. * @subpackage Customize
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Widget Form Customize Control class.
  11. *
  12. * @since 3.9.0
  13. *
  14. * @see WP_Customize_Control
  15. */
  16. class WP_Widget_Form_Customize_Control extends WP_Customize_Control {
  17. /**
  18. * Customize control type.
  19. *
  20. * @since 3.9.0
  21. * @var string
  22. */
  23. public $type = 'widget_form';
  24. /**
  25. * Widget ID.
  26. *
  27. * @since 3.9.0
  28. * @var string
  29. */
  30. public $widget_id;
  31. /**
  32. * Widget ID base.
  33. *
  34. * @since 3.9.0
  35. * @var string
  36. */
  37. public $widget_id_base;
  38. /**
  39. * Sidebar ID.
  40. *
  41. * @since 3.9.0
  42. * @var string
  43. */
  44. public $sidebar_id;
  45. /**
  46. * Widget status.
  47. *
  48. * @since 3.9.0
  49. * @var bool True if new, false otherwise. Default false.
  50. */
  51. public $is_new = false;
  52. /**
  53. * Widget width.
  54. *
  55. * @since 3.9.0
  56. * @var int
  57. */
  58. public $width;
  59. /**
  60. * Widget height.
  61. *
  62. * @since 3.9.0
  63. * @var int
  64. */
  65. public $height;
  66. /**
  67. * Widget mode.
  68. *
  69. * @since 3.9.0
  70. * @var bool True if wide, false otherwise. Default false.
  71. */
  72. public $is_wide = false;
  73. /**
  74. * Gather control params for exporting to JavaScript.
  75. *
  76. * @since 3.9.0
  77. *
  78. * @global array $wp_registered_widgets
  79. */
  80. public function to_json() {
  81. global $wp_registered_widgets;
  82. parent::to_json();
  83. $exported_properties = array( 'widget_id', 'widget_id_base', 'sidebar_id', 'width', 'height', 'is_wide' );
  84. foreach ( $exported_properties as $key ) {
  85. $this->json[ $key ] = $this->$key;
  86. }
  87. // Get the widget_control and widget_content.
  88. require_once ABSPATH . 'wp-admin/includes/widgets.php';
  89. $widget = $wp_registered_widgets[ $this->widget_id ];
  90. if ( ! isset( $widget['params'][0] ) ) {
  91. $widget['params'][0] = array();
  92. }
  93. $args = array(
  94. 'widget_id' => $widget['id'],
  95. 'widget_name' => $widget['name'],
  96. );
  97. $args = wp_list_widget_controls_dynamic_sidebar(
  98. array(
  99. 0 => $args,
  100. 1 => $widget['params'][0],
  101. )
  102. );
  103. $widget_control_parts = $this->manager->widgets->get_widget_control_parts( $args );
  104. $this->json['widget_control'] = $widget_control_parts['control'];
  105. $this->json['widget_content'] = $widget_control_parts['content'];
  106. }
  107. /**
  108. * Override render_content to be no-op since content is exported via to_json for deferred embedding.
  109. *
  110. * @since 3.9.0
  111. */
  112. public function render_content() {}
  113. /**
  114. * Whether the current widget is rendered on the page.
  115. *
  116. * @since 4.0.0
  117. *
  118. * @return bool Whether the widget is rendered.
  119. */
  120. public function active_callback() {
  121. return $this->manager->widgets->is_widget_rendered( $this->widget_id );
  122. }
  123. }