widget.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @class FLWidgetModule
  4. */
  5. class FLWidgetModule extends FLBuilderModule {
  6. /**
  7. * @return void
  8. */
  9. public function __construct() {
  10. parent::__construct(array(
  11. 'name' => __( 'Widget', 'fl-builder' ),
  12. 'description' => __( 'Display a WordPress widget.', 'fl-builder' ),
  13. 'group' => __( 'WordPress Widgets', 'fl-builder' ),
  14. 'category' => __( 'WordPress Widgets', 'fl-builder' ),
  15. 'editor_export' => false,
  16. 'partial_refresh' => true,
  17. ));
  18. }
  19. /**
  20. * @return void
  21. */
  22. public function update( $settings ) {
  23. // Make sure we have a widget.
  24. if ( ! isset( $settings->widget ) || ! class_exists( $settings->widget ) ) {
  25. return $settings;
  26. }
  27. // Get the widget instance.
  28. $class = $settings->widget;
  29. $instance = new $class();
  30. // Get the widget settings.
  31. $settings_key = 'widget-' . $instance->id_base;
  32. $widget_settings = array();
  33. if ( isset( $settings->$settings_key ) ) {
  34. $widget_settings = (array) $settings->$settings_key;
  35. }
  36. // Run the widget update method.
  37. $widget_settings = $instance->update( $widget_settings, array() );
  38. // Save the widget settings as an object.
  39. if ( is_array( $widget_settings ) ) {
  40. $settings->$settings_key = (object) $widget_settings;
  41. }
  42. // Delete the WordPress cache for this widget.
  43. wp_cache_delete( $settings->widget, 'widget' );
  44. // Return the settings.
  45. return $settings;
  46. }
  47. /**
  48. * @since 1.10.6
  49. * @param string $class
  50. * @param object $instance
  51. * @param array $settings
  52. * @return void
  53. */
  54. static public function render_form( $class, $instance, $settings ) {
  55. if ( 'WP_Widget_Text' === $class ) {
  56. // Render the legacy text form since the one in 4.8 doesn't work in the builder.
  57. include FL_BUILDER_DIR . 'modules/widget/includes/settings-text-widget.php';
  58. } else {
  59. $instance->form( $settings );
  60. }
  61. }
  62. }
  63. /**
  64. * Register the module and its form settings.
  65. */
  66. FLBuilder::register_module('FLWidgetModule', array(
  67. 'general' => array( // Tab
  68. 'title' => __( 'General', 'fl-builder' ), // Tab title
  69. 'file' => FL_BUILDER_DIR . 'modules/widget/includes/settings-general.php',
  70. ),
  71. ));