PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wr-pagebuilder/core/helper/layout.php

https://gitlab.com/hunt9310/ras
PHP | 307 lines | 179 code | 41 blank | 87 comment | 35 complexity | 975861a93e4d1b91d6a12e9cde091999 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package WR PageBuilder
  5. * @author WooRockets Team <support@www.woorockets.com>
  6. * @copyright Copyright (C) 2012 www.woorockets.com. All Rights Reserved.
  7. * @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
  8. *
  9. * Websites: http://www.www.woorockets.com
  10. * Technical Support: Feedback - http://www.www.woorockets.com
  11. */
  12. /**
  13. * @todo : Related page template functions
  14. */
  15. if ( ! class_exists( 'WR_Pb_Helper_Layout' ) ) {
  16. class WR_Pb_Helper_Layout {
  17. /**
  18. * Save premade layouts file
  19. *
  20. * @param type $layout_name
  21. * @param type $layout_content
  22. */
  23. static function save_premade_layouts( $layout_name, $layout_content ) {
  24. $error = 0;
  25. $upload_dir = WR_Pb_Helper_Functions::get_wp_upload_folder( '/wr-pb-layout/' . WR_PAGEBUILDER_USER_LAYOUT );
  26. $layout_name = preg_replace( '/([\[\]\\/\:\*\?"<>|])*/', '', $layout_name );
  27. $file_name = sanitize_title( $layout_name );
  28. $file = $upload_dir . '/layout-' . $file_name . '.tpl';
  29. // if layout name is existed, show error
  30. if ( file_exists( $file ) ) {
  31. $error = 1;
  32. } else {
  33. // create file & store layout information
  34. $fp = fopen( $file, 'w' );
  35. fwrite( $fp, '[wr_layout name="' . $layout_name . '"]' );
  36. fwrite( $fp, $layout_content );
  37. fclose( $fp );
  38. }
  39. return $error;
  40. }
  41. /**
  42. * Get name of premade layouts file
  43. */
  44. static function get_premade_layouts() {
  45. $path = WR_Pb_Helper_Functions::get_wp_upload_folder( '/wr-pb-layout/' );
  46. $upload_dir = array();
  47. while ( $d = glob( $path . '/*', GLOB_ONLYDIR ) ) {
  48. $path .= '/*';
  49. foreach ( $d as $adir ) {
  50. $upload_dir[] = $adir;
  51. }
  52. }
  53. $files = $providers = array();
  54. $dirs = $upload_dir;
  55. foreach ( $dirs as $dir ) {
  56. $provider_id = self::get_provider_info( $dir, 'id' );
  57. // providerid - provider names
  58. $providers[$provider_id] = self::get_provider_info( $dir );
  59. // providerid - layouts
  60. foreach ( glob( $dir . '/*.tpl' ) as $filename ) {
  61. if ( ! isset ( $files[$provider_id] ) ) {
  62. $files[$provider_id] = array();
  63. }
  64. $files[$provider_id][basename( $filename )] = $filename;
  65. }
  66. }
  67. return array( 'providers' => $providers, 'files' => $files );
  68. }
  69. /**
  70. * Get uri from dir path
  71. *
  72. * @param type $dir
  73. * @param type $file
  74. *
  75. * @return type
  76. */
  77. static function get_uri( $dir, $file ) {
  78. if ( $dir == WR_PB_PREMADE_LAYOUT ) {
  79. $uri = WR_PB_PREMADE_LAYOUT_URI;
  80. } else {
  81. $path_parts = pathinfo( $dir );
  82. $uri = WR_Pb_Helper_Functions::get_wp_upload_url( '/wr-pb-layout/' ) . $path_parts['basename'];
  83. }
  84. return "$uri/$file";
  85. }
  86. /**
  87. * Get content of premade layouts file, prinrt as template
  88. */
  89. static function print_premade_layouts() {
  90. $files = self::get_premade_layouts();
  91. foreach ( $files as $provider => $layouts ) {
  92. foreach ( $layouts as $name => $path ) {
  93. $content = self::extract_layout_data( $path, 'content' );
  94. echo balanceTags( "<script type='text/html' id='tmpl-layout-$name'>\n$content\n</script>\n" );
  95. }
  96. }
  97. }
  98. /**
  99. * Read file line by line
  100. *
  101. * @param type $path
  102. *
  103. * @return type
  104. */
  105. static function extract_layout_data( $path, $data ) {
  106. $fp = @fopen( $path, 'r' );
  107. if ( $fp ) {
  108. $contents = fread( $fp, filesize( $path ) );
  109. $pattern = '/\[wr_layout\s+([A-Za-z0-9_-]+=\"[^"\']*\"\s*)*\s*\]/';
  110. fclose( $fp );
  111. if ( in_array( $data, array( 'name', 'description', 'title' ) ) ) {
  112. return self::extract_data_from_shortcode( $pattern, $contents, $data );
  113. } else {
  114. if ( $data == 'content' ) {
  115. return preg_replace( $pattern, '', $contents );
  116. }
  117. }
  118. }
  119. }
  120. /**
  121. * Extract shortcode param from content
  122. *
  123. * @param type $pattern
  124. * @param type $contents
  125. * @param type $data
  126. *
  127. * @return type
  128. */
  129. static function extract_data_from_shortcode( $pattern, $contents, $data ) {
  130. preg_match( $pattern, $contents, $matches );
  131. $layout_info = isset ( $matches[0] ) ? $matches[0] : '';
  132. $params = array();
  133. preg_match_all( '/[A-Za-z0-9_-]+=\"[^"\']*\"/u', $layout_info, $tmp_params, PREG_PATTERN_ORDER );
  134. foreach ( $tmp_params[0] as $param_value ) {
  135. $output = array();
  136. preg_match_all( '/([A-Za-z0-9_-]+)=\"([^"\']*)\"/u', $param_value, $output, PREG_SET_ORDER );
  137. foreach ( $output as $item ) {
  138. $params[$item[1]] = urldecode( $item[2] );
  139. }
  140. }
  141. return isset ( $params[$data] ) ? $params[$data] : '';
  142. }
  143. /**
  144. * Get provider id of layout folder: Search for provider.info file to get provider name
  145. *
  146. * @param type $dir
  147. */
  148. static function get_provider_info( $dir, $info = 'name' ) {
  149. if ( $info == 'id' ) {
  150. $path_parts = pathinfo( $dir );
  151. return ( ( $dir == WR_PB_PREMADE_LAYOUT ) ? 'wr_pb' : $path_parts['basename'] );
  152. }
  153. if ( $dir == WR_PB_PREMADE_LAYOUT ) {
  154. return __( 'WR Templates', WR_PBL );
  155. }
  156. // Get provider info from xml file
  157. $path = $dir . '/provider.xml';
  158. if ( file_exists( $path ) ) {
  159. $dom_object = new DOMDocument();
  160. if ( $dom_object->load( $path ) ) {
  161. $node = $dom_object->getElementsByTagName( $info );
  162. if ( $node ) {
  163. return $node->item( 0 )->nodeValue;
  164. }
  165. }
  166. }
  167. return __( 'Your Templates', WR_PBL );
  168. }
  169. /**
  170. * Import layout from folder
  171. *
  172. * @param type $file
  173. */
  174. static function import( $dir ) {
  175. $provider_name = self::get_provider_info( $dir, 'name' );
  176. $folder_to_create = ( $provider_name == __( 'Your Templates', WR_PBL ) ) ? WR_PAGEBUILDER_USER_LAYOUT : sanitize_title( $provider_name );
  177. $new_dir = WR_Pb_Helper_Functions::get_wp_upload_folder( '/wr-pb-layout/' . $folder_to_create, false );
  178. // if this is new provider, rename tmp folder to provider name
  179. if ( ! is_dir( $new_dir ) ) {
  180. return rename( $dir, $new_dir );
  181. } // move templates file & thumbnail to existed folder of provider
  182. else {
  183. foreach ( glob( $dir . '/*.*' ) as $filename ) {
  184. $path_parts = pathinfo( $filename );
  185. $name = $path_parts['basename'];
  186. $ext = $path_parts['extension'];
  187. // only copy image & template file
  188. if ( in_array( strtolower( $ext ), array( 'png', 'gif', 'jpg', 'jpeg', 'tpl', 'xml' ) ) ) {
  189. copy( $filename, "$new_dir/$name" );
  190. }
  191. }
  192. return true;
  193. }
  194. return false;
  195. }
  196. /**
  197. * Remove group layout
  198. *
  199. * @param type $group
  200. */
  201. static function remove_group( $group ) {
  202. $group = substr( $group, 0, - 7 );
  203. $dir = WR_Pb_Helper_Functions::get_wp_upload_folder( '/wr-pb-layout/' . $group, false );
  204. if ( is_dir( $dir ) ) {
  205. WR_Pb_Utils_Common::recursive_delete( $dir );
  206. // if directory still exits, false
  207. if ( is_dir( $dir ) ) {
  208. return false;
  209. }
  210. return true;
  211. }
  212. return false;
  213. }
  214. /**
  215. * Check if file with custom extension exists
  216. *
  217. * @param type $dir
  218. * @param type $layout_name
  219. *
  220. * @return type
  221. */
  222. static function check_ext_exist( $dir, $layout_name ) {
  223. $images_ext = array( 'png', 'gif', 'jpg', 'jpeg' );
  224. $got_ext = '';
  225. foreach ( $images_ext as $ext ) {
  226. if ( file_exists( $dir . "/$layout_name.$ext" ) ) {
  227. $got_ext = $ext;
  228. } else if ( file_exists( $dir . "/$layout_name." . strtoupper( $ext ) ) ) {
  229. $got_ext = strtoupper( $ext );
  230. }
  231. }
  232. return $got_ext;
  233. }
  234. /**
  235. * Remove group in layout
  236. *
  237. * @param type $group
  238. * @param type $layout
  239. */
  240. static function remove_layout( $group, $layout ) {
  241. $layout_name = str_replace( '.tpl', '', $layout );
  242. $dir = WR_Pb_Helper_Functions::get_wp_upload_folder( '/wr-pb-layout/' . $group, false );
  243. $deleted = array();
  244. if ( is_dir( $dir ) ) {
  245. // remove .tpl file
  246. $layout_file = $dir . "/$layout_name.tpl";
  247. if ( file_exists( $layout_file ) ) {
  248. $deleted[] = unlink( $layout_file ) ? 1 : 0;
  249. }
  250. $thumbnail = "$dir/$layout_name.png";
  251. $got_ext = self::check_ext_exist( $dir, $layout_name );
  252. if ( ! empty( $got_ext ) ) {
  253. $thumbnail = "$dir/$layout_name.$got_ext";
  254. if ( file_exists( $thumbnail ) ) {
  255. $deleted[] = unlink( $thumbnail ) ? 1 : 0;
  256. }
  257. }
  258. if ( in_array( 0, $deleted ) ) {
  259. return false;
  260. }
  261. return true;
  262. }
  263. return false;
  264. }
  265. }
  266. }