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

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

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