[wp-trac] [WordPress Trac] #53625: The 'explode' function does not work on widget block editor.

WordPress Trac noreply at wordpress.org
Fri Jul 9 07:21:24 UTC 2021


#53625: The 'explode' function does not work on widget block editor.
-----------------------------------------------+---------------------------
 Reporter:  subrataemfluence                   |       Owner:  (none)
     Type:  defect (bug)                       |      Status:  new
 Priority:  normal                             |   Milestone:  Awaiting
                                               |  Review
Component:  Widgets                            |     Version:  trunk
 Severity:  major                              |  Resolution:
 Keywords:  has-screenshots reporter-feedback  |     Focuses:
                                               |  administration
-----------------------------------------------+---------------------------

Comment (by subrataemfluence):

 Hi, @andraganescu here is the codebase. Please let me know if I need to
 provide anything else.

 The widget is built as a separate file (widget.php) and has been
 `required` in a file called `setup.php`. This file also enqueues the
 JavaScript that I am using. The file structure is like this:

 [ A ]
 **File:**
 `wp-content/plugins/platform-subscribers/index.php`

 **Content:**
 {{{#!php
 <?php
 require_once 'setup.php';
 }}}


 [ B ]
 **File:**
 `wp-content/plugins/platform-subscribers/setup.php`

 **Content:**

 {{{#!php
 <?php
 require_once 'widget.php';

 function ps_load_widget() {
    $platform_subscriber = new PlatformSubscriber();
    register_widget( $platform_subscriber );
 }

 add_action( 'widgets_init', 'ps_load_widget' );

 function enqueue_plugin_scripts() {
    wp_enqueue_script(
       'ps-settings',
       plugins_url( '/ps-settings.js', __FILE__ ),
       array( 'jquery' ),
       '1.0',
       false
    );
 }

 add_action( 'admin_enqueue_scripts', 'enqueue_plugin_scripts' );
 }}}


 [ C ]
 **File:**
 `wp-content/plugins/platform-subscribers/widget.php`

 **Content:**

 {{{#!php
 <?php
 class PlatformSubscriber extends WP_Widget {
    public function __construct() {
       $id_base         = 'platform_subscriber';
       $widget_options  = array(
          'classname'   => 'platform_subscriber',
          'description' => 'Creates an email signup form for your
 visitors.'
       );
       $control_options = array(
          'width' => 400,
          'id_base' => $id_base
       );

       parent::__construct(
          $id_base,
          __( 'Platform Subscriber', 'ps_platform' ),
          $widget_options,
          $control_options
       );
    }

    /**
       * @param array $instance
       *
       * @return string|void
       * Widget backend.
    */
    public function form( $instance ) {
       $this->create_backend_form( $instance );
    }

    /**
       * @param array $new_instance
       * @param array $old_instance
       *
       * @return array
       *
       * Updating widget replacing old instance with new.
    */
    public function update( $new_instance, $old_instance ) {
       $instance = $old_instance;

       $instance['title']  = strip_tags( $new_instance['title'] );
       $instance['about']  = strip_tags( $new_instance['about'] );

       $groups             = $new_instance['groups'];
       $instance['groups'] = array_pop ( explode( ',', $groups ) );

       return $instance;
    }

    /**
       * @param array $args
       * @param array $instance
       *
       * Create the widget front-end.
       * Will add the actual functionality later
    */
    public function widget( $args, $instance ) {
       $title = apply_filters( 'widget_title', $instance['title'] );

       echo $args['before_widget'];
       if( ! empty( $title ) ) {
          echo $args['before_title'] . $title . $args['after_title'];
       }

       echo __( 'Hello World!', 'ps_platform' );
       echo $args['after_widget'];
    }


    /**
       * @param $instance
       * Creates the backend form
    */
    protected function create_backend_form( $instance ) {
       if( ! isset( $instance['title'] ) ) {
          $title = __( 'New Platform Title', 'ps_platform' );
       } else {
          $title = $instance['title'];
       }

       if( ! isset( $instance['about'] ) ) {
          $about = '';
       } else {
          $about = esc_html( $instance['about'] );
       }

       if( ! isset( $instance['groups'] ) ) {
          $selectedGroups = '';
       } else {
          $selectedGroups =  $instance['groups'];
       }
       ?>

       <p>
          <label for="<?= $this->get_field_id( 'title' ); ?>">
             <?php _e( 'Title ' ) ?> *
          </label>
          <input
             class="widefat"
             type="text"
             required
             id="<?= $this->get_field_id( 'title' ); ?>"
             name="<?= $this->get_field_name( 'title' ) ?>"
             value="<?= esc_attr( $title ) ?>"
          />
       </p>
       <p>
          <label for="<?= $this->get_field_id( 'about' ) ?>">
             <?php _e( 'Description' ) ?>
          </label>
          <textarea
             class="widefat"
             name="<?= $this->get_field_name( 'about' ) ?>"
             id="<?= $this->get_field_id( 'about' ) ?>"
             cols="30" rows="3" maxlength="150" required
             placeholder="Help text for visitor"><?= $about ?></textarea>
          </p>
          <div>
             <p>
                <input
                   type="checkbox"
                   id="<?= $this->get_field_id('online-conference-ticket')
 ?>"
                   name="<?= $this->get_field_name('online-conference-
 ticket') ?>"
                   value="396060"
                   onclick="updateSelection( this, <?= $this->number ?> );"
                />
                <label
                   id="label-<?= $this->get_field_id('online-conference-
 ticket') ?>"
                   for="<?= $this->get_field_id('online-conference-ticket')
 ?>"
                   onclick="updateSelection( this, <?= $this->number ?>
 );">
                   Online Conference Ticket
                </label>
             </p>
             <p>
                <input
                   type="checkbox"
                   id="<?= $this->get_field_id('marketing-platform-demo')
 ?>"
                   name="<?= $this->get_field_name('marketing-platform-
 demo') ?>"
                   value="237500"
                   onclick="updateSelection( this, <?= $this->number ?> );"
                />
                <label
                   id="label-<?= $this->get_field_id('marketing-platform-
 demo') ?>"
                   for="<?= $this->get_field_id('marketing-platform-demo')
 ?>"
                   onclick="updateSelection( this, <?= $this->number ?>
 );">
                   Marketing Platform Demo
                </label>
             </p>
             <p>
                <input
                   type="checkbox"
                   id="<?= $this->get_field_id('beer-online-conference')
 ?>"
                   name="<?= $this->get_field_name('beer-online-
 conference') ?>"
                   value="457844"
                   onclick="updateSelection( this, <?= $this->number ?> );"
                />
                <label
                   id="label-<?= $this->get_field_id('beer-online-
 conference') ?>"
                   for="<?= $this->get_field_id('beer-online-conference')
 ?>"
                   onclick="updateSelection( this, <?= $this->number ?>
 );">
                   Beer Online Conference
                </label>
             </p>
          </div>
          <p>
             <label for="<?= $this->get_field_id( 'groups' ) ?>">
                <?php _e( 'Selected groups' ) ?> *
             </label>
             <input
                class="widefat"
                type="text"
                required
                id="<?= $this->get_field_id( 'groups' ); ?>"
                name="<?= $this->get_field_name( 'groups' ) ?>"
                value="<?= esc_attr( $selectedGroups ) ?>"
             />
          </p>

          <?php
    }
 }
 }}}


 [ D ]
 **File:**
 `wp-content/plugins/platform-subscribers/ps-settings.js`

 **Content:**

 {{{
 function updateSelection( obj, widget_number ) {
     var selectedGroupsBoxId = 'widget-platform_subscriber-' +
 widget_number + '-groups';
     var parentDiv = jQuery(obj).closest( 'div' );
     var commaGroups = '';

     jQuery( parentDiv ).find( 'input:checked' ).each( function() {
         commaGroups = commaGroups + jQuery(this).attr('value') + ',';
     } );

     jQuery( '#' + selectedGroupsBoxId ).val( commaGroups );
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53625#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list