PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-lister-for-ebay/classes/model/TemplatesModel.php

https://bitbucket.org/sanders_nick/my-maxi-skirt
PHP | 491 lines | 301 code | 124 blank | 66 comment | 36 complexity | f2e4e5f80cd835e333298dd2e042c77f MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-1.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. class TemplatesModel extends WPL_Model {
  3. public $foldername;
  4. public $folderpath;
  5. public $stylesheet;
  6. function TemplatesModel( $foldername = false )
  7. {
  8. global $wpl_logger;
  9. $this->logger = &$wpl_logger;
  10. if ( $foldername ) {
  11. // folder name
  12. $foldername = basename($foldername);
  13. $this->foldername = $foldername;
  14. // full absolute paths
  15. $upload_dir = wp_upload_dir();
  16. $this->folderpath = $upload_dir['basedir'] . '/wp-lister/templates/' . $foldername;
  17. $this->stylesheet = $this->folderpath . '/style.css';
  18. // save / return item (?)
  19. $this->item = $this->getItem( $foldername );
  20. return $this->item;
  21. }
  22. }
  23. function getAll() {
  24. // get user templates
  25. $upload_dir = wp_upload_dir();
  26. // if there is a problem with the uploads folder, wp might return an error
  27. if ( $upload_dir['error'] ) {
  28. $this->showMessage( $upload_dir['error'], 1, true );
  29. return array();
  30. }
  31. $templates = array();
  32. $files = glob( $upload_dir['basedir'].'/wp-lister/templates/*/template.html' );
  33. foreach ($files as $file) {
  34. // save template path relative to WP_CONTENT_DIR
  35. // $file = str_replace(WP_CONTENT_DIR,'',$file);
  36. $file = basename(dirname( $file ));
  37. $templates[] = $this->getItem( $file );
  38. }
  39. return $templates;
  40. }
  41. function getBuiltIn() {
  42. // get build in templates
  43. $files = glob( WPLISTER_PATH . '/templates/*/template.html' );
  44. $templates = array();
  45. foreach ($files as $file) {
  46. // save template path relative to WP_CONTENT_DIR
  47. $file = str_replace(WP_CONTENT_DIR,'',$file);
  48. $templates[] = $this->getItem( $file, false, 'built-in' );
  49. }
  50. return $templates;
  51. }
  52. function getItem( $foldername = false, $fullpath = false, $type = 'user' ) {
  53. // set templates root folder
  54. $upload_dir = wp_upload_dir();
  55. $templates_dir = $upload_dir['basedir'].'/wp-lister/templates/';
  56. if ( $fullpath ) {
  57. // do nothing
  58. } elseif ( $foldername ) {
  59. $fullpath = $templates_dir . $foldername;
  60. } else {
  61. $fullpath = $this->folderpath;
  62. }
  63. // build item
  64. $item = array();
  65. // default template name
  66. $item['template_name'] = basename($fullpath);
  67. $item['template_path'] = str_replace(WP_CONTENT_DIR,'',$fullpath);
  68. // last modified date
  69. $item['last_modified'] = filemtime($fullpath.'/template.html');
  70. // template type
  71. $item['type'] = $type;
  72. // template slug
  73. $item['template_id'] = urlencode( $item['template_name'] );
  74. // check css file for more info
  75. $stylesheet = $fullpath . '/style.css';
  76. if ( file_exists( $stylesheet ) ) {
  77. // $stylesheet = dirname( )
  78. $tplroot = realpath( dirname($stylesheet).'/..' );
  79. $tplfolder = basename(dirname($stylesheet));
  80. // wp_get_theme is WP 3.4+ only
  81. if ( function_exists('wp_get_theme')) {
  82. $tpl = wp_get_theme( $tplfolder, $tplroot );
  83. // echo "<pre>";print_r($tpl);echo "</pre>";
  84. $item['template_name'] = $tpl->Template;
  85. $item['template_version'] = $tpl->Version;
  86. $item['template_description'] = $tpl->Description;
  87. } else {
  88. $item['template_name'] = basename($fullpath);
  89. $item['template_version'] = '';
  90. $item['template_description'] = 'Please update to WordPress 3.4';
  91. }
  92. }
  93. return $item;
  94. }
  95. function newItem() {
  96. $item = array(
  97. "template_id" => false,
  98. "template_name" => "New listing template",
  99. "template_path" => "enter a unique folder name here",
  100. "template_version" => "1.0",
  101. "template_description" => ""
  102. );
  103. $this->folderpath = WPLISTER_PATH . '/templates/default';
  104. return $item;
  105. }
  106. public function processItem( $item ) {
  107. $listing = new ListingsModel();
  108. $ibm = new ItemBuilderModel();
  109. // load template content
  110. $tpl_html = $this->getContent();
  111. // handle errors
  112. if ( ! $tpl_html ) {
  113. $this->logger->error( 'template not found ' . $item['template'] );
  114. $this->logger->error( 'should be here: ' . WP_CONTENT_DIR . '/uploads/wp-lister/templates/' . $item['template'] );
  115. echo 'Template not found: '.$item['template'];
  116. die();
  117. }
  118. // $this->logger->debug( 'template loaded from ' . $tpl_path );
  119. // $this->logger->info( $tpl_html );
  120. // handle variations
  121. $variations_html = '';
  122. if ( ProductWrapper::hasVariations( $item['post_id'] ) ) {
  123. // generate variations table
  124. $variations_html = $this->getVariationsHTML( $item );
  125. // add variations table to item description
  126. if ( @$item['profile_data']['details']['add_variations_table'] ) {
  127. $item['post_content'] .= $variations_html;
  128. }
  129. }
  130. // handle shopp addons
  131. $addons_html = '';
  132. if ( ProductWrapper::plugin == 'shopp' ) {
  133. // generate addons table
  134. $addons_html = $this->getAddonsHTML( $item );
  135. // add variations table to item description
  136. if ( @$item['profile_data']['details']['add_variations_table'] ) {
  137. $item['post_content'] .= $addons_html;
  138. }
  139. }
  140. // remove ALL links from post content by default
  141. // TODO: make this an option in settings
  142. $item['post_content'] = preg_replace('#<a.*?>([^<]*)</a>#i', '$1', $item['post_content'] );
  143. // replace shortcodes
  144. $tpl_html = str_replace( '[[product_title]]', $ibm->prepareTitleAsHTML( $item['auction_title'] ), $tpl_html );
  145. if ( 'off' == get_option( 'wplister_process_shortcodes', 'content' ) ) {
  146. $tpl_html = str_replace( '[[product_content]]', $item['post_content'], $tpl_html );
  147. } else {
  148. $tpl_html = str_replace( '[[product_content]]', apply_filters('the_content', $item['post_content'] ), $tpl_html );
  149. }
  150. $tpl_html = str_replace( '[[product_variations]]', $variations_html, $tpl_html );
  151. $tpl_html = str_replace( '[[product_addons]]', $addons_html, $tpl_html );
  152. $tpl_html = str_replace( '[[product_excerpt]]', $listing->getRawPostExcerpt( $item['post_id'] ), $tpl_html );
  153. $tpl_html = str_replace( '[[product_additional_content]]', $listing->getRawPostExcerpt( $item['post_id'] ), $tpl_html );
  154. $tpl_html = str_replace( '[[product_price]]', number_format_i18n( floatval($item['price']), 2 ), $tpl_html );
  155. $tpl_html = str_replace( '[[product_price_raw]]', $item['price'], $tpl_html );
  156. $tpl_html = str_replace( '[[product_weight]]', ProductWrapper::getWeight( $item['post_id'], true ), $tpl_html );
  157. // dimensions
  158. $dimensions = ProductWrapper::getDimensions( $item['post_id'] );
  159. $width = @$dimensions['width'] . ' ' . @$dimensions['width_unit'];
  160. $height = @$dimensions['height'] . ' ' . @$dimensions['height_unit'];
  161. $length = @$dimensions['length'] . ' ' . @$dimensions['length_unit'];
  162. $tpl_html = str_replace( '[[product_width]]' , $width, $tpl_html );
  163. $tpl_html = str_replace( '[[product_height]]', $height, $tpl_html );
  164. $tpl_html = str_replace( '[[product_length]]', $length, $tpl_html );
  165. // attributes
  166. $tpl_html = $this->processAttributeShortcodes( $item['post_id'], $tpl_html);
  167. // handle images...
  168. $main_image = $ibm->getProductMainImageURL( $item['post_id'] );
  169. $images = $ibm->getProductImagesURL( $item['post_id'] );
  170. $this->logger->debug( 'images found ' . print_r($images,1) );
  171. // [[product_main_image]]
  172. $the_main_image = '<img class="wpl_product_image" src="'.$main_image.'" alt="main product image" />';
  173. $tpl_html = str_replace( '[[product_main_image]]', $the_main_image, $tpl_html );
  174. // [[product_main_image_url]]
  175. $tpl_html = str_replace( '[[product_main_image_url]]', $main_image, $tpl_html );
  176. // handle [[add_img_1]] to [[add_img_9]]
  177. // and [[add_img_url_1]] to [[add_img_url_9]]
  178. for ($i=0; $i < 9; $i++) {
  179. if ( isset( $images[ $i ] ) ) {
  180. $img_url = $images[ $i ];
  181. $img_tag = '<img class="wpl_additional_product_image img_'.($i+1).'" src="'.$img_url.'" />';
  182. } else {
  183. $img_url = '';
  184. $img_tag = '';
  185. }
  186. $tpl_html = str_replace( '[[img_'.($i+1).']]', $img_tag, $tpl_html );
  187. $tpl_html = str_replace( '[[img_url_'.($i+1).']]', $img_url, $tpl_html );
  188. }
  189. // handle all additional images
  190. // [[additional_product_images]]
  191. $imagelist = '';
  192. if ( count($images) > 1 ) {
  193. // loop all images
  194. for ($i=0; $i < count($images); $i++) {
  195. $image_url = $images[$i];
  196. $image_alt = basename( $images[$i] );
  197. $imagelist .= '<a onmouseover="javascript:if (typeof wplOnThumbnailHover == \'function\') wplOnThumbnailHover(\''.$image_url.'\');return false;" href="#">';
  198. $imagelist .= '<img class="wpl_thumb thumb_'.($i+1).'" src="'.$image_url.'" alt="'.$image_alt.'" /></a>'."\n";
  199. }
  200. }
  201. $tpl_html = str_replace( '[[additional_product_images]]', $imagelist, $tpl_html );
  202. // process wp shortcodes in listing template - if enabled
  203. if ( 'full' == get_option( 'wplister_process_shortcodes', 'content' ) ) {
  204. $tpl_html = do_shortcode( $tpl_html );
  205. }
  206. // return html
  207. return $tpl_html;
  208. }
  209. public function processAttributeShortcodes( $post_id, $tpl_html ) {
  210. // attribute shortcodes i.e. [[attribute_Brand]]
  211. $product_attributes = ProductWrapper::getAttributes( $post_id );
  212. $this->logger->info('product_attributes: '.print_r($product_attributes,1));
  213. if ( preg_match_all("/\\[\\[attribute_(.*)\\]\\]/uUsm", $tpl_html, $matches ) ) {
  214. foreach ( $matches[1] as $attribute ) {
  215. if ( isset( $product_attributes[ $attribute ] )){
  216. $attribute_value = $product_attributes[ $attribute ];
  217. } else {
  218. $attribute_value = '';
  219. }
  220. $tpl_html = str_replace( '[[attribute_'.$attribute.']]', $attribute_value, $tpl_html );
  221. }
  222. }
  223. return $tpl_html;
  224. }
  225. function getAddonsHTML( $item ) {
  226. $addons = ProductWrapper::getAddons( $item['post_id'] );
  227. $variations_html .= '<table style="margin-bottom: 8px;">';
  228. foreach ($addons as $addonGroup) {
  229. // first column: quantity
  230. $variations_html .= '<tr><td colspan="2" align="left"><h5>';
  231. $variations_html .= $addonGroup->name;
  232. $variations_html .= '</h5></td></tr>';
  233. foreach ($addonGroup->options as $addon) {
  234. $variations_html .= '<tr><td align="left">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
  235. $variations_html .= $addon->name;
  236. $variations_html .= '</td><td align="right">';
  237. $variations_html .= number_format_i18n( $addon->price, 2 );
  238. $variations_html .= '</td></tr>';
  239. }
  240. }
  241. $variations_html .= '</table>';
  242. return $variations_html;
  243. }
  244. function getVariationsHTML( $item ) {
  245. $listingsModel = new ListingsModel();
  246. $profile_data = maybe_unserialize( $item['profile_data'] );
  247. $variations = ProductWrapper::getVariations( $item['post_id'] );
  248. $variations_html = '<div class="variations_list" style="margin:10px 0;">';
  249. $variations_html .= '<table style="margin-bottom: 8px;">';
  250. //table header
  251. if (true) {
  252. // first column: quantity
  253. $variations_html .= '<tr>';
  254. foreach ($variations[0]['variation_attributes'] as $name => $value) {
  255. $variations_html .= '<th>';
  256. $variations_html .= $name;
  257. $variations_html .= '</th>';
  258. }
  259. // last column: price
  260. $variations_html .= '<th align="right">';
  261. $variations_html .= __('Price','wplister');
  262. $variations_html .= '</th></tr>';
  263. }
  264. //table body
  265. foreach ($variations as $var) {
  266. // first column: quantity
  267. // $variations_html .= '<tr><td align="right">';
  268. // $variations_html .= intval( $var['stock'] ) . '&nbsp;x';
  269. // $variations_html .= '</td><td>';
  270. $variations_html .= '<tr>';
  271. foreach ($var['variation_attributes'] as $name => $value) {
  272. // $variations_html .= $name.': '.$value ;
  273. $variations_html .= '<td>';
  274. $variations_html .= $value ;
  275. $variations_html .= '</td>';
  276. }
  277. // $variations_html .= '('.$var['sku'].') ';
  278. // $variations_html .= '('.$var['image'].') ';
  279. // last column: price
  280. $variations_html .= '<td align="right">';
  281. $price = $listingsModel->applyProfilePrice( $var['price'], $profile_data['details']['start_price'] );
  282. $variations_html .= number_format_i18n( $price, 2 );
  283. $variations_html .= '</td></tr>';
  284. }
  285. $variations_html .= '</table>';
  286. $variations_html .= '</div>';
  287. // return html
  288. return $variations_html;
  289. }
  290. function getContent() {
  291. // load template.html
  292. $tpl_html = $this->getHTML();
  293. // load and embed style.css
  294. $tpl_css = $this->getCSS();
  295. $tpl_html = '<style type="text/css">'.$tpl_css.'</style>'."\n\n".$tpl_html;
  296. // include header.php
  297. $tpl_header = $this->getDynamicContent( $this->folderpath . '/header.php' );
  298. $tpl_html = $tpl_header."\n\n".$tpl_html;
  299. // include footer.php
  300. $tpl_footer = $this->getDynamicContent( $this->folderpath . '/footer.php' );
  301. $tpl_html = $tpl_html."\n\n".$tpl_footer;
  302. return $tpl_html;
  303. }
  304. function getHTML( $folder = false) {
  305. if ( ! $folder ) $folder = $this->folderpath;
  306. $file = $folder . '/template.html';
  307. return file_get_contents( $file );
  308. }
  309. function getCSS( $folder = false ) {
  310. if ( ! $folder ) $folder = $this->folderpath;
  311. $file = $folder . '/style.css';
  312. return file_get_contents( $file );
  313. }
  314. function getHeader( $folder = false ) {
  315. if ( ! $folder ) $folder = $this->folderpath;
  316. $file = $folder . '/header.php';
  317. return @file_get_contents( $file );
  318. }
  319. function getFooter( $folder = false ) {
  320. if ( ! $folder ) $folder = $this->folderpath;
  321. $file = $folder . '/footer.php';
  322. return @file_get_contents( $file );
  323. }
  324. public function getDynamicContent( $sFile, $inaData = array() ) {
  325. if ( !is_file( $sFile ) ) {
  326. $this->showMessage("File not found: ".$sFile,1,1);
  327. return false;
  328. }
  329. if ( count( $inaData ) > 0 ) {
  330. extract( $inaData, EXTR_PREFIX_ALL, 'wpl' );
  331. }
  332. ob_start();
  333. include( $sFile );
  334. $sContents = ob_get_contents();
  335. ob_end_clean();
  336. return $sContents;
  337. }
  338. function deleteTemplate( $id ) {
  339. $item = $this->getItem( $id );
  340. $fullpath = WP_CONTENT_DIR . $item['template_path'];
  341. // delete each template file
  342. $files = glob( $fullpath . '/*' );
  343. foreach ($files as $file) {
  344. unlink($file);
  345. }
  346. // delete folder
  347. rmdir($fullpath);
  348. }
  349. static function getCache() {
  350. $templates_cache = get_option( 'wplister_templates_cache' );
  351. if ( $templates_cache == '' )
  352. return array();
  353. return $templates_cache;
  354. }
  355. static function getNameFromCache( $id ) {
  356. $templates_cache = self::getCache();
  357. if ( isset( $templates_cache[ $id ] ) )
  358. return $templates_cache[ $id ]['template_name'];
  359. return $id;
  360. }
  361. function insertTemplate($id, $data) {
  362. }
  363. function updateTemplate($id, $data) {
  364. }
  365. function duplicateTemplate($id) {
  366. }
  367. }