ReduxFramework_media

Main ReduxFramework_media class.

Defined (1)

The class is defined in the following location(s).

/inc/ReduxFramework/ReduxCore/inc/fields/media/field_media.php  
  1. class ReduxFramework_media { 
  2.  
  3. /** 
  4. * Field Constructor. 
  5. * Required - must call the parent constructor, then assign field and value to vars, and obviously call the render field function 
  6. * @since 1.0.0 
  7. * @access public 
  8. * @return void 
  9. */ 
  10. function __construct( $field = array(), $value = '', $parent ) { 
  11. $this->parent = $parent; 
  12. $this->field = $field; 
  13. $this->value = $value; 
  14.  
  15. /** 
  16. * Field Render Function. 
  17. * Takes the vars and outputs the HTML for the field in the settings 
  18. * @since 1.0.0 
  19. * @access public 
  20. * @return void 
  21. */ 
  22. public function render() { 
  23.  
  24. // No errors please 
  25. $defaults = array( 
  26. 'id' => '',  
  27. 'url' => '',  
  28. 'width' => '',  
  29. 'height' => '',  
  30. 'thumbnail' => '',  
  31. ); 
  32.  
  33. $this->value = wp_parse_args( $this->value, $defaults ); 
  34.  
  35. if (isset($this->field['mode']) && $this->field['mode'] == false) { 
  36. $this->field['mode'] = 0; 
  37.  
  38. if ( ! isset( $this->field['mode'] ) ) { 
  39. $this->field['mode'] = "image"; 
  40.  
  41. if (!isset($this->field['library_filter'])) { 
  42. $libFilter = ''; 
  43. } else { 
  44. if (!is_array($this->field['library_filter'])) { 
  45. $this->field['library_filter'] = array($this->field['library_filter']); 
  46.  
  47. $mimeTypes = get_allowed_mime_types(); 
  48.  
  49. $libArray = $this->field['library_filter']; 
  50.  
  51. $jsonArr = array(); 
  52.  
  53. // Enum mime types 
  54. foreach ($mimeTypes as $ext => $type) { 
  55. if (strpos($ext, '|')) { 
  56. $expArr = explode('|', $ext); 
  57.  
  58. foreach($expArr as $ext) { 
  59. if (in_array($ext, $libArray )) { 
  60. $jsonArr[$ext] = $type; 
  61. } elseif (in_array($ext, $libArray )) { 
  62. $jsonArr[$ext] = $type; 
  63.  
  64.  
  65. $libFilter = urlencode(json_encode($jsonArr)); 
  66.  
  67. if ( empty( $this->value ) && ! empty( $this->field['default'] ) ) { // If there are standard values and value is empty 
  68. if ( is_array( $this->field['default'] ) ) { 
  69. if ( ! empty( $this->field['default']['id'] ) ) { 
  70. $this->value['id'] = $this->field['default']['id']; 
  71.  
  72. if ( ! empty( $this->field['default']['url'] ) ) { 
  73. $this->value['url'] = $this->field['default']['url']; 
  74. } else { 
  75. if ( is_numeric( $this->field['default'] ) ) { // Check if it's an attachment ID 
  76. $this->value['id'] = $this->field['default']; 
  77. } else { // Must be a URL 
  78. $this->value['url'] = $this->field['default']; 
  79.  
  80.  
  81. if ( empty( $this->value['url'] ) && ! empty( $this->value['id'] ) ) { 
  82. $img = wp_get_attachment_image_src( $this->value['id'], 'full' ); 
  83. $this->value['url'] = $img[0]; 
  84. $this->value['width'] = $img[1]; 
  85. $this->value['height'] = $img[2]; 
  86.  
  87. $hide = 'hide '; 
  88.  
  89. if ( ( isset( $this->field['preview'] ) && $this->field['preview'] === false ) ) { 
  90. $this->field['class'] .= " noPreview"; 
  91.  
  92. if ( ( ! empty( $this->field['url'] ) && $this->field['url'] === true ) || isset( $this->field['preview'] ) && $this->field['preview'] === false ) { 
  93. $hide = ''; 
  94.  
  95. $placeholder = isset( $this->field['placeholder'] ) ? $this->field['placeholder'] : __( 'No media selected', 'redux-framework' ); 
  96.  
  97. $readOnly = ' readonly="readonly"'; 
  98. if ( isset( $this->field['readonly'] ) && $this->field['readonly'] === false ) { 
  99. $readOnly = ''; 
  100.  
  101. echo '<input placeholder="' . $placeholder . '" type="text" class="' . $hide . 'upload regular-text ' . $this->field['class'] . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[url]" id="' . $this->parent->args['opt_name'] . '[' . $this->field['id'] . '][url]" value="' . $this->value['url'] . '"' . $readOnly . '/>'; 
  102. echo '<input type="hidden" class="data" data-mode="' . $this->field['mode'] . '" />'; 
  103. echo '<input type="hidden" class="library-filter" data-lib-filter="' . $libFilter . '" />'; 
  104. echo '<input type="hidden" class="upload-id ' . $this->field['class'] . '" name="' . $this->field['name'] . $this->field['name_suffix'] . '[id]" id="' . $this->parent->args['opt_name'] . '[' . $this->field['id'] . '][id]" value="' . $this->value['id'] . '" />'; 
  105. echo '<input type="hidden" class="upload-height" name="' . $this->field['name'] . $this->field['name_suffix'] . '[height]" id="' . $this->parent->args['opt_name'] . '[' . $this->field['id'] . '][height]" value="' . $this->value['height'] . '" />'; 
  106. echo '<input type="hidden" class="upload-width" name="' . $this->field['name'] . $this->field['name_suffix'] . '[width]" id="' . $this->parent->args['opt_name'] . '[' . $this->field['id'] . '][width]" value="' . $this->value['width'] . '" />'; 
  107. echo '<input type="hidden" class="upload-thumbnail" name="' . $this->field['name'] . $this->field['name_suffix'] . '[thumbnail]" id="' . $this->parent->args['opt_name'] . '[' . $this->field['id'] . '][thumbnail]" value="' . $this->value['thumbnail'] . '" />'; 
  108.  
  109. //Preview 
  110. $hide = ''; 
  111.  
  112. if ( ( isset( $this->field['preview'] ) && $this->field['preview'] === false ) || empty( $this->value['url'] ) ) { 
  113. $hide = 'hide '; 
  114.  
  115. if ( empty( $this->value['thumbnail'] ) && ! empty( $this->value['url'] ) ) { // Just in case 
  116. if ( ! empty( $this->value['id'] ) ) { 
  117. $image = wp_get_attachment_image_src( $this->value['id'], array( 
  118. 150,  
  119. 150 
  120. ) ); 
  121. $this->value['thumbnail'] = $image[0]; 
  122. } else { 
  123. $this->value['thumbnail'] = $this->value['url']; 
  124.  
  125. echo '<div class="' . $hide . 'screenshot">'; 
  126. echo '<a class="of-uploaded-image" href="' . $this->value['url'] . '" target="_blank">'; 
  127. echo '<img class="redux-option-image" id="image_' . $this->field['id'] . '" src="' . $this->value['thumbnail'] . '" alt="" target="_blank" rel="external" />'; 
  128. echo '</a>'; 
  129. echo '</div>'; 
  130.  
  131. //Upload controls DIV 
  132. echo '<div class="upload_button_div">'; 
  133.  
  134. //If the user has WP3.5+ show upload/remove button 
  135. echo '<span class="button media_upload_button" id="' . $this->field['id'] . '-media">' . __( 'Upload', 'redux-framework' ) . '</span>'; 
  136.  
  137. $hide = ''; 
  138. if ( empty( $this->value['url'] ) || $this->value['url'] == '' ) { 
  139. $hide = ' hide'; 
  140.  
  141. echo '<span class="button remove-image' . $hide . '" id="reset_' . $this->field['id'] . '" rel="' . $this->field['id'] . '">' . __( 'Remove', 'redux-framework' ) . '</span>'; 
  142.  
  143. echo '</div>'; 
  144.  
  145. /** 
  146. * Enqueue Function. 
  147. * If this field requires any scripts, or css define this function and register/enqueue the scripts/css 
  148. * @since 1.0.0 
  149. * @access public 
  150. * @return void 
  151. */ 
  152. public function enqueue() { 
  153.  
  154. wp_enqueue_script( 
  155. 'redux-field-media-js',  
  156. ReduxFramework::$_url . 'assets/js/media/media' . Redux_Functions::isMin() . '.js',  
  157. array( 'jquery', 'redux-js' ),  
  158. time(),  
  159. true 
  160. ); 
  161.  
  162. redux_enqueue_style( 
  163. $this->parent,  
  164. 'redux-field-media-css',  
  165. ReduxFramework::$_url . 'inc/fields/media/field_media.css',  
  166. ReduxFramework::$_dir . 'inc/fields/media',  
  167. array(),  
  168. time(),  
  169. false 
  170. );  
  171.  
  172. // wp_enqueue_style( 
  173. // 'redux-field-media-css',  
  174. // ReduxFramework::$_url . 'inc/fields/media/field_media.css',  
  175. // time(),  
  176. // true 
  177. // );