/src/lib/collection/shortcodes.php

https://github.com/mgsisk/webcomic · PHP · 795 lines · 379 code · 90 blank · 326 comment · 50 complexity · 519a45606401d93af2dff35250b77181 MD5 · raw file

  1. <?php
  2. /**
  3. * Collection shortcodes
  4. *
  5. * @package Webcomic
  6. */
  7. namespace Mgsisk\Webcomic\Collection;
  8. /**
  9. * Add shortcodes.
  10. *
  11. * @return void
  12. */
  13. function shortcodes() {
  14. add_shortcode( 'webcomic_collection_count', __NAMESPACE__ . '\webcomic_collection_count_shortcode' );
  15. add_shortcode( 'webcomic_collection_description', __NAMESPACE__ . '\webcomic_collection_description_shortcode' );
  16. add_shortcode( 'webcomic_collection_media', __NAMESPACE__ . '\webcomic_collection_media_shortcode' );
  17. add_shortcode( 'webcomic_collection_title', __NAMESPACE__ . '\webcomic_collection_title_shortcode' );
  18. add_shortcode( 'webcomic_collection_updated', __NAMESPACE__ . '\webcomic_collection_updated_shortcode' );
  19. add_shortcode( 'webcomic_collection_link', __NAMESPACE__ . '\webcomic_collection_link_shortcode' );
  20. add_shortcode( 'webcomic_collections_list', __NAMESPACE__ . '\webcomic_collections_list_shortcode' );
  21. add_shortcode( 'webcomic_infinite_link', __NAMESPACE__ . '\webcomic_infinite_link_shortcode' );
  22. add_shortcode( 'webcomic_link', __NAMESPACE__ . '\webcomic_link_shortcode' );
  23. add_shortcode( 'first_webcomic_link', __NAMESPACE__ . '\webcomic_link_shortcode' );
  24. add_shortcode( 'previous_webcomic_link', __NAMESPACE__ . '\webcomic_link_shortcode' );
  25. add_shortcode( 'next_webcomic_link', __NAMESPACE__ . '\webcomic_link_shortcode' );
  26. add_shortcode( 'last_webcomic_link', __NAMESPACE__ . '\webcomic_link_shortcode' );
  27. add_shortcode( 'random_webcomic_link', __NAMESPACE__ . '\webcomic_link_shortcode' );
  28. add_shortcode( 'webcomic_media', __NAMESPACE__ . '\webcomic_media_shortcode' );
  29. add_shortcode( 'has_webcomic_media', __NAMESPACE__ . '\has_webcomic_media_shortcode' );
  30. add_shortcode( 'is_a_webcomic', __NAMESPACE__ . '\is_a_webcomic_shortcode' );
  31. add_shortcode( 'is_a_first_webcomic', __NAMESPACE__ . '\is_a_webcomic_shortcode' );
  32. add_shortcode( 'is_a_previous_webcomic', __NAMESPACE__ . '\is_a_webcomic_shortcode' );
  33. add_shortcode( 'is_a_next_webcomic', __NAMESPACE__ . '\is_a_webcomic_shortcode' );
  34. add_shortcode( 'is_a_last_webcomic', __NAMESPACE__ . '\is_a_webcomic_shortcode' );
  35. add_shortcode( 'is_a_webcomic_media', __NAMESPACE__ . '\is_a_webcomic_media_shortcode' );
  36. add_shortcode( 'is_a_webcomic_page', __NAMESPACE__ . '\is_a_webcomic_page_shortcode' );
  37. add_shortcode( 'is_webcomic', __NAMESPACE__ . '\is_webcomic_shortcode' );
  38. add_shortcode( 'is_first_webcomic', __NAMESPACE__ . '\is_webcomic_shortcode' );
  39. add_shortcode( 'is_previous_webcomic', __NAMESPACE__ . '\is_webcomic_shortcode' );
  40. add_shortcode( 'is_next_webcomic', __NAMESPACE__ . '\is_webcomic_shortcode' );
  41. add_shortcode( 'is_last_webcomic', __NAMESPACE__ . '\is_webcomic_shortcode' );
  42. add_shortcode( 'is_webcomic_media', __NAMESPACE__ . '\is_webcomic_media_shortcode' );
  43. add_shortcode( 'is_webcomic_page', __NAMESPACE__ . '\is_webcomic_page_shortcode' );
  44. add_shortcode( 'webcomics_list', __NAMESPACE__ . '\webcomics_list_shortcode' );
  45. }
  46. /**
  47. * Display a collection count.
  48. *
  49. * @uses get_webcomic_collection_count()
  50. * @param array $atts {
  51. * Optional attributes.
  52. *
  53. * @type mixed $collection Optional collection to display a comic count for.
  54. * }
  55. * @param string $content Unused shortcode content.
  56. * @param string $name Shortcode name.
  57. * @return string
  58. */
  59. function webcomic_collection_count_shortcode( $atts, string $content, string $name ) : string {
  60. $args = shortcode_atts(
  61. [
  62. 'collection' => null,
  63. ], $atts, $name
  64. );
  65. return (string) get_webcomic_collection_count( $args['collection'] );
  66. }
  67. /**
  68. * Display a collection description.
  69. *
  70. * @uses get_webcomic_collection_description()
  71. * @param array $atts {
  72. * Optional attributes.
  73. *
  74. * @type mixed $collection Optional collection to display a description for.
  75. * }
  76. * @param string $content Unused shortcode content.
  77. * @param string $name Shortcode name.
  78. * @return string
  79. */
  80. function webcomic_collection_description_shortcode( $atts, string $content, string $name ) : string {
  81. $args = shortcode_atts(
  82. [
  83. 'collection' => null,
  84. ], $atts, $name
  85. );
  86. return get_webcomic_collection_description( $args['collection'] );
  87. }
  88. /**
  89. * Display a collection image.
  90. *
  91. * @uses get_webcomic_collection_media()
  92. * @param array $atts {
  93. * Optional attributes.
  94. *
  95. * @type string $size Optional media size.
  96. * @type mixed $collection Optional collection to display an image for.
  97. * }
  98. * @param string $content Optional shortcode content; mapped to $args['size'].
  99. * @param string $name Shortcode name.
  100. * @return string
  101. */
  102. function webcomic_collection_media_shortcode( $atts, string $content, string $name ) : string {
  103. $args = shortcode_atts(
  104. [
  105. 'size' => 'full',
  106. 'collection' => null,
  107. ], $atts, $name
  108. );
  109. if ( $content ) {
  110. $args['size'] = $content;
  111. }
  112. return get_webcomic_collection_media( $args['size'], $args['collection'] );
  113. }
  114. /**
  115. * Display a collection title.
  116. *
  117. * @uses get_webcomic_collection_title()
  118. * @param array $atts {
  119. * Optional attributes.
  120. *
  121. * @type mixed $collection Optional collection to display a title for.
  122. * }
  123. * @param string $content Unused shortcode content.
  124. * @param string $name Shortcode name.
  125. * @return string
  126. */
  127. function webcomic_collection_title_shortcode( $atts, string $content, string $name ) : string {
  128. if ( isset( $atts['prefix'] ) ) {
  129. webcomic_error( __( 'The classic behavior of [webcomic_collection_title] is deprecated; please refer to the webcomic_collection_title_shortcode() documentation for updated usage information.', 'webcomic' ) );
  130. return webcomic_compat( 'webcomic_collection_title_shortcode_', [ $atts, $content, $name ], '' );
  131. }
  132. $args = shortcode_atts(
  133. [
  134. 'collection' => null,
  135. ], $atts, $name
  136. );
  137. return get_webcomic_collection_title( $args['collection'] );
  138. }
  139. /**
  140. * Display a collection updated time.
  141. *
  142. * @uses get_webcomic_collection_updated()
  143. * @param array $atts {
  144. * Optional attributes.
  145. *
  146. * @type string $format Optional datetime format.
  147. * @type mixed $collection Optional collection to display an updated
  148. * datetime for.
  149. * }
  150. * @param string $content Optional shortcode content; mapped to $args['format'].
  151. * @param string $name Shortcode name.
  152. * @return string
  153. */
  154. function webcomic_collection_updated_shortcode( $atts, string $content, string $name ) : string {
  155. $args = shortcode_atts(
  156. [
  157. 'format' => '',
  158. 'collection' => null,
  159. ], $atts, $name
  160. );
  161. if ( $content ) {
  162. $args['format'] = do_shortcode( htmlspecialchars_decode( $content ) );
  163. }
  164. return get_webcomic_collection_updated( $args['format'], $args['collection'] );
  165. }
  166. /**
  167. * Display a collection link.
  168. *
  169. * @uses get_webcomic_collection_link()
  170. * @param array $atts {
  171. * Optional attributes.
  172. *
  173. * @type string $link Optional link text, like 'before{{text}}after'.
  174. * @type mixed $collection Optional collection to display a link for.
  175. * @type mixed $post Optional reference post.
  176. * @type array $args Optional arguments.
  177. * }
  178. * @param string $content Optional shortcode content; mapped to $args['link'].
  179. * @param string $name Shortcode name.
  180. * @return string
  181. */
  182. function webcomic_collection_link_shortcode( $atts, string $content, string $name ) : string {
  183. if ( isset( $atts['format'] ) ) {
  184. webcomic_error( __( 'The classic behavior of [webcomic_collection_link] is deprecated; please refer to the webcomic_collection_link_shortcode() documentation for updated usage information.', 'webcomic' ) );
  185. return webcomic_compat( 'webcomic_collection_link_shortcode_', [ $atts, $content, $name ], '' );
  186. }
  187. $args = shortcode_atts(
  188. [
  189. 'link' => '%title',
  190. 'collection' => null,
  191. 'post' => null,
  192. 'args' => [],
  193. ], $atts, $name
  194. );
  195. if ( $content ) {
  196. $args['link'] = do_shortcode( htmlspecialchars_decode( $content ) );
  197. }
  198. $args['link'] = htmlspecialchars_decode( $args['link'] );
  199. if ( is_string( $args['args'] ) ) {
  200. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  201. }
  202. return get_webcomic_collection_link( $args['link'], $args['collection'], $args['post'], $args['args'] );
  203. }
  204. /**
  205. * Display a list of collections.
  206. *
  207. * @uses get_webcomic_collections_list()
  208. * @param array $atts {
  209. * Optional attributes.
  210. *
  211. * @type string $format Optional list format, like before{{join}}after.
  212. * Including `<select>` or `<optgroup>` elements will
  213. * convert links to `<option>` elements. Using
  214. * 'webcomics_optgroup' as the join will replace
  215. * collection links with a list of comic `<option>`
  216. * elements wrapped in an `<optgroup>`.
  217. * @type string $link Optional link text, like before{{text}}after.
  218. * @type mixed $link_post Optional reference post for collection links.
  219. * @type array $link_args Optional link arguments.
  220. * @type string $feed Optional collection feed link text.
  221. * @type string $feed_type Optional collection feed type; one of atom,
  222. * rss, or rss2.
  223. * @type int $cloud_min Optional weighted list minimum font size.
  224. * @type int $cloud_max Optional weighted list maximum font size.
  225. * @type string $current Optional collection ID of the current collection.
  226. * @type string $walker Optional custom Walker class to use instead of
  227. * Mgsisk\Webcomic\Collection\Walker\CollectionLister.
  228. * @type array $webcomics Optional get_webcomics_list() arguments.
  229. * @type bool $hide_empty Whether to include or exclude empty
  230. * collections.
  231. * @type int $limit Optional maximum number of collections to return.
  232. * @type mixed $not_related_by Optional taxonomies the collections must
  233. * not be related by.
  234. * @type string $order Optional collection sort order; one of asc or desc.
  235. * @type string $orderby Optional collection sort field; one of name,
  236. * slug, count, updated, or rand.
  237. * @type mixed $related_to Optional post object, term object, or
  238. * collection ID the collections must be related
  239. * to.
  240. * @type mixed $related_by Optional taxonomies the collections must be
  241. * related by.
  242. * }
  243. * @param string $content Optional shortcode content; mapped to $args['link'].
  244. * @param string $name Shortcode name.
  245. * @return string
  246. */
  247. function webcomic_collections_list_shortcode( $atts, string $content, string $name ) : string {
  248. $args = shortcode_atts(
  249. [
  250. 'cloud_max' => 0,
  251. 'cloud_min' => 0,
  252. 'current' => '',
  253. 'feed_type' => 'atom',
  254. 'feed' => '',
  255. 'format' => ', ',
  256. 'hide_empty' => true,
  257. 'limit' => 0,
  258. 'link_args' => [],
  259. 'link_post' => null,
  260. 'link' => '%title',
  261. 'not_related_by' => [],
  262. 'order' => 'asc',
  263. 'orderby' => 'name',
  264. 'related_by' => [],
  265. 'related_to' => null,
  266. 'walker' => '',
  267. 'webcomics' => [],
  268. ], $atts, $name
  269. );
  270. $args['format'] = htmlspecialchars_decode( $args['format'] );
  271. $args['cloud_min'] = (int) $args['cloud_min'];
  272. $args['cloud_max'] = (int) $args['cloud_max'];
  273. if ( $content ) {
  274. $args['link'] = do_shortcode( htmlspecialchars_decode( $content ) );
  275. }
  276. $args['link'] = htmlspecialchars_decode( $args['link'] );
  277. if ( is_string( $args['link_args'] ) ) {
  278. parse_str( htmlspecialchars_decode( $args['link_args'] ), $args['link_args'] );
  279. }
  280. if ( is_string( $args['webcomics'] ) ) {
  281. parse_str( htmlspecialchars_decode( $args['webcomics'] ), $args['webcomics'] );
  282. }
  283. if ( is_string( $args['related_to'] && ! webcomic_collection_exists( $args['related_to'] ) ) ) {
  284. $args['related_to'] = (int) $args['related_to'];
  285. }
  286. return get_webcomic_collections_list( $args );
  287. }
  288. /**
  289. * Display an infinite comic link.
  290. *
  291. * @uses get_webcomic_infinite_link()
  292. * @param array $atts {
  293. * Optional attributes.
  294. *
  295. * @type string $link Optional link text, like before{{text}}after.
  296. * }
  297. * @param string $content Optional shortcode content; mapped to $args['link'].
  298. * @param string $name Shortcode name.
  299. * @return string
  300. */
  301. function webcomic_infinite_link_shortcode( $atts, string $content, string $name ) : string {
  302. $args = shortcode_atts(
  303. [
  304. 'link' => '',
  305. ], $atts, $name
  306. );
  307. if ( $content ) {
  308. $args['link'] = do_shortcode( htmlspecialchars_decode( $content ) );
  309. }
  310. $args['link'] = htmlspecialchars_decode( $args['link'] );
  311. return get_webcomic_infinite_link( $args['link'] );
  312. }
  313. /**
  314. * Display a link to a comic.
  315. *
  316. * @uses get_webcomic_link()
  317. * @param array $atts {
  318. * Optional attributes.
  319. *
  320. * @type string $link Optional link text, like before{{text}}after.
  321. * @type mixed $post Optional post to get a link for.
  322. * @type array $args Optional arguments. The shortcode name determines the
  323. * value of the relation argument.
  324. * }
  325. * @param string $content Optional shortcode content; mapped to $args['link'].
  326. * @param string $name Shortcode name.
  327. * @return string
  328. * @suppress PhanTypeMismatchDimFetch - Array access to $args['link'] incorrectly triggers this.
  329. */
  330. function webcomic_link_shortcode( $atts, string $content, string $name ) : string {
  331. if ( isset( $atts['format'] ) || isset( $atts['in_same_term'] ) || isset( $atts['excluded_terms'] ) || isset( $atts['taxonomy'] ) || isset( $atts['collection'] ) || isset( $atts['the_post'] ) || isset( $atts['cache'] ) ) {
  332. // Translators: The shortcode name.
  333. webcomic_error( sprintf( __( 'The classic behavior of [%s] is deprecated; please refer to the webcomic_link_shortcode() documentation for updated usage information.', 'webcomic' ), $name ) );
  334. return webcomic_compat( 'webcomic_link_shortcode_', [ $atts, $content, $name ], '' );
  335. }
  336. $args = shortcode_atts(
  337. [
  338. 'link' => [
  339. 'webcomic' => '%title',
  340. 'first' => '&laquo;',
  341. 'previous' => '&lsaquo;',
  342. 'next' => '&rsaquo;',
  343. 'last' => '&raquo;',
  344. 'random' => '&infin;',
  345. ],
  346. 'post' => null,
  347. 'args' => [],
  348. ], $atts, $name
  349. );
  350. if ( is_string( $args['args'] ) ) {
  351. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  352. }
  353. $args['args']['relation'] = substr( $name, 0, strpos( $name, '_' ) );
  354. if ( $content ) {
  355. $args['link'] = do_shortcode( htmlspecialchars_decode( $content ) );
  356. } elseif ( is_array( $args['link'] ) ) {
  357. $args['link'] = $args['link'][ $args['args']['relation'] ];
  358. }
  359. $args['link'] = htmlspecialchars_decode( $args['link'] );
  360. if ( 'webcomic' === $args['args']['relation'] ) {
  361. unset( $args['args']['relation'] );
  362. }
  363. return get_webcomic_link( $args['link'], $args['post'], $args['args'] );
  364. }
  365. /**
  366. * Display comic media.
  367. *
  368. * @uses get_webcomic_media()
  369. * @param array $atts {
  370. * Optional attributes.
  371. *
  372. * @type string $format Optional media format, like
  373. * before{{join}}after{size}. Size may be be any valid
  374. * image size or a comma-separated list of width and
  375. * height pixel values (in that order), and may be
  376. * specified without the rest of the format arguments.
  377. * @type mixed $post Optional post to get comic media for.
  378. * @type array $args Optional arguments.
  379. * }
  380. * @param string $content Optional shortcode content, mapped to $atts['format'].
  381. * @param string $name Shortcode name.
  382. * @return string
  383. */
  384. function webcomic_media_shortcode( $atts, string $content, string $name ) : string {
  385. $args = shortcode_atts(
  386. [
  387. 'format' => 'full',
  388. 'post' => null,
  389. 'args' => [],
  390. ], $atts, $name
  391. );
  392. if ( $content ) {
  393. $args['format'] = do_shortcode( htmlspecialchars_decode( $content ) );
  394. }
  395. if ( is_string( $args['args'] ) ) {
  396. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  397. }
  398. return get_webcomic_media( $args['format'], $args['post'], $args['args'] );
  399. }
  400. /**
  401. * Display content if the post has comic media.
  402. *
  403. * @uses has_webcomic_media()
  404. * @param array $atts {
  405. * Optional attributes.
  406. *
  407. * @type mixed $post Optional post to check for comic media.
  408. * @type mixed $count Optional media count. May be an integer or a
  409. * comparison argument, like `< 3`. Valid comparison
  410. * operatorsare `<`, `lt`, `<=`, `le`, `>`, `gt`, `>=`,
  411. * `ge`, `==`, `=`, `eq`, `!=`, `<>`, and `ne`.
  412. * }
  413. * @param string $content Content to display if the post has comic media.
  414. * @param string $name Shortcode name.
  415. * @return string
  416. */
  417. function has_webcomic_media_shortcode( $atts, string $content, string $name ) : string {
  418. if ( ! $content ) {
  419. return '';
  420. }
  421. $args = shortcode_atts(
  422. [
  423. 'post' => null,
  424. 'count' => '',
  425. ], $atts, $name
  426. );
  427. $match = [];
  428. if ( ! has_webcomic_media( $args['post'] ) ) {
  429. return '';
  430. } elseif ( preg_match( '/^(<|lt|<=|le|>|gt|>=|ge|==|=|eq|!=|<>|ne)\s*(\d+)$/', htmlspecialchars_decode( $args['count'] ), $match ) && ! version_compare( (string) count( get_post_meta( $args['post'], 'webcomic_media' ) ), $match[2], $match[1] ) ) {
  431. return '';
  432. }
  433. return do_shortcode( $content );
  434. }
  435. /**
  436. * Display content if the post is a comic.
  437. *
  438. * @uses is_a_webcomic()
  439. * @param array $atts {
  440. * Optional attributes.
  441. *
  442. * @type mixed $post Optional post to check.
  443. * @type mixed $relative Optional reference post.
  444. * @type array $args Optional arguments. The shortcode name determines the
  445. * value of the relation argument.
  446. * }
  447. * @param string $content Content to display if the post is a comic.
  448. * @param string $name Shortcode name.
  449. * @return string
  450. */
  451. function is_a_webcomic_shortcode( $atts, string $content, string $name ) : string {
  452. if ( ! $content ) {
  453. return '';
  454. }
  455. $args = shortcode_atts(
  456. [
  457. 'post' => null,
  458. 'relative' => null,
  459. 'args' => [],
  460. ], $atts, $name
  461. );
  462. if ( is_string( $args['args'] ) ) {
  463. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  464. }
  465. if ( empty( $args['args']['relation'] ) ) {
  466. $args['args']['relation'] = substr( $name, 5, strpos( $name, '_', 5 ) - 5 );
  467. if ( 'web' === $args['args']['relation'] ) {
  468. $args['args']['relation'] = '';
  469. }
  470. }
  471. if ( ! is_a_webcomic( $args['post'], $args['relative'], $args['args'] ) ) {
  472. return '';
  473. }
  474. return do_shortcode( $content );
  475. }
  476. /**
  477. * Display content if the post is comic media.
  478. *
  479. * @uses is_a_webcomic_media()
  480. * @param array $atts {
  481. * Optional attributes.
  482. *
  483. * @type mixed $post Optional post to check.
  484. * @type array $collections Optional collections to check for.
  485. * }
  486. * @param string $content Content to display if the post is comic media.
  487. * @param string $name Shortcode name.
  488. * @return string
  489. */
  490. function is_a_webcomic_media_shortcode( $atts, string $content, string $name ) : string {
  491. if ( ! $content ) {
  492. return '';
  493. }
  494. $args = shortcode_atts(
  495. [
  496. 'post' => null,
  497. 'collections' => [],
  498. ], $atts, $name
  499. );
  500. if ( ! is_array( $args['collections'] ) ) {
  501. $args['collections'] = explode( ',', $args['collections'] );
  502. }
  503. if ( ! is_a_webcomic_media( $args['post'], $args['collections'] ) ) {
  504. return '';
  505. }
  506. return do_shortcode( $content );
  507. }
  508. /**
  509. * Display content if the page is related to a comic collection.
  510. *
  511. * @uses is_a_webcomic_page()
  512. * @param array $atts {
  513. * Optional attributes.
  514. *
  515. * @type mixed $page Optional page to check.
  516. * @type mixed $collections Optional collections to check for.
  517. * }
  518. * @param string $content Content to display if the page is related to a comic
  519. * collection.
  520. * @param string $name Shortcode name.
  521. * @return string
  522. */
  523. function is_a_webcomic_page_shortcode( $atts, string $content, string $name ) : string {
  524. if ( ! $content ) {
  525. return '';
  526. }
  527. $args = shortcode_atts(
  528. [
  529. 'page' => null,
  530. 'collections' => null,
  531. ], $atts, $name
  532. );
  533. if ( ! is_a_webcomic_page( $args['page'], $args['collections'] ) ) {
  534. return '';
  535. }
  536. return do_shortcode( $content );
  537. }
  538. /**
  539. * Display content if the query is for a comic.
  540. *
  541. * @uses is_webcomic()
  542. * @param array $atts {
  543. * Optional attributes.
  544. *
  545. * @type mixed $collections Optional collections to check for.
  546. * @type mixed $posts Optional posts to check for.
  547. * @type mixed $relative Optional reference post.
  548. * @type array $args Optional arguments. The shortcode name determines the
  549. * value of the relation argument.
  550. * }
  551. * @param string $content Content to display if the query is for a comic.
  552. * @param string $name Shortcode name.
  553. * @return string
  554. */
  555. function is_webcomic_shortcode( $atts, string $content, string $name ) : string {
  556. if ( ! $content ) {
  557. return '';
  558. }
  559. $args = shortcode_atts(
  560. [
  561. 'collections' => null,
  562. 'posts' => null,
  563. 'relative' => null,
  564. 'args' => [],
  565. ], $atts, $name
  566. );
  567. if ( is_string( $args['args'] ) ) {
  568. parse_str( htmlspecialchars_decode( $args['args'] ), $args['args'] );
  569. }
  570. if ( empty( $args['args']['relation'] ) ) {
  571. $args['args']['relation'] = substr( $name, 3, strpos( $name, '_', 3 ) - 3 );
  572. if ( 'webco' === $args['args']['relation'] ) {
  573. $args['args']['relation'] = '';
  574. }
  575. }
  576. if ( ! is_webcomic( $args['collections'], $args['posts'], $args['relative'], $args['args'] ) ) {
  577. return '';
  578. }
  579. return do_shortcode( $content );
  580. }
  581. /**
  582. * Display content if the query is for comic media.
  583. *
  584. * @uses is_webcomic_media()
  585. * @param array $atts {
  586. * Optional attributes.
  587. *
  588. * @type array $collections Optional collections to check for.
  589. * }
  590. * @param string $content Content to display if the query is for comic media.
  591. * @param string $name Shortcode name.
  592. * @return string
  593. */
  594. function is_webcomic_media_shortcode( $atts, string $content, string $name ) : string {
  595. if ( ! $content ) {
  596. return '';
  597. }
  598. $args = shortcode_atts(
  599. [
  600. 'collections' => [],
  601. ], $atts, $name
  602. );
  603. if ( ! is_array( $args['collections'] ) ) {
  604. $args['collections'] = explode( ',', $args['collections'] );
  605. }
  606. if ( ! is_webcomic_media( $args['collections'] ) ) {
  607. return '';
  608. }
  609. return do_shortcode( $content );
  610. }
  611. /**
  612. * Display content if the query is for a page related to a comic collection.
  613. *
  614. * @uses is_webcomic_page()
  615. * @param array $atts {
  616. * Optional attributes.
  617. *
  618. * @type mixed $collections Optional collections to check for.
  619. * @type mixed $pages Optional pages to check for.
  620. * }
  621. * @param string $content Content to display if the query is for a page related
  622. * to a comic collection.
  623. * @param string $name Shortcode name.
  624. * @return string
  625. */
  626. function is_webcomic_page_shortcode( $atts, string $content, string $name ) : string {
  627. if ( ! $content ) {
  628. return '';
  629. }
  630. $args = shortcode_atts(
  631. [
  632. 'collections' => null,
  633. 'pages' => null,
  634. ], $atts, $name
  635. );
  636. if ( ! is_webcomic_page( $args['collections'], $args['pages'] ) ) {
  637. return '';
  638. }
  639. return do_shortcode( $content );
  640. }
  641. /**
  642. * Display a list of comics.
  643. *
  644. * @uses get_webcomics_list()
  645. * @param array $atts {
  646. * Optional attributes.
  647. *
  648. * @type string $format Optional list format, like before{{join}}after.
  649. * Including `<select>` or `<optgroup>` elements will
  650. * convert links to `<option>` elements.
  651. * @type string $link Optional link text, like before{{text}}after.
  652. * @type array $link_args Optional link arguments.
  653. * @type int $cloud_min Optional weighted list minimum font size.
  654. * @type int $cloud_max Optional weighted list maximum font size.
  655. * @type int $current Optional post ID of the current comic.
  656. * @type string $walker Optional custom Walker class to use instead of
  657. * Mgsisk\Webcomic\Collection\Walker\ComicLister.
  658. * @type mixed $related_to Optional post the comics must be related to.
  659. * @type mixed $related_by Optional taxonomies the comics must be related
  660. * by.
  661. * @type mixed $not_related_by Optional taxonomies the comics must not be
  662. * related by.
  663. * @type string $order Optional post sort order; one of asc or desc.
  664. * @type string $orderby Optional post sort field; one of date, none, name,
  665. * author, title, modified, menu_order, parent, ID,
  666. * rand, relevance, or comment_count.
  667. * @type int $posts_per_page Optional number of posts to retrieve.
  668. * }
  669. * @param string $content Optional shortcode content; mapped to $args['link'].
  670. * @param string $name Shortcode name.
  671. * @return string
  672. */
  673. function webcomics_list_shortcode( $atts, string $content, string $name ) : string {
  674. $args = shortcode_atts(
  675. [
  676. 'cloud_max' => 0,
  677. 'cloud_min' => 0,
  678. 'current' => 0,
  679. 'format' => ', ',
  680. 'link_args' => [],
  681. 'link' => '%title',
  682. 'not_related_by' => [],
  683. 'order' => 'asc',
  684. 'orderby' => 'date',
  685. 'posts_per_page' => -1,
  686. 'related_by' => [],
  687. 'related_to' => null,
  688. 'walker' => '',
  689. ], $atts, $name
  690. );
  691. $args['link'] = htmlspecialchars_decode( $args['link'] );
  692. $args['format'] = htmlspecialchars_decode( $args['format'] );
  693. $args['cloud_min'] = (int) $args['cloud_min'];
  694. $args['cloud_max'] = (int) $args['cloud_max'];
  695. if ( $content ) {
  696. $args['link'] = do_shortcode( htmlspecialchars_decode( $content ) );
  697. }
  698. if ( is_string( $args['link_args'] ) ) {
  699. parse_str( htmlspecialchars_decode( $args['link_args'] ), $args['link_args'] );
  700. }
  701. if ( ! is_array( $args['orderby'] ) ) {
  702. $args['orderby'] = explode( ',', $args['orderby'] );
  703. }
  704. return get_webcomics_list( $args );
  705. }