4d17f3950716f92389a043153006d1df29680cdf.svn-base 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
<?php if ( ! defined( 'ABSPATH' ) ) {
	die;
} // Cannot access pages directly.
/**
 *
 * Field: Image
 *
 */
if ( ! class_exists( 'Exopite_Simple_Options_Framework_Field_image' ) ) {

	class Exopite_Simple_Options_Framework_Field_image extends Exopite_Simple_Options_Framework_Fields {

		public function __construct( $field, $value = '', $unique = '', $config = array() ) {
			parent::__construct( $field, $value, $unique, $config );
		}

		/**
		 * Get an attachment ID given a URL.
		 *
		 * @param string $url
		 *
		 * @return int Attachment ID on success, 0 on failure
		 */
		function get_attachment_id( $url ) {
			$attachment_id = 0;
			$dir           = wp_upload_dir();

			// To handle relative urls
			if ( substr( $url, 0, strlen( '/' ) ) === '/' ) {

				$url = get_site_url() . $url;
			}
			if ( false !== strpos( $url, $dir['baseurl'] . '/' ) ) { // Is URL in uploads directory?

				$file       = basename( $url );
				$query_args = array(
					'post_type'   => 'attachment',
					'post_status' => 'inherit',
					'fields'      => 'ids',
					'meta_query'  => array(
						array(
							'value'   => $file,
							'compare' => 'LIKE',
							'key'     => '_wp_attachment_metadata',
						),
					)
				);
				$query      = new WP_Query( $query_args );
				if ( $query->have_posts() ) {
					foreach ( $query->posts as $post_id ) {
						$meta                = wp_get_attachment_metadata( $post_id );
						$original_file       = basename( $meta['file'] );
						$cropped_image_files = wp_list_pluck( $meta['sizes'], 'file' );
						if ( $original_file === $file || in_array( $file, $cropped_image_files ) ) {
							$attachment_id = $post_id;
							break;
						}
					}
				}
			}

			return $attachment_id;
		}

		public function output() {

			/**
			 * Open WordPress Media Uploader with PHP and JavaScript
			 *
			 * @link https://rudrastyh.com/wordpress/customizable-media-uploader.html
			 */

			echo $this->element_before();

			$preview = '';
			$value   = $this->element_value();
			$add     = ( ! empty( $this->field['add_title'] ) ) ? $this->field['add_title'] : esc_attr__( '选择图片', 'exopite-sof' );
			$hidden  = ( empty( $value ) ) ? ' hidden' : '';
			$classes = ( isset( $this->field['class'] ) ) ? implode( ' ', explode( ' ', $this->field['class'] ) ) : '';

			if ( ! empty( $value ) ) {
				$attachment = wp_get_attachment_image_src( $this->get_attachment_id( $value ), 'thumbnail' );
				$preview    = $attachment[0];
			}

			echo '<div class="exopite-sof-media exopite-sof-image ' . $classes . '" ' . $this->element_attributes() . '>';
			echo '<div class="exopite-sof-image-preview' . $hidden . '">';
			echo '<div class="exopite-sof-image-inner"><i class="fa fa-times exopite-sof-image-remove"></i><img src="' . $preview . '" alt="preview" /></div>';
			echo '</div>';

			echo '<input type="text" name="' . $this->element_name() . '" value="' . $this->element_value() . '">';
			echo '<a href="#" class="button button-primary exopite-sof-button">' . $add . '</a>';
			echo '</div>';
			echo $this->element_after();

		}

		public static function enqueue( $args ) {

			wp_enqueue_media();

		}

	}

}