PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class-quimo-field-mapper.php

https://bitbucket.org/quimo/quimo-post-catcher
PHP | 206 lines | 151 code | 16 blank | 39 comment | 36 complexity | bde67bf9470dc511a4e673f4bbd8ab58 MD5 | raw file
  1. <?php
  2. /**
  3. * Plugin Name: Quimo Post Catcher
  4. * Plugin URI: https://bitbucket.org/quimo/quimo-field-mapper/src/master/
  5. * Description: Mappa i campi del resultset di WP_Query su un specifico template
  6. * Version: 1.0
  7. * Author: Simone Alati
  8. * Author URI: https://www.simonealati.it
  9. */
  10. /*
  11. ███████╗██╗███████╗██╗ ██████╗ ███╗ ███╗ █████╗ ██████╗ ██████╗ ███████╗██████╗
  12. ██╔════╝██║██╔════╝██║ ██╔══██╗ ████╗ ████║██╔══██╗██╔══██╗██╔══██╗██╔════╝██╔══██╗
  13. █████╗ ██║█████╗ ██║ ██║ ██║ ██╔████╔██║███████║██████╔╝██████╔╝█████╗ ██████╔╝
  14. ██╔══╝ ██║██╔══╝ ██║ ██║ ██║ ██║╚██╔╝██║██╔══██║██╔═══╝ ██╔═══╝ ██╔══╝ ██╔══██╗
  15. ██║ ██║███████╗███████╗██████╔╝ ██║ ╚═╝ ██║██║ ██║██║ ██║ ███████╗██║ ██║
  16. ╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
  17. http://patorjk.com/software/taag/#p=display&f=ANSI%20Shadow&t=Field%20mapper
  18. */
  19. class FieldMapper {
  20. private $data = array();
  21. function show ($args, $template, $random = 0, $debug = 0) {
  22. $this->get($args, $debug);
  23. if (!empty($this->data)) return $this->render($template, $random, $debug);
  24. return false;
  25. }
  26. public function get_quick($args, $debug = 0) {
  27. $loop = new WP_Query($args);
  28. if ($loop->have_posts()) {
  29. if ($debug) $this->debug('', 'start');
  30. $i = 0;
  31. while ($loop->have_posts()) {
  32. $loop->the_post();
  33. $id = get_the_ID();
  34. $this->data[$i]['permalink'] = get_the_permalink($id);
  35. $this->data[$i]['title'] = get_the_title();
  36. $this->data[$i]['excerpt'] = nl2br(get_the_excerpt());
  37. /* immagini */
  38. if (function_exists('has_post_thumbnail') && has_post_thumbnail($id)) {
  39. $dummy = wp_get_attachment_image_src(get_post_thumbnail_id($id),'thumbnail');
  40. $this->data[$i]['thumbnail'] = $dummy[0];
  41. }
  42. if ($debug) $this->debug($this->data[$i], 'loop');
  43. $i++;
  44. }
  45. }
  46. wp_reset_query();
  47. wp_reset_postdata();
  48. }
  49. public function get($args, $debug = 0) {
  50. $loop = new WP_Query($args);
  51. if ($loop->have_posts()) {
  52. if ($debug) $this->debug('', 'start');
  53. $i = 0;
  54. while ($loop->have_posts()) {
  55. $loop->the_post();
  56. $id = get_the_ID();
  57. $this->data[$i]['permalink'] = get_the_permalink($id);
  58. $this->data[$i]['title'] = get_the_title();
  59. $this->data[$i]['content'] = $this->get_content();
  60. $this->data[$i]['excerpt'] = nl2br(get_the_excerpt());
  61. $this->data[$i]['content_notag'] = get_the_content();
  62. /* immagini */
  63. if (function_exists('has_post_thumbnail') && has_post_thumbnail($id)) {
  64. $dummy = wp_get_attachment_image_src(get_post_thumbnail_id($id),'thumbnail');
  65. $this->data[$i]['thumbnail'] = $dummy[0];
  66. $dummy = wp_get_attachment_image_src(get_post_thumbnail_id($id),'medium');
  67. $this->data[$i]['medium'] = $dummy[0];
  68. $dummy = wp_get_attachment_image_src(get_post_thumbnail_id($id),'large');
  69. $this->data[$i]['large'] = $dummy[0];
  70. $dummy = wp_get_attachment_image_src(get_post_thumbnail_id($id),'full');
  71. $this->data[$i]['full'] = $dummy[0];
  72. }
  73. /* advanced custom fields */
  74. if (function_exists('get_fields')) {
  75. $fields = get_fields($id);
  76. if ($fields) {
  77. foreach( $fields as $name => $value ) {
  78. $this->data[$i][$name] = $value;
  79. }
  80. }
  81. }
  82. /* woocommerce product */
  83. if ($args['post_type'] == 'product') {
  84. if (function_exists('wc_get_product') && wc_get_product($id)) {
  85. $product = wc_get_product($id);
  86. if ($product) {
  87. if ($product->is_type('simple')) {
  88. $this->data[$i]['price'] = $product->get_price();
  89. } elseif ($product->is_type('variable')) {
  90. $price_min = $product->get_variation_price('min');
  91. $price_max = $product->get_variation_price('max');
  92. $price_range = ($price_min != $price_max) ? number_format((float)$price_min, 2, ',', '') . ' - ' . number_format((float)$price_max, 2, ',', '') : number_format((float)$price_min, 2, ',', '');
  93. $this->data[$i]['price'] = $price_range . get_woocommerce_currency_symbol();
  94. }
  95. }
  96. }
  97. }
  98. if ($debug) $this->debug($this->data[$i], 'loop');
  99. $i++;
  100. }
  101. return true;
  102. } else return false;
  103. wp_reset_query();
  104. wp_reset_postdata();
  105. }
  106. public function render($template, $random = 0, $debug = 0) {
  107. if ($template) {
  108. // recupero il template
  109. $tmpl_url = POST_CATCHER_TEMPLATE_FOLDER . $template.'.html';
  110. $template_content = ($debug) ? file_get_contents($tmpl_url) : @file_get_contents($tmpl_url);
  111. /* metodo alternativo a file_get_contents
  112. $ch = curl_init();
  113. curl_setopt($ch, CURLOPT_URL, $tmpl_url);
  114. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  115. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  116. $template_content = curl_exec($ch);
  117. curl_close($ch);
  118. */
  119. // sostituisco i [+placeholder+]
  120. if ($debug) $this->debug($tmpl_url, 'template');
  121. if ($template_content && !empty($this->data)) {
  122. $html = '';
  123. for ($i = 0; $i < count($this->data); $i++) {
  124. $row = $template_content;
  125. foreach ($this->data[$i] as $key => $value) {
  126. if (is_array($value)) {
  127. /* se il campo è un array (repeater) recupero la sezione di template da ripetere */
  128. if ($random) shuffle($value);
  129. $pos = strpos($row, '[+'.$key.'+]');
  130. if ($pos !== false) {
  131. $section = self::getStringBetween($row, '[+'.$key.'+]', '[+/'.$key.'+]');
  132. $row = str_replace('[+'.$key.'+]'.$section.'[+/'.$key.'+]', "", $row);
  133. $sub_html = '';
  134. for ($j = 0; $j < count($value); $j++) {
  135. /* ciclo nei sottocampi del repeater */
  136. $sub_template = $section;
  137. if ($value[$j]) {
  138. foreach ($value[$j] as $sub_name => $sub_value) {
  139. $sub_template = str_replace("[+".$sub_name."+]",$sub_value,$sub_template);
  140. }
  141. }
  142. $sub_html .= $sub_template;
  143. }
  144. $row = substr_replace($row, $sub_html, $pos, 0);
  145. }
  146. } else {
  147. /* se il campo non è un array */
  148. $row = str_replace("[+".$key."+]", $value, $row);
  149. }
  150. }
  151. $html .= $row;
  152. }
  153. return $html;
  154. }
  155. return false;
  156. }
  157. return false;
  158. }
  159. private function get_content($more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
  160. $content = get_the_content($more_link_text, $stripteaser, $more_file);
  161. //$content = apply_filters('the_content', $content);
  162. //$content = str_replace(']]>', ']]&gt;', $content);
  163. return $content;
  164. }
  165. private function debug($data, $action) {
  166. switch($action) {
  167. case 'start':
  168. ?><br>RISULTATI DI WP_Query<br><?php
  169. break;
  170. case 'loop':
  171. ?><a href="<?php echo $data['permalink'] ?>"><?php echo $data['title'] ?></a><br><?php
  172. break;
  173. case 'template':
  174. ?><br>TEMPLATE SU CUI MAPPARE I RISULTATI<br><?php
  175. ?><a target="_blank" href="<?php echo $data ?>">Clicca qui</a><br><?php
  176. break;
  177. }
  178. }
  179. private static function getStringBetween($string, $start, $end) {
  180. $string = ' ' . $string;
  181. $ini = strpos($string, $start);
  182. if ($ini == 0) return '';
  183. $ini += strlen($start);
  184. $len = strpos($string, $end, $ini) - $ini;
  185. return substr($string, $ini, $len);
  186. }
  187. }