mirror of
https://github.com/10h30/display-featured-image-genesis.git
synced 2026-07-16 04:53:21 +09:00
Add shortcode support
This commit is contained in:
@@ -331,6 +331,12 @@ class Display_Featured_Image_Genesis {
|
||||
require_once plugin_dir_path( __FILE__ ) . 'widgets/displayfeaturedimagegenesis-' . $file . '-widget.php';
|
||||
register_widget( $widget );
|
||||
}
|
||||
|
||||
require_once plugin_dir_path( __FILE__ ) . 'widgets/class-displayfeaturedimagegenesis-widgets-shortcodes.php';
|
||||
$shortcode_class = new DisplayFeaturedImageGenesisWidgetsShortcodes();
|
||||
foreach ( array( 'author', 'post_type', 'term' ) as $shortcode ) {
|
||||
add_shortcode( "displayfeaturedimagegenesis_{$shortcode}", array( $shortcode_class, "shortcode_{$shortcode}" ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class DisplayFeaturedImageGenesisWidgetsShortcodes
|
||||
*/
|
||||
class DisplayFeaturedImageGenesisWidgetsShortcodes {
|
||||
|
||||
/**
|
||||
* @param $atts
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function shortcode_author( $atts ) {
|
||||
$class = 'Display_Featured_Image_Genesis_Author_Widget';
|
||||
$defaults = $this->get_defaults( $class );
|
||||
$atts = shortcode_atts( $defaults, $atts, 'displayfeaturedimagegenesis_author' );
|
||||
$atts = $this->validate_shortcode( $atts, $class );
|
||||
|
||||
return $this->do_shortcode( $atts, $class );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $atts
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function shortcode_post_type( $atts ) {
|
||||
$class = 'Display_Featured_Image_Genesis_Widget_CPT';
|
||||
$defaults = $this->get_defaults( $class );
|
||||
$atts = shortcode_atts( $defaults, $atts, 'displayfeaturedimagegenesis_post_type' );
|
||||
$atts = $this->validate_shortcode( $atts, $class );
|
||||
|
||||
return $this->do_shortcode( $atts, $class );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $atts
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function shortcode_term( $atts ) {
|
||||
$class = 'Display_Featured_Image_Genesis_Widget_Taxonomy';
|
||||
$defaults = $this->get_defaults( $class );
|
||||
$atts = shortcode_atts( $defaults, $atts, 'displayfeaturedimagegenesis_term' );
|
||||
$atts = $this->validate_shortcode( $atts, $class );
|
||||
|
||||
return $this->do_shortcode( $atts, $class );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $class
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function get_defaults( $class ) {
|
||||
$widget_class = new $class();
|
||||
|
||||
return $widget_class->defaults();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $atts
|
||||
* @param $class
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function do_shortcode( $atts, $class ) {
|
||||
$args = array(
|
||||
'id' => 'displayfeaturedimagegenesis-shortcode',
|
||||
);
|
||||
ob_start();
|
||||
the_widget( $class, $atts, $args );
|
||||
$output = ob_get_clean();
|
||||
|
||||
return do_shortcode( trim( $output ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $atts
|
||||
* @param $class
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function validate_shortcode( $atts, $class ) {
|
||||
$fields = new $class();
|
||||
foreach ( $fields->get_fields( $atts ) as $field ) {
|
||||
$value = $field['args']['id'];
|
||||
if ( ! isset( $atts[ $value ] ) ) {
|
||||
continue;
|
||||
}
|
||||
switch ( $field['method'] ) {
|
||||
// Sanitize numbers
|
||||
case 'number':
|
||||
$atts[ $value ] = $atts[ $value ] ? absint( $atts[ $value ] ) : '';
|
||||
break;
|
||||
|
||||
// Sanitize checkboxes
|
||||
case 'checkbox':
|
||||
$atts[ $value ] = filter_var( $atts[ $value ], FILTER_VALIDATE_BOOLEAN );
|
||||
break;
|
||||
|
||||
// Sanitize text fields
|
||||
case 'text':
|
||||
$atts[ $value ] = strip_tags( $atts[ $value ] );
|
||||
break;
|
||||
|
||||
// Escape select options
|
||||
case 'select':
|
||||
$atts[ $value ] = esc_attr( $atts[ $value ] );
|
||||
break;
|
||||
|
||||
case 'textarea':
|
||||
if ( function_exists( 'sanitize_textarea_field' ) ) {
|
||||
$atts[ $value ] = sanitize_textarea_field( $atts[ $value ] );
|
||||
} else {
|
||||
$atts[ $value ] = esc_textarea( $atts[ $value ] );
|
||||
}
|
||||
break;
|
||||
|
||||
// Default
|
||||
default:
|
||||
$atts[ $value ] = esc_attr( $atts[ $value ] );
|
||||
break;
|
||||
}
|
||||
} // End foreach().
|
||||
$atts['title'] = strip_tags( $atts['title'] );
|
||||
|
||||
return $atts;
|
||||
}
|
||||
}
|
||||
@@ -135,18 +135,23 @@ class Display_Featured_Image_Genesis_Author_Widget extends WP_Widget {
|
||||
*/
|
||||
public function update( $new_instance, $old_instance ) {
|
||||
|
||||
$fields = array_merge(
|
||||
$new_instance['user'] = (int) $new_instance['user'];
|
||||
$updater = new DisplayFeaturedImageGenesisWidgetsUpdate();
|
||||
|
||||
return $updater->update( $new_instance, $old_instance, $this->get_fields() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all widget fields.
|
||||
* @return array
|
||||
*/
|
||||
public function get_fields() {
|
||||
return array_merge(
|
||||
$this->get_image_fields(),
|
||||
$this->get_gravatar_fields(),
|
||||
$this->get_description_fields(),
|
||||
$this->get_archive_fields()
|
||||
);
|
||||
|
||||
$new_instance['user'] = (int) $new_instance['user'];
|
||||
$updater = new DisplayFeaturedImageGenesisWidgetsUpdate();
|
||||
|
||||
return $updater->update( $new_instance, $old_instance, $fields );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -193,16 +193,23 @@ class Display_Featured_Image_Genesis_Widget_CPT extends WP_Widget {
|
||||
*/
|
||||
function update( $new_instance, $old_instance ) {
|
||||
|
||||
$form = $this->get_form_class( $new_instance );
|
||||
$fields = array_merge(
|
||||
$updater = new DisplayFeaturedImageGenesisWidgetsUpdate();
|
||||
|
||||
return $updater->update( $new_instance, $old_instance, $this->get_fields( $new_instance ) );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all widget fields.
|
||||
* @return array
|
||||
*/
|
||||
public function get_fields( $instance = array() ) {
|
||||
$form = $this->get_form_class( $instance );
|
||||
return array_merge(
|
||||
$this->get_post_type_fields(),
|
||||
$form->get_text_fields(),
|
||||
$form->get_image_fields()
|
||||
);
|
||||
$updater = new DisplayFeaturedImageGenesisWidgetsUpdate();
|
||||
|
||||
return $updater->update( $new_instance, $old_instance, $fields );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -194,15 +194,22 @@ class Display_Featured_Image_Genesis_Widget_Taxonomy extends WP_Widget {
|
||||
*/
|
||||
public function update( $new_instance, $old_instance ) {
|
||||
|
||||
$form = $this->get_form_class( $new_instance );
|
||||
$fields = array_merge(
|
||||
$form->get_text_fields(),
|
||||
$this->get_taxonomy_fields( $new_instance ),
|
||||
$form->get_image_fields()
|
||||
);
|
||||
$updater = new DisplayFeaturedImageGenesisWidgetsUpdate();
|
||||
|
||||
return $updater->update( $new_instance, $old_instance, $fields );
|
||||
return $updater->update( $new_instance, $old_instance, $this->get_fields( $new_instance ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all widget fields.
|
||||
* @return array
|
||||
*/
|
||||
public function get_fields( $instance = array() ) {
|
||||
$form = $this->get_form_class( $instance );
|
||||
return array_merge(
|
||||
$form->get_text_fields(),
|
||||
$this->get_taxonomy_fields( $instance ),
|
||||
$form->get_image_fields()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user