/wp-content/plugins/js_composer/include/params/vc_grid_item/attributes.php

https://gitlab.com/ezgonzalez/integral · PHP · 426 lines · 211 code · 41 blank · 174 comment · 16 complexity · a362c1d1346d7de7471349db50e90f1f MD5 · raw file

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * Build css classes from terms of the post.
  7. *
  8. * @param $value
  9. * @param $data
  10. *
  11. * @since 4.4
  12. *
  13. * @return string
  14. */
  15. function vc_gitem_template_attribute_filter_terms_css_classes( $value, $data ) {
  16. $output = '';
  17. /**
  18. * @var null|Wp_Post $post ;
  19. */
  20. extract( array_merge( array(
  21. 'post' => null,
  22. ), $data ) );
  23. if ( isset( $post->filter_terms ) && is_array( $post->filter_terms ) ) {
  24. foreach ( $post->filter_terms as $t ) {
  25. $output .= ' vc_grid-term-' . $t; // @todo fix #106154391786878 $t is array
  26. }
  27. }
  28. return $output;
  29. }
  30. /**
  31. * Get image for post
  32. *
  33. * @param $data
  34. *
  35. * @return mixed|string|void
  36. */
  37. function vc_gitem_template_attribute_post_image( $value, $data ) {
  38. /**
  39. * @var null|Wp_Post $post ;
  40. */
  41. extract( array_merge( array(
  42. 'post' => null,
  43. 'data' => '',
  44. ), $data ) );
  45. if ( 'attachment' === $post->post_type ) {
  46. return wp_get_attachment_image( $post->ID, 'large' );
  47. }
  48. $html = get_the_post_thumbnail( $post->ID );
  49. return apply_filters( 'vc_gitem_template_attribute_post_image_html', $html );
  50. }
  51. function vc_gitem_template_attribute_featured_image( $value, $data ) {
  52. /**
  53. * @var Wp_Post $post
  54. * @var string $data
  55. */
  56. extract( array_merge( array(
  57. 'post' => null,
  58. 'data' => '',
  59. ), $data ) );
  60. return vc_include_template( 'params/vc_grid_item/attributes/featured_image.php', array(
  61. 'post' => $post,
  62. 'data' => $data,
  63. ) );
  64. }
  65. /**
  66. * Create new btn
  67. *
  68. * @param $value
  69. * @param $data
  70. *
  71. * @since 4.5
  72. *
  73. * @return mixed
  74. */
  75. function vc_gitem_template_attribute_vc_btn( $value, $data ) {
  76. /**
  77. * @var Wp_Post $post
  78. * @var string $data
  79. */
  80. extract( array_merge( array(
  81. 'post' => null,
  82. 'data' => '',
  83. ), $data ) );
  84. return vc_include_template( 'params/vc_grid_item/attributes/vc_btn.php', array(
  85. 'post' => $post,
  86. 'data' => $data,
  87. ) );
  88. }
  89. /**
  90. * Get post image url
  91. *
  92. * @param $data
  93. *
  94. * @return string
  95. */
  96. function vc_gitem_template_attribute_post_image_url( $value, $data, $user_empty = true ) {
  97. $output = '';
  98. /**
  99. * @var null|Wp_Post $post ;
  100. */
  101. extract( array_merge( array(
  102. 'post' => null,
  103. 'data' => '',
  104. ), $data ) );
  105. if ( 'attachment' === $post->post_type ) {
  106. $src = wp_get_attachment_image_src( $post->ID, 'large' );
  107. } else {
  108. $attachment_id = get_post_thumbnail_id( $post->ID );
  109. $src = wp_get_attachment_image_src( $attachment_id, 'large' );
  110. }
  111. if ( empty( $src ) && ! empty( $data ) ) {
  112. $output = esc_attr( rawurldecode( $data ) );
  113. } elseif ( ! empty( $src ) ) {
  114. $output = $src[0];
  115. } elseif ( $user_empty ) {
  116. $output = vc_asset_url( 'vc/vc_gitem_image.png' );
  117. }
  118. return apply_filters( 'vc_gitem_template_attribute_post_image_url_value', $output );
  119. }
  120. /**
  121. * Get post image url with href for a dom element
  122. *
  123. * @param $value
  124. * @param $data
  125. *
  126. * @return string
  127. */
  128. function vc_gitem_template_attribute_post_image_url_href( $value, $data ) {
  129. $link = vc_gitem_template_attribute_post_image_url( $value, $data );
  130. return strlen( $link ) ? ' href="' . esc_attr( $link ) . '"' : '';
  131. }
  132. /**
  133. * Add image url as href with css classes for PrettyPhoto js plugin.
  134. *
  135. * @param $value
  136. * @param $data
  137. *
  138. * @return string
  139. */
  140. function vc_gitem_template_attribute_post_image_url_attr_prettyphoto( $value, $data ) {
  141. $data_default = $data;
  142. /**
  143. * @var Wp_Post $post ;
  144. */
  145. extract( array_merge( array(
  146. 'post' => null,
  147. 'data' => '',
  148. ), $data ) );
  149. $href = vc_gitem_template_attribute_post_image_url_href( $value, array( 'post' => $post, 'data' => '' ) );
  150. $rel = ' data-rel="' . esc_attr( 'prettyPhoto[rel-' . md5( vc_request_param( 'shortcode_id' ) ) . ']' ) . '"';
  151. return $href . $rel . ' class="' . esc_attr( $data . ( strlen( $href ) ? ' prettyphoto' : '' ) )
  152. . '" title="' . esc_attr(
  153. apply_filters( 'vc_gitem_template_attribute_post_title', $post->post_title, $data_default ) ) . '"';
  154. }
  155. /**
  156. * Get post image alt
  157. *
  158. * @return string
  159. */
  160. function vc_gitem_template_attribute_post_image_alt( $value, $data ) {
  161. if ( empty( $data['post']->ID ) ) {
  162. return '';
  163. }
  164. if ( 'attachment' === $data['post']->post_type ) {
  165. $attachment_id = $data['post']->ID;
  166. } else {
  167. $attachment_id = get_post_thumbnail_id( $data['post']->ID );
  168. }
  169. if ( ! $attachment_id ) {
  170. return '';
  171. }
  172. $alt = trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) );
  173. return apply_filters( 'vc_gitem_template_attribute_post_image_url_value', $alt );
  174. }
  175. /**
  176. * Get post image url
  177. *
  178. * @param $data
  179. *
  180. * @return string
  181. */
  182. function vc_gitem_template_attribute_post_image_background_image_css( $value, $data ) {
  183. $output = '';
  184. /**
  185. * @var null|Wp_Post $post ;
  186. */
  187. extract( array_merge( array(
  188. 'post' => null,
  189. 'data' => '',
  190. ), $data ) );
  191. if ( 'attachment' === $post->post_type ) {
  192. $src = wp_get_attachment_image_src( $post->ID, 'large' );
  193. } else {
  194. $attachment_id = get_post_thumbnail_id( $post->ID );
  195. $src = wp_get_attachment_image_src( $attachment_id, 'large' );
  196. }
  197. if ( ! empty( $src ) ) {
  198. $output = 'background-image: url(' . $src[0] . ') !important;';
  199. } else {
  200. $output = 'background-image: url(' . vc_asset_url( 'vc/vc_gitem_image.png' ) . ') !important;';
  201. }
  202. return apply_filters( 'vc_gitem_template_attribute_post_image_background_image_css_value', $output );
  203. }
  204. /**
  205. * Get post link
  206. *
  207. * @param $data
  208. *
  209. * @return bool|string
  210. */
  211. function vc_gitem_template_attribute_post_link_url( $value, $data ) {
  212. /**
  213. * @var null|Wp_Post $post ;
  214. */
  215. extract( array_merge( array(
  216. 'post' => null,
  217. ), $data ) );
  218. return get_permalink( $post->ID );
  219. }
  220. /**
  221. * Get post date
  222. *
  223. * @param $data
  224. *
  225. * @return bool|int|string
  226. */
  227. function vc_gitem_template_attribute_post_date( $value, $data ) {
  228. /**
  229. * @var null|Wp_Post $post ;
  230. */
  231. extract( array_merge( array(
  232. 'post' => null,
  233. ), $data ) );
  234. return get_the_date( '', $post->ID );
  235. }
  236. /**
  237. * Get post date time
  238. *
  239. * @param $data
  240. *
  241. * @return bool|int|string
  242. */
  243. function vc_gitem_template_attribute_post_datetime( $value, $data ) {
  244. /**
  245. * @var null|Wp_Post $post ;
  246. */
  247. extract( array_merge( array(
  248. 'post' => null,
  249. ), $data ) );
  250. return get_the_time( 'F j, Y g:i', $post->ID );
  251. }
  252. /**
  253. * Get custom fields.
  254. *
  255. * @param $data
  256. *
  257. * @return mixed|string
  258. */
  259. function vc_gitem_template_attribute_post_meta_value( $value, $data ) {
  260. /**
  261. * @var null|Wp_Post $post ;
  262. * @var string $data ;
  263. */
  264. extract( array_merge( array(
  265. 'post' => null,
  266. 'data' => '',
  267. ), $data ) );
  268. return strlen( $data ) > 0 ? get_post_meta( $post->ID, $data, true ) : $value;
  269. }
  270. /**
  271. * Get post data. Used as wrapper for others post data attributes.
  272. *
  273. * @param $data
  274. *
  275. * @return mixed|string
  276. */
  277. function vc_gitem_template_attribute_post_data( $value, $data ) {
  278. /**
  279. * @var null|Wp_Post $post ;
  280. * @var string $data ;
  281. */
  282. extract( array_merge( array(
  283. 'post' => null,
  284. 'data' => '',
  285. ), $data ) );
  286. return strlen( $data ) > 0 ? apply_filters( 'vc_gitem_template_attribute_' . $data, (
  287. isset( $post->$data ) ? $post->$data : ''
  288. ), array( 'post' => $post, 'data' => '' ) ) : $value;
  289. }
  290. /**
  291. * Get post excerpt. Used as wrapper for others post data attributes.
  292. *
  293. * @param $data
  294. *
  295. * @return mixed|string
  296. */
  297. function vc_gitem_template_attribute_post_excerpt( $value, $data ) {
  298. /**
  299. * @var null|Wp_Post $post ;
  300. * @var string $data ;
  301. */
  302. extract( array_merge( array(
  303. 'post' => null,
  304. 'data' => '',
  305. ), $data ) );
  306. return apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $value ) );
  307. }
  308. /**
  309. * Get post excerpt. Used as wrapper for others post data attributes.
  310. *
  311. * @param $data
  312. *
  313. * @return mixed|string
  314. */
  315. function vc_gitem_template_attribute_post_title( $value, $data ) {
  316. /**
  317. * @var null|Wp_Post $post ;
  318. * @var string $data ;
  319. */
  320. extract( array_merge( array(
  321. 'post' => null,
  322. 'data' => '',
  323. ), $data ) );
  324. return the_title( '', '', false );
  325. }
  326. function vc_gitem_template_attribute_post_author( $value, $data ) {
  327. /**
  328. * @var null|Wp_Post $post ;
  329. * @var string $data ;
  330. */
  331. extract( array_merge( array(
  332. 'post' => null,
  333. 'data' => '',
  334. ), $data ) );
  335. return get_the_author();
  336. }
  337. function vc_gitem_template_attribute_post_author_href( $value, $data ) {
  338. /**
  339. * @var null|Wp_Post $post ;
  340. * @var string $data ;
  341. */
  342. extract( array_merge( array(
  343. 'post' => null,
  344. 'data' => '',
  345. ), $data ) );
  346. return get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) );
  347. }
  348. function vc_gitem_template_attribute_post_categories( $value, $data ) {
  349. /**
  350. * @var null|Wp_Post $post ;
  351. * @var string $data ;
  352. */
  353. extract( array_merge( array(
  354. 'post' => null,
  355. 'data' => '',
  356. ), $data ) );
  357. $atts_extended = array();
  358. parse_str( $data, $atts_extended );
  359. return vc_include_template( 'params/vc_grid_item/attributes/post_categories.php', array(
  360. 'post' => $post,
  361. 'atts' => $atts_extended['atts'],
  362. ) );
  363. }
  364. /**
  365. * Adding filters to parse grid template.
  366. */
  367. add_filter( 'vc_gitem_template_attribute_filter_terms_css_classes', 'vc_gitem_template_attribute_filter_terms_css_classes', 10, 2 );
  368. add_filter( 'vc_gitem_template_attribute_post_image', 'vc_gitem_template_attribute_post_image', 10, 2 );
  369. add_filter( 'vc_gitem_template_attribute_post_image_url', 'vc_gitem_template_attribute_post_image_url', 10, 2 );
  370. add_filter( 'vc_gitem_template_attribute_post_image_url_href', 'vc_gitem_template_attribute_post_image_url_href', 10, 2 );
  371. add_filter( 'vc_gitem_template_attribute_post_image_url_attr_prettyphoto', 'vc_gitem_template_attribute_post_image_url_attr_prettyphoto', 10, 2 );
  372. add_filter( 'vc_gitem_template_attribute_post_image_alt', 'vc_gitem_template_attribute_post_image_alt', 10, 2 );
  373. add_filter( 'vc_gitem_template_attribute_post_link_url', 'vc_gitem_template_attribute_post_link_url', 10, 2 );
  374. add_filter( 'vc_gitem_template_attribute_post_date', 'vc_gitem_template_attribute_post_date', 10, 2 );
  375. add_filter( 'vc_gitem_template_attribute_post_datetime', 'vc_gitem_template_attribute_post_datetime', 10, 2 );
  376. add_filter( 'vc_gitem_template_attribute_post_meta_value', 'vc_gitem_template_attribute_post_meta_value', 10, 2 );
  377. add_filter( 'vc_gitem_template_attribute_post_data', 'vc_gitem_template_attribute_post_data', 10, 2 );
  378. add_filter( 'vc_gitem_template_attribute_post_image_background_image_css', 'vc_gitem_template_attribute_post_image_background_image_css', 10, 2 );
  379. add_filter( 'vc_gitem_template_attribute_post_excerpt', 'vc_gitem_template_attribute_post_excerpt', 10, 2 );
  380. add_filter( 'vc_gitem_template_attribute_post_title', 'vc_gitem_template_attribute_post_title', 10, 2 );
  381. add_filter( 'vc_gitem_template_attribute_post_author', 'vc_gitem_template_attribute_post_author', 10, 2 );
  382. add_filter( 'vc_gitem_template_attribute_post_author_href', 'vc_gitem_template_attribute_post_author_href', 10, 2 );
  383. add_filter( 'vc_gitem_template_attribute_post_categories', 'vc_gitem_template_attribute_post_categories', 10, 2 );
  384. add_filter( 'vc_gitem_template_attribute_featured_image', 'vc_gitem_template_attribute_featured_image', 10, 2 );
  385. add_filter( 'vc_gitem_template_attribute_vc_btn', 'vc_gitem_template_attribute_vc_btn', 10, 2 );