/src/lib/commerce/shortcodes.php

https://github.com/mgsisk/webcomic · PHP · 458 lines · 191 code · 41 blank · 226 comment · 8 complexity · 97ff117eb4c4cd2fdd7bb7cc1f0547fe MD5 · raw file

  1. <?php
  2. /**
  3. * Commerce shortcodes
  4. *
  5. * @package Webcomic
  6. */
  7. namespace Mgsisk\Webcomic\Commerce;
  8. /**
  9. * Add shortcodes.
  10. *
  11. * @return void
  12. */
  13. function shortcodes() {
  14. add_shortcode( 'webcomic_collection_cart_link', __NAMESPACE__ . '\webcomic_collection_cart_link_shortcode' );
  15. add_shortcode( 'webcomic_collection_currency', __NAMESPACE__ . '\webcomic_collection_currency_shortcode' );
  16. add_shortcode( 'webcomic_collection_donation', __NAMESPACE__ . '\webcomic_collection_donation_shortcode' );
  17. add_shortcode( 'webcomic_collection_donation_link', __NAMESPACE__ . '\webcomic_collection_donation_link_shortcode' );
  18. add_shortcode( 'webcomic_collection_print_name', __NAMESPACE__ . '\webcomic_collection_print_name_shortcode' );
  19. add_shortcode( 'webcomic_collection_print_price', __NAMESPACE__ . '\webcomic_collection_print_price_shortcode' );
  20. add_shortcode( 'webcomic_collection_print_stock', __NAMESPACE__ . '\webcomic_collection_print_stock_shortcode' );
  21. add_shortcode( 'webcomic_print_adjust', __NAMESPACE__ . '\webcomic_print_adjust_shortcode' );
  22. add_shortcode( 'webcomic_print_left', __NAMESPACE__ . '\webcomic_print_left_shortcode' );
  23. add_shortcode( 'webcomic_print_price', __NAMESPACE__ . '\webcomic_print_price_shortcode' );
  24. add_shortcode( 'webcomic_print_sold', __NAMESPACE__ . '\webcomic_print_sold_shortcode' );
  25. add_shortcode( 'webcomic_prints_list', __NAMESPACE__ . '\webcomic_prints_list_shortcode' );
  26. add_shortcode( 'has_webcomic_print', __NAMESPACE__ . '\has_webcomic_print_shortcode' );
  27. }
  28. /**
  29. * Display a collection cart link.
  30. *
  31. * @uses get_webcomic_collection_cart_link()
  32. * @param array $atts {
  33. * Optional attributes.
  34. *
  35. * @type string $link Optional link text, like 'before{{text}}after'.
  36. * @type mixed $collection Optional collection to get a cart link for.
  37. * }
  38. * @param string $content Optional shortcode content; mapped to $args['link'].
  39. * @param string $name Shortcode name.
  40. * @return string
  41. */
  42. function webcomic_collection_cart_link_shortcode( $atts, string $content, string $name ) : string {
  43. $args = shortcode_atts(
  44. [
  45. 'link' => '',
  46. 'collection' => null,
  47. ], $atts, $name
  48. );
  49. if ( $content ) {
  50. $args['link'] = do_shortcode( htmlspecialchars_decode( $content ) );
  51. }
  52. $args['link'] = htmlspecialchars_decode( $args['link'] );
  53. return get_webcomic_collection_cart_link( $args['link'], $args['collection'] );
  54. }
  55. /**
  56. * Display the collection currency.
  57. *
  58. * @uses get_webcomic_collection_currency()
  59. * @param array $atts {
  60. * Optional attributes.
  61. *
  62. * @type mixed $collection Optional collection to get the currency for.
  63. * }
  64. * @param string $content Optional shortcode content; mapped to $args['link'].
  65. * @param string $name Shortcode name.
  66. * @return string
  67. */
  68. function webcomic_collection_currency_shortcode( $atts, string $content, string $name ) : string {
  69. $args = shortcode_atts(
  70. [
  71. 'collection' => null,
  72. ], $atts, $name
  73. );
  74. return get_webcomic_collection_currency( $args['collection'] );
  75. }
  76. /**
  77. * Display a collection donation amount.
  78. *
  79. * @uses get_webcomic_collection_donation()
  80. * @param array $atts {
  81. * Optional attributes.
  82. *
  83. * @type mixed $collection Optional collection to get a donation amount for.
  84. * }
  85. * @param string $content Unused shortcode content.
  86. * @param string $name Shortcode name.
  87. * @return string
  88. */
  89. function webcomic_collection_donation_shortcode( $atts, string $content, string $name ) : string {
  90. $args = shortcode_atts(
  91. [
  92. 'collection' => null,
  93. ], $atts, $name
  94. );
  95. return get_webcomic_collection_donation( $args['collection'] );
  96. }
  97. /**
  98. * Display a collection donation link.
  99. *
  100. * @uses get_webcomic_collection_donation_link()
  101. * @param array $atts {
  102. * Optional attributes.
  103. *
  104. * @type string $link Optional link text, like 'before{{text}}after'.
  105. * @type mixed $collection Optional collection to get a donation link for.
  106. * }
  107. * @param string $content Optional shortcode content; mapped to $args['link'].
  108. * @param string $name Shortcode name.
  109. * @return string
  110. */
  111. function webcomic_collection_donation_link_shortcode( $atts, string $content, string $name ) : string {
  112. $args = shortcode_atts(
  113. [
  114. 'link' => '',
  115. 'collection' => null,
  116. ], $atts, $name
  117. );
  118. if ( $content ) {
  119. $args['link'] = do_shortcode( htmlspecialchars_decode( $content ) );
  120. }
  121. $args['link'] = htmlspecialchars_decode( $args['link'] );
  122. return get_webcomic_collection_donation_link( $args['link'], $args['collection'] );
  123. }
  124. /**
  125. * Display a collection print name.
  126. *
  127. * @uses get_webcomic_collection_print_name()
  128. * @param array $atts {
  129. * Optional attributes.
  130. *
  131. * @type string $type The print name to get.
  132. * @type mixed $collection Optional collection to get the print name from.
  133. * }
  134. * @param string $content Unused shortcode content.
  135. * @param string $name Shortcode name.
  136. * @return string
  137. */
  138. function webcomic_collection_print_name_shortcode( $atts, string $content, string $name ) : string {
  139. $args = shortcode_atts(
  140. [
  141. 'type' => '',
  142. 'collection' => null,
  143. ], $atts, $name
  144. );
  145. return get_webcomic_collection_print_name( $args['type'], $args['collection'] );
  146. }
  147. /**
  148. * Display a collection print price.
  149. *
  150. * @uses get_webcomic_collection_print_price()
  151. * @param array $atts {
  152. * Optional attributes.
  153. *
  154. * @type string $type The print price to get.
  155. * @type mixed $collection Optional collection to get the print price from.
  156. * }
  157. * @param string $content Unused shortcode content.
  158. * @param string $name Shortcode name.
  159. * @return string
  160. */
  161. function webcomic_collection_print_price_shortcode( $atts, string $content, string $name ) : string {
  162. $args = shortcode_atts(
  163. [
  164. 'type' => '',
  165. 'collection' => null,
  166. ], $atts, $name
  167. );
  168. return get_webcomic_collection_print_price( $args['type'], $args['collection'] );
  169. }
  170. /**
  171. * Display a collection print stock.
  172. *
  173. * @uses get_webcomic_collection_print_stock()
  174. * @param array $atts {
  175. * Optional attributes.
  176. *
  177. * @type string $type The print stock to get.
  178. * @type mixed $collection Optional collection to get the print stock from.
  179. * }
  180. * @param string $content Unused shortcode content.
  181. * @param string $name Shortcode name.
  182. * @return string
  183. */
  184. function webcomic_collection_print_stock_shortcode( $atts, string $content, string $name ) : string {
  185. $args = shortcode_atts(
  186. [
  187. 'type' => '',
  188. 'collection' => null,
  189. ], $atts, $name
  190. );
  191. return get_webcomic_collection_print_stock( $args['type'], $args['collection'] );
  192. }
  193. /**
  194. * Display a comic print adjustment.
  195. *
  196. * @uses get_webcomic_print_adjust()
  197. * @param array $atts {
  198. * Optional attributes.
  199. *
  200. * @type string $type The print price adjustment to get.
  201. * @type mixed $post Optional post to get the price adjustment from.
  202. * }
  203. * @param string $content Unused shortcode content.
  204. * @param string $name Shortcode name.
  205. * @return string
  206. */
  207. function webcomic_print_adjust_shortcode( $atts, string $content, string $name ) : string {
  208. $args = shortcode_atts(
  209. [
  210. 'type' => '',
  211. 'post' => null,
  212. ], $atts, $name
  213. );
  214. return (string) get_webcomic_print_adjust( $args['type'], $args['post'] );
  215. }
  216. /**
  217. * Display a comic print left count.
  218. *
  219. * @uses get_webcomic_print_left()
  220. * @param array $atts {
  221. * Optional attributes.
  222. *
  223. * @type string $type The print left count to get.
  224. * @type mixed $post Optional post to get the left count from.
  225. * }
  226. * @param string $content Unused shortcode content.
  227. * @param string $name Shortcode name.
  228. * @return string
  229. */
  230. function webcomic_print_left_shortcode( $atts, string $content, string $name ) : string {
  231. $args = shortcode_atts(
  232. [
  233. 'type' => '',
  234. 'post' => null,
  235. ], $atts, $name
  236. );
  237. return get_webcomic_print_left( $args['type'], $args['post'] );
  238. }
  239. /**
  240. * Display a comic print price.
  241. *
  242. * @uses get_webcomic_print_price()
  243. * @param array $atts {
  244. * Optional attributes.
  245. *
  246. * @type string $type The print price to get.
  247. * @type mixed $post Optional post to get the price from.
  248. * }
  249. * @param string $content Unused shortcode content.
  250. * @param string $name Shortcode name.
  251. * @return string
  252. */
  253. function webcomic_print_price_shortcode( $atts, string $content, string $name ) : string {
  254. $args = shortcode_atts(
  255. [
  256. 'type' => '',
  257. 'post' => null,
  258. ], $atts, $name
  259. );
  260. return get_webcomic_print_price( $args['type'], $args['post'] );
  261. }
  262. /**
  263. * Display a comic print adjustment.
  264. *
  265. * @uses get_webcomic_print_sold()
  266. * @param array $atts {
  267. * Optional attributes.
  268. *
  269. * @type string $type The print sold count to get.
  270. * @type mixed $post Optional post to get the sold count from.
  271. * }
  272. * @param string $content Unused shortcode content.
  273. * @param string $name Shortcode name.
  274. * @return string
  275. */
  276. function webcomic_print_sold_shortcode( $atts, string $content, string $name ) : string {
  277. $args = shortcode_atts(
  278. [
  279. 'type' => '',
  280. 'post' => null,
  281. ], $atts, $name
  282. );
  283. return (string) get_webcomic_print_sold( $args['type'], $args['post'] );
  284. }
  285. /**
  286. * Display a list of comic prints.
  287. *
  288. * @uses get_webcomic_prints_list()
  289. * @param array $atts {
  290. * Optional attributes.
  291. *
  292. * @type string $format Optional list format, like before{{join}}after.
  293. * Including `<select>` or `<optgroup>` elements will
  294. * convert links to `<option>` elements.
  295. * @type string $link Optional link text, like before{{text}}after.
  296. * @type mixed $link_post Optional reference post for print links.
  297. * @type array $link_args Optional arguments for print links.
  298. * @type int $cloud_min Optional weighted list minimum font size.
  299. * @type int $cloud_max Optional weighted list maximum font size.
  300. * @type string $cloud_val Optional print value to use for cloud
  301. * calculations; one of sold, adjust, base, left,
  302. * price, or stock.
  303. * @type string $walker Optional custom Walker class to use instead of
  304. * Mgsisk\Webcomic\Commerce\Walker\PrintLister.
  305. * @type mixed $post Optional post to get prints for.
  306. * @type string $order How to sort prints; one of asc or desc.
  307. * @type string $limit Whether to limit the number of prints returned.
  308. * @type string $orderby What to sort prints by; one of name, adjust,
  309. * base, left, price, sold, slug, or stock.
  310. * @type bool $hide_empty Whether to include or exclude sold out prints.
  311. * @type bool $hide_infinite Whether to include or exclude prints with
  312. * infinite stock.
  313. * @type int $adjust_min Optional minimum price adjustment prints must
  314. * have.
  315. * @type int $adjust_max Optional maximum price adjustment prints must
  316. * have.
  317. * @type float $base_min Optional minimum base price prints must have.
  318. * @type float $base_max Optional maximum base price prints must have.
  319. * @type int $left_min Optional minimum left count prints must have.
  320. * @type int $left_max Optional maximum left count prints must have.
  321. * @type float $price_min Optional minimum price prints must have.
  322. * @type float $price_max Optional maximum price prints must have.
  323. * @type int $sold_min Optional minimum sold count prints must have.
  324. * @type int $sold_max Optional maximum sold count prints must have.
  325. * @type int $stock_min Optional minimum stock prints must have.
  326. * @type int $stock_max Optional maximum stock prints must have.
  327. * @type array $slug__in Optional slugs the collections must match.
  328. * @type array $slug__not_in Optional slugs the collections must not
  329. * match.
  330. * }
  331. * @param string $content Unused shortcode content.
  332. * @param string $name Shortcode name.
  333. * @return string
  334. */
  335. function webcomic_prints_list_shortcode( $atts, string $content, string $name ) : string {
  336. $args = shortcode_atts(
  337. [
  338. 'adjust_max' => null,
  339. 'adjust_min' => null,
  340. 'base_max' => null,
  341. 'base_min' => null,
  342. 'cloud_max' => null,
  343. 'cloud_min' => null,
  344. 'cloud_val' => 'sold',
  345. 'format' => ', ',
  346. 'hide_empty' => true,
  347. 'hide_infinite' => false,
  348. 'left_max' => null,
  349. 'left_min' => null,
  350. 'limit' => 0,
  351. 'link_args' => [],
  352. 'link_post' => null,
  353. 'link' => '%print-name - %print-price %print-currency',
  354. 'order' => 'asc',
  355. 'orderby' => 'name',
  356. 'post' => null,
  357. 'price_max' => null,
  358. 'price_min' => null,
  359. 'slug__in' => '',
  360. 'slug__not_in' => '',
  361. 'sold_max' => null,
  362. 'sold_min' => null,
  363. 'stock_max' => null,
  364. 'stock_min' => null,
  365. 'walker' => '',
  366. ], $atts, $name
  367. );
  368. foreach ( $args as $key => $value ) {
  369. if ( false === strpos( $key, '_min' ) && false === strpos( $key, '_max' ) ) {
  370. continue;
  371. } elseif ( null === $value ) {
  372. unset( $args[ $key ] );
  373. continue;
  374. }
  375. $args[ $key ] = (int) $value;
  376. }
  377. $args['format'] = htmlspecialchars_decode( $args['format'] );
  378. $args['cloud_min'] = (int) $args['cloud_min'];
  379. $args['cloud_max'] = (int) $args['cloud_max'];
  380. if ( $content ) {
  381. $args['link'] = do_shortcode( htmlspecialchars_decode( $content ) );
  382. }
  383. $args['link'] = htmlspecialchars_decode( $args['link'] );
  384. if ( is_string( $args['link_args'] ) ) {
  385. parse_str( htmlspecialchars_decode( $args['link_args'] ), $args['link_args'] );
  386. }
  387. return get_webcomic_prints_list( $args );
  388. }
  389. /**
  390. * Display content if the post has a comic print.
  391. *
  392. * @uses has_webcomic_print()
  393. * @param array $atts {
  394. * Optional attributes.
  395. *
  396. * @type mixed $type Optional print to check for.
  397. * @type mixed $post Optional post to check for prints.
  398. * }
  399. * @param string $content Content to display if the post has a comic print.
  400. * @param string $name Shortcode name.
  401. * @return string
  402. */
  403. function has_webcomic_print_shortcode( $atts, string $content, string $name ) : string {
  404. if ( ! $content ) {
  405. return '';
  406. }
  407. $args = shortcode_atts(
  408. [
  409. 'type' => null,
  410. 'post' => null,
  411. ], $atts, $name
  412. );
  413. if ( ! has_webcomic_print( $args['type'], $args['post'] ) ) {
  414. return '';
  415. }
  416. return do_shortcode( $content );
  417. }