'full', // The size of the image to use in the generated CSS 'placeholder' => '', // show this when blank ); /** * Constructor * * @return void * @since 1.5 */ function __construct( $settings, $owner ) { parent::__construct( $settings, $owner ); add_filter( 'tf_generate_css_upload_' . $this->getOptionNamespace(), array( $this, 'generateCSS' ), 10, 2 ); add_action( 'tf_livepreview_pre_' . $this->getOptionNamespace(), array( $this, 'preLivePreview' ), 10, 3 ); add_action( 'tf_livepreview_post_' . $this->getOptionNamespace(), array( $this, 'postLivePreview' ), 10, 3 ); } /** * Generates CSS for the font, this is used in TitanFrameworkCSS * * @param string $css The CSS generated * @param TitanFrameworkOption $option The current option being processed * @return string The CSS generated * @since 1.5 */ public function generateCSS( $css, $option ) { if ( $this->settings['id'] != $option->settings['id'] ) { return $css; } $value = $this->getValue(); if ( empty( $value ) ) { return $css; } if ( is_numeric( $value ) ) { $size = ! empty( $option->settings['size'] ) ? $option->settings['size'] : 'thumbnail'; $attachment = wp_get_attachment_image_src( $value, $size ); $value = $attachment[0]; } $css .= '$' . $option->settings['id'] . ': url(' . $value . ');'; if ( ! empty( $option->settings['css'] ) ) { // In the css parameter, we accept the term `value` as our current value, // translate it into the SaSS variable for the current option $css .= str_replace( 'value', '#{$' . $option->settings['id'] . '}', $option->settings['css'] ); } return $css; } /** * The upload option gives out an attachment ID. Live previews will not work since we cannot get * the upload URL from an ID easily. Use a specially created Ajax Handler for just getting the URL. * * @since 1.9 * * @see tf_upload_option_customizer_get_value() */ public function preLivePreview( $optionID, $optionType, $option ) { if ( $optionID != $this->settings['id'] ) { return; } $nonce = wp_create_nonce( 'tf_upload_option_nonce' ); $size = ! empty( $this->settings['size'] ) ? $this->settings['size'] : 'thumbnail'; ?> wp.ajax.send( 'tf_upload_option_customizer_get_value', { data: { nonce: '', size: '', id: value }, success: function( data ) { var $ = jQuery; var value = data; settings['id'] ) { return; } // Close the ajax call ?> } }); echoOptionHeader(); // display the preview image $value = $this->getValue(); if ( is_numeric( $value ) ) { // gives us an array with the first element as the src or false on fail $value = wp_get_attachment_image_src( $value, array( 150, 150 ) ); } if ( ! is_array( $value ) ) { $value = $this->getValue(); } else { $value = $value[0]; } $previewImage = ''; if ( ! empty( $value ) ) { $previewImage = ""; } echo "
{$this->description}
"; } } } } if ( ! function_exists( 'tf_upload_option_customizer_get_value' ) ) { add_action( 'wp_ajax_tf_upload_option_customizer_get_value', 'tf_upload_option_customizer_get_value' ); /** * Returns the image URL from an attachment ID & size * * @see TitanFrameworkOptionUpload->preLivePreview() */ function tf_upload_option_customizer_get_value() { if ( ! empty( $_POST['nonce'] ) && ! empty( $_POST['id'] ) && ! empty( $_POST['size'] ) ) { $nonce = sanitize_text_field( $_POST['nonce'] ); $attachmentID = sanitize_text_field( $_POST['id'] ); $size = sanitize_text_field( $_POST['size'] ); if ( wp_verify_nonce( $nonce, 'tf_upload_option_nonce' ) ) { $attachment = wp_get_attachment_image_src( $attachmentID, $size ); if ( ! empty( $attachment ) ) { wp_send_json_success( $attachment[0] ); } } } // Instead of doing a wp_send_json_error, send a blank value instead so // Javascript adjustments still get executed wp_send_json_success( '' ); } }