mirror of
https://github.com/10h30/display-featured-image-genesis.git
synced 2026-06-05 15:08:20 +09:00
Merge branch 'feature/term_meta' into develop
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
* @author Robin Cornett <hello@robincornett.com>
|
||||
* @license GPL-2.0+
|
||||
* @link http://robincornett.com
|
||||
* @copyright 2014-2015 Robin Cornett Creative, LLC
|
||||
* @copyright 2014-2015-2015 Robin Cornett Creative, LLC
|
||||
*
|
||||
* @wordpress-plugin
|
||||
* Plugin Name: Display Featured Image for Genesis
|
||||
@@ -31,6 +31,7 @@ if ( ! defined( 'DISPLAYFEATUREDIMAGEGENESIS_BASENAME' ) ) {
|
||||
function display_featured_image_genesis_require() {
|
||||
$files = array(
|
||||
'class-displayfeaturedimagegenesis',
|
||||
'class-displayfeaturedimagegenesis-helper',
|
||||
'class-displayfeaturedimagegenesis-admin',
|
||||
'class-displayfeaturedimagegenesis-author',
|
||||
'class-displayfeaturedimagegenesis-common',
|
||||
@@ -48,6 +49,7 @@ function display_featured_image_genesis_require() {
|
||||
display_featured_image_genesis_require();
|
||||
|
||||
// Instantiate dependent classes
|
||||
$displayfeaturedimagegenesis_helper = new Display_Featured_Image_Genesis_Helper();
|
||||
$displayfeaturedimagegenesis_admin = new Display_Featured_Image_Genesis_Admin();
|
||||
$displayfeaturedimagegenesis_author = new Display_Featured_Image_Genesis_Author();
|
||||
$displayfeaturedimagegenesis_common = new Display_Featured_Image_Genesis_Common();
|
||||
|
||||
@@ -106,15 +106,14 @@ class Display_Featured_Image_Genesis_Admin {
|
||||
return;
|
||||
}
|
||||
|
||||
$term_meta = get_option( "displayfeaturedimagegenesis_$term_id" );
|
||||
|
||||
if ( empty( $term_meta['term_image'] ) ) {
|
||||
$image_id = displayfeaturedimagegenesis_term_image( $term_id );
|
||||
if ( ! $image_id ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$taxonomy = filter_input( INPUT_POST, 'taxonomy', FILTER_SANITIZE_STRING );
|
||||
$taxonomy = ! is_null( $taxonomy ) ? $taxonomy : get_current_screen()->taxonomy;
|
||||
$image_id = displayfeaturedimagegenesis_check_image_id( $term_meta['term_image'] );
|
||||
$image_id = displayfeaturedimagegenesis_check_image_id( $image_id );
|
||||
|
||||
$args = array(
|
||||
'image_id' => $image_id,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
class Display_Featured_Image_Genesis_Author {
|
||||
class Display_Featured_Image_Genesis_Author extends Display_Featured_Image_Genesis_Helper {
|
||||
|
||||
protected $settings;
|
||||
protected $name;
|
||||
@@ -22,7 +22,7 @@ class Display_Featured_Image_Genesis_Author {
|
||||
add_action( 'edit_user_profile_update', array( $this, 'save_profile_fields' ) );
|
||||
}
|
||||
|
||||
function do_author_fields( $user ) {
|
||||
public function do_author_fields( $user ) {
|
||||
|
||||
$id = get_the_author_meta( $this->name, $user->ID );
|
||||
|
||||
@@ -33,10 +33,10 @@ class Display_Featured_Image_Genesis_Author {
|
||||
|
||||
echo '<td>';
|
||||
if ( $id ) {
|
||||
echo wp_kses_post( $this->settings->render_image_preview( $id ) );
|
||||
echo wp_kses_post( $this->render_image_preview( $id ) );
|
||||
}
|
||||
|
||||
$this->settings->render_buttons( $id, $this->name );
|
||||
$this->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>';
|
||||
@@ -44,7 +44,7 @@ class Display_Featured_Image_Genesis_Author {
|
||||
echo '</table>';
|
||||
}
|
||||
|
||||
function save_profile_fields( $user_id ) {
|
||||
public function save_profile_fields( $user_id ) {
|
||||
|
||||
if ( ! current_user_can( 'edit_user', $user_id ) ) {
|
||||
return false;
|
||||
@@ -57,10 +57,57 @@ class Display_Featured_Image_Genesis_Author {
|
||||
$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 );
|
||||
$new_value = $this->validate_author_image( $new_value, $old_value );
|
||||
|
||||
update_user_meta( $user_id, $this->name, $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 2.3.0
|
||||
*/
|
||||
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 2.3.0
|
||||
*/
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,11 +156,10 @@ class Display_Featured_Image_Genesis_Common {
|
||||
}
|
||||
// taxonomy
|
||||
if ( is_category() || is_tag() || is_tax() ) {
|
||||
$t_id = $object->term_id;
|
||||
$term_meta = get_option( "displayfeaturedimagegenesis_$t_id" );
|
||||
// if there is a term image
|
||||
if ( ! empty( $term_meta['term_image'] ) ) {
|
||||
$image_id = displayfeaturedimagegenesis_check_image_id( $term_meta['term_image'] );
|
||||
$term_id = $object->term_id;
|
||||
$term_meta = displayfeaturedimagegenesis_term_image( $term_id );
|
||||
if ( $term_meta ) {
|
||||
$image_id = $term_meta;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* @author Robin Cornett <hello@robincornett.com>
|
||||
* @license GPL-2.0+
|
||||
* @link http://robincornett.com
|
||||
* @copyright 2014 Robin Cornett Creative, LLC
|
||||
* @copyright 2014-2015 Robin Cornett Creative, LLC
|
||||
*/
|
||||
|
||||
class Display_Featured_Image_Genesis_Description {
|
||||
|
||||
@@ -0,0 +1,255 @@
|
||||
<?php
|
||||
/**
|
||||
* @package DisplayFeaturedImageGenesis
|
||||
* @author Robin Cornett <hello@robincornett.com>
|
||||
* @license GPL-2.0+
|
||||
* @link http://robincornett.com
|
||||
* @copyright 2014-2015 Robin Cornett Creative, LLC
|
||||
*/
|
||||
|
||||
class Display_Featured_Image_Genesis_Helper {
|
||||
|
||||
/**
|
||||
* Generic function to add settings sections
|
||||
*
|
||||
* @since x.y.z
|
||||
*/
|
||||
protected function add_sections( $sections ) {
|
||||
|
||||
foreach ( $sections as $section ) {
|
||||
add_settings_section(
|
||||
$section['id'],
|
||||
$section['title'],
|
||||
array( $this, $section['id'] . '_section_description' ),
|
||||
$this->page
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic function to add settings fields
|
||||
* @param array $sections registered sections
|
||||
* @return array all settings fields
|
||||
*
|
||||
* @since x.y.z
|
||||
*/
|
||||
protected function add_fields( $fields, $sections ) {
|
||||
|
||||
foreach ( $fields as $field ) {
|
||||
add_settings_field(
|
||||
'[' . $field['id'] . ']',
|
||||
sprintf( '<label for="%s">%s</label>', $field['id'], $field['title'] ),
|
||||
array( $this, $field['callback'] ),
|
||||
$this->page,
|
||||
$sections[ $field['section'] ]['id'],
|
||||
empty( $field['args'] ) ? array() : $field['args']
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Echoes out the section description.
|
||||
* @param string $description text string for description
|
||||
* @return string as paragraph and escaped
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
protected function print_section_description( $description ) {
|
||||
echo wp_kses_post( wpautop( $description ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic callback to create a number field setting.
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function do_number( $args ) {
|
||||
|
||||
printf( '<label for="%s[%s]">%s</label>', esc_attr( $this->page ),esc_attr( $args['setting'] ), esc_attr( $args['label'] ) );
|
||||
printf( '<input type="number" step="1" min="%1$s" max="%2$s" id="%5$s[%3$s]" name="%5$s[%3$s]" value="%4$s" class="small-text" />',
|
||||
(int) $args['min'],
|
||||
(int) $args['max'],
|
||||
esc_attr( $args['setting'] ),
|
||||
esc_attr( $this->displaysetting[ $args['setting'] ] ),
|
||||
esc_attr( $this->page )
|
||||
);
|
||||
$this->do_description( $args['setting'] );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* generic checkbox function (for all checkbox settings)
|
||||
* @return 0 1 checkbox
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function do_checkbox( $args ) {
|
||||
printf( '<input type="hidden" name="displayfeaturedimagegenesis[%s]" value="0" />', esc_attr( $args['setting'] ) );
|
||||
printf( '<label for="displayfeaturedimagegenesis[%1$s]"><input type="checkbox" name="displayfeaturedimagegenesis[%1$s]" id="displayfeaturedimagegenesis[%1$s]" value="1" %2$s class="code" />%3$s</label>',
|
||||
esc_attr( $args['setting'] ),
|
||||
checked( 1, esc_attr( $this->displaysetting[ $args['setting'] ] ), false ),
|
||||
esc_attr( $args['label'] )
|
||||
);
|
||||
$this->do_description( $args['setting'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic callback to display a field description.
|
||||
* @param string $args setting name used to identify description callback
|
||||
* @return string Description to explain a field.
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
protected function do_description( $args ) {
|
||||
$function = $args . '_description';
|
||||
if ( ! method_exists( $this, $function ) ) {
|
||||
return;
|
||||
}
|
||||
$description = $this->$function();
|
||||
printf( '<p class="description">%s</p>', wp_kses_post( $description ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* display image preview
|
||||
* @param variable $id featured image ID
|
||||
* @return $image image preview
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function render_image_preview( $id ) {
|
||||
if ( empty( $id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$id = displayfeaturedimagegenesis_check_image_id( $id );
|
||||
$preview = wp_get_attachment_image_src( (int) $id, 'medium' );
|
||||
$image = sprintf( '<div class="upload_logo_preview"><img src="%s" /></div>', $preview[0] );
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* show image select/delete buttons
|
||||
* @param variable $id image ID
|
||||
* @param varable $name name for value/ID/class
|
||||
* @return $buttons select/delete image buttons
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function render_buttons( $id, $name ) {
|
||||
$id = displayfeaturedimagegenesis_check_image_id( $id );
|
||||
$id = $id ? (int) $id : '';
|
||||
printf( '<input type="hidden" class="upload_image_id" id="%1$s" name="%1$s" value="%2$s" />', esc_attr( $name ), esc_attr( $id ) );
|
||||
printf( '<input id="%s" type="button" class="upload_default_image button-secondary" value="%s" />',
|
||||
esc_attr( $name ),
|
||||
esc_attr__( 'Select Image', 'display-featured-image-genesis' )
|
||||
);
|
||||
if ( ! empty( $id ) ) {
|
||||
printf( ' <input type="button" class="delete_image button-secondary" value="%s" />',
|
||||
esc_attr__( 'Delete Image', 'display-featured-image-genesis' )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the user has permission to save the information from the submenu
|
||||
* page.
|
||||
*
|
||||
* @since 2.3.0
|
||||
* @access protected
|
||||
*
|
||||
* @param string $action The name of the action specified on the submenu page
|
||||
* @param string $nonce The nonce specified on the submenu page
|
||||
*
|
||||
* @return bool True if the user has permission to save; false, otherwise.
|
||||
* @author Tom McFarlin (https://tommcfarlin.com/save-wordpress-submenu-page-options/)
|
||||
*/
|
||||
protected function user_can_save( $action, $nonce ) {
|
||||
$is_nonce_set = isset( $_POST[ $nonce ] );
|
||||
$is_valid_nonce = false;
|
||||
|
||||
if ( $is_nonce_set ) {
|
||||
$is_valid_nonce = wp_verify_nonce( $_POST[ $nonce ], $action );
|
||||
}
|
||||
return ( $is_nonce_set && $is_valid_nonce );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns previous value for image if not correct file type/size
|
||||
* @param string $new_value New value
|
||||
* @return string New or previous value, depending on allowed image size.
|
||||
* @since 1.2.2
|
||||
*/
|
||||
protected function validate_image( $new_value, $old_value, $label, $size_to_check ) {
|
||||
|
||||
// ok for field to be empty
|
||||
if ( ! $new_value ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$new_value = displayfeaturedimagegenesis_check_image_id( $new_value );
|
||||
$old_value = displayfeaturedimagegenesis_check_image_id( $old_value );
|
||||
$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' ), $label );
|
||||
|
||||
if ( $valid && $width > $size_to_check ) {
|
||||
return (int) $new_value;
|
||||
}
|
||||
|
||||
$new_value = $old_value;
|
||||
if ( ! $valid ) {
|
||||
$message = __( 'Sorry, that is an invalid file type.', 'display-featured-image-genesis' );
|
||||
$class = 'invalid';
|
||||
} elseif ( $width <= $size_to_check ) {
|
||||
$message = __( 'Sorry, your image is too small.', 'display-featured-image-genesis' );
|
||||
$class = 'weetiny';
|
||||
}
|
||||
|
||||
add_settings_error(
|
||||
$old_value,
|
||||
esc_attr( $class ),
|
||||
esc_attr( $message . $reset ),
|
||||
'error'
|
||||
);
|
||||
|
||||
return (int) $new_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* returns file extension
|
||||
* @since 1.2.2
|
||||
*/
|
||||
protected function get_file_ext( $file ) {
|
||||
$parsed = @parse_url( $file, PHP_URL_PATH );
|
||||
return $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if file type is image
|
||||
* @return file check file extension against list
|
||||
* @since 1.2.2
|
||||
*/
|
||||
protected function is_valid_img_ext( $file ) {
|
||||
$file_ext = $this->get_file_ext( $file );
|
||||
|
||||
$is_valid_types = (array) apply_filters( 'displayfeaturedimage_valid_img_types', array( 'jpg', 'jpeg', 'png', 'gif' ) );
|
||||
|
||||
return ( $file_ext && in_array( $file_ext, $is_valid_types ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a 1 or 0, for all truthy / falsy values.
|
||||
*
|
||||
* Uses double casting. First, we cast to bool, then to integer.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @param mixed $new_value Should ideally be a 1 or 0 integer passed in
|
||||
* @return integer 1 or 0.
|
||||
*/
|
||||
protected function one_zero( $new_value ) {
|
||||
return (int) (bool) $new_value;
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
* @author Robin Cornett <hello@robincornett.com>
|
||||
* @license GPL-2.0+
|
||||
* @link http://robincornett.com
|
||||
* @copyright 2014 Robin Cornett Creative, LLC
|
||||
* @copyright 2014-2015 Robin Cornett Creative, LLC
|
||||
*/
|
||||
|
||||
class Display_Featured_Image_Genesis_Output {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* @author Robin Cornett <hello@robincornett.com>
|
||||
* @license GPL-2.0+
|
||||
* @link http://robincornett.com
|
||||
* @copyright 2014 Robin Cornett Creative, LLC
|
||||
* @copyright 2014-2015 Robin Cornett Creative, LLC
|
||||
*/
|
||||
|
||||
class Display_Featured_Image_Genesis_RSS {
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
* @author Robin Cornett <hello@robincornett.com>
|
||||
* @license GPL-2.0+
|
||||
* @link http://robincornett.com
|
||||
* @copyright 2014 Robin Cornett Creative, LLC
|
||||
* @copyright 2014-2015 Robin Cornett Creative, LLC
|
||||
*/
|
||||
|
||||
class Display_Featured_Image_Genesis_Settings {
|
||||
class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Genesis_Helper {
|
||||
|
||||
/**
|
||||
* variable set for featured image option
|
||||
@@ -31,7 +31,7 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
|
||||
add_theme_page(
|
||||
__( 'Display Featured Image for Genesis', 'display-featured-image-genesis' ),
|
||||
__( 'Display Featured Image Settings', 'display-featured-image-genesis' ),
|
||||
__( 'Display Featured Image for Genesis', 'display-featured-image-genesis' ),
|
||||
'manage_options',
|
||||
$this->page,
|
||||
array( $this, 'do_settings_form' )
|
||||
@@ -41,7 +41,6 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
add_action( 'load-appearance_page_displayfeaturedimagegenesis', array( $this, 'help' ) );
|
||||
|
||||
$this->displaysetting = $this->get_display_setting();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,11 +50,17 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
* @since 1.4.0
|
||||
*/
|
||||
public function do_settings_form() {
|
||||
if ( $this->terms_need_updating() ) {
|
||||
$this->update_delete_term_meta();
|
||||
}
|
||||
$page_title = get_admin_page_title();
|
||||
$heading = $GLOBALS['wp_version'] >= '4.3' ? 'h1' : 'h2';
|
||||
|
||||
echo '<div class="wrap">';
|
||||
printf( '<%1$s>%2$s</%1$s>', esc_attr( $heading ), esc_attr( $page_title ) );
|
||||
if ( $this->terms_need_updating() ) {
|
||||
$this->term_meta_notice();
|
||||
}
|
||||
echo '<form action="options.php" method="post">';
|
||||
settings_fields( 'displayfeaturedimagegenesis' );
|
||||
do_settings_sections( 'displayfeaturedimagegenesis' );
|
||||
@@ -64,7 +69,6 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
settings_errors();
|
||||
echo '</form>';
|
||||
echo '</div>';
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,15 +136,7 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
);
|
||||
}
|
||||
|
||||
foreach ( $sections as $section ) {
|
||||
add_settings_section(
|
||||
$section['id'],
|
||||
$section['title'],
|
||||
array( $this, $section['id'] . '_section_description' ),
|
||||
$this->page
|
||||
);
|
||||
}
|
||||
|
||||
$this->add_sections( $sections );
|
||||
$this->register_fields( $sections );
|
||||
}
|
||||
|
||||
@@ -217,17 +213,7 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $this->fields as $field ) {
|
||||
add_settings_field(
|
||||
'[' . $field['id'] . ']',
|
||||
sprintf( '<label for="%s">%s</label>', $field['id'], $field['title'] ),
|
||||
array( $this, $field['callback'] ),
|
||||
$this->page,
|
||||
$sections[ $field['section'] ]['id'],
|
||||
empty( $field['args'] ) ? array() : $field['args']
|
||||
);
|
||||
}
|
||||
|
||||
$this->add_fields( $this->fields, $sections );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -252,36 +238,6 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
$this->print_section_description( $description );
|
||||
}
|
||||
|
||||
/**
|
||||
* Echoes out the section description.
|
||||
* @param string $description text string for description
|
||||
* @return string as paragraph and escaped
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
protected function print_section_description( $description ) {
|
||||
echo wp_kses_post( wpautop( $description ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic callback to create a number field setting.
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function do_number( $args ) {
|
||||
|
||||
printf( '<label for="%s[%s]">%s</label>', esc_attr( $this->page ),esc_attr( $args['setting'] ), esc_attr( $args['label'] ) );
|
||||
printf( '<input type="number" step="1" min="%1$s" max="%2$s" id="%5$s[%3$s]" name="%5$s[%3$s]" value="%4$s" class="small-text" />',
|
||||
(int) $args['min'],
|
||||
(int) $args['max'],
|
||||
esc_attr( $args['setting'] ),
|
||||
esc_attr( $this->displaysetting[ $args['setting'] ] ),
|
||||
esc_attr( $this->page )
|
||||
);
|
||||
$this->do_description( $args['setting'] );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Description for less_header setting.
|
||||
* @return string description
|
||||
@@ -325,22 +281,6 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* generic checkbox function (for all checkbox settings)
|
||||
* @return 0 1 checkbox
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function do_checkbox( $args ) {
|
||||
printf( '<input type="hidden" name="displayfeaturedimagegenesis[%s]" value="0" />', esc_attr( $args['setting'] ) );
|
||||
printf( '<label for="displayfeaturedimagegenesis[%1$s]"><input type="checkbox" name="displayfeaturedimagegenesis[%1$s]" id="displayfeaturedimagegenesis[%1$s]" value="1" %2$s class="code" />%3$s</label>',
|
||||
esc_attr( $args['setting'] ),
|
||||
checked( 1, esc_attr( $this->displaysetting[ $args['setting'] ] ), false ),
|
||||
esc_attr( $args['label'] )
|
||||
);
|
||||
$this->do_description( $args['setting'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom Post Type image uploader
|
||||
*
|
||||
@@ -350,8 +290,6 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
*/
|
||||
public function set_cpt_image( $args ) {
|
||||
|
||||
$item = Display_Featured_Image_Genesis_Common::get_image_variables();
|
||||
|
||||
$post_type = $args['post_type']->name;
|
||||
if ( empty( $this->displaysetting['post_type'][ $post_type ] ) ) {
|
||||
$this->displaysetting['post_type'][ $post_type ] = $id = '';
|
||||
@@ -375,96 +313,6 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
printf( '<p class="description">%s</p>', wp_kses_post( $description ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* display image preview
|
||||
* @param variable $id featured image ID
|
||||
* @return $image image preview
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function render_image_preview( $id ) {
|
||||
if ( empty( $id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$id = displayfeaturedimagegenesis_check_image_id( $id );
|
||||
$preview = wp_get_attachment_image_src( (int) $id, 'medium' );
|
||||
$image = sprintf( '<div class="upload_logo_preview"><img src="%s" /></div>', $preview[0] );
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* show image select/delete buttons
|
||||
* @param variable $id image ID
|
||||
* @param varable $name name for value/ID/class
|
||||
* @return $buttons select/delete image buttons
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public function render_buttons( $id, $name ) {
|
||||
$id = displayfeaturedimagegenesis_check_image_id( $id );
|
||||
$id = $id ? (int) $id : '';
|
||||
printf( '<input type="hidden" class="upload_image_id" id="%1$s" name="%1$s" value="%2$s" />', esc_attr( $name ), esc_attr( $id ) );
|
||||
printf( '<input id="%s" type="button" class="upload_default_image button-secondary" value="%s" />',
|
||||
esc_attr( $name ),
|
||||
esc_attr__( 'Select Image', 'display-featured-image-genesis' )
|
||||
);
|
||||
if ( ! empty( $id ) ) {
|
||||
printf( ' <input type="button" class="delete_image button-secondary" value="%s" />',
|
||||
esc_attr__( 'Delete Image', 'display-featured-image-genesis' )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save extra taxonomy fields callback function.
|
||||
* @param term id $term_id the id of the term
|
||||
* @return updated option updated option for term featured image
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public function save_taxonomy_custom_meta( $term_id ) {
|
||||
|
||||
if ( isset( $_POST['displayfeaturedimagegenesis'] ) ) {
|
||||
$t_id = $term_id;
|
||||
$displaysetting = get_option( "displayfeaturedimagegenesis_$t_id" );
|
||||
$cat_keys = array_keys( $_POST['displayfeaturedimagegenesis'] );
|
||||
$is_updated = false;
|
||||
foreach ( $cat_keys as $key ) {
|
||||
if ( isset( $_POST['displayfeaturedimagegenesis'][ $key ] ) ) {
|
||||
$displaysetting[ $key ] = $_POST['displayfeaturedimagegenesis'][ $key ];
|
||||
if ( $_POST['displayfeaturedimagegenesis']['term_image'] === $displaysetting[ $key ] ) {
|
||||
$displaysetting[ $key ] = $this->validate_taxonomy_image( $_POST['displayfeaturedimagegenesis'][ $key ] );
|
||||
if ( false !== $displaysetting[ $key ] ) {
|
||||
$is_updated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Save the option array.
|
||||
if ( $is_updated ) {
|
||||
update_option( "displayfeaturedimagegenesis_$t_id", $displaysetting );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic callback to display a field description.
|
||||
* @param string $args setting name used to identify description callback
|
||||
* @return string Description to explain a field.
|
||||
*
|
||||
* @since 2.3.0
|
||||
*/
|
||||
protected function do_description( $args ) {
|
||||
$function = $args . '_description';
|
||||
if ( ! method_exists( $this, $function ) ) {
|
||||
return;
|
||||
}
|
||||
$description = $this->$function();
|
||||
printf( '<p class="description">%s</p>', wp_kses_post( $description ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* validate all inputs
|
||||
* @param string $new_value various settings
|
||||
@@ -515,29 +363,6 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the user has permission to save the information from the submenu
|
||||
* page.
|
||||
*
|
||||
* @since 2.3.0
|
||||
* @access private
|
||||
*
|
||||
* @param string $action The name of the action specified on the submenu page
|
||||
* @param string $nonce The nonce specified on the submenu page
|
||||
*
|
||||
* @return bool True if the user has permission to save; false, otherwise.
|
||||
* @author Tom McFarlin (https://tommcfarlin.com/save-wordpress-submenu-page-options/)
|
||||
*/
|
||||
private function user_can_save( $action, $nonce ) {
|
||||
$is_nonce_set = isset( $_POST[ $nonce ] );
|
||||
$is_valid_nonce = false;
|
||||
|
||||
if ( $is_nonce_set ) {
|
||||
$is_valid_nonce = wp_verify_nonce( $_POST[ $nonce ], $action );
|
||||
}
|
||||
return ( $is_nonce_set && $is_valid_nonce );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns previous value for image if not correct file type/size
|
||||
* @param string $new_value New value
|
||||
@@ -581,113 +406,6 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
return (int) $new_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns false value for image if not correct file type/size
|
||||
* @param string $new_value New value
|
||||
* @return string New value or false, depending on allowed image size.
|
||||
* @since 2.0.0
|
||||
*/
|
||||
protected function validate_taxonomy_image( $new_value ) {
|
||||
|
||||
// if the image was selected using the old URL method
|
||||
$new_value = displayfeaturedimagegenesis_check_image_id( $new_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];
|
||||
|
||||
// ok for field to be empty
|
||||
if ( $new_value && ( ! $valid || $width <= $medium ) ) {
|
||||
$new_value = false;
|
||||
}
|
||||
|
||||
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 2.3.0
|
||||
*/
|
||||
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 2.3.0
|
||||
*/
|
||||
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
|
||||
*/
|
||||
protected function get_file_ext( $file ) {
|
||||
$parsed = @parse_url( $file, PHP_URL_PATH );
|
||||
return $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* check if file type is image
|
||||
* @return file check file extension against list
|
||||
* @since 1.2.2
|
||||
*/
|
||||
protected function is_valid_img_ext( $file ) {
|
||||
$file_ext = $this->get_file_ext( $file );
|
||||
|
||||
$is_valid_types = (array) apply_filters( 'displayfeaturedimage_valid_img_types', array( 'jpg', 'jpeg', 'png', 'gif' ) );
|
||||
|
||||
return ( $file_ext && in_array( $file_ext, $is_valid_types ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a 1 or 0, for all truthy / falsy values.
|
||||
*
|
||||
* Uses double casting. First, we cast to bool, then to integer.
|
||||
*
|
||||
* @since 1.3.0
|
||||
*
|
||||
* @param mixed $new_value Should ideally be a 1 or 0 integer passed in
|
||||
* @return integer 1 or 0.
|
||||
*/
|
||||
protected function one_zero( $new_value ) {
|
||||
return (int) (bool) $new_value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Help tab for media screen
|
||||
* @return help tab with verbose information for plugin
|
||||
@@ -784,4 +502,128 @@ class Display_Featured_Image_Genesis_Settings {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For 4.4, output a notice explaining that old term options can be updated to term_meta.
|
||||
* Options are to update all terms or to ignore, and do by hand.
|
||||
* @since 2.4.0
|
||||
*/
|
||||
protected function term_meta_notice() {
|
||||
$screen = get_current_screen();
|
||||
if ( 'appearance_page_displayfeaturedimagegenesis' !== $screen->id ) {
|
||||
return;
|
||||
}
|
||||
$terms = $this->get_affected_terms();
|
||||
if ( empty( $terms ) ) {
|
||||
return;
|
||||
}
|
||||
$message = sprintf( '<p>%s</p>', __( 'WordPress 4.4 introduced term metadata for categories, tags, and other taxonomies. I\'d like to automatically convert your existing term images to use this new awesomeness.', 'display-featured-image-genesis' ) );
|
||||
$message .= sprintf( '<p>%s</p>', __( 'This will modify your database, so if you\'d rather do it yourself, you can. Here\'s a list of the affected terms:', 'display-featured-image-genesis' ) );
|
||||
$message .= '<ul style="margin-left:40px;">';
|
||||
foreach ( $terms as $term ) {
|
||||
$message .= edit_term_link( $term->name, '<li>', '</li>', $term, false );
|
||||
}
|
||||
$message .= '</ul>';
|
||||
$message .= sprintf( '<p>%s</p>', __( 'If you update the terms by hand, this notice will disappear, or you can dismiss the notice. Or, you know, click the Update button to do it all at once. Just please double check your terms after the update process.', 'display-featured-image-genesis' ) );
|
||||
echo '<div class="updated">' . wp_kses_post( $message );
|
||||
echo '<form action="" method="post">';
|
||||
wp_nonce_field( 'displayfeaturedimagegenesis_metanonce', 'displayfeaturedimagegenesis_metanonce', false );
|
||||
$buttons = array(
|
||||
array(
|
||||
'value' => __( 'Update My Terms', 'display-featured-image-genesis' ),
|
||||
'name' => 'displayfeaturedimagegenesis_termmeta',
|
||||
'class' => 'button-primary',
|
||||
),
|
||||
array(
|
||||
'value' => __( 'Dismiss (I\'ve got this!)', 'display-featured-image-genesis' ),
|
||||
'name' => 'displayfeaturedimagegenesis_termmetadismiss',
|
||||
'class' => 'button-secondary',
|
||||
),
|
||||
);
|
||||
echo '<p>';
|
||||
foreach ( $buttons as $button ) {
|
||||
printf( '<input type="submit" class="%s" name="%s" value="%s" style="margin-right:12px;" />',
|
||||
esc_attr( $button['class'] ),
|
||||
esc_attr( $button['name'] ),
|
||||
esc_attr( $button['value'] )
|
||||
);
|
||||
}
|
||||
echo '</p>';
|
||||
echo '</form>';
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Update and/or delete term_meta and wp_options
|
||||
* @since 2.4.0
|
||||
*/
|
||||
protected function update_delete_term_meta() {
|
||||
|
||||
if ( isset( $_POST['displayfeaturedimagegenesis_termmeta'] ) ) {
|
||||
if ( ! check_admin_referer( 'displayfeaturedimagegenesis_metanonce', 'displayfeaturedimagegenesis_metanonce' ) ) {
|
||||
return;
|
||||
}
|
||||
$terms = $this->get_affected_terms();
|
||||
foreach ( $terms as $term ) {
|
||||
$term_id = $term->term_id;
|
||||
$option = get_option( "displayfeaturedimagegenesis_{$term_id}" );
|
||||
if ( false !== $option ) {
|
||||
$image_id = (int) displayfeaturedimagegenesis_check_image_id( $option['term_image'] );
|
||||
update_term_meta( $term_id, 'displayfeaturedimagegenesis', $image_id );
|
||||
delete_option( "displayfeaturedimagegenesis_{$term_id}" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( isset( $_POST['displayfeaturedimagegenesis_termmeta'] ) || isset( $_POST['displayfeaturedimagegenesis_termmetadismiss'] ) ) {
|
||||
if ( ! check_admin_referer( 'displayfeaturedimagegenesis_metanonce', 'displayfeaturedimagegenesis_metanonce' ) ) {
|
||||
return;
|
||||
}
|
||||
update_option( 'displayfeaturedimagegenesis_updatedterms', true );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get IDs of terms with featured images
|
||||
* @param array $term_ids empty array
|
||||
* @return array all terms with featured images
|
||||
* @since 2.4.0
|
||||
*/
|
||||
protected function get_affected_terms( $affected_terms = array() ) {
|
||||
$args = array(
|
||||
'public' => true,
|
||||
'show_ui' => true,
|
||||
);
|
||||
$taxonomies = get_taxonomies( $args, 'objects' );
|
||||
|
||||
foreach ( $taxonomies as $tax ) {
|
||||
$args = array(
|
||||
'orderby' => 'name',
|
||||
'order' => 'ASC',
|
||||
'hide_empty' => false,
|
||||
);
|
||||
$terms = get_terms( $tax->name, $args );
|
||||
foreach ( $terms as $term ) {
|
||||
$term_id = $term->term_id;
|
||||
$option = get_option( "displayfeaturedimagegenesis_{$term_id}" );
|
||||
if ( false !== $option ) {
|
||||
$affected_terms[] = $term;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $affected_terms;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether terms need to be udpated
|
||||
* @return boolean true if on 4.4 and wp_options for terms exist; false otherwise
|
||||
*
|
||||
* @since 2.4.0
|
||||
*/
|
||||
protected function terms_need_updating() {
|
||||
$updated = get_option( 'displayfeaturedimagegenesis_updatedterms', false );
|
||||
if ( ! $updated && function_exists( 'get_term_meta' ) ) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
class Display_Featured_Image_Genesis_Taxonomies {
|
||||
class Display_Featured_Image_Genesis_Taxonomies extends Display_Featured_Image_Genesis_Helper {
|
||||
|
||||
protected $settings;
|
||||
|
||||
@@ -17,7 +17,7 @@ class Display_Featured_Image_Genesis_Taxonomies {
|
||||
*/
|
||||
public function set_taxonomy_meta() {
|
||||
|
||||
$this->settings = new Display_Featured_Image_Genesis_Settings();
|
||||
register_meta( 'term', 'displayfeaturedimagegenesis', array( $this, 'validate_taxonomy_image' ) );
|
||||
|
||||
$args = array(
|
||||
'public' => true,
|
||||
@@ -27,8 +27,9 @@ class Display_Featured_Image_Genesis_Taxonomies {
|
||||
foreach ( $taxonomies as $taxonomy ) {
|
||||
add_action( "{$taxonomy}_add_form_fields", array( $this, 'add_taxonomy_meta_fields' ), 5, 2 );
|
||||
add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_taxonomy_meta_fields' ), 5, 2 );
|
||||
add_action( "edited_{$taxonomy}", array( $this->settings, 'save_taxonomy_custom_meta' ), 10, 2 );
|
||||
add_action( "create_{$taxonomy}", array( $this->settings, 'save_taxonomy_custom_meta' ), 10, 2 );
|
||||
add_action( "edited_{$taxonomy}", array( $this, 'save_taxonomy_custom_meta' ) );
|
||||
add_action( "create_{$taxonomy}", array( $this, 'save_taxonomy_custom_meta' ) );
|
||||
add_action( "edit_{$taxonomy}", array( $this, 'save_taxonomy_custom_meta' ) );
|
||||
add_action( 'load-edit-tags.php', array( $this, 'help' ) );
|
||||
}
|
||||
|
||||
@@ -43,6 +44,7 @@ class Display_Featured_Image_Genesis_Taxonomies {
|
||||
|
||||
?>
|
||||
<div class="form-field term-image-wrap">
|
||||
<?php wp_nonce_field( 'displayfeaturedimagegenesis', 'displayfeaturedimagegenesis' ); ?>
|
||||
<label for="displayfeaturedimagegenesis[term_image]"><?php esc_attr_e( 'Featured Image', 'display-featured-image-genesis' ); ?></label>
|
||||
<input type="hidden" class="upload_image_id" id="term_image_id" name="displayfeaturedimagegenesis[term_image]" />
|
||||
<input id="upload_default_image" type="button" class="upload_default_image button-secondary" value="<?php esc_attr_e( 'Select Image', 'display-featured-image-genesis' ); ?>" />
|
||||
@@ -63,21 +65,19 @@ class Display_Featured_Image_Genesis_Taxonomies {
|
||||
*/
|
||||
public function edit_taxonomy_meta_fields( $term ) {
|
||||
|
||||
$t_id = $term->term_id;
|
||||
$displaysetting = get_option( "displayfeaturedimagegenesis_$t_id" );
|
||||
$id = '';
|
||||
$term_id = $term->term_id;
|
||||
$image_id = displayfeaturedimagegenesis_term_image( $term_id );
|
||||
|
||||
echo '<tr class="form-field term-image-wrap">';
|
||||
printf( '<th scope="row" valign="top"><label for="displayfeaturedimagegenesis[term_image]">%s</label></th>',
|
||||
esc_attr__( 'Featured Image', 'display-featured-image-genesis' )
|
||||
);
|
||||
echo '<td>';
|
||||
$id = $displaysetting['term_image'];
|
||||
$name = 'displayfeaturedimagegenesis[term_image]';
|
||||
if ( ! empty( $id ) ) {
|
||||
echo wp_kses_post( $this->settings->render_image_preview( $id ) );
|
||||
if ( $image_id ) {
|
||||
echo wp_kses_post( $this->render_image_preview( $image_id ) );
|
||||
}
|
||||
$this->settings->render_buttons( $id, $name );
|
||||
$this->render_buttons( $image_id, $name );
|
||||
echo '<p class="description">';
|
||||
printf(
|
||||
esc_attr__( 'Set Featured Image for %1$s.', 'display-featured-image-genesis' ),
|
||||
@@ -88,6 +88,106 @@ class Display_Featured_Image_Genesis_Taxonomies {
|
||||
echo '</tr>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Save extra taxonomy fields callback function.
|
||||
* @param term id $term_id the id of the term
|
||||
* @return updated option updated option for term featured image
|
||||
*
|
||||
* @since 2.0.0
|
||||
*/
|
||||
public function save_taxonomy_custom_meta( $term_id ) {
|
||||
|
||||
$input = $_POST['displayfeaturedimagegenesis'];
|
||||
if ( ! isset( $input ) ) {
|
||||
return;
|
||||
}
|
||||
$displaysetting = get_option( "displayfeaturedimagegenesis_$term_id", false );
|
||||
$action = function_exists( 'get_term_meta' ) ? 'update_term_meta' : 'update_options_meta';
|
||||
$this->$action( $term_id, $input, $displaysetting );
|
||||
}
|
||||
|
||||
/**
|
||||
* update/delete term meta
|
||||
* @param int $term_id term id
|
||||
* @param array $displaysetting old option, if it exists
|
||||
* @return term_meta
|
||||
*
|
||||
* @since 2.4.0
|
||||
*/
|
||||
protected function update_term_meta( $term_id, $input, $displaysetting ) {
|
||||
$new_image = $this->validate_taxonomy_image( $input['term_image'] );
|
||||
if ( null === $new_image ) {
|
||||
// if the new image is empty, delete term_meta and old option
|
||||
delete_term_meta( $term_id, 'displayfeaturedimagegenesis' );
|
||||
delete_option( "displayfeaturedimagegenesis_$term_id" );
|
||||
} elseif ( false !== $new_image ) {
|
||||
// if the new image is different from the existing term meta
|
||||
$current_setting = get_term_meta( $term_id, 'displayfeaturedimagegenesis' );
|
||||
if ( $current_setting !== $new_image ) {
|
||||
update_term_meta( $term_id, 'displayfeaturedimagegenesis', (int) $new_image );
|
||||
}
|
||||
// if there is a valid image, and the old setting exists
|
||||
if ( $displaysetting ) {
|
||||
delete_option( "displayfeaturedimagegenesis_$term_id" );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* update term option (for sites running WP below 4.4)
|
||||
* @param int $term_id term id
|
||||
* @param array $displaysetting previous option, if it exists
|
||||
* @return array updated option
|
||||
*
|
||||
* @since 2.4.0
|
||||
*/
|
||||
protected function update_options_meta( $term_id, $input, $displaysetting ) {
|
||||
$cat_keys = array_keys( $input );
|
||||
$is_updated = false;
|
||||
foreach ( $cat_keys as $key ) {
|
||||
if ( isset( $input[ $key ] ) ) {
|
||||
$displaysetting[ $key ] = $input[ $key ];
|
||||
if ( $input['term_image'] !== $displaysetting[ $key ] ) {
|
||||
break;
|
||||
}
|
||||
$displaysetting[ $key ] = $this->validate_taxonomy_image( $input[ $key ] );
|
||||
if ( null === $displaysetting[ $key ] ) {
|
||||
delete_option( "displayfeaturedimagegenesis_$term_id" );
|
||||
} elseif ( false !== $displaysetting[ $key ] ) {
|
||||
$is_updated = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Save the option array.
|
||||
if ( $is_updated ) {
|
||||
update_option( "displayfeaturedimagegenesis_$term_id", $displaysetting );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns false value for image if not correct file type/size
|
||||
* @param string $new_value New value
|
||||
* @return string New value or false, depending on allowed image size.
|
||||
* @since 2.0.0
|
||||
*/
|
||||
protected function validate_taxonomy_image( $new_value ) {
|
||||
|
||||
if ( ! $new_value ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$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 ( $valid && $width > $medium ) {
|
||||
return (int) $new_value;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Help tab for media screen
|
||||
* @return help tab with verbose information for plugin
|
||||
@@ -119,7 +219,7 @@ class Display_Featured_Image_Genesis_Taxonomies {
|
||||
* @param integer @new_term_id The ID of the newly created term.
|
||||
*
|
||||
*/
|
||||
function split_shared_term( $old_term_id, $new_term_id ) {
|
||||
public function split_shared_term( $old_term_id, $new_term_id ) {
|
||||
|
||||
$old_setting = get_option( "displayfeaturedimagegenesis_$old_term_id" );
|
||||
$new_setting = get_option( "displayfeaturedimagegenesis_$new_term_id" );
|
||||
@@ -133,5 +233,4 @@ class Display_Featured_Image_Genesis_Taxonomies {
|
||||
update_option( "displayfeaturedimagegenesis_$new_term_id", $new_setting );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* @package DisplayFeaturedImageGenesis
|
||||
* @author Robin Cornett <hello@robincornett.com>
|
||||
* @link https://github.com/robincornett/display-featured-image-genesis/
|
||||
* @copyright 2014 Robin Cornett
|
||||
* @copyright 2014-2015 Robin Cornett
|
||||
* @license GPL-2.0+
|
||||
*/
|
||||
|
||||
@@ -170,9 +170,14 @@ class Display_Featured_Image_Genesis {
|
||||
wp_register_script( 'displayfeaturedimage-upload', plugins_url( '/includes/js/settings-upload.js', dirname( __FILE__ ) ), array( 'jquery', 'media-upload', 'thickbox' ), $version );
|
||||
wp_register_script( 'widget_selector', plugins_url( '/includes/js/widget-selector.js', dirname( __FILE__ ) ), array( 'jquery' ), $version );
|
||||
|
||||
$screen = get_current_screen();
|
||||
$screen = get_current_screen();
|
||||
$screen_ids = array(
|
||||
'appearance_page_displayfeaturedimagegenesis',
|
||||
'profile',
|
||||
'user-edit',
|
||||
);
|
||||
|
||||
if ( 'appearance_page_displayfeaturedimagegenesis' === $screen->id || ! empty( $screen->taxonomy ) || 'profile' === $screen->id ) {
|
||||
if ( in_array( $screen->id, $screen_ids, true ) || ! empty( $screen->taxonomy ) ) {
|
||||
wp_enqueue_media();
|
||||
wp_enqueue_script( 'displayfeaturedimage-upload' );
|
||||
wp_localize_script( 'displayfeaturedimage-upload', 'objectL10n', array(
|
||||
@@ -184,7 +189,7 @@ class Display_Featured_Image_Genesis {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( in_array( $screen->id, array( 'widgets', 'customize' ) ) ) {
|
||||
if ( in_array( $screen->id, array( 'widgets', 'customize' ), true ) ) {
|
||||
wp_enqueue_script( 'widget_selector' );
|
||||
wp_localize_script( 'widget_selector', 'displayfeaturedimagegenesis_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
|
||||
}
|
||||
@@ -228,5 +233,4 @@ class Display_Featured_Image_Genesis {
|
||||
$links[] = sprintf( '<a href="%s">%s</a>', esc_url( admin_url( 'themes.php?page=displayfeaturedimagegenesis' ) ), esc_attr__( 'Settings', 'display-featured-image-genesis' ) );
|
||||
return $links;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,10 +22,9 @@ function display_featured_image_genesis_get_term_image_id( $image_id = '' ) {
|
||||
$terms = wp_get_object_terms( get_the_ID(), $taxonomies, $args );
|
||||
|
||||
foreach ( $terms as $term ) {
|
||||
$t_id = $term->term_id;
|
||||
$term_meta = get_option( "displayfeaturedimagegenesis_$t_id" );
|
||||
if ( ! empty( $term_meta['term_image'] ) ) {
|
||||
$image_id = displayfeaturedimagegenesis_check_image_id( $term_meta['term_image'] );
|
||||
$term_id = $term->term_id;
|
||||
$image_id = displayfeaturedimagegenesis_term_image( $term_id );
|
||||
if ( $image_id ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -34,6 +33,26 @@ function display_featured_image_genesis_get_term_image_id( $image_id = '' ) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to retrieve the term image ID, whether as term_meta or wp_options
|
||||
* @param int $term_id term ID
|
||||
* @param string $image_id image ID
|
||||
* @return int image ID
|
||||
* @since 2.4.0
|
||||
*/
|
||||
function displayfeaturedimagegenesis_term_image( $term_id, $image_id = '' ) {
|
||||
if ( function_exists( 'get_term_meta' ) ) {
|
||||
$image_id = get_term_meta( $term_id, 'displayfeaturedimagegenesis', true );
|
||||
}
|
||||
if ( ! $image_id ) {
|
||||
$term_meta = get_option( "displayfeaturedimagegenesis_$term_id" );
|
||||
if ( $term_meta ) {
|
||||
$image_id = displayfeaturedimagegenesis_check_image_id( $term_meta['term_image'] );
|
||||
}
|
||||
}
|
||||
return $image_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get the term image URL.
|
||||
* @param string $size image size
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* @package DisplayFeaturedImageGenesis
|
||||
* @author Robin Cornett <hello@robincornett.com>
|
||||
* @link http://robincornett.com
|
||||
* @copyright 2014 Robin Cornett Creative, LLC
|
||||
* @copyright 2014-2015 Robin Cornett Creative, LLC
|
||||
* @license GPL-2.0+
|
||||
* @since 2.0.0
|
||||
*/
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
* @author Robin Cornett <hello@robincornett.com>
|
||||
* @license GPL-2.0+
|
||||
* @link http://robincornett.com
|
||||
* @copyright 2014 Robin Cornett Creative, LLC
|
||||
* @copyright 2014-2015 Robin Cornett Creative, LLC
|
||||
* @since 2.0.0
|
||||
*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user