PageRenderTime 321ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/auto-repair/vamtam-editor/ajax.php

https://gitlab.com/swapnil2sutar/guruautolines
PHP | 386 lines | 300 code | 85 blank | 1 comment | 54 complexity | ae4cdaf01d9940130f986202cca7ddcb MD5 | raw file
  1. <?php
  2. class WPV_Editor_AJAX {
  3. public function __construct() {
  4. $actions = array( 'markup', 'config', 'get_slide_html', 'init_html' );
  5. foreach ( $actions as $name ) {
  6. add_action( "wp_ajax_wpv_editor_$name", array( &$this, $name ) );
  7. }
  8. $this->special_markup_list = apply_filters( 'wpv_editor_special_markup_list', array( 'accordion', 'tabs', 'services_expandable' ) );
  9. }
  10. public function markup() {
  11. $id = ( isset( $_POST['element'] ) ) ? $_POST['element'] : '';
  12. if ( !empty( $id ) ) {
  13. echo $this->markup_handler( $id ); // xss ok
  14. }
  15. exit;
  16. }
  17. private function get_uniqid_suffix() {
  18. return str_replace( '.', '-', uniqid( '', true ) ).'-'.mt_rand();
  19. }
  20. private function get_uniqid( $id ) {
  21. return 'wpved-'.$id.'-'.$this->get_uniqid_suffix();
  22. }
  23. private function markup_handler( $id, $attributes=null, $content='' ) {
  24. global $wpv_sc;
  25. $accepted = '';
  26. if ( !is_null( $attributes ) ) {
  27. if ( $id == 'column' ) {
  28. $accepted = $content;
  29. $id .= '-'.str_replace( '/', '', $attributes['width'] );
  30. } else {
  31. if ( isset( $wpv_sc[$id]["accepting"] ) && $wpv_sc[$id]["accepting"] ) {
  32. $accepted = $content;
  33. } else {
  34. if ( in_array( $id, $this->special_markup_list ) ) {
  35. $accepted = $this->get_special_markup( $id, $content );
  36. } else {
  37. $attributes['html-content'] = $content;
  38. }
  39. }
  40. }
  41. }
  42. ob_start();
  43. if ( strpos( $id, 'column' ) === 0 ) :
  44. ?>
  45. <div id="<?php echo esc_attr( $this->get_uniqid( $id ) ) ?>" class="wpv_ed_column wpv_sortable inner-sortable column column-<?php echo esc_attr( preg_replace( '/column-(\d)(\d)/', '$1-$2', $id ) ) ?> expandable expanded" data-basename="column">
  46. <?php echo $this->getControls( 'size name add edit clone delete handle', preg_replace( '/column-(\d)(\d)/', '$1/$2', $id ), __( 'Column', 'auto-repair' ), $attributes ) // xss ok ?>
  47. <?php echo $this->loopShortcodeParameters( 'column', $attributes ) // xss ok ?>
  48. <?php echo $accepted // xss ok ?>
  49. </div>
  50. <?php else : ?>
  51. <?php
  52. $controls = isset( $wpv_sc[$id]['controls'] ) ? $wpv_sc[$id]['controls'] : 'name delete';
  53. $class = isset( $wpv_sc[$id]["class"] ) ? $wpv_sc[$id]["class"] : '';
  54. $accepting = isset( $wpv_sc[$id]["accepting"] ) && $wpv_sc[$id]["accepting"] ? 'inner-sortable expanded' : '';
  55. $size = isset( $attributes['column_width'] ) ? $attributes['column_width'] : '1/1';
  56. $size_class = 'column-'.str_replace( '/', '-', $size );
  57. $expandable = strpos( $controls, 'handle' ) !== false ? 'expandable' : 'non-expandable';
  58. if ( strpos( $controls, 'always-expanded' ) )
  59. $expandable = 'expandable expanded';
  60. ?>
  61. <div id="<?php echo esc_attr( $this->get_uniqid( $id ) ) ?>" class="wpved-<?php echo esc_attr( "$id $accepting $class" ) ?> wpv_sortable column <?php echo esc_attr( "$size_class $expandable" ) ?>" <?php echo $this->getCallbacks( $id ) ?> data-basename="<?php echo esc_attr( $id ) ?>">
  62. <?php echo $this->getControls( $controls, $size, $wpv_sc[$id]['name'], $attributes ) // xss ok ?>
  63. <?php echo $this->loopShortcodeParameters( $id, $attributes ) // xss ok ?>
  64. <?php echo $accepted // xss ok ?>
  65. </div>
  66. <?php
  67. endif;
  68. return ob_get_clean();
  69. }
  70. private function getControls( $controls, $size_str, $name='', $atts = array() ) {
  71. $output = '<div class="controls">';
  72. $orig_name = $name;
  73. if ( isset( $atts['column_title'] ) ) {
  74. if ( !empty( $atts['column_title'] ) && $atts['column_title'] !== 'undefined' ) {
  75. $name = $atts['column_title'];
  76. }
  77. } elseif ( isset( $atts['title'] ) && ! empty( $atts['title'] ) && $atts['title'] !== 'undefined' ) {
  78. $name = $atts['title'];
  79. } elseif ( isset( $atts['name'] ) && ! empty( $atts['name'] ) && $atts['name'] !== 'undefined' ) {
  80. $name = $atts['name'];
  81. }
  82. $name = strip_tags( $name );
  83. $controls_html = array(
  84. 'name' => "<span class='column-name' data-orig-title='" . esc_attr( $orig_name ) . "'>$name</span>",
  85. 'size' => "<div class='column-size-wrapper'>
  86. <div class='column-increase-decrease'><a class='column-increase icon-plus' href='#' title='" . esc_attr__( 'Increase width', 'auto-repair' ) . "'></a> <a class='column-decrease icon-minus' href='#' title='" . esc_attr__( 'Decrease width', 'auto-repair' ) . "'></a></div>
  87. <span class='column-size'>$size_str</span>
  88. </div>",
  89. // 'add' => " <a class='column-add icon-plus' href='#' title='".__( 'Insert element', 'auto-repair' )."'></a> ",
  90. 'clone' => " <a class='column-clone icon-copy' href='#' title='" . esc_attr__( 'Clone', 'auto-repair' )."'></a> ",
  91. 'edit' => ' <a class="column-edit icon-edit" href="#" title="' . esc_attr__( 'Edit shortcode properties', 'auto-repair' ).'"></a>',
  92. 'delete' => ' <a class="column-remove icon-remove" href="#" title="' . esc_attr__( 'Remove shortcode', 'auto-repair' ).'"></a>',
  93. 'handle' => '<div class="handlediv" title="' . esc_attr__( 'Click to toggle', 'auto-repair' ).'"><br /></div>',
  94. );
  95. if ( $controls == 'full' )
  96. $controls = 'size edit delete';
  97. $controls = explode( ' ', $controls );
  98. foreach ( $controls as $c ) {
  99. if ( isset( $controls_html[$c] ) )
  100. $output .= $controls_html[$c];
  101. }
  102. $output .= '</div>';
  103. return $output;
  104. }
  105. private function getCallbacks( $id ) {
  106. global $wpv_sc;
  107. $output = '';
  108. if ( isset( $wpv_sc[$id]['callbacks'] ) )
  109. $output = esc_attr( json_encode( $wpv_sc[$id]['callbacks'] ) );
  110. return "data-callbacks='$output'";
  111. }
  112. private function loopShortcodeParameters( $id, $attributes ) {
  113. global $wpv_sc;
  114. $output = '';
  115. if ( isset( $wpv_sc[$id]['options'] ) ) {
  116. foreach ( $wpv_sc[$id]['options'] as $param ) {
  117. if ( $param['type'] == 'select-row' ) {
  118. foreach ( $param['selects'] as $sid=>$s ) {
  119. $value = isset( $attributes[$sid] ) ? $attributes[$sid] : null;
  120. $s['type'] = 'select';
  121. $s['id'] = $sid;
  122. $output .= $this->formatParam( $s, $value );
  123. }
  124. } elseif ( $param['type'] == 'range-row' ) {
  125. foreach ( $param['ranges'] as $sid=>$s ) {
  126. $value = isset( $attributes[$sid] ) ? $attributes[$sid] : null;
  127. $s['type'] = 'range';
  128. $s['id'] = $sid;
  129. $output .= $this->formatParam( $s, $value );
  130. }
  131. } elseif ( $param['type'] == 'color-row' ) {
  132. foreach ( $param['inputs'] as $sid=>$s ) {
  133. $value = isset( $attributes[$sid] ) ? $attributes[$sid] : null;
  134. $s['type'] = 'color';
  135. $s['id'] = $sid;
  136. $output .= $this->formatParam( $s, $value );
  137. }
  138. } elseif ( $param['type'] == 'background' ) {
  139. $opts = explode( ',', $param['only'] );
  140. $opt_types = array(
  141. 'color' => 'color',
  142. 'opacity' => 'range',
  143. 'image' => 'upload',
  144. 'repeat' => 'select',
  145. 'attachment' => 'select',
  146. 'position' => 'select',
  147. 'size' => 'toggle',
  148. );
  149. foreach ( $opts as $opt_name ) {
  150. $oid = $param['id'].'_'.$opt_name;
  151. $value = isset( $attributes[$oid] ) ? $attributes[$oid] : null;
  152. $default = ( $opt_name == 'size' ) ? 'auto' : '';
  153. $output .= $this->formatParam( array(
  154. 'id' => $oid,
  155. 'type' => $opt_types[$opt_name],
  156. 'default' => $default,
  157. ), $value );
  158. }
  159. } elseif ( isset( $param['id'] ) ) {
  160. $value = isset( $attributes[$param['id']] ) ? $attributes[$param['id']] : null;
  161. $output .= $this->formatParam( $param, $value );
  162. }
  163. }
  164. }
  165. return $output;
  166. }
  167. private function formatParam( $param, $value ) {
  168. extract( $param );
  169. if ( is_null( $value ) )
  170. $value = isset( $default ) ? $default : '';
  171. $class = isset( $class ) ? $class : '';
  172. $placeholder = isset( $placeholder ) ? $placeholder : ( isset( $name ) ? $name : '' );
  173. if ( is_array( $value ) )
  174. $value = json_encode( $value );
  175. if ( isset( $type ) ) {
  176. $attr = "class='wpv-ed-param-holder $id $type $class' name='$id'";
  177. if ( !isset( $holder ) || $holder == 'hidden' )
  178. return "<input type='hidden' $attr value='".esc_attr( $value )."' />";
  179. if ( $holder == 'img' )
  180. return "<img src='".esc_attr( $value )."' $attr placeholder='$placeholder' />";
  181. if ( $holder != 'textarea' )
  182. $value = wpautop( $value );
  183. return "<$holder $attr placeholder='$placeholder'>$value</$holder>";
  184. }
  185. }
  186. private function get_special_markup( $id, $content ) {
  187. $result = apply_filters( 'wpv_editor_get_special_markup', $content, $id );
  188. if ( $result !== $content )
  189. return $result;
  190. ob_start();
  191. switch ( $id ) {
  192. case 'accordion':
  193. if ( !wpv_sub_shortcode( 'pane', $content, $params, $sub_contents ) )
  194. return $content;
  195. echo '<div class="wpv_accordion">';
  196. foreach ( $sub_contents as $i=>$text ) : ?>
  197. <div>
  198. <h3 class="title-wrapper clearfix">
  199. <a class="accordion-title"><?php echo $params[$i]['title'] // xss ok ?></a>
  200. <a class="accordion-remove icon-remove" title="<?php esc_attr_e( 'Remove', 'auto-repair' ) ?>"></a>
  201. <a class="accordion-clone icon-copy" title="<?php esc_attr_e( 'Clone', 'auto-repair' ) ?>"></a>
  202. <a class="accordion-background-selector" data-background-image="<?php if ( isset( $params[$i]['background_image'] ) ) echo esc_attr( $params[$i]['background_image'] ) ?>" title="<?php esc_attr_e( 'Change Pane Background', 'auto-repair' ) ?>"><?php wpv_icon( 'image' ) ?></a>
  203. </h3>
  204. <div class="pane clearfix inner-sortable"><?php echo $this->do_parse( $text ) // xss ok ?></div>
  205. </div>
  206. <?php endforeach;
  207. echo '<div><h3><a class="accordion-add icon-plus"></a></h3></div>';
  208. echo '</div>';
  209. break;
  210. case 'tabs':
  211. if ( !wpv_sub_shortcode( 'tab', $content, $params, $sub_contents ) )
  212. return $content;
  213. $suffix = 'tabs-' . $this->get_uniqid_suffix();
  214. echo '<div class="wpv_tabs"><ul>';
  215. foreach ( $params as $i=>$pi ) :
  216. $p = shortcode_atts( array(
  217. 'title' => '',
  218. 'icon' => '',
  219. ), $pi );
  220. ?>
  221. <li>
  222. <a href="#tabs-<?php echo esc_attr( $suffix . $i ) ?>" class="tab-title"><?php echo $p['title'] // xss ok ?></a>
  223. <a class="tab-remove icon-remove" title="<?php esc_attr_e( 'Remove', 'auto-repair' ) ?>"></a>
  224. <a class="tab-clone icon-copy" title="<?php esc_attr_e( 'Clone', 'auto-repair' ) ?>"></a>
  225. <a class="wpv-icon-selector-trigger tab-icon-selector vamtam-icon <?php echo esc_attr( wpv_get_icon_type( $p['icon'] ) ) ?> <?php if ( empty( $p['icon'] ) ) echo 'no-icon' ?>" data-icon-name="<?php echo esc_attr( $p['icon'] ) ?>" title="<?php esc_attr_e( 'Change Icon', 'auto-repair' ) ?>"><?php wpv_icon( $p['icon'] ) ?></a>
  226. </li>
  227. <?php endforeach;
  228. echo '<li class="ui-state-default"><a class="tab-add icon-plus"></a></li>';
  229. echo '</ul>';
  230. foreach ( $sub_contents as $i=>$text ) : ?>
  231. <div id="tabs-<?php echo $suffix.$i ?>" class="clearfix inner-sortable"><?php echo $this->do_parse( $text ) // xss ok ?></div>
  232. <?php endforeach;
  233. echo '</div>';
  234. break;
  235. case 'services_expandable':
  236. echo "<textarea class='inner-content'>$content</textarea>"; // xss ok
  237. break;
  238. default:
  239. echo $content; // xss ok
  240. }
  241. return ob_get_clean();
  242. }
  243. public function config() {
  244. global $wpv_sc;
  245. include_once 'config-generator.php';
  246. $id = ( isset( $_POST['element'] ) ) ? $_POST['element'] : '';
  247. WPV_Editor_Shortcode_Config::setConfig( $wpv_sc[$id] )->render();
  248. exit;
  249. }
  250. public function get_slide_html() {
  251. $id = $this->get_uniqid_suffix();
  252. $value = array( 'static' => false );
  253. include WPV_ADMIN_HELPERS . 'config_generator/slide.php';
  254. exit;
  255. }
  256. public function init_html() {
  257. echo $this->do_parse( $_POST['content'] ); // xss ok
  258. exit;
  259. }
  260. private function do_parse( $content ) {
  261. global $wpv_sc;
  262. require_once 'parser.php';
  263. $content = stripslashes( $content );
  264. $content = wpv_fix_shortcodes( $content );
  265. try {
  266. $parser = new WPV_Editor_Parser( $content, $wpv_sc );
  267. $tree = $parser->parse();
  268. return $this->html_from_tree_node( $tree );
  269. } catch ( Exception $e ) {
  270. return '<span style="font: 14px / 18px sans-serif;">'.$e->getMessage().'</span>';
  271. }
  272. }
  273. private function html_from_tree_node( $tree ) {
  274. global $wpv_sc;
  275. $result = '';
  276. $column_atts = array( 'width', 'title', 'divider', 'title_type', 'animation' );
  277. foreach ( $tree->children as $node ) {
  278. $content = $node->content;
  279. $implicit_column = ( $node->type === 'column' &&
  280. ( isset( $node->atts['implicit'] ) && $node->atts['implicit'] === 'true' ) &&
  281. count( $node->children ) <= 1 );
  282. if ( $node->type == 'ROOT' ||
  283. $node->type === 'column' ||
  284. ( isset( $wpv_sc[$node->type]['accepting'] ) && $wpv_sc[$node->type]['accepting'] )
  285. ) {
  286. if ( $implicit_column ) {
  287. foreach ( $node->children as $inner_node ) {
  288. foreach ( $column_atts as $catt ) {
  289. if ( isset( $node->atts[$catt] ) )
  290. $inner_node->atts['column_'.$catt] = $node->atts[$catt];
  291. }
  292. }
  293. }
  294. $content = $this->html_from_tree_node( $node );
  295. }
  296. $result .= $implicit_column ? $content : $this->markup_handler( $node->type, $node->atts, $content );
  297. }
  298. return $result;
  299. }
  300. };
  301. new WPV_Editor_AJAX();