<?php /**
 * Redux Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * any later version.
 *
 * Redux Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Redux Framework. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     ReduxFramework
 * @subpackage  Field_Media
 * @author      Daniel J Griffiths (Ghost1227)
 * @author      Dovy Paukstys
 * @version     3.0.0
 */

// Exit if accessed directly
if( !defined( 'ABSPATH' ) ) exit;

// Don't duplicate me!
if( !class_exists( 'ReduxFramework_media' ) ) {

    /**
     * Main ReduxFramework_media class
     *
     * @since       1.0.0
     */
    class ReduxFramework_media extends ReduxFramework {
    
        /**
         * Field Constructor.
         *
         * Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function
         *
         * @since       1.0.0
         * @access      public
         * @return      void
         */
        public function __construct( $field = array(), $value ='', $parent ) {
        
            parent::__construct( $parent-&gt;sections, $parent-&gt;args, $parent-&gt;extra_tabs );

            $this-&gt;field = $field;
            $this-&gt;value = $value;
        
        }

        /**
         * Field Render Function.
         *
         * Takes the vars and outputs the HTML for the field in the settings
         *
         * @since       1.0.0
         * @access      public
         * @return      void
         */
        public function render() {

            // No errors please
            $defaults = array(
                'id'        =&gt; '',
                'url'       =&gt; '',
                'width'     =&gt; '',
                'height'    =&gt; '',
                'thumbnail' =&gt; ''
            );

            $this-&gt;value = wp_parse_args( $this-&gt;value, $defaults );

            if( empty( $this-&gt;value ) &amp;&amp; !empty( $this-&gt;field['default'] ) ) { // If there are standard values and value is empty
                if( is_array( $this-&gt;field['default'] ) ) {
                    if( !empty( $this-&gt;field['default']['id'] ) ) {
                        $this-&gt;value['id'] = $this-&gt;field['default']['id'];
                    }

                    if( !empty( $this-&gt;field['default']['url'] ) ) {
                        $this-&gt;value['url'] = $this-&gt;field['default']['url'];
                    }           
                } else {
                    if( is_numeric( $this-&gt;field['default'] ) ) { // Check if it's an attachment ID
                        $this-&gt;value['id'] = $this-&gt;field['default'];
                    } else { // Must be a URL
                        $this-&gt;value['url'] = $this-&gt;field['default'];
                    }           
                }
            }


            if( empty( $this-&gt;value['url'] ) &amp;&amp; !empty( $this-&gt;value['id'] ) ) {
                $img = wp_get_attachment_image_src( $this-&gt;value['id'], 'full' );
                $this-&gt;value['url'] = $img[0];
                $this-&gt;value['width'] = $img[1];
                $this-&gt;value['height'] = $img[2];
            }

            $hide = 'hide ';

            if( (isset( $this-&gt;field['preview'] ) &amp;&amp; $this-&gt;field['preview'] === false) ) {
                $this-&gt;field['class'] .= " noPreview";
            }

            if( ( !empty( $this-&gt;field['url'] ) &amp;&amp; $this-&gt;field['url'] === true ) || isset( $this-&gt;field['preview'] ) &amp;&amp; $this-&gt;field['preview'] === false ) {
                $hide = '';
            }   

            echo '<input placeholder="'.__('None media selected', 'redux-framework').'" type="text" class="' . $hide . 'upload ' . $this-&gt;field['class'] . '" name="' . $this-&gt;args['opt_name'] . '[' . $this-&gt;field['id'] . '][url]" id="' . $this-&gt;args['opt_name'] . '[' . $this-&gt;field['id'] . '][url]" value="' . $this-&gt;value['url'] . '" readonly>';
            echo '<input type="hidden" class="upload-id ' . $this-&gt;field['class'] . '" name="' . $this-&gt;args['opt_name'] . '[' . $this-&gt;field['id'] . '][id]" id="' . $this-&gt;args['opt_name'] . '[' . $this-&gt;field['id'] . '][id]" value="' . $this-&gt;value['id'] . '">';
            echo '<input type="hidden" class="upload-height" name="' . $this-&gt;args['opt_name'] . '[' . $this-&gt;field['id'] . '][height]" id="' . $this-&gt;args['opt_name'] . '[' . $this-&gt;field['id'] . '][height]" value="' . $this-&gt;value['height'] . '">';
            echo '<input type="hidden" class="upload-width" name="' . $this-&gt;args['opt_name'] . '[' . $this-&gt;field['id'] . '][width]" id="' . $this-&gt;args['opt_name'] . '[' . $this-&gt;field['id'] . '][width]" value="' . $this-&gt;value['width'] . '">';
            echo '<input type="hidden" class="upload-thumbnail" name="' . $this-&gt;args['opt_name'] . '[' . $this-&gt;field['id'] . '][thumbnail]" id="' . $this-&gt;args['opt_name'] . '[' . $this-&gt;field['id'] . '][thumbnail]" value="' . $this-&gt;value['thumbnail'] . '">';

            //Preview
            $hide = '';

            if( (isset( $this-&gt;field['preview'] ) &amp;&amp; $this-&gt;field['preview'] === false) || empty( $this-&gt;value['url'] ) ) {
                $hide = 'hide ';
            }

            if ( empty( $this-&gt;value['thumbnail'] ) &amp;&amp; !empty( $this-&gt;value['url'] ) ) { // Just in case
                if ( !empty( $this-&gt;value['id'] ) ) {
                    $image = wp_get_attachment_image_src( $this-&gt;value['id'], array(150, 150) );
                    $this-&gt;value['thumbnail'] = $image[0];
                } else {
                    $this-&gt;value['thumbnail'] = $this-&gt;value['url'];    
                }
            }

            echo '<div class="' . $hide . 'screenshot">';
            echo '<a class="of-uploaded-image" href="'%20.%20%24this-&gt;value%5B'url'%5D%20.%20'">';
            echo '<img class="redux-option-image" id="image_' . $this-&gt;field['id'] . '" src="'%20.%20%24this-&gt;value%5B'thumbnail'%5D%20.%20'" alt="">';
            echo '</a>';
            echo '</div>';
        
            //Upload controls DIV
            echo '<div class="upload_button_div">';

            //If the user has WP3.5+ show upload/remove button
            echo '<span class="button media_upload_button" id="' . $this-&gt;field['id'] . '-media">' . __( 'Upload', 'redux-framework' ) . '</span>';
            
            $hide = '';
            if( empty( $this-&gt;value['url'] ) || $this-&gt;value['url'] == '' )
                $hide =' hide';

            echo '<span class="button remove-image' . $hide . '" id="reset_' . $this-&gt;field['id'] . '" rel="' . $this-&gt;field['id'] . '">' . __( 'Remove', 'redux-framework' ) . '</span>';

            echo '</div>';    
            
        }

        /**
         * Enqueue Function.
         *
         * If this field requires any scripts, or css define this function and register/enqueue the scripts/css
         *
         * @since       1.0.0
         * @access      public
         * @return      void
         */
        public function enqueue() {

            if( function_exists( 'wp_enqueue_media' ) ) {
                wp_enqueue_media();
            } else {
                wp_enqueue_script( 'media-upload' );
                wp_enqueue_script( 'thickbox' );
                wp_enqueue_style( 'thickbox' );
            }

            wp_enqueue_script(
                'redux-field-media-js',
                ReduxFramework::$_url . 'inc/fields/media/field_media.js',
                array( 'jquery', 'wp-color-picker' ),
                time(),
                true
            );

            wp_enqueue_style(
                'redux-field-media-css',
                ReduxFramework::$_url . 'inc/fields/media/field_media.css',
                time(),
                true
            );

        }
    }
}