'atom-builder-page-widget', 'description' => esc_html__( 'Features a page content with simple display options', 'atom-builder' ), 'customize_selective_refresh' => true, ) ); // Enqueue style if widget is active (appears in a sidebar) or if in Customizer preview. if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { add_action( 'wp_enqueue_scripts', array( $this, 'atom_builder_widget_enqueue_scripts' ) ); } } /** * Front-end display of widget. * * @see WP_Widget::widget() * * @param array $args Widget arguments. * @param array $instance Saved values from database. */ public function widget( $args, $instance ) { // Merge defaults arguments with user-submitted settings of the instance $defaults = $this->atom_builder_get_page_widget_default_settings(); $instance = wp_parse_args( $instance, $defaults ); // Echo the standard widget wrapper and title echo $args['before_widget']; if ( ! empty( $instance['title'] ) ) { echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title']; } // Prepare the query if ( empty( $instance['selected_page']) ){ $instance['selected_page'] = get_the_ID(); } $query = new WP_Query( array( 'page_id' => $instance['selected_page'], 'post_type' => 'page') ); // Load the template if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); atom_builder_get_widget_template( $instance, 'widget-page' ); endwhile; endif; wp_reset_postdata(); // Close the wrapper echo $args['after_widget']; } /** * Back-end widget form. * * @see WP_Widget::form() * * @param array $instance Previously saved values from database. */ public function form( $instance ) { // Merge defaults arguments with user-submitted settings of the instance $defaults = $this->atom_builder_get_page_widget_default_settings(); $instance = wp_parse_args( $instance, $defaults ); ?>
atom_builder_widget_radio_setting_html( $instance, 'thumbnail_option' ); ?>
atom_builder_widget_radio_setting_html( $instance, 'content_size' ); ?>
atom_builder_widget_radio_setting_html( $instance, 'text_style' ); ?>
atom_builder_sanitize_page_widget_option( $new_instance['thumbnail_option'], 'thumbnail_option' ); $instance['content_size'] = $this->atom_builder_sanitize_page_widget_option( $new_instance['content_size'], 'content_size' ); $instance['text_style'] = $this->atom_builder_sanitize_page_widget_option( $new_instance['text_style'], 'text_style'); $instance['display_full_page'] = atom_builder_sanitize_checkbox( $new_instance['display_full_page'] ); return apply_filters( 'atom_builder_page_widget_updated_instance', $instance, $new_instance ); } /** * Get widget default settings array * * @return array $defaults Defaults settings for the widget **/ public function atom_builder_get_page_widget_default_settings() { $defaults = array( 'title' => '', 'selected_page' => '', 'thumbnail_option' => 'left-thumbnail', 'content_size' => 'one-half', 'text_style' => 'text-left', 'display_full_page' => '', ); return apply_filters( 'atom_builder_builder_page_widget_default_settings', $defaults ); } /** * Gets an array of registered options for a given widget's setting. * * @param string $setting The widget setting to retrieve registered values for. * @return array $registered_options An array of registered option for the given setting **/ public function atom_builder_get_page_widget_registered_options( $setting = '' ){ switch ( $setting ) { case 'thumbnail_option': $registered_thumbnail_options = array( 'no-thumbnail' => __( 'No thumbnail', 'atom-builder' ), 'left-thumbnail' => __( 'Left thumbnail', 'atom-builder' ), 'right-thumbnail' => __( 'Right thumbnail', 'atom-builder' ), ); return apply_filters( 'atom_builder_page_widget_thumbnail_options', $registered_thumbnail_options ); break; case 'content_size': $registered_content_size_options = array( 'one-third' => __( 'One third', 'atom-builder' ), 'one-half' => __( 'One half', 'atom-builder' ), 'two-thirds' => __( 'Two thirds', 'atom-builder' ), ); return apply_filters( 'atom_builder_page_widget_content_size_options', $registered_content_size_options ); break; case 'text_style': $registered_text_style_options = array( 'text-center' => __( 'Centered text', 'atom-builder' ), 'text-left' => __( 'Left-aligned', 'atom-builder' ), 'text-right' => __( 'Right-aligned', 'atom-builder' ), ); return apply_filters( 'atom_builder_page_widget_text_style_options', $registered_text_style_options ); break; default: return array(); break; } } /** * Sanitize the page widget options. * * @param string $value The value of the option to sanitize. * @param object $setting The name of the widget setting. * @return string $value The sanitized value. */ public function atom_builder_sanitize_page_widget_option( $value, $setting ) { $valid = $this->atom_builder_get_page_widget_registered_options( $setting ); return atom_builder_sanitize_radio( $value, $valid ); } /** * Prints out a set of radio inputs for the given setting. * * @param string $setting The registered setting you want to output radio buttons for. * @param array $instance The current instance of the widget. **/ public function atom_builder_widget_radio_setting_html( $instance, $setting ){ // Get an array of registered options $options = $this->atom_builder_get_page_widget_registered_options( $setting ); // Loop throught the options and create the radio buttons. foreach ( $options as $value => $label ) { echo '