PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/js_composer/include/params/vc_grid_element/vc_grid_element.php

https://gitlab.com/oxidigitaluser/liguelista
PHP | 274 lines | 234 code | 17 blank | 23 comment | 5 complexity | 686bd84f1bfb99b4ce7fedb7392d8bcf MD5 | raw file
  1. <?php
  2. class Vc_Grid_Element
  3. {
  4. protected $template = '';
  5. protected $html_template = false;
  6. protected $post = false;
  7. protected $attributes = array();
  8. protected $grid_atts = array();
  9. protected $is_end = false;
  10. protected static $templates_added = false;
  11. protected $shortcodes = array(
  12. 'vc_gitem_row',
  13. 'vc_gitem_col',
  14. 'vc_gitem_post_title',
  15. 'vc_gitem_icon'
  16. );
  17. public function shortcodes() {
  18. return $this->shortcodes;
  19. }
  20. function setTemplate($template)
  21. {
  22. $this->template = $template;
  23. $this->parseTemplate($template);
  24. }
  25. function template()
  26. {
  27. return $this->template;
  28. }
  29. public function parseTemplate($template)
  30. {
  31. $this->setShortcodes();
  32. $this->html_template = do_shortcode($template);
  33. }
  34. function renderItem(WP_Post $post)
  35. {
  36. $attributes = $this->attributes();
  37. $pattern = array();
  38. $replacement = array();
  39. foreach ($attributes as $attr) {
  40. $pattern[] = '/\{\{' . preg_quote($attr, '') . '\}\}/';
  41. $replacement[] = $this->attribute($attr, $post);
  42. }
  43. $css_class_items = 'vc_grid-item ' . ($this->isEnd() ? ' vc_grid-last-item ' : '')
  44. . ' vc_grid-thumb vc_theme-thumb-full-overlay vc_animation-slide-left vc_col-sm-'
  45. . $this->gridAttribute('element_width', 12);
  46. foreach ($post->filter_terms as $t) {
  47. $css_class_items .= ' vc_grid-term-' . $t;
  48. }
  49. return '<div class="'
  50. . $css_class_items
  51. . '">' . "\n" . preg_replace($pattern, $replacement, $this->html_template)
  52. . "\n" . '</div>' . "\n";
  53. }
  54. public function renderParam()
  55. {
  56. $output = '<div class="vc_grid-element-constructor" data-vc-grid-element="builder"></div>'
  57. .'<a href="#" data-vc-control="add-row">' . __('Add row', 'js_composer') . '</a>';
  58. if (self::$templates_added === false) {
  59. foreach($this->shortcodes as $tag) {
  60. $method = vc_camel_case($tag . '_template');
  61. if(method_exists($this, $method)) {
  62. $content = $this->$method();
  63. } else {
  64. $content = $this->vcDefaultTemplate($tag);
  65. }
  66. $output .= '<script type="text/template" data-vc-grid-element-template="'
  67. . esc_attr($tag) .'">' . $content . '</script>';
  68. $output .= '<script type="text/template" data-vc-grid-element-template="modal">'
  69. . '<div class="vc_grid-element-modal-title"><# title #></div>'
  70. . '<div class="vc_grid-element-modal-controls"><# controls #></div>'
  71. . '<div class="vc_grid-element-modal-body"><# body #></div>'
  72. . '</script>';
  73. }
  74. self::$templates_added = true;
  75. }
  76. return $output;
  77. }
  78. public function setGridAttributes($grid_atts)
  79. {
  80. $this->grid_atts = $grid_atts;
  81. }
  82. public function gridAttribute($name, $default = '')
  83. {
  84. return isset($this->grid_atts[$name]) ? $this->grid_atts[$name] : $default;
  85. }
  86. public function setAttribute($name)
  87. {
  88. $this->attributes[] = $name;
  89. }
  90. public function attributes()
  91. {
  92. return $this->attributes;
  93. }
  94. public function attribute($name, $post)
  95. {
  96. if (method_exists($this, 'attribute' . ucfirst($name))) {
  97. $method_name = 'attribute' . ucfirst($name);
  98. return $this->$method_name($post);
  99. }
  100. if (isset($post->$name)) return $post->$name;
  101. return '';
  102. }
  103. public function setIsEnd($is_end = true)
  104. {
  105. $this->is_end = $is_end;
  106. }
  107. public function isEnd()
  108. {
  109. return $this->is_end;
  110. }
  111. /**
  112. * Set elements templates.
  113. */
  114. protected function setShortcodes()
  115. {
  116. foreach ($this->shortcodes as $tag) {
  117. add_shortcode($tag, array($this, vc_camel_case($tag . '_shortcode')));
  118. }
  119. }
  120. // Templates {{
  121. public function vcGitemRowShortcode($atts, $content = '')
  122. {
  123. return '<div class="vc_row vc_gitem-row'
  124. . $this->gridAttribute('element_width') . '">'
  125. . "\n" . do_shortcode($content) . "\n" . '</div>';
  126. }
  127. public function vcGitemRowTemplate()
  128. {
  129. $output = '<div class="vc_gitem-wrapper">';
  130. $output .= '<div class="vc_t-grid-controls vc_t-grid-controls-row" data-vc-element-shortcode="controls">';
  131. // Move control
  132. $output .= '<a class="vc_t-grid-control vc_t-grid-control-move" href="#" title="'
  133. .__('Drag row to reorder', 'js_composer').'" data-vc-element-control="move"><i class="vc_t-grid-icon vc_t-grid-icon-move"></i></a>';
  134. // Layout control
  135. $output .= '<span class="vc_t-grid-control vc_t-grid-control-layouts" style="display: none;">'
  136. // vc_col-sm-12
  137. . '<a class="vc_t-grid-control vc_t-grid-control-layout" data-cells="12" title="'
  138. . __('1/1', 'js_composer') . '" data-vc-element-control="layouts">'
  139. .'<i class="vc_t-grid-icon vc_t-grid-icon-layout-12"></i></a>'
  140. // vc_col-sm-6 + vc_col-sm-6
  141. . '<a class="vc_t-grid-control vc_t-grid-control-layout" data-cells="6_6" title="'
  142. . __('1/2 + 1/2', 'js_composer') . '" data-vc-element-control="layouts">'
  143. . '<i class="vc_t-grid-icon vc_t-grid-icon-layout-6-6"></i></a>'
  144. // vc_col-sm-4 + vc_col-sm-4 + vc_col-sm-4
  145. . '<a class="vc_t-grid-control vc_t-grid-control-layout" data-cells="4_4_4" title="'
  146. . __('1/3 + 1/3 + 1/3', 'js_composer') . '" data-vc-element-control="layouts">'
  147. . '<i class="vc_t-grid-icon vc_t-grid-icon-layout-4-4-4"></i></a>'
  148. . '</span>'
  149. . '<span class="vc_pull-right">'
  150. // Destroy control
  151. . '<a class="vc_t-grid-control vc_t-grid-control-destroy" href="#" title="'
  152. . __('Delete this row', 'js_composer') . '" data-vc-element-control="destroy">'
  153. . '<i class="vc_t-grid-icon vc_t-grid-icon-destroy"></i>'
  154. . '</a>'
  155. . '</span>';
  156. $output .= '</div>';
  157. $output .= '<div data-vc-element-shortcode="content" class="vc_row vc_gitem-content"></div>';
  158. $output .= '</div>';
  159. return $output;
  160. }
  161. public function vcGitemColShortcode($atts, $content = '')
  162. {
  163. $width = '12';
  164. $atts = shortcode_atts(array(
  165. 'width' => '12'
  166. ), $atts);
  167. extract($atts);
  168. return '<div class="vc_col-sm-' . $width . ' vc_gitem-col">'
  169. . "\n" . do_shortcode($content) . "\n" . '</div>';
  170. }
  171. public function vcGitemColTemplate()
  172. {
  173. $output = '<div class="vc_gitem-wrapper">';
  174. // Controls
  175. // Control "Add"
  176. $controls = '<a class="vc_t-grid-control vc_t-grid-control-add" href="#" title="'
  177. . __('Prepend to this column', 'js_composer') . '" data-vc-element-control="add">'
  178. . '<i class="vc_t-grid-icon vc_t-grid-icon-add"></i>'
  179. . '</a>';
  180. $output .= '<div class="vc_t-grid-controls vc_t-grid-controls-col" data-vc-element-shortcode="controls">'
  181. . $controls
  182. . '</div>';
  183. // Content
  184. $output .= '<div data-vc-element-shortcode="content" class="vc_gitem-content">'
  185. .'</div>';
  186. /*
  187. $output .= '<div class="vc_t-grid-controls vc_t-grid-controls-col vc_t-grid-controls-bottom">'
  188. .$controls
  189. . '</div>';
  190. */
  191. $output .= '</div>';
  192. return $output;
  193. }
  194. public function vcGitemPostTitleShortcode($atts, $content = '')
  195. {
  196. $atts = shortcode_atts(array(
  197. ), $atts);
  198. extract($atts);
  199. $this->setAttribute('post_title');
  200. return '<h3 data-vc-element-shortcode="content" class="vc_ptitle">{{post_title}}</h3>';
  201. }
  202. public function vcDefaultTemplate($tag) {
  203. $name = preg_replace('/^vc_gitem_/', '', $tag);
  204. $title = ucfirst(preg_replace('/\_/', ' ', $name));
  205. return '<div class="vc_gitem-wrapper">'
  206. . $this->elementControls(
  207. __($title, 'js_composer'),
  208. preg_match( '/^post/', $name) ? 'orange' : 'green'
  209. )
  210. . '</div>';
  211. }
  212. protected function elementControls($title, $theme = null) {
  213. return '<div class="vc_t-grid-controls vc_t-grid-controls-element'
  214. . ( is_string($theme) ? ' vc_th-controls-element-'.$theme : '' )
  215. . '" data-vc-element-shortcode="controls">'
  216. // Move control
  217. . '<a class="vc_t-grid-control vc_t-grid-control-move" href="#" title="'
  218. . __('Drag to reorder', 'js_composer').'" data-vc-element-control="move">'
  219. . '<i class="vc_t-grid-icon vc_t-grid-icon-move"></i>'
  220. .'</a>'
  221. // Label
  222. . '<span class="vc_t-grid-control vc_t-grid-control-name" data-vc-element-control="name">
  223. ' . $title
  224. . '</span>'
  225. // Edit control
  226. . '<a class="vc_t-grid-control vc_t-grid-control-edit" data-vc-element-control="edit">'
  227. . '<i class="vc_t-grid-icon vc_t-grid-icon-edit"></i>'
  228. . '</a>'
  229. // Delete control
  230. . '<a class="vc_t-grid-control vc_t-grid-control-destroy" data-vc-element-control="destroy">'
  231. . '<i class="vc_t-grid-icon vc_t-grid-icon-destroy"></i>'
  232. . '</a>'
  233. . '</div>';
  234. }
  235. // }}
  236. }
  237. function vc_vc_grid_element_form_field($settings, $value)
  238. {
  239. $grid_element = new Vc_Grid_Element();
  240. return '<div data-vc-grid-element="container" data-vc-grid-tags-list="'
  241. . esc_attr( json_encode( $grid_element->shortcodes() ) ) . '">'
  242. . '<input data-vc-grid-element="value" type="hidden" name="' . $settings['param_name']
  243. . '" class="wpb_vc_param_value wpb-textinput '
  244. . $settings['param_name'] . ' ' . $settings['type'] . '_field" '
  245. . ' value="'
  246. . esc_attr($value) . '">'
  247. . $grid_element->renderParam()
  248. . '</div>';
  249. }
  250. function vc_load_vc_grid_element_param() {
  251. vc_add_shortcode_param(
  252. 'vc_grid_element',
  253. 'vc_vc_grid_element_form_field',
  254. vc_asset_url('js/params/vc_grid_element.js')
  255. );
  256. }
  257. add_action('vc_load_default_params', 'vc_load_vc_grid_element_param');