catalog.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. //catalogus functionaliteit //iTsOc
  3. namespace ITSociaal\Wordpress\Themes\EnfoldChild\Shopp;
  4. ?>
  5. <?php
  6. error_reporting(~0);
  7. /**
  8. * Return first element of given array
  9. *
  10. * @param array $array
  11. *
  12. * @return mixed Value of first element
  13. */
  14. function first($array) {
  15. return array_values($array)[0];
  16. }
  17. /**
  18. * Query a DOMDocument with XPath
  19. *
  20. * @param \DOMDocument $doc
  21. * @param string $query
  22. *
  23. * @return \DOMNode[] Query results or empty array
  24. */
  25. function qxpath(\DOMDocument $doc, $query) {
  26. return iterator_to_array(
  27. (new \DOMXPath($doc))->query($query)
  28. );
  29. }
  30. /**
  31. * Parse a given HTML fragment into a DOMDocument
  32. *
  33. * @param string $html Valid HTML
  34. *
  35. * @return \DOMDocument
  36. */
  37. function html2dom($html) {
  38. return \DOMDocument::loadHTML(
  39. '<?xml version="1.1" encoding="utf-8" ?>' . "\n" .
  40. $html,
  41. 0
  42. |LIBXML_NONET
  43. |LIBXML_NOWARNING
  44. |LIBXML_NOERROR
  45. //|LIBXML_HTML_NOIMPLIED//does not work
  46. |LIBXML_HTML_NODEFDTD
  47. );
  48. }
  49. // shopp( 'storefront.catalog-products' ) does not include a list
  50. // of categories. There oughta be an easier way to fix this thing.
  51. print(call_user_func_array(
  52. function(\DOMDocument $dCatalog) {
  53. // bookkeeping :P
  54. $bPrev = libxml_use_internal_errors(true);
  55. // Get main category element
  56. $elRoot = first(qxpath($dCatalog, '//*[@class="category"][1]'));
  57. // Generate list of categories
  58. $sCategoriesHtml = shopp('storefront', 'category-list', [
  59. // Add div tag for later retrieval
  60. 'after' => '</div>',
  61. 'before' =>
  62. '<div class="custom-category-list">',
  63. 'hierarchy' => true,
  64. 'products' => true,
  65. 'return' => 'true', // why the fuck is this not default?
  66. //'title' => 'Producten menu',
  67. ]);
  68. // XPath to find navigation element
  69. $sNavXpath = '//section[contains(@class, "navigation")][1]';
  70. // Manipulate the DOM
  71. first(qxpath($dCatalog, $sNavXpath))
  72. ->insertBefore(
  73. $dCatalog->importNode(
  74. first(qxpath(
  75. html2dom($sCategoriesHtml),
  76. // Find the above div tag
  77. "//div[1]"
  78. )),
  79. true
  80. ),
  81. first(qxpath(
  82. $dCatalog,
  83. $sNavXpath . '//*[@id="shopp-catalog-orderby-menu"]/..'
  84. ))
  85. );
  86. libxml_use_internal_errors($bPrev);
  87. return $dCatalog->saveHTML($elRoot);
  88. },
  89. [
  90. // Original catalog html
  91. html2dom(shopp( 'storefront', 'catalog-products', [
  92. "controls" => true,
  93. "return" => true,
  94. ])),
  95. ]
  96. ));
  97. /* vi:set ts=4 sw=4 noet: */