PageRenderTime 31ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/types/embedded/includes/fields/file.php

https://bitbucket.org/cisash/fananeen
PHP | 324 lines | 249 code | 22 blank | 53 comment | 42 complexity | 895a7bc24d9bc0a49e42aba50f39c9a6 MD5 | raw file
  1. <?php
  2. /**
  3. * Register data (called automatically).
  4. *
  5. * @return type
  6. */
  7. function wpcf_fields_file() {
  8. return array(
  9. 'id' => 'wpcf-file',
  10. 'title' => __('File', 'wpcf'),
  11. 'description' => __('File', 'wpcf'),
  12. 'validate' => array('required'),
  13. 'meta_box_js' => array(
  14. 'wpcf-jquery-fields-file' => array(
  15. 'inline' => 'wpcf_fields_file_meta_box_js_inline',
  16. )
  17. ),
  18. );
  19. }
  20. /**
  21. * Form data for post edit page.
  22. *
  23. * @param type $field
  24. */
  25. function wpcf_fields_file_meta_box_form($field, $element, $image = false) {
  26. add_thickbox();
  27. $type = $field['type'] == 'image' ? 'image' : 'file';
  28. $button_text = $type == 'image' ? __('Upload image', 'wpcf') : __('Upload file',
  29. 'wpcf');
  30. // Set ID
  31. $element_id = !empty($element['#id']) ? $element['#id'] : 'wpcf-fields-' . $field['slug'];
  32. $attachment_id = false;
  33. // Get attachment by guid
  34. global $wpdb;
  35. if (!empty($field['value'])) {
  36. $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}
  37. WHERE post_type = 'attachment' AND guid=%s",
  38. $field['value']));
  39. }
  40. // Set preview
  41. $preview = '';
  42. if (!isset($field['wpml_action']) || $field['wpml_action'] != 'copy') {
  43. if (!empty($attachment_id)) {
  44. $preview = wp_get_attachment_image($attachment_id, 'thumbnail');
  45. } else {
  46. // If external image set preview
  47. $file_path=parse_url($field['value']);
  48. if ($file_path && isset($file_path['path']))
  49. $file=pathinfo($file_path['path']);
  50. else
  51. $file = pathinfo($field['value']);
  52. if (isset($file['extension'])
  53. && in_array($file['extension'],
  54. array('jpg', 'jpeg', 'gif', 'png'))) {
  55. $preview = '<img alt="" src="' . $field['value'] . '" />';
  56. }
  57. }
  58. }
  59. // Set button
  60. if (!isset($field['wpml_action']) || $field['wpml_action'] != 'copy') {
  61. if (!empty($field['#attributes']['readonly']) || !empty($field['#attributes']['disabled'])) {
  62. $button = '';
  63. } else {
  64. $button = '<a href="javascript:void(0);"'
  65. . ' class="wpcf-fields-' . $type . '-upload-link button-secondary"'
  66. . ' id="' . $element_id . '-upload">'
  67. . $button_text . '</a>';
  68. }
  69. } else {
  70. $button = '';
  71. }
  72. // Set form
  73. $form = array(
  74. '#type' => 'textfield',
  75. '#id' => $element_id . '-upload-holder',
  76. '#name' => 'wpcf[' . $field['slug'] . ']',
  77. '#suffix' => '&nbsp;' . $button,
  78. '#after' => '<div id="' . $element_id
  79. . '-upload-holder-preview"'
  80. . ' class="wpcf-fields-file-preview">' . $preview . '</div>',
  81. '#attributes' => array('class' => 'wpcf-fields-file-textfield'),
  82. );
  83. return $form;
  84. }
  85. /**
  86. * Renders inline JS.
  87. */
  88. function wpcf_fields_file_meta_box_js_inline() {
  89. global $post;
  90. ?>
  91. <script type="text/javascript">
  92. //<![CDATA[
  93. jQuery(document).ready(function(){
  94. window.wpcf_formfield = false;
  95. jQuery('.wpcf-fields-file-upload-link').live('click', function() {
  96. window.wpcf_formfield = '#'+jQuery(this).attr('id')+'-holder';
  97. tb_show('<?php
  98. _e('Upload file', 'wpcf');
  99. ?>', 'media-upload.php?post_id=<?php echo $post->ID; ?>&type=file&wpcf-fields-media-insert=1&TB_iframe=true');
  100. return false;
  101. });
  102. });
  103. function wpcfFieldsFileMediaInsert(url, type) {
  104. jQuery(window.wpcf_formfield).val(url);
  105. if (type == 'image') {
  106. jQuery(window.wpcf_formfield+'-preview').html('<img src="'+url+'" />');
  107. } else {
  108. jQuery(window.wpcf_formfield+'-preview').html('');
  109. }
  110. tb_remove();
  111. window.wpcf_formfield = false;
  112. }
  113. //]]>
  114. </script>
  115. <?php
  116. }
  117. /**
  118. * Media popup JS.
  119. */
  120. function wpcf_fields_file_media_admin_head() {
  121. ?>
  122. <script type="text/javascript">
  123. function wpcfFieldsFileMediaTrigger(guid, type) {
  124. window.parent.wpcfFieldsFileMediaInsert(guid, type);
  125. window.parent.jQuery('#TB_closeWindowButton').trigger('click');
  126. }
  127. </script>
  128. <style type="text/css">
  129. tr.submit { display: none; }
  130. </style>
  131. <?php
  132. }
  133. /**
  134. * Adds 'Types' column to media item table.
  135. *
  136. * @param type $form_fields
  137. * @param type $post
  138. * @return type
  139. */
  140. function wpcf_fields_file_attachment_fields_to_edit_filter($form_fields, $post) {
  141. $type = (strpos($post->post_mime_type, 'image/') !== false) ? 'image' : 'file';
  142. $form_fields['wpcf_fields_file'] = array(
  143. 'label' => __('Types', 'wpcf'),
  144. 'input' => 'html',
  145. 'html' => '<a href="#" title="' . $post->guid
  146. . '" class="wpcf-fields-file-insert-button'
  147. . ' button-primary" onclick="wpcfFieldsFileMediaTrigger(\''
  148. . $post->guid . '\', \'' . $type . '\')">'
  149. . __('Use as field value', 'wpcf') . '</a><br /><br />',
  150. // 'helps' => __('Set this file as file value', 'wpcf'),
  151. );
  152. return $form_fields;
  153. }
  154. /**
  155. * View function.
  156. *
  157. * @param type $params
  158. */
  159. function wpcf_fields_file_view($params) {
  160. $output = '';
  161. if (isset($params['link']) && $params['link'] == 'true') {
  162. $title = '';
  163. $add = '';
  164. if (!empty($params['title'])) {
  165. $add .= ' title="' . $params['title'] . '"';
  166. $title .= $params['title'];
  167. } else {
  168. $add .= ' title="' . $params['field_value'] . '"';
  169. $title .= $params['field_value'];
  170. }
  171. if (!empty($params['class'])) {
  172. $add .= ' class="' . $params['class'] . '"';
  173. }
  174. if (!empty($params['style'])) {
  175. $add .= ' style="' . $params['style'] . '"';
  176. }
  177. $output = '<a href="' . $params['field_value'] . '"' . $add . '>'
  178. . $title . '</a>';
  179. } else {
  180. $output = $params['field_value'];
  181. }
  182. return $output;
  183. }
  184. /**
  185. * Editor callback form.
  186. */
  187. function wpcf_fields_file_editor_callback() {
  188. wp_enqueue_style('wpcf-fields-file',
  189. WPCF_EMBEDDED_RES_RELPATH . '/css/basic.css', array(), WPCF_VERSION);
  190. // Get field
  191. $field = wpcf_admin_fields_get_field($_GET['field_id']);
  192. if (empty($field)) {
  193. _e('Wrong field specified', 'wpcf');
  194. die();
  195. }
  196. // Get post_ID
  197. $post_ID = false;
  198. if (isset($_POST['post_id'])) {
  199. $post_ID = intval($_POST['post_id']);
  200. } else {
  201. $http_referer = explode('?', $_SERVER['HTTP_REFERER']);
  202. parse_str($http_referer[1], $http_referer);
  203. if (isset($http_referer['post'])) {
  204. $post_ID = $http_referer['post'];
  205. }
  206. }
  207. // Get attachment
  208. $attachment_id = false;
  209. if ($post_ID) {
  210. $file = get_post_meta($post_ID,
  211. wpcf_types_get_meta_prefix($field) . $field['slug'], true);
  212. if (!empty($file)) {
  213. // Get attachment by guid
  214. global $wpdb;
  215. $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}
  216. WHERE post_type = 'attachment' AND guid=%s",
  217. $file));
  218. }
  219. }
  220. $last_settings = wpcf_admin_fields_get_field_last_settings($_GET['field_id']);
  221. $form = array();
  222. $form['#form']['callback'] = 'wpcf_fields_file_editor_submit';
  223. if ($attachment_id) {
  224. $form['preview'] = array(
  225. '#type' => 'markup',
  226. '#markup' => '<div class="message updated" style="margin: 0 0 20px 0"><p>'
  227. . $file . '</p></div>',
  228. );
  229. }
  230. $form['link'] = array(
  231. '#type' => 'checkbox',
  232. '#title' => __('Display as link', 'wpcf'),
  233. '#name' => 'link',
  234. '#default_value' => isset($last_settings['link']) ? $last_settings['link'] : 1,
  235. );
  236. $form['title'] = array(
  237. '#type' => 'textfield',
  238. '#title' => __('Link title', 'wpcf'),
  239. '#name' => 'title',
  240. '#value' => isset($last_settings['title']) ? $last_settings['title'] : '',
  241. );
  242. $form['class'] = array(
  243. '#type' => 'textfield',
  244. '#title' => __('Class', 'wpcf'),
  245. '#name' => 'class',
  246. '#value' => isset($last_settings['class']) ? $last_settings['class'] : '',
  247. );
  248. $form['style'] = array(
  249. '#type' => 'textfield',
  250. '#title' => __('Style', 'wpcf'),
  251. '#name' => 'style',
  252. '#value' => isset($last_settings['style']) ? $last_settings['style'] : '',
  253. );
  254. $form['submit'] = array(
  255. '#type' => 'submit',
  256. '#name' => 'submit',
  257. '#value' => __('Insert shortcode', 'wpcf'),
  258. '#attributes' => array('class' => 'button-primary'),
  259. );
  260. $f = wpcf_form('wpcf-form', $form);
  261. wpcf_admin_ajax_head('Insert email', 'wpcf');
  262. echo '<form method="post" action="">';
  263. echo $f->renderForm();
  264. echo '</form>';
  265. wpcf_admin_ajax_footer();
  266. }
  267. /**
  268. * Editor callback form submit.
  269. */
  270. function wpcf_fields_file_editor_submit() {
  271. $add = '';
  272. if (!empty($_POST['link'])) {
  273. $add .= ' link="true"';
  274. if (!empty($_POST['title'])) {
  275. $add .= ' title="' . strval($_POST['title']) . '"';
  276. }
  277. }
  278. if (!empty($_POST['class'])) {
  279. $add .= ' class="' . $_POST['class'] . '"';
  280. }
  281. if (!empty($_POST['style'])) {
  282. $add .= ' style="' . $_POST['style'] . '"';
  283. }
  284. $field = wpcf_admin_fields_get_field($_GET['field_id']);
  285. if (!empty($field)) {
  286. $shortcode = wpcf_fields_get_shortcode($field, $add);
  287. wpcf_admin_fields_save_field_last_settings($_GET['field_id'], $_POST);
  288. echo editor_admin_popup_insert_shortcode_js($shortcode);
  289. die();
  290. }
  291. }
  292. /**
  293. * Filters media TABs.
  294. *
  295. * @param type $tabs
  296. * @return type
  297. */
  298. function wpcf_fields_file_media_upload_tabs_filter($tabs) {
  299. unset($tabs['type_url']);
  300. return $tabs;
  301. }