2015-11-17 14:03:41 -05:00
|
|
|
<?php
|
2017-10-25 11:27:19 -04:00
|
|
|
|
2015-11-17 14:03:41 -05:00
|
|
|
/**
|
2017-10-25 11:27:19 -04:00
|
|
|
* Class Display_Featured_Image_Genesis_Helper
|
|
|
|
|
* @package DisplayFeaturedImageGenesis
|
|
|
|
|
* @copyright 2017 Robin Cornett
|
2015-11-17 14:03:41 -05:00
|
|
|
*/
|
2016-07-03 07:32:02 -04:00
|
|
|
class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisGetSetting {
|
2015-11-17 14:03:41 -05:00
|
|
|
|
2016-07-03 07:42:41 -04:00
|
|
|
/**
|
|
|
|
|
* Variable for the plugin setting.
|
|
|
|
|
* @var $setting
|
|
|
|
|
*/
|
2016-03-31 11:35:12 -04:00
|
|
|
protected $setting;
|
|
|
|
|
|
2016-07-03 07:42:41 -04:00
|
|
|
/**
|
|
|
|
|
* Base id/slug for the settings page.
|
|
|
|
|
* @var string $page
|
|
|
|
|
*/
|
2016-03-31 11:35:12 -04:00
|
|
|
protected $page = 'displayfeaturedimagegenesis';
|
|
|
|
|
|
2015-11-17 14:03:41 -05:00
|
|
|
/**
|
|
|
|
|
* Generic function to add settings sections
|
|
|
|
|
*
|
2015-12-01 10:39:23 -05:00
|
|
|
* @since 2.4.0
|
2017-10-25 10:40:17 -04:00
|
|
|
*
|
|
|
|
|
* @param $sections
|
2015-11-17 14:03:41 -05:00
|
|
|
*/
|
|
|
|
|
protected function add_sections( $sections ) {
|
|
|
|
|
|
|
|
|
|
foreach ( $sections as $section ) {
|
|
|
|
|
add_settings_section(
|
2016-04-01 17:06:57 -04:00
|
|
|
$this->page . '_' . $section['id'],
|
2015-11-17 14:03:41 -05:00
|
|
|
$section['title'],
|
2017-10-25 11:27:19 -04:00
|
|
|
array( $this, 'section_description' ),
|
2016-04-01 17:06:57 -04:00
|
|
|
$this->page . '_' . $section['id']
|
2015-11-17 14:03:41 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generic function to add settings fields
|
2017-10-25 10:40:17 -04:00
|
|
|
*
|
|
|
|
|
* @param $fields
|
2015-11-17 14:03:41 -05:00
|
|
|
* @param array $sections registered sections
|
|
|
|
|
*
|
2015-12-01 10:39:23 -05:00
|
|
|
* @since 2.4.0
|
2015-11-17 14:03:41 -05:00
|
|
|
*/
|
|
|
|
|
protected function add_fields( $fields, $sections ) {
|
|
|
|
|
foreach ( $fields as $field ) {
|
|
|
|
|
add_settings_field(
|
|
|
|
|
'[' . $field['id'] . ']',
|
2016-04-19 11:57:23 -04:00
|
|
|
sprintf( '<label for="%s">%s</label>', $field['id'], $field['title'] ),
|
2015-11-17 14:03:41 -05:00
|
|
|
array( $this, $field['callback'] ),
|
2016-04-01 17:06:57 -04:00
|
|
|
$this->page . '_' . $sections[ $field['section'] ]['id'],
|
|
|
|
|
$this->page . '_' . $sections[ $field['section'] ]['id'],
|
2017-10-25 10:40:17 -04:00
|
|
|
$field
|
2015-11-17 14:03:41 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-02 10:17:38 -04:00
|
|
|
/**
|
|
|
|
|
* Set which tab is considered active.
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 2.5.0
|
|
|
|
|
*/
|
|
|
|
|
protected function get_active_tab() {
|
2017-10-25 10:40:17 -04:00
|
|
|
$tab = filter_input( INPUT_GET, 'tab', FILTER_SANITIZE_STRING );
|
|
|
|
|
|
|
|
|
|
return $tab ? $tab : 'main';
|
2016-07-02 10:17:38 -04:00
|
|
|
}
|
|
|
|
|
|
2015-11-17 14:03:41 -05:00
|
|
|
/**
|
|
|
|
|
* Echoes out the section description.
|
2017-10-25 10:40:17 -04:00
|
|
|
*
|
2015-11-17 14:03:41 -05:00
|
|
|
* @since 2.3.0
|
2017-10-25 11:27:19 -04:00
|
|
|
*
|
|
|
|
|
* @param $section
|
2015-11-17 14:03:41 -05:00
|
|
|
*/
|
2017-10-25 11:27:19 -04:00
|
|
|
public function section_description( $section ) {
|
|
|
|
|
$id = str_replace( "{$this->page}_", '', $section['id'] );
|
|
|
|
|
$method = "{$id}_section_description";
|
|
|
|
|
if ( method_exists( $this, $method ) ) {
|
|
|
|
|
echo wp_kses_post( wpautop( $this->$method() ) );
|
|
|
|
|
}
|
2015-11-17 14:03:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generic callback to create a number field setting.
|
|
|
|
|
*
|
|
|
|
|
* @since 2.3.0
|
2017-10-25 10:40:17 -04:00
|
|
|
*
|
|
|
|
|
* @param $args
|
2015-11-17 14:03:41 -05:00
|
|
|
*/
|
|
|
|
|
public function do_number( $args ) {
|
2016-04-18 11:53:43 -04:00
|
|
|
printf( '<label for="%5$s[%3$s]"><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" />%6$s</label>',
|
2015-11-17 14:03:41 -05:00
|
|
|
(int) $args['min'],
|
|
|
|
|
(int) $args['max'],
|
2017-10-25 10:40:17 -04:00
|
|
|
esc_attr( $args['id'] ),
|
|
|
|
|
esc_attr( $this->setting[ $args['id'] ] ),
|
2016-04-18 11:53:43 -04:00
|
|
|
esc_attr( $this->page ),
|
|
|
|
|
esc_attr( $args['label'] )
|
2015-11-17 14:03:41 -05:00
|
|
|
);
|
2017-10-25 10:40:17 -04:00
|
|
|
$this->do_description( $args );
|
2015-11-17 14:03:41 -05:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* generic checkbox function (for all checkbox settings)
|
|
|
|
|
*
|
|
|
|
|
* @since 2.3.0
|
2017-10-25 10:40:17 -04:00
|
|
|
*
|
|
|
|
|
* @param $args
|
2015-11-17 14:03:41 -05:00
|
|
|
*/
|
|
|
|
|
public function do_checkbox( $args ) {
|
2016-03-31 11:36:55 -04:00
|
|
|
$setting = $this->get_checkbox_setting( $args );
|
2017-10-25 10:40:17 -04:00
|
|
|
printf( '<input type="hidden" name="%s[%s]" value="0" />', esc_attr( $this->page ), esc_attr( $args['id'] ) );
|
2016-03-31 12:39:35 -04:00
|
|
|
printf( '<label for="%4$s[%1$s]" style="margin-right:12px;"><input type="checkbox" name="%4$s[%1$s]" id="%4$s[%1$s]" value="1" %2$s class="code" />%3$s</label>',
|
2017-10-25 10:40:17 -04:00
|
|
|
esc_attr( $args['id'] ),
|
2016-03-07 13:08:59 -05:00
|
|
|
checked( 1, esc_attr( $setting ), false ),
|
2016-03-31 11:36:55 -04:00
|
|
|
esc_attr( $args['label'] ),
|
|
|
|
|
esc_attr( $this->page )
|
2015-11-17 14:03:41 -05:00
|
|
|
);
|
2017-10-25 10:40:17 -04:00
|
|
|
$this->do_description( $args );
|
2015-11-17 14:03:41 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-31 11:36:55 -04:00
|
|
|
/**
|
|
|
|
|
* Get the current value for the checkbox.
|
2017-10-25 10:40:17 -04:00
|
|
|
*
|
2016-03-31 11:36:55 -04:00
|
|
|
* @param $args
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
protected function get_checkbox_setting( $args ) {
|
2017-10-25 10:40:17 -04:00
|
|
|
$setting = isset( $this->setting[ $args['id'] ] ) ? $this->setting[ $args['id'] ] : 0;
|
2016-04-04 15:50:31 -04:00
|
|
|
if ( isset( $args['setting_name'] ) && isset( $this->setting[ $args['setting_name'] ][ $args['name'] ] ) ) {
|
|
|
|
|
$setting = $this->setting[ $args['setting_name'] ][ $args['name'] ];
|
2016-03-31 11:36:55 -04:00
|
|
|
}
|
2017-10-25 10:40:17 -04:00
|
|
|
|
2016-03-31 11:36:55 -04:00
|
|
|
return $setting;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 12:38:55 -04:00
|
|
|
/**
|
|
|
|
|
* Build a checkbox array.
|
2017-10-25 10:40:17 -04:00
|
|
|
*
|
2016-03-31 12:38:55 -04:00
|
|
|
* @param $args
|
|
|
|
|
*/
|
|
|
|
|
public function do_checkbox_array( $args ) {
|
2017-10-24 18:04:55 -04:00
|
|
|
foreach ( $args['options'] as $key => $value ) {
|
2016-03-31 12:38:55 -04:00
|
|
|
$type_args = array(
|
2017-10-25 10:40:17 -04:00
|
|
|
'id' => "{$args['id']}][{$key}",
|
2017-10-24 18:04:55 -04:00
|
|
|
'label' => $value,
|
2017-10-25 10:40:17 -04:00
|
|
|
'setting_name' => $args['id'],
|
2017-10-24 18:04:55 -04:00
|
|
|
'name' => $key,
|
2016-03-31 12:38:55 -04:00
|
|
|
);
|
|
|
|
|
$this->do_checkbox( $type_args );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-06 09:42:48 -04:00
|
|
|
/**
|
|
|
|
|
* radio buttons
|
|
|
|
|
* @since 2.6.0
|
2017-10-25 10:40:17 -04:00
|
|
|
*
|
|
|
|
|
* @param $args
|
2016-07-06 09:42:48 -04:00
|
|
|
*/
|
|
|
|
|
public function do_radio_buttons( $args ) {
|
|
|
|
|
echo '<fieldset>';
|
2017-10-25 10:40:17 -04:00
|
|
|
printf( '<legend class="screen-reader-text">%s</legend>', esc_html( $args['legend'] ) );
|
2016-07-06 09:42:48 -04:00
|
|
|
foreach ( $args['buttons'] as $key => $button ) {
|
|
|
|
|
printf( '<label for="%5$s[%1$s][%2$s]" style="margin-right:12px !important;"><input type="radio" id="%5$s[%1$s][%2$s]" name="%5$s[%1$s]" value="%2$s"%3$s />%4$s</label> ',
|
2017-10-25 10:40:17 -04:00
|
|
|
esc_attr( $args['id'] ),
|
2016-07-06 09:42:48 -04:00
|
|
|
esc_attr( $key ),
|
2017-10-25 10:40:17 -04:00
|
|
|
checked( $key, $this->setting[ $args['id'] ], false ),
|
2016-07-06 09:42:48 -04:00
|
|
|
esc_attr( $button ),
|
|
|
|
|
esc_attr( $this->page )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
echo '</fieldset>';
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 19:47:25 -04:00
|
|
|
/**
|
|
|
|
|
* Output a select field.
|
|
|
|
|
*
|
|
|
|
|
* @param $args
|
|
|
|
|
*/
|
|
|
|
|
public function do_select( $args ) {
|
|
|
|
|
printf( '<select id="%2$s[%1$s]" name="%2$s[%1$s]">',
|
|
|
|
|
esc_attr( $args['id'] ),
|
|
|
|
|
esc_attr( $this->page )
|
|
|
|
|
);
|
|
|
|
|
foreach ( (array) $args['options'] as $option => $label ) {
|
|
|
|
|
printf( '<option value="%s" %s>%s</option>',
|
|
|
|
|
esc_attr( $option ),
|
|
|
|
|
selected( $option, $this->setting[ $args['id'] ], false ),
|
|
|
|
|
esc_attr( $label )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
echo '</select>';
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-17 14:03:41 -05:00
|
|
|
/**
|
|
|
|
|
* Generic callback to display a field description.
|
2017-10-25 10:40:17 -04:00
|
|
|
*
|
|
|
|
|
* @param $args array
|
2015-11-17 14:03:41 -05:00
|
|
|
*
|
|
|
|
|
* @since 2.3.0
|
|
|
|
|
*/
|
|
|
|
|
protected function do_description( $args ) {
|
2017-10-25 10:40:17 -04:00
|
|
|
$description = isset( $args['description'] ) ? $args['description'] : false;
|
|
|
|
|
$function = $args['id'] . '_description';
|
|
|
|
|
if ( method_exists( $this, $function ) ) {
|
|
|
|
|
$description = $this->$function();
|
|
|
|
|
}
|
|
|
|
|
if ( ! $description ) {
|
2015-11-17 14:03:41 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
printf( '<p class="description">%s</p>', wp_kses_post( $description ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* display image preview
|
2017-10-25 10:40:17 -04:00
|
|
|
*
|
2016-04-04 15:50:31 -04:00
|
|
|
* @param int $id featured image ID
|
2017-10-25 10:40:17 -04:00
|
|
|
* @param $alt string description for alt text
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2015-11-17 14:03:41 -05:00
|
|
|
*
|
|
|
|
|
* @since 2.3.0
|
|
|
|
|
*/
|
2016-04-26 11:34:55 -04:00
|
|
|
public function render_image_preview( $id, $alt = '' ) {
|
2015-11-17 14:03:41 -05:00
|
|
|
if ( empty( $id ) ) {
|
2017-10-25 10:40:17 -04:00
|
|
|
return '';
|
2015-11-17 14:03:41 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-25 10:40:17 -04:00
|
|
|
$id = displayfeaturedimagegenesis_check_image_id( $id );
|
|
|
|
|
/* translators: the placeholder refers to which featured image */
|
2016-04-26 11:34:55 -04:00
|
|
|
$alt_text = sprintf( __( '%s featured image', 'display-featured-image-genesis' ), esc_attr( $alt ) );
|
2016-04-18 11:53:13 -04:00
|
|
|
$preview = wp_get_attachment_image_src( (int) $id, 'medium' );
|
2017-10-25 10:40:17 -04:00
|
|
|
|
|
|
|
|
return sprintf( '<div class="upload_logo_preview"><img src="%s" alt="%s" /></div>', esc_url( $preview[0] ), esc_attr( $alt_text ) );
|
2015-11-17 14:03:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* show image select/delete buttons
|
2017-10-25 10:40:17 -04:00
|
|
|
*
|
|
|
|
|
* @param int $id image ID
|
2016-04-04 10:31:57 -04:00
|
|
|
* @param string $name name for value/ID/class
|
2015-11-17 14:03:41 -05:00
|
|
|
*
|
|
|
|
|
* @since 2.3.0
|
|
|
|
|
*/
|
|
|
|
|
public function render_buttons( $id, $name ) {
|
|
|
|
|
$id = displayfeaturedimagegenesis_check_image_id( $id );
|
|
|
|
|
$id = $id ? (int) $id : '';
|
2016-04-18 11:53:13 -04:00
|
|
|
printf( '<input type="hidden" class="upload_image_id" name="%1$s" value="%2$s" />', esc_attr( $name ), esc_attr( $id ) );
|
2015-11-17 14:03:41 -05:00
|
|
|
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' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-04 10:31:57 -04:00
|
|
|
/**
|
2016-04-25 11:50:27 -04:00
|
|
|
* Get all public content types, not including built in.
|
|
|
|
|
*
|
|
|
|
|
* @since 2.5.0
|
2016-04-04 10:31:57 -04:00
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function get_content_types() {
|
2017-10-25 10:40:17 -04:00
|
|
|
$args = array(
|
2016-04-04 10:31:57 -04:00
|
|
|
'public' => true,
|
|
|
|
|
'_builtin' => false,
|
|
|
|
|
'has_archive' => true,
|
|
|
|
|
);
|
|
|
|
|
$output = 'names';
|
|
|
|
|
|
|
|
|
|
return get_post_types( $args, $output );
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-25 11:50:27 -04:00
|
|
|
/**
|
|
|
|
|
* Get all public content types, including built in.
|
|
|
|
|
*
|
|
|
|
|
* @since 2.5.0
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function get_content_types_built_in() {
|
|
|
|
|
$built_in = array( 'post', 'page' );
|
|
|
|
|
$post_types = $this->get_content_types();
|
2017-10-25 10:40:17 -04:00
|
|
|
|
2016-04-25 11:50:27 -04:00
|
|
|
return array_merge( $built_in, $post_types );
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-17 14:03:41 -05:00
|
|
|
/**
|
|
|
|
|
* Determines if the user has permission to save the information from the submenu
|
|
|
|
|
* page.
|
|
|
|
|
*
|
|
|
|
|
* @since 2.3.0
|
|
|
|
|
* @access protected
|
|
|
|
|
*
|
2017-10-25 10:40:17 -04:00
|
|
|
* @param string $action The name of the action specified on the submenu page
|
|
|
|
|
* @param string $nonce The nonce specified on the submenu page
|
2015-11-17 14:03:41 -05:00
|
|
|
*
|
|
|
|
|
* @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 );
|
|
|
|
|
}
|
2017-10-25 10:40:17 -04:00
|
|
|
|
2015-11-17 14:03:41 -05:00
|
|
|
return ( $is_nonce_set && $is_valid_nonce );
|
|
|
|
|
}
|
|
|
|
|
}
|