beaver.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Beaver template widget
  4. *
  5. * @package vamtam/consulting
  6. */
  7. class Vamtam_Beaver_Widget extends WP_Widget {
  8. public function __construct() {
  9. $widget_options = array(
  10. 'classname' => 'vamtam_beaver',
  11. 'description' => esc_html__( 'Display a saved layout from the VamTam Builder', 'vamtam-consulting' ),
  12. );
  13. parent::__construct( 'Vamtam_Beaver_Widget', esc_html__( 'VamTam Builder Layout', 'vamtam-consulting' ) , $widget_options );
  14. }
  15. public function widget( $args, $instance ) {
  16. if ( class_exists( 'FLBuilderShortcodes' ) ) {
  17. echo $args['before_widget']; // xss ok
  18. if ( $instance['title'] ) {
  19. echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title']; // xss ok
  20. }
  21. echo FLBuilderShortcodes::insert_layout( array( // xss ok
  22. 'type' => 'fl-builder-template',
  23. 'slug' => $instance['slug'],
  24. ) );
  25. echo $args['after_widget']; // xss ok
  26. }
  27. }
  28. public function update( $new_instance, $old_instance ) {
  29. $instance = $old_instance;
  30. $instance['slug'] = preg_replace( '/^beaver-/', '', $new_instance['slug'] );
  31. $instance['title'] = $new_instance['title'];
  32. return $instance;
  33. }
  34. public function form( $instance ) {
  35. $options = vamtam_get_beaver_layouts( array(
  36. '' => esc_html__( '-- Select Layout--', 'vamtam-consulting' ),
  37. ) );
  38. $slug = isset( $instance['slug'] ) ? esc_attr( $instance['slug'] ) : '';
  39. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
  40. ?>
  41. <p>
  42. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'vamtam-consulting' ); ?></label>
  43. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  44. </p>
  45. <p>
  46. <label for="<?php echo esc_attr( $this->get_field_id( 'slug' ) ); ?>"><?php esc_html_e( 'Template:', 'vamtam-consulting' ); ?></label>
  47. <select id="<?php echo esc_attr( $this->get_field_id( 'slug' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'slug' ) ); ?>">
  48. <?php foreach ( $options as $opt_value => $opt_text ) : ?>
  49. <option value="<?php echo esc_attr( $opt_value )?>" <?php selected( $opt_value, $slug ) ?>><?php echo esc_html( $opt_text ) ?></option>
  50. <?php endforeach; ?>
  51. </select>
  52. </p>
  53. <?php
  54. }
  55. }
  56. register_widget( 'Vamtam_Beaver_Widget' );