mirror of
https://github.com/10h30/display-featured-image-genesis.git
synced 2026-07-15 12:33:19 +09:00
Create an image settings class
I am sure that right now this breaks meta classes.
This commit is contained in:
@@ -13,6 +13,12 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG
|
||||
*/
|
||||
protected $page = 'displayfeaturedimagegenesis';
|
||||
|
||||
/**
|
||||
* The image settings class.
|
||||
* @var $images \DisplayFeaturedImageGenesisSettingsImages
|
||||
*/
|
||||
private $images;
|
||||
|
||||
/**
|
||||
* Generic function to add settings sections
|
||||
*
|
||||
@@ -229,6 +235,21 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG
|
||||
echo '</select>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Maybe instantiate the image settings class and do the image field.
|
||||
*
|
||||
* @param $args
|
||||
*
|
||||
* @since 3.1.0
|
||||
*/
|
||||
protected function do_image( $args ) {
|
||||
if ( ! isset( $this->images ) ) {
|
||||
include_once 'class-displayfeaturedimagegenesis-settings-images.php';
|
||||
$this->images = new DisplayFeaturedImageGenesisSettingsImages( $this->setting );
|
||||
}
|
||||
$this->images->do_image( $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic callback to display a field description.
|
||||
*
|
||||
@@ -248,51 +269,6 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG
|
||||
printf( '<p class="description">%s</p>', wp_kses_post( $description ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* display image preview
|
||||
*
|
||||
* @param int $id featured image ID
|
||||
* @param $alt string description for alt text
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function render_image_preview( $id, $alt = '' ) {
|
||||
if ( empty( $id ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/* translators: the placeholder refers to which featured image */
|
||||
$alt_text = sprintf( __( '%s featured image', 'display-featured-image-genesis' ), esc_attr( $alt ) );
|
||||
$preview = wp_get_attachment_image_src( (int) $id, 'medium' );
|
||||
|
||||
return sprintf( '<div class="upload-image-preview"><img src="%s" alt="%s" /></div>', esc_url( $preview[0] ), esc_attr( $alt_text ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* show image select/delete buttons
|
||||
*
|
||||
* @param int $id image ID
|
||||
* @param string $name name for value/ID/class
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function render_buttons( $id, $name ) {
|
||||
$id = $id ? (int) $id : '';
|
||||
printf( '<input type="hidden" class="upload-image-id" name="%1$s" value="%2$s" />', esc_attr( $name ), esc_attr( $id ) );
|
||||
printf(
|
||||
'<button id="%s" class="upload-image button-secondary">%s</button>',
|
||||
esc_attr( $name ),
|
||||
esc_attr__( 'Select Image', 'display-featured-image-genesis' )
|
||||
);
|
||||
printf(
|
||||
' <button class="delete-image button-secondary"%s>%s</button>',
|
||||
empty( $id ) ? 'style="display:none;"' : '',
|
||||
esc_attr__( 'Delete Image', 'display-featured-image-genesis' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all public content types, not including built in.
|
||||
*
|
||||
@@ -365,29 +341,6 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if file type is image. updated to use WP function.
|
||||
* @return bool
|
||||
* @since 1.2.2
|
||||
* @since 2.5.0
|
||||
*/
|
||||
protected function is_valid_img_ext( $file ) {
|
||||
$valid = wp_check_filetype( $file );
|
||||
|
||||
return (bool) in_array( $valid['ext'], $this->allowed_file_types(), true );
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the array of allowed image/file types.
|
||||
* @return array
|
||||
* @since 2.5.0
|
||||
*/
|
||||
protected function allowed_file_types() {
|
||||
$allowed = apply_filters( 'displayfeaturedimage_valid_img_types', array( 'jpg', 'jpeg', 'png', 'gif' ) );
|
||||
|
||||
return is_array( $allowed ) ? $allowed : explode( ',', $allowed );
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the user has permission to save the information from the submenu
|
||||
* page.
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class DisplayFeaturedImageGenesisSettingsImages
|
||||
*/
|
||||
class DisplayFeaturedImageGenesisSettingsImages {
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $page = 'displayfeaturedimagegenesis';
|
||||
|
||||
/**
|
||||
* The plugin setting.
|
||||
* @var array $setting
|
||||
*/
|
||||
private $setting;
|
||||
|
||||
/**
|
||||
* DisplayFeaturedImageGenesisSettingsImages constructor.
|
||||
*
|
||||
* @param $setting
|
||||
*/
|
||||
public function __construct( $setting ) {
|
||||
$this->setting = $setting;
|
||||
}
|
||||
|
||||
/**
|
||||
* Do the image settings field.
|
||||
*
|
||||
* @param $args
|
||||
*
|
||||
* @since 3.1.0
|
||||
*/
|
||||
public function do_image( $args ) {
|
||||
$posts_page = $this->has_page_for_posts( $args['id'] );
|
||||
if ( $posts_page ) {
|
||||
/* translators: the link is to edit the posts page. */
|
||||
printf( wp_kses_post( __( 'You may set a fallback image for Posts on your <a href="%s">posts page</a>.', 'display-featured-image-genesis' ) ), esc_url( $posts_page ) );
|
||||
|
||||
return;
|
||||
}
|
||||
$value = $this->get_image_value( $args['id'] );
|
||||
$name = $this->get_image_name( $args['id'] );
|
||||
if ( ! empty( $value ) ) {
|
||||
echo wp_kses_post( $this->render_image_preview( $value, $args['id'] ) );
|
||||
}
|
||||
$this->render_buttons( $value, $name );
|
||||
|
||||
if ( empty( $id ) || in_array( $args['id'], array( 'default', 'search', 'fourohfour', 'post' ), true ) ) {
|
||||
return;
|
||||
}
|
||||
$this->do_cpt_description( $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the CPT description.
|
||||
*
|
||||
* @param $args
|
||||
*/
|
||||
private function do_cpt_description( $args ) {
|
||||
$archive_link = get_post_type_archive_link( $args['id'] );
|
||||
if ( ! $archive_link ) {
|
||||
return;
|
||||
}
|
||||
$description = sprintf(
|
||||
/* translators: placeholder is the post type name. */
|
||||
__( 'View your <a href="%1$s" target="_blank">%2$s</a> archive.', 'display-featured-image-genesis' ),
|
||||
esc_url( $archive_link ),
|
||||
esc_attr( $args['title'] )
|
||||
);
|
||||
printf( '<p class="description">%s</p>', wp_kses_post( $description ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a posts page has been set: if so, return the edit link for it, otherwise return false.
|
||||
*
|
||||
* @param $id
|
||||
*
|
||||
* @return bool|string|null
|
||||
* @since 3.1.0
|
||||
*/
|
||||
private function has_page_for_posts( $id ) {
|
||||
if ( 'post' !== $id ) {
|
||||
return false;
|
||||
}
|
||||
$show_on_front = get_option( 'show_on_front' );
|
||||
$posts_page = get_option( 'page_for_posts' );
|
||||
|
||||
return ( 'page' === $show_on_front && $posts_page ) ? get_edit_post_link( $posts_page ) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the image setting value.
|
||||
* @since 3.1.0
|
||||
* @param $id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_image_value( $id ) {
|
||||
$value = '';
|
||||
if ( 'default' === $id && $this->setting[ $id ] ) {
|
||||
$value = $this->setting[ $id ];
|
||||
} elseif ( ! empty( $this->setting['post_type'][ $id ] ) ) {
|
||||
$value = $this->setting['post_type'][ $id ];
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the image setting name.
|
||||
* @since 3.1.0
|
||||
* @param $id
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_image_name( $id ) {
|
||||
if ( 'default' === $id ) {
|
||||
$name = "{$this->page}[{$id}]";
|
||||
} else {
|
||||
$name = "{$this->page}[post_type][{$id}]";
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* display image preview
|
||||
*
|
||||
* @param int $id featured image ID
|
||||
* @param $alt string description for alt text
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function render_image_preview( $id, $alt = '' ) {
|
||||
if ( empty( $id ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
/* translators: the placeholder refers to which featured image */
|
||||
$alt_text = sprintf( __( '%s featured image', 'display-featured-image-genesis' ), esc_attr( $alt ) );
|
||||
$preview = wp_get_attachment_image_src( (int) $id, 'medium' );
|
||||
|
||||
return sprintf( '<div class="upload-image-preview"><img src="%s" alt="%s" /></div>', esc_url( $preview[0] ), esc_attr( $alt_text ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* show image select/delete buttons
|
||||
*
|
||||
* @param int $id image ID
|
||||
* @param string $name name for value/ID/class
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function render_buttons( $id, $name ) {
|
||||
$id = $id ? (int) $id : '';
|
||||
printf( '<input type="hidden" class="upload-image-id" name="%1$s" value="%2$s" />', esc_attr( $name ), esc_attr( $id ) );
|
||||
printf(
|
||||
'<button id="%s" class="upload-image button-secondary">%s</button>',
|
||||
esc_attr( $name ),
|
||||
esc_attr__( 'Select Image', 'display-featured-image-genesis' )
|
||||
);
|
||||
printf(
|
||||
' <button class="delete-image button-secondary"%s>%s</button>',
|
||||
empty( $id ) ? 'style="display:none;"' : '',
|
||||
esc_attr__( 'Delete Image', 'display-featured-image-genesis' )
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -204,82 +204,6 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen
|
||||
return __( 'Optionally, change the hook location and priority of the featured image output. Use with caution. Note: this will change the hook/priority of the featured image sitewide. If you need to make changes based on content type, check the readme for code examples.', 'display-featured-image-genesis' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Default image uploader
|
||||
*
|
||||
* @since 1.2.1
|
||||
*
|
||||
* @param $args
|
||||
*/
|
||||
public function set_default_image( $args ) {
|
||||
|
||||
$id = $this->setting[ $args['id'] ] ? $this->setting[ $args['id'] ] : '';
|
||||
$name = $this->page . '[' . $args['id'] . ']';
|
||||
if ( ! empty( $id ) ) {
|
||||
echo wp_kses_post( $this->render_image_preview( $id, $args['id'] ) );
|
||||
}
|
||||
$this->render_buttons( $id, $name );
|
||||
$this->do_description( $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom Post Type image uploader
|
||||
*
|
||||
* @since 2.0.0
|
||||
*
|
||||
* @param $args
|
||||
*/
|
||||
public function set_cpt_image( $args ) {
|
||||
|
||||
$this->do_image_buttons( $args );
|
||||
|
||||
if ( empty( $id ) || in_array( $args['id'], array( 'search', 'fourohfour', 'post' ), true ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->do_cpt_description( $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the featured image preview and buttons.
|
||||
*
|
||||
* @param $args
|
||||
*/
|
||||
protected function do_image_buttons( $args ) {
|
||||
$show_on_front = get_option( 'show_on_front' );
|
||||
$posts_page = get_option( 'page_for_posts' );
|
||||
if ( 'page' === $show_on_front && $posts_page && 'post' === $args['id'] ) {
|
||||
$link = get_edit_post_link( $posts_page );
|
||||
/* translators: the link is to edit the posts page. */
|
||||
printf( wp_kses_post( __( 'You may set a fallback image for Posts on your <a href="%s">posts page</a>.', 'display-featured-image-genesis' ) ), esc_url( $link ) );
|
||||
return;
|
||||
}
|
||||
$id = isset( $this->setting['post_type'][ $args['id'] ] ) && $this->setting['post_type'][ $args['id'] ] ? $this->setting['post_type'][ $args['id'] ] : '';
|
||||
$name = $this->page . '[post_type][' . esc_attr( $args['id'] ) . ']';
|
||||
if ( $id ) {
|
||||
echo wp_kses_post( $this->render_image_preview( $id, $args['id'] ) );
|
||||
}
|
||||
$this->render_buttons( $id, $name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the CPT description.
|
||||
*
|
||||
* @param $args
|
||||
*/
|
||||
protected function do_cpt_description( $args ) {
|
||||
$archive_link = get_post_type_archive_link( $args['id'] );
|
||||
if ( ! $archive_link ) {
|
||||
return;
|
||||
}
|
||||
/* translators: placeholder is the post type name. */
|
||||
$description = sprintf( __( 'View your <a href="%1$s" target="_blank">%2$s</a> archive.', 'display-featured-image-genesis' ),
|
||||
esc_url( $archive_link ),
|
||||
esc_attr( $args['title'] )
|
||||
);
|
||||
printf( '<p class="description">%s</p>', wp_kses_post( $description ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user