__( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), ) // ); /** * Constructor */ public function __construct() { parent::__construct( 'onyx_poll', __('Poll', 'acf-onyx-poll'), array( 'description' => __('Display an Onyx Poll.', 'acf-onyx-poll') ) ); } /** * Creating widget front-end */ public function widget($args, $instance) { $title = apply_filters('widget_title', ($instance['title'] ?? '')); $poll = absint($instance['poll'] ?? 0); $css = $instance['css'] ?? 'standard'; echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; echo do_shortcode("[onyx-poll id=$poll class='$css']"); echo $args['after_widget']; } /** * Creating widget Backend */ public function form($instance) { $title_id = $this->get_field_id('title'); $title_label = __('Title', 'acf-onyx-poll'); $title_name = $this->get_field_name('title'); $title_value = !empty($instance['title']) ? esc_attr($instance['title']) : ''; $poll_id = $this->get_field_id('poll'); $poll_label = __('Poll', 'acf-onyx-poll'); $poll_name = $this->get_field_name('poll'); $css_id = $this->get_field_id('css'); $css_label = __('Style', 'acf-onyx-poll'); $css_name = $this->get_field_name('css'); $instance['css'] = $instance['css'] ?? ''; $instance['poll'] = $instance['poll'] ?? false; $poll_options = $this->query_polls($instance['poll']); $form = "

"; if (!OnyxPollsStorage::settings()['css']) { $form .= "

"; } echo $form; } /** * Updating widget replacing old instances with new */ public function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['poll'] = !empty($new_instance['poll']) && OnyxPolls::has_poll(absint($new_instance['poll'])) ? absint($new_instance['poll']) : ''; $instance['css'] = ($new_instance['css'] ?? '') === 'twitter' ? 'twitter' : 'standard'; return $instance; } /** * Query polls for widget select * * @param int $option optional */ public function query_polls($option) { $response = null; $poll_query = new WP_Query(array( 'post_type' => 'onyxpolls', 'posts_per_page' => 10, 'suppress_filters' => true, 'no_found_rows' => true )); if ($poll_query->have_posts()) { foreach ($poll_query->posts as $post) { $select = ((int) $option === $post->ID) ? 'selected' : null; $response .= ""; } } return $response; } } add_action('widgets_init', function() { register_widget('OnyxPollsWidget'); }); ?>