* @license GPL-2.0+ * @link https://robincornett.com * @copyright 2014-2016 Robin Cornett Creative, LLC */ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisGetSetting { /** * Variable for the plugin setting. * @var $setting */ protected $setting; /** * Base id/slug for the settings page. * @var string $page */ protected $page = 'displayfeaturedimagegenesis'; /** * Generic function to add settings sections * * @since 2.4.0 */ protected function add_sections( $sections ) { foreach ( $sections as $section ) { add_settings_section( $this->page . '_' . $section['id'], $section['title'], array( $this, $section['id'] . '_section_description' ), $this->page . '_' . $section['id'] ); } } /** * Generic function to add settings fields * @param array $sections registered sections * @return array all settings fields * * @since 2.4.0 */ protected function add_fields( $fields, $sections ) { foreach ( $fields as $field ) { add_settings_field( '[' . $field['id'] . ']', sprintf( '', $field['id'], $field['title'] ), array( $this, $field['callback'] ), $this->page . '_' . $sections[ $field['section'] ]['id'], $this->page . '_' . $sections[ $field['section'] ]['id'], empty( $field['args'] ) ? array() : $field['args'] ); } } /** * Set which tab is considered active. * @return string * @since 2.5.0 */ protected function get_active_tab() { return isset( $_GET['tab'] ) ? $_GET['tab'] : 'main'; } /** * 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( '', (int) $args['min'], (int) $args['max'], esc_attr( $args['setting'] ), esc_attr( $this->setting[ $args['setting'] ] ), esc_attr( $this->page ), esc_attr( $args['label'] ) ); $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 ) { $setting = $this->get_checkbox_setting( $args ); printf( '', esc_attr( $this->page ), esc_attr( $args['setting'] ) ); printf( '', esc_attr( $args['setting'] ), checked( 1, esc_attr( $setting ), false ), esc_attr( $args['label'] ), esc_attr( $this->page ) ); $this->do_description( $args['setting'] ); } /** * Get the current value for the checkbox. * @param $args * * @return int */ protected function get_checkbox_setting( $args ) { $setting = isset( $this->setting[ $args['setting'] ] ) ? $this->setting[ $args['setting'] ] : 0; if ( isset( $args['setting_name'] ) && isset( $this->setting[ $args['setting_name'] ][ $args['name'] ] ) ) { $setting = $this->setting[ $args['setting_name'] ][ $args['name'] ]; } return $setting; } /** * Build a checkbox array. * @param $args */ public function do_checkbox_array( $args ) { $post_types = $this->get_content_types_built_in(); foreach ( $post_types as $post_type ) { $object = get_post_type_object( $post_type ); $type_args = array( 'setting' => "{$args['setting']}][{$post_type}", 'label' => $object->label, 'setting_name' => $args['setting'], 'name' => $post_type, ); $this->do_checkbox( $type_args ); } } /** * 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( '
%s
', wp_kses_post( $description ) ); } /** * display image preview * @param int $id featured image ID * @param $alt string description for alt text * @return $image image preview * * @since 2.3.0 */ public function render_image_preview( $id, $alt = '' ) { if ( empty( $id ) ) { return; } $id = displayfeaturedimagegenesis_check_image_id( $id ); $alt_text = sprintf( __( '%s featured image', 'display-featured-image-genesis' ), esc_attr( $alt ) ); $preview = wp_get_attachment_image_src( (int) $id, 'medium' ); $image = sprintf( '