PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/codepress-admin-columns/classes/values.php

https://bitbucket.org/antonyravel/cape-resorts
PHP | 539 lines | 282 code | 92 blank | 165 comment | 65 complexity | 1ff86dbe4c2943729796a209b2a1ab6d MD5 | raw file
  1. <?php
  2. /**
  3. * CPAC_Values Class
  4. *
  5. * @since 1.4.4
  6. *
  7. */
  8. class CPAC_Values
  9. {
  10. protected $excerpt_length, $thumbnail_size;
  11. /**
  12. * Constructor
  13. *
  14. * @since 1.0
  15. */
  16. function __construct()
  17. {
  18. // number of words
  19. $this->excerpt_length = 20;
  20. $this->thumbnail_size = apply_filters( 'cpac_thumbnail_size', array(80,80) );
  21. }
  22. /**
  23. * Admin requests for orderby column
  24. *
  25. * @since 1.0
  26. */
  27. public function get_stored_columns($type)
  28. {
  29. return Codepress_Admin_Columns::get_stored_columns($type);
  30. }
  31. /**
  32. * Checks if column-meta key exists
  33. *
  34. * @since 1.0
  35. */
  36. public static function is_column_meta( $id = '' )
  37. {
  38. return Codepress_Admin_Columns::is_column_meta( $id );
  39. }
  40. /**
  41. * Returns excerpt
  42. *
  43. * @since 1.0
  44. */
  45. protected function get_post_excerpt($post_id)
  46. {
  47. global $post;
  48. $save_post = $post;
  49. $post = get_post($post_id);
  50. $excerpt = get_the_excerpt();
  51. $post = $save_post;
  52. $output = $this->get_shortened_string($excerpt, $this->excerpt_length );
  53. return $output;
  54. }
  55. /**
  56. * Returns shortened string
  57. *
  58. * @since 1.0
  59. */
  60. protected function get_shortened_string($string = '', $num_words = 55, $more = null)
  61. {
  62. if (!$string)
  63. return false;
  64. return wp_trim_words( $string, $num_words, $more );
  65. }
  66. /**
  67. * Get image from assets folder
  68. *
  69. * @since 1.3.1
  70. */
  71. protected function get_asset_image($name = '', $title = '')
  72. {
  73. if ( $name )
  74. return sprintf("<img alt='' src='%s' title='%s'/>", CPAC_URL."/assets/images/{$name}", $title);
  75. }
  76. /**
  77. * Shorten URL
  78. *
  79. * @since 1.3.1
  80. */
  81. protected function get_shorten_url($url = '')
  82. {
  83. if ( !$url )
  84. return false;
  85. // shorten url
  86. $short_url = url_shorten( $url );
  87. return "<a title='{$url}' href='{$url}'>{$short_url}</a>";
  88. }
  89. /**
  90. * Get column value of post attachments
  91. *
  92. * @since 1.0
  93. */
  94. protected function get_column_value_attachments( $post_id )
  95. {
  96. $result = '';
  97. $attachment_ids = $this->get_attachment_ids($post_id);
  98. if ( $attachment_ids ) {
  99. foreach ( $attachment_ids as $attach_id ) {
  100. if ( wp_get_attachment_image($attach_id) )
  101. $result .= wp_get_attachment_image( $attach_id, $this->thumbnail_size, true );
  102. }
  103. }
  104. return $result;
  105. }
  106. /**
  107. * Get column value of post attachments
  108. *
  109. * @since 1.2.1
  110. */
  111. protected function get_attachment_ids( $post_id )
  112. {
  113. return Codepress_Admin_Columns::get_attachment_ids( $post_id );
  114. }
  115. /**
  116. * Get a thumbnail
  117. *
  118. * @since 1.0
  119. */
  120. protected function get_thumbnail( $image = '' )
  121. {
  122. if ( empty($image) )
  123. return false;
  124. // get thumbnail image size
  125. $image_size = $this->thumbnail_size; // w, h
  126. // incase the thumbnail dimension is set by name
  127. if ( !is_array($image_size) ) {
  128. global $_wp_additional_image_sizes;
  129. if ( isset($_wp_additional_image_sizes[$image_size]) ) {
  130. $_size = $_wp_additional_image_sizes[$image_size];
  131. $image_size = array( $_size['width'], $_size['height'] );
  132. }
  133. }
  134. // fallback for image size incase the passed size name does not exists
  135. if ( !isset($image_size[0]) || !isset($image_size[1]) ) {
  136. $image_size = array(80, 80);
  137. }
  138. // get correct image path
  139. $image_path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $image);
  140. // resize image
  141. if ( file_exists($image_path) && $this->is_image($image_path) ) {
  142. $resized = image_resize( $image_path, $image_size[0], $image_size[1], true);
  143. // resize worked
  144. if ( ! is_wp_error( $resized ) ) {
  145. $image = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $resized);
  146. return "<img src='{$image}' alt='' width='{$image_size[0]}' height='{$image_size[1]}' />";
  147. }
  148. // resizing failed so let's return full image with maxed dimensions
  149. else {
  150. return "<img src='{$image}' alt='' style='max-width:{$image_size[0]}px;max-height:{$image_size[1]}px' />";
  151. }
  152. }
  153. return false;
  154. }
  155. /**
  156. * Checks an URL for image extension
  157. *
  158. * @since 1.2
  159. */
  160. protected function is_image($url)
  161. {
  162. $validExt = array('.jpg', '.jpeg', '.gif', '.png', '.bmp');
  163. $ext = strrchr($url, '.');
  164. return in_array($ext, $validExt);
  165. }
  166. /**
  167. * Get a thumbnail
  168. *
  169. * @since 1.3.1
  170. */
  171. protected function get_media_thumbnails($meta)
  172. {
  173. $meta = $this->strip_trim( str_replace(' ','', $meta) );
  174. // split media ids
  175. $media_ids = array($meta);
  176. if ( strpos($meta, ',') !== false )
  177. $media_ids = explode(',', $meta);
  178. // check if media exists
  179. $thumbs = '';
  180. foreach ( $media_ids as $media_id )
  181. if ( is_numeric($media_id) )
  182. $thumbs .= wp_get_attachment_url($media_id) ? "<span class='cpac-column-value-image'>".wp_get_attachment_image( $media_id, array(80,80), true )."</span>" : '';
  183. return $thumbs;
  184. }
  185. /**
  186. * Convert file size to readable format
  187. *
  188. * @since 1.4.5
  189. */
  190. function get_readable_filesize($size)
  191. {
  192. $filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
  193. return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0 Bytes';
  194. }
  195. /**
  196. * Get column value of Custom Field
  197. *
  198. * @since 1.0
  199. */
  200. protected function get_column_value_custom_field($object_id, $column_name, $meta_type = 'post')
  201. {
  202. /** Users */
  203. if ( 'user' == $meta_type ) {
  204. $type = 'wp-users';
  205. }
  206. /** Media */
  207. elseif ( 'media' == $meta_type ) {
  208. $type = 'wp-media';
  209. $meta_type = 'post';
  210. }
  211. /** Posts */
  212. else {
  213. $type = get_post_type($object_id);
  214. }
  215. // get column
  216. $columns = $this->get_stored_columns($type);
  217. // inputs
  218. $field = isset($columns[$column_name]['field']) ? $columns[$column_name]['field'] : '';
  219. $fieldtype = isset($columns[$column_name]['field_type']) ? $columns[$column_name]['field_type'] : '';
  220. $before = isset($columns[$column_name]['before']) ? $columns[$column_name]['before'] : '';
  221. $after = isset($columns[$column_name]['after']) ? $columns[$column_name]['after'] : '';
  222. // rename hidden custom fields to their original name
  223. $field = substr($field,0,10) == "cpachidden" ? str_replace('cpachidden','',$field) : $field;
  224. // Get meta field value
  225. $meta = get_metadata($meta_type, $object_id, $field, true);
  226. // multiple meta values
  227. if ( ( $fieldtype == 'array' && is_array($meta) ) || is_array($meta) ) {
  228. $meta = get_metadata($meta_type, $object_id, $field, true);
  229. $meta = $this->recursive_implode(', ', $meta);
  230. }
  231. // make sure there are no serialized arrays or null data
  232. if ( !is_string($meta) )
  233. return false;
  234. // handles each field type differently..
  235. switch ($fieldtype) :
  236. // Image
  237. case "image" :
  238. $meta = $this->get_thumbnail($meta);
  239. break;
  240. // Media Library ID
  241. case "library_id" :
  242. $meta = $this->get_media_thumbnails($meta);
  243. break;
  244. // Excerpt
  245. case "excerpt" :
  246. $meta = $this->get_shortened_string($meta, $this->excerpt_length);
  247. break;
  248. // Date
  249. case "date" :
  250. $meta = $this->get_date($meta);
  251. break;
  252. // Post Title
  253. case "title_by_id" :
  254. $titles = $this->get_custom_field_value_title($meta);
  255. if ( $titles )
  256. $meta = $titles;
  257. break;
  258. // User Name
  259. case "user_by_id" :
  260. $names = $this->get_custom_field_value_user($meta);
  261. if ( $names )
  262. $meta = $names;
  263. break;
  264. // Checkmark
  265. case "checkmark" :
  266. $checkmark = $this->get_asset_image('checkmark.png');
  267. if ( empty($meta) || 'false' === $meta || '0' === $meta ) {
  268. $checkmark = '';
  269. }
  270. $meta = $checkmark;
  271. break;
  272. // Color
  273. case "color" :
  274. if ( !empty($meta) ) {
  275. $meta = "<div class='cpac-color'><span style='background-color:{$meta}'></span>{$meta}</div>";
  276. }
  277. break;
  278. endswitch;
  279. // filter for customization
  280. $meta = apply_filters('cpac_get_column_value_custom_field', $meta, $fieldtype, $field, $type, $object_id );
  281. // add before and after string
  282. if ( $meta ) {
  283. $meta = "{$before}{$meta}{$after}";
  284. }
  285. return $meta;
  286. }
  287. /**
  288. * Get custom field value 'Title by ID'
  289. *
  290. * @since 1.3
  291. */
  292. protected function get_custom_field_value_title($meta)
  293. {
  294. //remove white spaces and strip tags
  295. $meta = $this->strip_trim( str_replace(' ','', $meta) );
  296. // var
  297. $ids = $titles = array();
  298. // check for multiple id's
  299. if ( strpos($meta, ',') !== false )
  300. $ids = explode(',',$meta);
  301. elseif ( is_numeric($meta) )
  302. $ids[] = $meta;
  303. // display title with link
  304. if ( $ids && is_array($ids) ) {
  305. foreach ( $ids as $id ) {
  306. $title = is_numeric($id) ? get_the_title($id) : '';
  307. $link = get_edit_post_link($id);
  308. if ( $title )
  309. $titles[] = $link ? "<a href='{$link}'>{$title}</a>" : $title;
  310. }
  311. }
  312. return implode('<span class="cpac-divider"></span>', $titles);
  313. }
  314. /**
  315. * Get custom field value 'User by ID'
  316. *
  317. * @since 1.4.6.3
  318. */
  319. protected function get_custom_field_value_user($meta)
  320. {
  321. //remove white spaces and strip tags
  322. $meta = $this->strip_trim( str_replace(' ','', $meta) );
  323. // var
  324. $ids = $names = array();
  325. // check for multiple id's
  326. if ( strpos($meta, ',') !== false )
  327. $ids = explode(',',$meta);
  328. elseif ( is_numeric($meta) )
  329. $ids[] = $meta;
  330. // display username
  331. if ( $ids && is_array($ids) ) {
  332. foreach ( $ids as $id ) {
  333. if ( !is_numeric($id) )
  334. continue;
  335. $userdata = get_userdata($id);
  336. if ( is_object($userdata) && !empty( $userdata->display_name ) ) {
  337. $names[] = $userdata->display_name;
  338. }
  339. }
  340. }
  341. return implode('<span class="cpac-divider"></span>', $names);
  342. }
  343. /**
  344. * Get column value of Custom Field
  345. *
  346. * @since 1.2
  347. */
  348. protected function get_user_column_value_custom_field($user_id, $id)
  349. {
  350. $columns = $this->get_stored_columns('wp-users');
  351. // inputs
  352. $field = isset($columns[$id]['field']) ? $columns[$id]['field'] : '';
  353. $fieldtype = isset($columns[$id]['field_type']) ? $columns[$id]['field_type'] : '';
  354. $before = isset($columns[$id]['before']) ? $columns[$id]['before'] : '';
  355. $after = isset($columns[$id]['after']) ? $columns[$id]['after'] : '';
  356. // Get meta field value
  357. $meta = get_user_meta($user_id, $field, true);
  358. // multiple meta values
  359. if ( ( $fieldtype == 'array' && is_array($meta) ) || is_array($meta) ) {
  360. $meta = get_user_meta($user_id, $field);
  361. $meta = $this->recursive_implode(', ', $meta);
  362. }
  363. // make sure there are no serialized arrays or empty meta data
  364. if ( empty($meta) || !is_string($meta) )
  365. return false;
  366. // handles each field type differently..
  367. switch ($fieldtype) :
  368. // Image
  369. case "image" :
  370. $meta = $this->get_thumbnail($meta);
  371. break;
  372. // Media Library ID
  373. case "library_id" :
  374. $meta = $this->get_media_thumbnails($meta);
  375. break;
  376. // Excerpt
  377. case "excerpt" :
  378. $meta = $this->get_shortened_string($meta, $this->excerpt_length);
  379. break;
  380. endswitch;
  381. // filter for customization
  382. $meta = apply_filters('cpac_get_user_column_value_custom_field', $meta, $fieldtype, $field );
  383. // add before and after string
  384. $meta = "{$before}{$meta}{$after}";
  385. return $meta;
  386. }
  387. /**
  388. * Implode for multi dimensional array
  389. *
  390. * @since 1.0
  391. */
  392. protected function recursive_implode( $glue, $pieces )
  393. {
  394. foreach( $pieces as $r_pieces ) {
  395. if( is_array( $r_pieces ) ) {
  396. $retVal[] = $this->recursive_implode( $glue, $r_pieces );
  397. }
  398. else {
  399. $retVal[] = $r_pieces;
  400. }
  401. }
  402. if ( isset($retVal) && is_array($retVal) )
  403. return implode( $glue, $retVal );
  404. return false;
  405. }
  406. /**
  407. * Strip tags and trim
  408. *
  409. * @since 1.3
  410. */
  411. protected function strip_trim($string)
  412. {
  413. return Codepress_Admin_Columns::strip_trim($string);
  414. }
  415. /**
  416. * Get date
  417. *
  418. * @since 1.3.1
  419. */
  420. protected function get_date( $date )
  421. {
  422. if ( empty( $date ) || in_array( $date, array( '0000-00-00 00:00:00', '0000-00-00', '00:00:00' ) ) )
  423. return false;
  424. // Parse with strtotime if it's:
  425. // - not numeric ( like a unixtimestamp )
  426. // - date format: yyyymmdd ( format used by ACF ) must start with 19xx or 20xx and is 8 long
  427. // @todo: in theory a numeric string of 8 can also be a unixtimestamp.
  428. // we need to replace this with an option to mark a date as unixtimestamp.
  429. if ( ! is_numeric($date) || ( is_numeric( $date ) && strlen( trim($date) ) == 8 && ( strpos( $date, '20' ) === 0 || strpos( $date, '19' ) === 0 ) ) )
  430. $date = strtotime($date);
  431. return date_i18n( get_option('date_format'), $date );
  432. }
  433. /**
  434. * Get time
  435. *
  436. * @since 1.3.1
  437. */
  438. protected function get_time($date)
  439. {
  440. if ( ! $date )
  441. return false;
  442. if ( ! is_numeric($date) )
  443. $date = strtotime($date);
  444. return date_i18n( get_option('time_format'), $date );
  445. }
  446. }
  447. ?>