PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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