/src/lib/taxonomy/shortcodes.php

https://github.com/mgsisk/webcomic · PHP · 537 lines · 250 code · 56 blank · 231 comment · 31 complexity · d4aed2197a54d14c25709113edafe400 MD5 · raw file

  1. <?php
  2. /**
  3. * Taxonomy shortcodes
  4. *
  5. * @package Webcomic
  6. */
  7. namespace Mgsisk\Webcomic\Taxonomy;
  8. /**
  9. * Add shortcodes.
  10. *
  11. * @return void
  12. */
  13. function shortcodes() {
  14. add_shortcode( 'webcomic_term_count', __NAMESPACE__ . '\webcomic_term_count_shortcode' );
  15. add_shortcode( 'webcomic_term_description', __NAMESPACE__ . '\webcomic_term_description_shortcode' );
  16. add_shortcode( 'webcomic_term_media', __NAMESPACE__ . '\webcomic_term_media_shortcode' );
  17. add_shortcode( 'webcomic_term_title', __NAMESPACE__ . '\webcomic_term_title_shortcode' );
  18. add_shortcode( 'webcomic_term_updated', __NAMESPACE__ . '\webcomic_term_updated_shortcode' );
  19. add_shortcode( 'webcomic_term_link', __NAMESPACE__ . '\webcomic_term_link_shortcode' );
  20. add_shortcode( 'first_webcomic_term_link', __NAMESPACE__ . '\webcomic_term_link_shortcode' );
  21. add_shortcode( 'previous_webcomic_term_link', __NAMESPACE__ . '\webcomic_term_link_shortcode' );
  22. add_shortcode( 'next_webcomic_term_link', __NAMESPACE__ . '\webcomic_term_link_shortcode' );
  23. add_shortcode( 'last_webcomic_term_link', __NAMESPACE__ . '\webcomic_term_link_shortcode' );
  24. add_shortcode( 'random_webcomic_term_link', __NAMESPACE__ . '\webcomic_term_link_shortcode' );
  25. add_shortcode( 'has_webcomic_term', __NAMESPACE__ . '\has_webcomic_term_shortcode' );
  26. add_shortcode( 'is_a_webcomic_term', __NAMESPACE__ . '\is_a_webcomic_term_shortcode' );
  27. add_shortcode( 'is_a_first_webcomic_term', __NAMESPACE__ . '\is_a_webcomic_term_shortcode' );
  28. add_shortcode( 'is_a_previous_webcomic_term', __NAMESPACE__ . '\is_a_webcomic_term_shortcode' );
  29. add_shortcode( 'is_a_next_webcomic_term', __NAMESPACE__ . '\is_a_webcomic_term_shortcode' );
  30. add_shortcode( 'is_a_last_webcomic_term', __NAMESPACE__ . '\is_a_webcomic_term_shortcode' );
  31. add_shortcode( 'is_webcomic_tax', __NAMESPACE__ . '\is_webcomic_tax_shortcode' );
  32. add_shortcode( 'is_first_webcomic_tax', __NAMESPACE__ . '\is_webcomic_tax_shortcode' );
  33. add_shortcode( 'is_previous_webcomic_tax', __NAMESPACE__ . '\is_webcomic_tax_shortcode' );
  34. add_shortcode( 'is_next_webcomic_tax', __NAMESPACE__ . '\is_webcomic_tax_shortcode' );
  35. add_shortcode( 'is_last_webcomic_tax', __NAMESPACE__ . '\is_webcomic_tax_shortcode' );
  36. add_shortcode( 'webcomic_terms_list', __NAMESPACE__ . '\webcomic_terms_list_shortcode' );
  37. }
  38. /**
  39. * Display a comic term count.
  40. *
  41. * @uses get_webcomic_term_count()
  42. * @param array $atts {
  43. * Optional attributes.
  44. *
  45. * @type mixed $term Optional term to get a post count for.
  46. * @type array $args Optional arguments.
  47. * }
  48. * @param string $content Unused shortcode content.
  49. * @param string $name Shortcode name.
  50. * @return string
  51. */
  52. function webcomic_term_count_shortcode( $atts, string $content, string $name ) : string {
  53. $args = shortcode_atts(
  54. [
  55. 'term' => null,
  56. 'args' => [],
  57. ], $atts, $name
  58. );
  59. if ( is_string( $args['args'] ) ) {
  60. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  61. }
  62. return (string) get_webcomic_term_count( $args['term'], $args['args'] );
  63. }
  64. /**
  65. * Display a comic term description.
  66. *
  67. * @uses get_webcomic_term_description()
  68. * @param array $atts {
  69. * Optional attributes.
  70. *
  71. * @type mixed $term Optional term to get a description for.
  72. * @type array $args Optional arguments.
  73. * }
  74. * @param string $content Unused shortcode content.
  75. * @param string $name Shortcode name.
  76. * @return string
  77. */
  78. function webcomic_term_description_shortcode( $atts, string $content, string $name ) : string {
  79. $args = shortcode_atts(
  80. [
  81. 'term' => null,
  82. 'args' => [],
  83. ], $atts, $name
  84. );
  85. if ( is_string( $args['args'] ) ) {
  86. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  87. }
  88. return get_webcomic_term_description( $args['collection'] );
  89. }
  90. /**
  91. * Display a comic term image.
  92. *
  93. * @uses get_webcomic_term_media()
  94. * @param array $atts {
  95. * Optional attributes.
  96. *
  97. * @type string $size Optional media size.
  98. * @type mixed $term Optional term to get an image for.
  99. * @type array $args Optional arguments.
  100. * }
  101. * @param string $content Optional shortcode content; mapped to $args['size'].
  102. * @param string $name Shortcode name.
  103. * @return string
  104. */
  105. function webcomic_term_media_shortcode( $atts, string $content, string $name ) : string {
  106. $args = shortcode_atts(
  107. [
  108. 'size' => 'full',
  109. 'term' => null,
  110. 'args' => [],
  111. ], $atts, $name
  112. );
  113. if ( $content ) {
  114. $args['size'] = $content;
  115. }
  116. if ( is_string( $args['args'] ) ) {
  117. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  118. }
  119. return get_webcomic_term_media( $args['size'], $args['term'], $args['args'] );
  120. }
  121. /**
  122. * Display a comic term title.
  123. *
  124. * @uses get_webcomic_term_title()
  125. * @param array $atts {
  126. * Optional attributes.
  127. *
  128. * @type mixed $term Optional term to get a title for.
  129. * @type array $args Optional arguments.
  130. * }
  131. * @param string $content Unused shortcode content.
  132. * @param string $name Shortcode name.
  133. * @return string
  134. */
  135. function webcomic_term_title_shortcode( $atts, string $content, string $name ) : string {
  136. $args = shortcode_atts(
  137. [
  138. 'term' => null,
  139. 'args' => [],
  140. ], $atts, $name
  141. );
  142. if ( is_string( $args['args'] ) ) {
  143. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  144. }
  145. return get_webcomic_term_title( $args['term'], $args['args'] );
  146. }
  147. /**
  148. * Display a comic term updated time.
  149. *
  150. * @uses get_webcomic_term_updated()
  151. * @param array $atts {
  152. * Optional attributes.
  153. *
  154. * @type string $format Optional datetime format.
  155. * @type mixed $term Optional term to get an updated datetime for.
  156. * @type array $args Optional arguments.
  157. * }
  158. * @param string $content Optional shortcode content; mapped to $args['format'].
  159. * @param string $name Shortcode name.
  160. * @return string
  161. */
  162. function webcomic_term_updated_shortcode( $atts, string $content, string $name ) : string {
  163. $args = shortcode_atts(
  164. [
  165. 'format' => '',
  166. 'term' => null,
  167. 'args' => [],
  168. ], $atts, $name
  169. );
  170. if ( $content ) {
  171. $args['format'] = do_shortcode( htmlspecialchars_decode( $content ) );
  172. }
  173. if ( is_string( $args['args'] ) ) {
  174. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  175. }
  176. return get_webcomic_term_updated( $args['format'], $args['term'], $args['args'] );
  177. }
  178. /**
  179. * Display a comic term link.
  180. *
  181. * @uses get_webcomic_term_link()
  182. * @param array $atts {
  183. * Optional attributes.
  184. *
  185. * @type string $link Optional link text, like before{{text}}after.
  186. * @type mixed $term Optional reference term.
  187. * @type array $args Optional arguments. The shortcode name determines the
  188. * value of the relation argument.
  189. * @type mixed $post Optional reference post.
  190. * @type array $post_args Optional post arguments.
  191. * }
  192. * @param string $content Optional shortcode content; mapped to $args['link'].
  193. * @param string $name Shortcode name.
  194. * @return string
  195. */
  196. function webcomic_term_link_shortcode( $atts, string $content, string $name ) : string {
  197. $args = shortcode_atts(
  198. [
  199. 'link' => '%title',
  200. 'term' => null,
  201. 'args' => [],
  202. 'post' => null,
  203. 'post_args' => [],
  204. ], $atts, $name
  205. );
  206. if ( is_string( $args['args'] ) ) {
  207. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  208. }
  209. if ( is_string( $args['post_args'] ) ) {
  210. parse_str( htmlspecialchars_decode( $args['post_args'] ), $args['post_args'] );
  211. }
  212. $args['args']['relation'] = substr( $name, 0, strpos( $name, '_' ) );
  213. if ( $content ) {
  214. $args['link'] = do_shortcode( htmlspecialchars_decode( $content ) );
  215. }
  216. $args['link'] = htmlspecialchars_decode( $args['link'] );
  217. if ( 'webcomic' === $args['args']['relation'] ) {
  218. unset( $args['args']['relation'] );
  219. }
  220. return get_webcomic_term_link( $args['link'], $args['term'], $args['args'], $args['post'], $args['post_args'] );
  221. }
  222. /**
  223. * Display content if the post has a comic term.
  224. *
  225. * @uses has_webcomic_term()
  226. * @param array $atts {
  227. * Optional attributes.
  228. *
  229. * @type string $taxonomy Optional taxonomy to check. May be a collection ID
  230. * (like webcomic1), a type of taxonomy (like character), a type of taxonomy
  231. * prefixed with a scope keyword (like own_character or
  232. * crossover_character), or empty.
  233. * @type mixed $term Optional term to check.
  234. * @type mixed $post Optional post to check.
  235. * }
  236. * @param string $content Content to display if the post has a comic term.
  237. * @param string $name Shortcode name.
  238. * @return string
  239. */
  240. function has_webcomic_term_shortcode( $atts, string $content, string $name ) : string {
  241. if ( ! $content ) {
  242. return '';
  243. }
  244. $args = shortcode_atts(
  245. [
  246. 'taxonomy' => '',
  247. 'term' => null,
  248. 'post' => null,
  249. ], $atts, $name
  250. );
  251. if ( ! has_webcomic_term( $args['taxonomy'], $args['term'], $args['post'] ) ) {
  252. return '';
  253. }
  254. return do_shortcode( $content );
  255. }
  256. /**
  257. * Display content if the term is a comic term.
  258. *
  259. * @uses is_a_webcomic_term()
  260. * @param array $atts {
  261. * Optional attributes.
  262. *
  263. * @type mixed $term Optional term to check.
  264. * @type mixed $relative Optional reference term.
  265. * @type array $args Optional arguments. The shortcode name determines the
  266. * value of the relation argument.
  267. * }
  268. * @param string $content Content to display if the term is a comic term.
  269. * @param string $name Shortcode name.
  270. * @return string
  271. */
  272. function is_a_webcomic_term_shortcode( $atts, string $content, string $name ) : string {
  273. if ( ! $content ) {
  274. return '';
  275. }
  276. $args = shortcode_atts(
  277. [
  278. 'term' => null,
  279. 'relative' => null,
  280. 'args' => [],
  281. ], $atts, $name
  282. );
  283. if ( is_string( $args['args'] ) ) {
  284. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  285. }
  286. if ( empty( $args['args']['relation'] ) ) {
  287. $args['args']['relation'] = substr( $name, 5, strpos( $name, '_', 5 ) - 5 );
  288. if ( 'web' === $args['args']['relation'] ) {
  289. $args['args']['relation'] = '';
  290. }
  291. }
  292. if ( ! is_a_webcomic_term( $args['term'], $args['relative'], $args['args'] ) ) {
  293. return '';
  294. }
  295. return do_shortcode( $content );
  296. }
  297. /**
  298. * Display content if the query is for a comic taxonomy archive.
  299. *
  300. * @uses is_webcomic_tax()
  301. * @param array $atts {
  302. * Optional attributes.
  303. *
  304. * @type mixed $taxonomies Optional taxonomies to check for.
  305. * @type mixed $terms Optional terms to check for.
  306. * @type mixed $relative Optional reference term.
  307. * @type array $args Optional arguments. The shortcode name determines the
  308. * value of the relation argument.
  309. * }
  310. * @param string $content Content to display if the query is for a comic
  311. * taxonomy archive.
  312. * @param string $name Shortcode name.
  313. * @return string
  314. */
  315. function is_webcomic_tax_shortcode( $atts, string $content, string $name ) : string {
  316. if ( ! $content ) {
  317. return '';
  318. }
  319. $args = shortcode_atts(
  320. [
  321. 'taxonomies' => null,
  322. 'terms' => null,
  323. 'relative' => null,
  324. 'args' => [],
  325. ], $atts, $name
  326. );
  327. if ( is_string( $args['taxonomies'] ) ) {
  328. $args['taxonomies'] = preg_split( '/&|,/', $args['taxonomies'] );
  329. }
  330. if ( is_string( $args['terms'] ) ) {
  331. $args['terms'] = preg_split( '/&|,/', $args['terms'] );
  332. }
  333. if ( is_string( $args['args'] ) ) {
  334. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  335. }
  336. if ( empty( $args['args']['relation'] ) ) {
  337. $args['args']['relation'] = substr( $name, 3, strpos( $name, '_', 3 ) - 3 );
  338. if ( 'webco' === $args['args']['relation'] ) {
  339. $args['args']['relation'] = '';
  340. }
  341. }
  342. if ( ! is_webcomic_tax( $args['taxonomies'], $args['terms'], $args['relative'], $args['args'] ) ) {
  343. return '';
  344. }
  345. return do_shortcode( $content );
  346. }
  347. /**
  348. * Display a list of comic terms.
  349. *
  350. * @uses get_webcomic_terms_list()
  351. * @param array $atts {
  352. * Optional attributes.
  353. *
  354. * @type int $cloud_max Optional weighted list maximum font size.
  355. * @type int $cloud_min Optional weighted list minimum font size.
  356. * @type array $current Optional term ID of the current term or terms.
  357. * @type string $feed_type Optional term feed type; one of atom, rss, or
  358. * rss2.
  359. * @type string $feed Optional term feed link text.
  360. * @type string $format Optional flat list format, like before{{join}}after.
  361. * Including `<select>` or `<optgroup>` elements will
  362. * convert links to `<option>` elements. Using
  363. * webcomics_optgroup as a join will replace collection
  364. * links with a list of comic `<option>` elements
  365. * wrapped in an `<optgroup>`. When $hierarchical is
  366. * true, before and after are mapped to the $start and
  367. * $end arguments.
  368. * @type string $end_el Optional text to append to list items when
  369. * $hierarchical is true.
  370. * @type string $end_lvl Optional text to append to a list level when
  371. * $hierarchical is true.
  372. * @type string $end Optional text to append to the list when $hierarchical
  373. * is true.
  374. * @type array $link_args Optional arguments for term links.
  375. * @type mixed $link_post Optional reference post for term links.
  376. * @type array $link_post_args Optional post arguments for term links.
  377. * @type string $link Optional link text, like before{{text}}after.
  378. * @type string $start_el Optional text to prepend to list items when
  379. * $hierarchical is true.
  380. * @type string $start_lvl Optional text to prepend to a list level when
  381. * $hierarchical is true.
  382. * @type string $start Optional text to prepend to the list when
  383. * $hierarchical is true.
  384. * @type string $walker Optional custom Walker class to use instead of
  385. * Mgsisk\Webcomic\Taxonomy\Walker\TermLister.
  386. * @type array $webcomics Optional get_webcomics_list() arguments.
  387. * @type int $webcomics_depth Optional depth to list comics at.
  388. * @type string $collection Optional collection ID; combined with $type to
  389. * produce a taxonomy when $type is specified.
  390. * If no $collection is specified, the requested
  391. * collection (if any) will be used.
  392. *
  393. * This behavior changes if an integer
  394. * $object_ids has been specified; in this case,
  395. * $collections will be determined as follows:
  396. * - If $collection is empty, $collection will
  397. * include all collections.
  398. * - If $collection is own, $collection will be
  399. * the $object_ids collection.
  400. * - If $collection is crossover, $collection will
  401. * be all collections except the $object_ids
  402. * collection.
  403. * @type string $type Optional taxonomy type, like character or storyline.
  404. * Specifying a $type overrides any specified
  405. * $taxonomy; it will be combined with the $collection
  406. * argument to produce a taxonomy.
  407. * @type mixed $taxonomy Taxonomy name, or array of taxonomies, to which
  408. * results should be limited.
  409. * @type bool $hide_empty Whether to hide terms not assigned to any posts.
  410. * @type mixed $object_ids Optional object ID or an array of object IDs.
  411. * Results will be limited to terms associated with
  412. * these objects.
  413. * @type string $order Whether to order terms in ascending or descending
  414. * order. Accepts 'asc' (ascending) or 'desc'
  415. * (descending).
  416. * @type string $orderby Field to order terms by. Accepts term fields (
  417. * 'name', 'slug', 'term_group', 'term_id', 'id',
  418. * 'description', 'parent') and 'count' for term
  419. * taxonomy count.
  420. * @type int $depth The maximum hierarchical depth.
  421. * }
  422. * @param string $content Optional shortcode content; mapped to $args['link'].
  423. * @param string $name Shortcode name.
  424. * @return string
  425. */
  426. function webcomic_terms_list_shortcode( $atts, string $content, string $name ) : string {
  427. $args = shortcode_atts(
  428. [
  429. 'cloud_max' => 0,
  430. 'cloud_min' => 0,
  431. 'collection' => '',
  432. 'current' => 0,
  433. 'depth' => 0,
  434. 'end_el' => '',
  435. 'end_lvl' => '',
  436. 'end' => '',
  437. 'feed_type' => 'atom',
  438. 'feed' => '',
  439. 'format' => ', ',
  440. 'hide_empty' => true,
  441. 'link_args' => [],
  442. 'link_post_args' => [],
  443. 'link_post' => null,
  444. 'link' => '%title',
  445. 'object_ids' => null,
  446. 'order' => 'asc',
  447. 'orderby' => '',
  448. 'start_el' => '',
  449. 'start_lvl' => '',
  450. 'start' => '',
  451. 'taxonomy' => [],
  452. 'type' => '',
  453. 'walker' => '',
  454. 'webcomics' => [],
  455. 'webcomics_depth' => null,
  456. ], $atts, $name
  457. );
  458. $args['link'] = htmlspecialchars_decode( $args['link'] );
  459. $args['format'] = htmlspecialchars_decode( $args['format'] );
  460. $args['cloud_min'] = (int) $args['cloud_min'];
  461. $args['cloud_max'] = (int) $args['cloud_max'];
  462. if ( $content ) {
  463. $args['link'] = do_shortcode( htmlspecialchars_decode( $content ) );
  464. }
  465. if ( ! $args['orderby'] ) {
  466. unset( $args['orderby'] );
  467. }
  468. if ( is_string( $args['link_args'] ) ) {
  469. parse_str( htmlspecialchars_decode( $args['link_args'] ), $args['link_args'] );
  470. }
  471. if ( is_string( $args['link_post_args'] ) ) {
  472. parse_str( htmlspecialchars_decode( $args['link_post_args'] ), $args['link_post_args'] );
  473. }
  474. if ( is_string( $args['taxonomy'] ) ) {
  475. parse_str( htmlspecialchars_decode( $args['taxonomy'] ), $args['taxonomy'] );
  476. }
  477. if ( is_string( $args['webcomics'] ) ) {
  478. parse_str( htmlspecialchars_decode( $args['webcomics'] ), $args['webcomics'] );
  479. }
  480. return get_webcomic_terms_list( $args );
  481. }