PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Site/wp-content/plugins/js_composer/include/params/vc_grid_item/class-vc-grid-item.php

https://gitlab.com/rubengrb/ws.vidrialum
PHP | 363 lines | 184 code | 39 blank | 140 comment | 15 complexity | 03838c553a624f8ee815e7505adac36d MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * Class Vc_Grid_Item to build grid item.
  7. */
  8. class Vc_Grid_Item {
  9. protected $template = '';
  10. protected $html_template = false;
  11. protected $post = false;
  12. protected $grid_atts = array();
  13. protected $is_end = false;
  14. protected static $templates_added = false;
  15. protected $shortcodes = false;
  16. protected $found_variables = false;
  17. protected static $predefined_templates = false;
  18. protected $template_id = false;
  19. protected static $custom_fields_meta_data = false;
  20. /**
  21. * Get shortcodes to build vc grid item templates.
  22. *
  23. * @return bool|mixed|void
  24. */
  25. public function shortcodes() {
  26. if ( false === $this->shortcodes ) {
  27. $this->shortcodes = include vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/shortcodes.php' );
  28. $this->shortcodes = apply_filters( 'vc_grid_item_shortcodes', $this->shortcodes );
  29. }
  30. add_filter( 'vc_shortcode_set_template_vc_icon', array( $this, 'addVcIconShortcodesTemplates' ) );
  31. add_filter( 'vc_shortcode_set_template_vc_button2', array( $this, 'addVcButton2ShortcodesTemplates' ) );
  32. add_filter( 'vc_shortcode_set_template_vc_single_image', array(
  33. $this,
  34. 'addVcSingleImageShortcodesTemplates',
  35. ) );
  36. add_filter( 'vc_shortcode_set_template_vc_custom_heading', array(
  37. $this,
  38. 'addVcCustomHeadingShortcodesTemplates',
  39. ) );
  40. add_filter( 'vc_shortcode_set_template_vc_btn', array( $this, 'addVcBtnShortcodesTemplates' ) );
  41. return $this->shortcodes;
  42. }
  43. /**
  44. * Used by filter vc_shortcode_set_template_vc_icon to set custom template for vc_icon shortcode.
  45. *
  46. * @param $template
  47. *
  48. * @return string
  49. */
  50. public function addVcIconShortcodesTemplates( $template ) {
  51. $file = vc_path_dir( 'TEMPLATES_DIR', 'params/vc_grid_item/shortcodes/vc_icon.php' );
  52. if ( is_file( $file ) ) {
  53. return $file;
  54. }
  55. return $template;
  56. }
  57. /**
  58. * Used by filter vc_shortcode_set_template_vc_button2 to set custom template for vc_button2 shortcode.
  59. *
  60. * @param $template
  61. *
  62. * @return string
  63. */
  64. public function addVcButton2ShortcodesTemplates( $template ) {
  65. $file = vc_path_dir( 'TEMPLATES_DIR', 'params/vc_grid_item/shortcodes/vc_button2.php' );
  66. if ( is_file( $file ) ) {
  67. return $file;
  68. }
  69. return $template;
  70. }
  71. /**
  72. * Used by filter vc_shortcode_set_template_vc_single_image to set custom template for vc_single_image shortcode.
  73. *
  74. * @param $template
  75. *
  76. * @return string
  77. */
  78. public function addVcSingleImageShortcodesTemplates( $template ) {
  79. $file = vc_path_dir( 'TEMPLATES_DIR', 'params/vc_grid_item/shortcodes/vc_single_image.php' );
  80. if ( is_file( $file ) ) {
  81. return $file;
  82. }
  83. return $template;
  84. }
  85. /**
  86. * Used by filter vc_shortcode_set_template_vc_custom_heading to set custom template for vc_custom_heading
  87. * shortcode.
  88. *
  89. * @param $template
  90. *
  91. * @return string
  92. */
  93. public function addVcCustomHeadingShortcodesTemplates( $template ) {
  94. $file = vc_path_dir( 'TEMPLATES_DIR', 'params/vc_grid_item/shortcodes/vc_custom_heading.php' );
  95. if ( is_file( $file ) ) {
  96. return $file;
  97. }
  98. return $template;
  99. }
  100. /**
  101. * Used by filter vc_shortcode_set_template_vc_button2 to set custom template for vc_button2 shortcode.
  102. *
  103. * @param $template
  104. *
  105. * @return string
  106. */
  107. public function addVcBtnShortcodesTemplates( $template ) {
  108. $file = vc_path_dir( 'TEMPLATES_DIR', 'params/vc_grid_item/shortcodes/vc_btn.php' );
  109. if ( is_file( $file ) ) {
  110. return $file;
  111. }
  112. return $template;
  113. }
  114. /**
  115. * Map shortcodes for vc_grid_item param type.
  116. */
  117. public function mapShortcodes() {
  118. // @kludge
  119. // TODO: refactor with with new way of roles for shortcodes.
  120. // NEW ROLES like post_type for shortcode and access policies.
  121. $shortcodes = $this->shortcodes();
  122. foreach ( $shortcodes as $shortcode_settings ) {
  123. vc_map( $shortcode_settings );
  124. }
  125. }
  126. /**
  127. * Get list of predefined templates.
  128. *
  129. * @return bool|mixed
  130. */
  131. public static function predefinedTemplates() {
  132. if ( false === self::$predefined_templates ) {
  133. self::$predefined_templates = apply_filters( 'vc_grid_item_predefined_templates',
  134. include vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/templates.php' ) );
  135. }
  136. return self::$predefined_templates;
  137. }
  138. /**
  139. * @param $id - Predefined templates id
  140. *
  141. * @return array|bool
  142. */
  143. public static function predefinedTemplate( $id ) {
  144. $predefined_templates = self::predefinedTemplates();
  145. if ( isset( $predefined_templates[ $id ]['template'] ) ) {
  146. return $predefined_templates[ $id ];
  147. }
  148. return false;
  149. }
  150. /**
  151. * Set template which should grid used when vc_grid_item param value is rendered.
  152. *
  153. * @param $id
  154. *
  155. * @return bool
  156. */
  157. public function setTemplateById( $id ) {
  158. require_once vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/templates.php' );
  159. if ( 0 === strlen( $id ) ) {
  160. return false;
  161. }
  162. if ( preg_match( '/^\d+$/', $id ) ) {
  163. $post = get_post( (int) $id );
  164. $post && $this->setTemplate( $post->post_content, $post->ID );
  165. return true;
  166. } elseif ( false !== ( $predefined_template = $this->predefinedTemplate( $id ) ) ) {
  167. $this->setTemplate( $predefined_template['template'], $id );
  168. return true;
  169. }
  170. return false;
  171. }
  172. /**
  173. * Setter for template attribute.
  174. *
  175. * @param $template
  176. * @param $template_id
  177. */
  178. public function setTemplate( $template, $template_id ) {
  179. $this->template = $template;
  180. $this->template_id = $template_id;
  181. $this->parseTemplate( $template );
  182. }
  183. /**
  184. * Getter for template attribute.
  185. * @return string
  186. */
  187. function template() {
  188. return $this->template;
  189. }
  190. /**
  191. * Add custom css from shortcodes that were mapped for vc grid item.
  192. * @return string
  193. */
  194. public function addShortcodesCustomCss() {
  195. $output = $shortcodes_custom_css = '';
  196. $id = $this->template_id;
  197. if ( preg_match( '/^\d+$/', $id ) ) {
  198. $shortcodes_custom_css = get_post_meta( $id, '_wpb_shortcodes_custom_css', true );
  199. } elseif ( false !== ( $predefined_template = $this->predefinedTemplate( $id ) ) ) {
  200. $shortcodes_custom_css = visual_composer()->parseShortcodesCustomCss( $predefined_template['template'] );
  201. }
  202. if ( ! empty( $shortcodes_custom_css ) ) {
  203. $shortcodes_custom_css = strip_tags( $shortcodes_custom_css );
  204. $output .= '<style type="text/css" data-type="vc_shortcodes-custom-css">';
  205. $output .= $shortcodes_custom_css;
  206. $output .= '</style>';
  207. }
  208. return $output;
  209. }
  210. /**
  211. * Generates html with template's variables for rendering new project.
  212. *
  213. * @param $template
  214. */
  215. public function parseTemplate( $template ) {
  216. $this->mapShortcodes();
  217. WPBMap::addAllMappedShortcodes();
  218. $attr = ' width="' . $this->gridAttribute( 'element_width', 12 ) . '"'
  219. . ' is_end="' . ( 'true' === $this->isEnd() ? 'true' : '' ) . '"';
  220. $template = preg_replace( '/(\[(\[?)vc_gitem\b)/', '$1' . $attr, $template );
  221. $this->html_template .= do_shortcode( $template );
  222. }
  223. /**
  224. * Regexp for variables.
  225. * @return string
  226. */
  227. public function templateVariablesRegex() {
  228. return '/\{\{' . '\{?' . '\s*' . '([^\}\:]+)(\:([^\}]+))?' . '\s*' . '\}\}' . '\}?/';
  229. }
  230. /**
  231. * Get default variables.
  232. *
  233. * @return array|bool
  234. */
  235. public function getTemplateVariables() {
  236. if ( ! is_array( $this->found_variables ) ) {
  237. preg_match_all( $this->templateVariablesRegex(), $this->html_template, $this->found_variables, PREG_SET_ORDER );
  238. }
  239. return $this->found_variables;
  240. }
  241. /**
  242. * Render item by replacing template variables for exact post.
  243. *
  244. * @param WP_Post $post
  245. *
  246. * @return mixed
  247. */
  248. function renderItem( WP_Post $post ) {
  249. $pattern = array();
  250. $replacement = array();
  251. $this->addAttributesFilters();
  252. foreach ( $this->getTemplateVariables() as $var ) {
  253. $pattern[] = '/' . preg_quote( $var[0], '/' ) . '/';
  254. $replacement[] = preg_replace( '/\\$/', '\\\$', $this->attribute( $var[1], $post, isset( $var[3] ) ? trim( $var[3] ) : '' ) );
  255. }
  256. return preg_replace( $pattern, $replacement, do_shortcode( $this->html_template ) );
  257. }
  258. /**
  259. * Adds filters to build templates variables values.
  260. */
  261. public function addAttributesFilters() {
  262. require_once vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/attributes.php' );
  263. }
  264. /**
  265. * Getter for Grid shortcode attributes.
  266. *
  267. * @param $grid_atts
  268. */
  269. public function setGridAttributes( $grid_atts ) {
  270. $this->grid_atts = $grid_atts;
  271. }
  272. /**
  273. * Setter for Grid shortcode attributes.
  274. *
  275. * @param $name
  276. * @param string $default
  277. *
  278. * @return string
  279. */
  280. public function gridAttribute( $name, $default = '' ) {
  281. return isset( $this->grid_atts[ $name ] ) ? $this->grid_atts[ $name ] : $default;
  282. }
  283. /**
  284. * Get attribute value for WP_post object.
  285. *
  286. * @param $name
  287. * @param $post
  288. * @param string $data
  289. *
  290. * @return mixed|void
  291. */
  292. public function attribute( $name, $post, $data = '' ) {
  293. $data = html_entity_decode( $data );
  294. return apply_filters( 'vc_gitem_template_attribute_' . trim( $name ),
  295. ( isset( $post->$name ) ? $post->$name : '' ), array(
  296. 'post' => $post,
  297. 'data' => $data,
  298. ) );
  299. }
  300. /**
  301. * Set that this is last items in the grid. Used for load more button and lazy loading.
  302. *
  303. * @param bool $is_end
  304. */
  305. public function setIsEnd( $is_end = true ) {
  306. $this->is_end = $is_end;
  307. }
  308. /**
  309. * Checks is the end.
  310. * @return bool
  311. */
  312. public function isEnd() {
  313. return $this->is_end;
  314. }
  315. /**
  316. * Set elements templates.
  317. */
  318. protected function setShortcodes() {
  319. foreach ( $this->shortcodes() as $tag => $settings ) {
  320. add_shortcode( $tag, array( $this, vc_camel_case( $tag . '_shortcode' ) ) );
  321. }
  322. }
  323. // }}
  324. }