mirror of
https://github.com/10h30/display-featured-image-genesis.git
synced 2026-07-17 13:33:32 +09:00
Add author profile featured image
This commit is contained in:
@@ -28,6 +28,7 @@ function display_featured_image_genesis_require() {
|
||||
$files = array(
|
||||
'class-displayfeaturedimagegenesis',
|
||||
'class-displayfeaturedimagegenesis-admin',
|
||||
'class-displayfeaturedimagegenesis-author',
|
||||
'class-displayfeaturedimagegenesis-common',
|
||||
'class-displayfeaturedimagegenesis-description',
|
||||
'class-displayfeaturedimagegenesis-output',
|
||||
@@ -58,12 +59,16 @@ $displayfeaturedimagegenesis_rss = new Display_Featured_Image_Genesis_RS
|
||||
$displayfeaturedimagegenesis_settings = new Display_Featured_Image_Genesis_Settings(
|
||||
$displayfeaturedimagegenesis_common
|
||||
);
|
||||
$displayfeaturedimagegenesis_author = new Display_Featured_Image_Genesis_Author(
|
||||
$displayfeaturedimagegenesis_settings
|
||||
);
|
||||
$displayfeaturedimagegenesis_taxonomies = new Display_Featured_Image_Genesis_Taxonomies(
|
||||
$displayfeaturedimagegenesis_settings
|
||||
);
|
||||
|
||||
$displayfeaturedimage = new Display_Featured_Image_Genesis(
|
||||
$displayfeaturedimagegenesis_admin,
|
||||
$displayfeaturedimagegenesis_author,
|
||||
$displayfeaturedimagegenesis_common,
|
||||
$displayfeaturedimagegenesis_description,
|
||||
$displayfeaturedimagegenesis_output,
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
class Display_Featured_Image_Genesis_Author {
|
||||
|
||||
protected $settings;
|
||||
protected $name;
|
||||
|
||||
public function __construct( $settings ) {
|
||||
$this->settings = $settings;
|
||||
$this->name = 'displayfeaturedimagegenesis';
|
||||
}
|
||||
|
||||
function do_author_fields( $user ) {
|
||||
|
||||
$id = get_the_author_meta( $this->name, $user->ID );
|
||||
|
||||
echo '<table class="form-table">';
|
||||
|
||||
echo '<tr class="user-featured-image-wrap">';
|
||||
echo '<th scope="row"><label for="' . $this->name . '">Featured Image</label></th>';
|
||||
|
||||
echo '<td>';
|
||||
if ( $id ) {
|
||||
echo wp_kses_post( $this->settings->render_image_preview( $id ) );
|
||||
}
|
||||
|
||||
$this->settings->render_buttons( $id, $this->name );
|
||||
echo '<p class="description">Upload an image to use as your author page featured image.</p>';
|
||||
echo '</td>';
|
||||
echo '</tr>';
|
||||
|
||||
echo '</table>';
|
||||
}
|
||||
|
||||
function save_profile_fields( $user_id ) {
|
||||
|
||||
if ( ! current_user_can( 'edit_user', $user_id ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( empty( $_POST['_wpnonce'] ) ) {
|
||||
wp_die( esc_attr__( 'Something unexpected happened. Please try again.', 'display-featured-image-genesis' ) );
|
||||
}
|
||||
|
||||
$new_value = $_POST[ $this->name ];
|
||||
$old_value = get_the_author_meta( $this->name, $user_id );
|
||||
if ( $old_value !== $new_value ) {
|
||||
$new_value = $this->settings->validate_author_image( $new_value, $old_value );
|
||||
|
||||
update_user_meta( $user_id, $this->name, $new_value );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -182,6 +182,9 @@ class Display_Featured_Image_Genesis_Common {
|
||||
}
|
||||
}
|
||||
}
|
||||
if ( is_author() ) {
|
||||
$image_id = get_the_author_meta( 'displayfeaturedimagegenesis_author', (int) get_query_var( 'author' ) );
|
||||
}
|
||||
// taxonomy
|
||||
if ( is_category() || is_tag() || is_tax() ) {
|
||||
$t_id = $object->term_id;
|
||||
|
||||
@@ -513,6 +513,54 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
return $new_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns old value for author image if not correct file type/size
|
||||
* @param string $new_value New value
|
||||
* @return string New value or old, depending on allowed image size.
|
||||
* @since x.y.z
|
||||
*/
|
||||
public function validate_author_image( $new_value, $old_value ) {
|
||||
|
||||
$medium = get_option( 'medium_size_w' );
|
||||
$source = wp_get_attachment_image_src( $new_value, 'full' );
|
||||
$valid = $this->is_valid_img_ext( $source[0] );
|
||||
$width = $source[1];
|
||||
|
||||
if ( ! $new_value || ( $new_value && $valid && $width > $medium ) ) {
|
||||
return $new_value;
|
||||
}
|
||||
|
||||
add_filter( 'user_profile_update_errors', array( $this, 'user_profile_error_message' ), 10, 3 );
|
||||
|
||||
return $old_value;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* User profile error message
|
||||
* @param var $errors error message depending on what's wrong
|
||||
* @param var $update whether or not to update
|
||||
* @param var $user user being updated
|
||||
* @return error message
|
||||
*
|
||||
* @since x.y.z
|
||||
*/
|
||||
public function user_profile_error_message( $errors, $update, $user ) {
|
||||
$new_value = (int) $_POST['displayfeaturedimagegenesis'];
|
||||
$medium = get_option( 'medium_size_w' );
|
||||
$source = wp_get_attachment_image_src( $new_value, 'full' );
|
||||
$valid = $this->is_valid_img_ext( $source[0] );
|
||||
$width = $source[1];
|
||||
$reset = sprintf( __( ' The %s Featured Image has been reset to the last valid setting.', 'display-featured-image-genesis' ), $user->display_name );
|
||||
|
||||
if ( ! $valid ) {
|
||||
$error = __( 'Sorry, that is an invalid file type.', 'display-featured-image-genesis' );
|
||||
} elseif ( $width <= $medium ) {
|
||||
$error = __( 'Sorry, your image is too small.', 'display-featured-image-genesis' );
|
||||
}
|
||||
$errors->add( 'profile_error', $error . $reset );
|
||||
}
|
||||
|
||||
/**
|
||||
* returns file extension
|
||||
* @since 1.2.2
|
||||
|
||||
@@ -16,8 +16,9 @@
|
||||
*/
|
||||
class Display_Featured_Image_Genesis {
|
||||
|
||||
function __construct( $admin, $common, $description, $output, $rss, $settings, $taxonomies ) {
|
||||
function __construct( $admin, $author, $common, $description, $output, $rss, $settings, $taxonomies ) {
|
||||
$this->admin = $admin;
|
||||
$this->author = $author;
|
||||
$this->common = $common;
|
||||
$this->description = $description;
|
||||
$this->output = $output;
|
||||
@@ -38,6 +39,7 @@ class Display_Featured_Image_Genesis {
|
||||
add_action( 'init', array( $this, 'add_plugin_supports' ) );
|
||||
add_action( 'admin_init', array( $this, 'check_settings' ) );
|
||||
add_action( 'admin_init', array( $this, 'set_taxonomy_meta' ) );
|
||||
add_action( 'admin_init', array( $this, 'set_author_meta' ) );
|
||||
add_action( 'widgets_init', array( $this, 'register_widgets' ) );
|
||||
add_action( 'admin_init', array( $this->admin, 'set_up_columns' ) );
|
||||
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
|
||||
@@ -169,6 +171,20 @@ class Display_Featured_Image_Genesis {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set new profile field for authors
|
||||
*
|
||||
* @since x.y.z
|
||||
*/
|
||||
public function set_author_meta() {
|
||||
// current user
|
||||
add_action( 'profile_personal_options', array( $this->author, 'do_author_fields' ) );
|
||||
add_action( 'personal_options_update', array( $this->author, 'save_profile_fields' ) );
|
||||
// not current user
|
||||
add_action( 'edit_user_profile', array( $this->author, 'do_author_fields' ) );
|
||||
add_action( 'edit_user_profile_update', array( $this->author, 'save_profile_fields' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up text domain for translations
|
||||
*
|
||||
@@ -194,7 +210,7 @@ class Display_Featured_Image_Genesis {
|
||||
|
||||
$screen = get_current_screen();
|
||||
|
||||
if ( 'appearance_page_displayfeaturedimagegenesis' === $screen->id || ! empty( $screen->taxonomy ) ) {
|
||||
if ( 'appearance_page_displayfeaturedimagegenesis' === $screen->id || ! empty( $screen->taxonomy ) || 'profile' === $screen->id ) {
|
||||
wp_enqueue_media();
|
||||
wp_enqueue_script( 'displayfeaturedimage-upload' );
|
||||
wp_localize_script( 'displayfeaturedimage-upload', 'objectL10n', array(
|
||||
|
||||
Reference in New Issue
Block a user