shortcode.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. <?php
  2. /**
  3. * @group shortcode
  4. */
  5. class Tests_Shortcode extends WP_UnitTestCase {
  6. protected $shortcodes = array( 'test-shortcode-tag', 'footag', 'bartag', 'baztag', 'dumptag', 'hyphen', 'hyphen-foo', 'hyphen-foo-bar', 'url', 'img' );
  7. function setUp() {
  8. parent::setUp();
  9. foreach ( $this->shortcodes as $shortcode ) {
  10. add_shortcode( $shortcode, array( $this, '_shortcode_' . str_replace( '-', '_', $shortcode ) ) );
  11. }
  12. $this->atts = null;
  13. $this->content = null;
  14. $this->tagname = null;
  15. }
  16. function tearDown() {
  17. global $shortcode_tags;
  18. foreach ( $this->shortcodes as $shortcode ) {
  19. unset( $shortcode_tags[ $shortcode ] );
  20. }
  21. parent::tearDown();
  22. }
  23. function _shortcode_test_shortcode_tag( $atts, $content = null, $tagname = null ) {
  24. $this->atts = $atts;
  25. $this->content = $content;
  26. $this->tagname = $tagname;
  27. $this->filter_atts_out = null;
  28. $this->filter_atts_pairs = null;
  29. $this->filter_atts_atts = null;
  30. }
  31. // [footag foo="bar"]
  32. function _shortcode_footag( $atts ) {
  33. $foo = isset( $atts['foo'] ) ? $atts['foo'] : '';
  34. return "foo = $foo";
  35. }
  36. // [bartag foo="bar"]
  37. function _shortcode_bartag( $atts ) {
  38. $processed_atts = shortcode_atts(
  39. array(
  40. 'foo' => 'no foo',
  41. 'baz' => 'default baz',
  42. ),
  43. $atts,
  44. 'bartag'
  45. );
  46. return "foo = {$processed_atts['foo']}";
  47. }
  48. // [baztag]content[/baztag]
  49. function _shortcode_baztag( $atts, $content = '' ) {
  50. return 'content = ' . do_shortcode( $content );
  51. }
  52. function _shortcode_dumptag( $atts ) {
  53. $out = '';
  54. foreach ( $atts as $k => $v ) {
  55. $out .= "$k = $v\n";
  56. }
  57. return $out;
  58. }
  59. function _shortcode_hyphen() {
  60. return __FUNCTION__;
  61. }
  62. function _shortcode_hyphen_foo() {
  63. return __FUNCTION__;
  64. }
  65. function _shortcode_hyphen_foo_bar() {
  66. return __FUNCTION__;
  67. }
  68. function _shortcode_url() {
  69. return 'http://www.wordpress.org/';
  70. }
  71. function _shortcode_img( $atts ) {
  72. $out = '<img';
  73. foreach ( $atts as $k => $v ) {
  74. $out .= " $k=\"$v\"";
  75. }
  76. $out .= ' />';
  77. return $out;
  78. }
  79. function test_noatts() {
  80. do_shortcode( '[test-shortcode-tag /]' );
  81. $this->assertSame( '', $this->atts );
  82. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  83. }
  84. function test_one_att() {
  85. do_shortcode( '[test-shortcode-tag foo="asdf" /]' );
  86. $this->assertSame( array( 'foo' => 'asdf' ), $this->atts );
  87. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  88. }
  89. function test_not_a_tag() {
  90. $out = do_shortcode( '[not-a-shortcode-tag]' );
  91. $this->assertSame( '[not-a-shortcode-tag]', $out );
  92. }
  93. /**
  94. * @ticket 17657
  95. */
  96. function test_tag_hyphen_not_tag() {
  97. $out = do_shortcode( '[dumptag-notreal]' );
  98. $this->assertSame( '[dumptag-notreal]', $out );
  99. }
  100. function test_tag_underscore_not_tag() {
  101. $out = do_shortcode( '[dumptag_notreal]' );
  102. $this->assertSame( '[dumptag_notreal]', $out );
  103. }
  104. function test_tag_not_tag() {
  105. $out = do_shortcode( '[dumptagnotreal]' );
  106. $this->assertSame( '[dumptagnotreal]', $out );
  107. }
  108. /**
  109. * @ticket 17657
  110. */
  111. function test_tag_hyphen() {
  112. $this->assertSame( '_shortcode_hyphen', do_shortcode( '[hyphen]' ) );
  113. $this->assertSame( '_shortcode_hyphen_foo', do_shortcode( '[hyphen-foo]' ) );
  114. $this->assertSame( '_shortcode_hyphen_foo_bar', do_shortcode( '[hyphen-foo-bar]' ) );
  115. $this->assertSame( '[hyphen-baz]', do_shortcode( '[hyphen-baz]' ) );
  116. $this->assertSame( '[hyphen-foo-bar-baz]', do_shortcode( '[hyphen-foo-bar-baz]' ) );
  117. }
  118. /**
  119. * @ticket 9405
  120. */
  121. function test_attr_hyphen() {
  122. do_shortcode( '[test-shortcode-tag foo="foo" foo-bar="foo-bar" foo-bar-="foo-bar-" -foo-bar="-foo-bar" -foo-bar-="-foo-bar-" foo-bar-baz="foo-bar-baz" -foo-bar-baz="-foo-bar-baz" foo--bar="foo--bar" /]' );
  123. $expected_attrs = array(
  124. 'foo' => 'foo',
  125. 'foo-bar' => 'foo-bar',
  126. 'foo-bar-' => 'foo-bar-',
  127. '-foo-bar' => '-foo-bar',
  128. '-foo-bar-' => '-foo-bar-',
  129. 'foo-bar-baz' => 'foo-bar-baz',
  130. '-foo-bar-baz' => '-foo-bar-baz',
  131. 'foo--bar' => 'foo--bar',
  132. );
  133. $this->assertSame( $expected_attrs, $this->atts );
  134. }
  135. function test_two_atts() {
  136. do_shortcode( '[test-shortcode-tag foo="asdf" bar="bing" /]' );
  137. $this->assertSame(
  138. array(
  139. 'foo' => 'asdf',
  140. 'bar' => 'bing',
  141. ),
  142. $this->atts
  143. );
  144. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  145. }
  146. function test_noatts_enclosing() {
  147. do_shortcode( '[test-shortcode-tag]content[/test-shortcode-tag]' );
  148. $this->assertSame( '', $this->atts );
  149. $this->assertSame( 'content', $this->content );
  150. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  151. }
  152. function test_one_att_enclosing() {
  153. do_shortcode( '[test-shortcode-tag foo="bar"]content[/test-shortcode-tag]' );
  154. $this->assertSame( array( 'foo' => 'bar' ), $this->atts );
  155. $this->assertSame( 'content', $this->content );
  156. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  157. }
  158. function test_two_atts_enclosing() {
  159. do_shortcode( '[test-shortcode-tag foo="bar" baz="bing"]content[/test-shortcode-tag]' );
  160. $this->assertSame(
  161. array(
  162. 'foo' => 'bar',
  163. 'baz' => 'bing',
  164. ),
  165. $this->atts
  166. );
  167. $this->assertSame( 'content', $this->content );
  168. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  169. }
  170. function test_unclosed() {
  171. $out = do_shortcode( '[test-shortcode-tag]' );
  172. $this->assertSame( '', $out );
  173. $this->assertSame( '', $this->atts );
  174. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  175. }
  176. function test_positional_atts_num() {
  177. $out = do_shortcode( '[test-shortcode-tag 123]' );
  178. $this->assertSame( '', $out );
  179. $this->assertSame( array( 0 => '123' ), $this->atts );
  180. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  181. }
  182. function test_positional_atts_url() {
  183. $out = do_shortcode( '[test-shortcode-tag https://www.youtube.com/watch?v=72xdCU__XCk]' );
  184. $this->assertSame( '', $out );
  185. $this->assertSame( array( 0 => 'https://www.youtube.com/watch?v=72xdCU__XCk' ), $this->atts );
  186. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  187. }
  188. function test_positional_atts_quotes() {
  189. $out = do_shortcode( '[test-shortcode-tag "something in quotes" "something else"]' );
  190. $this->assertSame( '', $out );
  191. $this->assertSame(
  192. array(
  193. 0 => 'something in quotes',
  194. 1 => 'something else',
  195. ),
  196. $this->atts
  197. );
  198. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  199. }
  200. function test_positional_atts_mixed() {
  201. $out = do_shortcode( '[test-shortcode-tag 123 https://wordpress.org/ 0 "foo" bar]' );
  202. $this->assertSame( '', $out );
  203. $this->assertSame(
  204. array(
  205. 0 => '123',
  206. 1 => 'https://wordpress.org/',
  207. 2 => '0',
  208. 3 => 'foo',
  209. 4 => 'bar',
  210. ),
  211. $this->atts
  212. );
  213. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  214. }
  215. function test_positional_and_named_atts() {
  216. $out = do_shortcode( '[test-shortcode-tag 123 url=https://wordpress.org/ foo bar="baz"]' );
  217. $this->assertSame( '', $out );
  218. $this->assertSame(
  219. array(
  220. 0 => '123',
  221. 'url' => 'https://wordpress.org/',
  222. 1 => 'foo',
  223. 'bar' => 'baz',
  224. ),
  225. $this->atts
  226. );
  227. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  228. }
  229. function test_footag_default() {
  230. $out = do_shortcode( '[footag]' );
  231. $this->assertSame( 'foo = ', $out );
  232. }
  233. function test_footag_val() {
  234. $val = rand_str();
  235. $out = do_shortcode( '[footag foo="' . $val . '"]' );
  236. $this->assertSame( 'foo = ' . $val, $out );
  237. }
  238. function test_nested_tags() {
  239. $out = do_shortcode( '[baztag][dumptag abc="foo" def=123 https://wordpress.org/][/baztag]' );
  240. $expected = "content = abc = foo\ndef = 123\n0 = https://wordpress.org\n";
  241. $this->assertSame( $expected, $out );
  242. }
  243. /**
  244. * @ticket 6518
  245. */
  246. function test_tag_escaped() {
  247. $out = do_shortcode( '[[footag]] [[bartag foo="bar"]]' );
  248. $this->assertSame( '[footag] [bartag foo="bar"]', $out );
  249. $out = do_shortcode( '[[footag /]] [[bartag foo="bar" /]]' );
  250. $this->assertSame( '[footag /] [bartag foo="bar" /]', $out );
  251. $out = do_shortcode( '[[baztag foo="bar"]the content[/baztag]]' );
  252. $this->assertSame( '[baztag foo="bar"]the content[/baztag]', $out );
  253. // Double escaped.
  254. $out = do_shortcode( '[[[footag]]] [[[bartag foo="bar"]]]' );
  255. $this->assertSame( '[[footag]] [[bartag foo="bar"]]', $out );
  256. }
  257. function test_tag_not_escaped() {
  258. // These have square brackets on either end but aren't actually escaped.
  259. $out = do_shortcode( '[[footag] [bartag foo="bar"]]' );
  260. $this->assertSame( '[foo = foo = bar]', $out );
  261. $out = do_shortcode( '[[footag /] [bartag foo="bar" /]]' );
  262. $this->assertSame( '[foo = foo = bar]', $out );
  263. $out = do_shortcode( '[[baztag foo="bar"]the content[/baztag]' );
  264. $this->assertSame( '[content = the content', $out );
  265. $out = do_shortcode( '[[not-a-tag]]' );
  266. $this->assertSame( '[[not-a-tag]]', $out );
  267. $out = do_shortcode( '[[[footag] [bartag foo="bar"]]]' );
  268. $this->assertSame( '[[foo = foo = bar]]', $out );
  269. }
  270. function test_mixed_tags() {
  271. $in = <<<EOF
  272. So this is a post with [footag foo="some stuff"] and a bunch of tags.
  273. [bartag]
  274. [baztag]
  275. Here's some content
  276. on more than one line
  277. [/baztag]
  278. [bartag foo=1] [baztag] [footag foo="2"] [baztag]
  279. [baztag]
  280. more content
  281. [/baztag]
  282. EOF;
  283. $expected = <<<EOF
  284. So this is a post with foo = some stuff and a bunch of tags.
  285. foo = no foo
  286. content =
  287. Here's some content
  288. on more than one line
  289. foo = 1 content = foo = 2 content =
  290. content =
  291. more content
  292. EOF;
  293. $out = do_shortcode( $in );
  294. $this->assertSame( strip_ws( $expected ), strip_ws( $out ) );
  295. }
  296. /**
  297. * @ticket 6562
  298. */
  299. function test_utf8_whitespace_1() {
  300. // NO-BREAK SPACE: U+00A0.
  301. do_shortcode( "[test-shortcode-tag foo=\"bar\" \xC2\xA0baz=\"123\"]" );
  302. $this->assertSame(
  303. array(
  304. 'foo' => 'bar',
  305. 'baz' => '123',
  306. ),
  307. $this->atts
  308. );
  309. $this->assertSame( '', $this->content );
  310. }
  311. /**
  312. * @ticket 6562
  313. */
  314. function test_utf8_whitespace_2() {
  315. // ZERO WIDTH SPACE: U+200B.
  316. do_shortcode( "[test-shortcode-tag foo=\"bar\" \xE2\x80\x8Babc=\"def\"]" );
  317. $this->assertSame(
  318. array(
  319. 'foo' => 'bar',
  320. 'abc' => 'def',
  321. ),
  322. $this->atts
  323. );
  324. $this->assertSame( '', $this->content );
  325. }
  326. /**
  327. * @ticket 14050
  328. */
  329. function test_shortcode_unautop() {
  330. // A blank line is added at the end, so test with it already there.
  331. $test_string = "[footag]\n";
  332. $this->assertSame( $test_string, shortcode_unautop( wpautop( $test_string ) ) );
  333. }
  334. function data_test_strip_shortcodes() {
  335. return array(
  336. array( 'before', 'before[gallery]' ),
  337. array( 'after', '[gallery]after' ),
  338. array( 'beforeafter', 'before[gallery]after' ),
  339. array( 'before[after', 'before[after' ),
  340. array( 'beforeafter', 'beforeafter' ),
  341. array( 'beforeafter', 'before[gallery id="123" size="medium"]after' ),
  342. array( 'before[unregistered_shortcode]after', 'before[unregistered_shortcode]after' ),
  343. array( 'beforeafter', 'before[footag]after' ),
  344. array( 'before after', 'before [footag]content[/footag] after' ),
  345. array( 'before after', 'before [footag foo="123"]content[/footag] after' ),
  346. );
  347. }
  348. /**
  349. * @ticket 10326
  350. *
  351. * @dataProvider data_test_strip_shortcodes
  352. *
  353. * @param string $expected Expected output.
  354. * @param string $content Content to run strip_shortcodes() on.
  355. */
  356. function test_strip_shortcodes( $expected, $content ) {
  357. $this->assertSame( $expected, strip_shortcodes( $content ) );
  358. }
  359. /**
  360. * @ticket 37767
  361. */
  362. function test_strip_shortcodes_filter() {
  363. add_filter( 'strip_shortcodes_tagnames', array( $this, '_filter_strip_shortcodes_tagnames' ) );
  364. $this->assertSame( 'beforemiddle [footag]after', strip_shortcodes( 'before[gallery]middle [footag]after' ) );
  365. remove_filter( 'strip_shortcodes_tagnames', array( $this, '_filter_strip_shortcodes_tagnames' ) );
  366. }
  367. function _filter_strip_shortcodes_tagnames() {
  368. return array( 'gallery' );
  369. }
  370. // Store passed in shortcode_atts_{$shortcode} args.
  371. function _filter_atts( $out, $pairs, $atts ) {
  372. $this->filter_atts_out = $out;
  373. $this->filter_atts_pairs = $pairs;
  374. $this->filter_atts_atts = $atts;
  375. return $out;
  376. }
  377. // Filter shortcode atts in various ways.
  378. function _filter_atts2( $out, $pairs, $atts ) {
  379. // If foo attribute equals "foo1", change it to be default value.
  380. if ( isset( $out['foo'] ) && 'foo1' === $out['foo'] ) {
  381. $out['foo'] = $pairs['foo'];
  382. }
  383. // If baz attribute is set, remove it.
  384. if ( isset( $out['baz'] ) ) {
  385. unset( $out['baz'] );
  386. }
  387. $this->filter_atts_out = $out;
  388. return $out;
  389. }
  390. function test_shortcode_atts_filter_passes_original_arguments() {
  391. add_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts' ), 10, 3 );
  392. do_shortcode( '[bartag foo="foo1" /]' );
  393. $this->assertSame(
  394. array(
  395. 'foo' => 'foo1',
  396. 'baz' => 'default baz',
  397. ),
  398. $this->filter_atts_out
  399. );
  400. $this->assertSame(
  401. array(
  402. 'foo' => 'no foo',
  403. 'baz' => 'default baz',
  404. ),
  405. $this->filter_atts_pairs
  406. );
  407. $this->assertSame( array( 'foo' => 'foo1' ), $this->filter_atts_atts );
  408. remove_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts' ), 10, 3 );
  409. }
  410. function test_shortcode_atts_filtering() {
  411. add_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts2' ), 10, 3 );
  412. $out = do_shortcode( '[bartag foo="foo1" baz="baz1" /]' );
  413. $this->assertSame( array( 'foo' => 'no foo' ), $this->filter_atts_out );
  414. $this->assertSame( 'foo = no foo', $out );
  415. $out = do_shortcode( '[bartag foo="foo2" /]' );
  416. $this->assertSame( 'foo = foo2', $out );
  417. remove_filter( 'shortcode_atts_bartag', array( $this, '_filter_atts2' ), 10, 3 );
  418. }
  419. /**
  420. * Check that shortcode_unautop() will always recognize spaces around shortcodes.
  421. *
  422. * @ticket 22692
  423. */
  424. function test_spaces_around_shortcodes() {
  425. $nbsp = "\xC2\xA0";
  426. $input = array();
  427. $input[] = '<p>[gallery ids="37,15,11"]</p>';
  428. $input[] = '<p> [gallery ids="37,15,11"] </p>';
  429. $input[] = "<p> {$nbsp}[gallery ids=\"37,15,11\"] {$nbsp}</p>";
  430. $input[] = '<p> &nbsp;[gallery ids="37,15,11"] &nbsp;</p>';
  431. $output = '[gallery ids="37,15,11"]';
  432. foreach ( $input as $in ) {
  433. $this->assertSame( $output, shortcode_unautop( $in ) );
  434. }
  435. }
  436. /**
  437. * Check for bugginess using normal input with latest patches.
  438. *
  439. * @dataProvider data_escaping
  440. */
  441. function test_escaping( $input, $output ) {
  442. return $this->assertSame( $output, do_shortcode( $input ) );
  443. }
  444. function data_escaping() {
  445. return array(
  446. array(
  447. '<!--[if lt IE 7]>',
  448. '<!--[if lt IE 7]>',
  449. ),
  450. array(
  451. '1 <a href="[test-shortcode-tag]"> 2 <a href="[test-shortcode-tag]" >',
  452. '1 <a href=""> 2 <a href="" >',
  453. ),
  454. array(
  455. '1 <a noise="[test-shortcode-tag]"> 2 <a noise=" [test-shortcode-tag] " >',
  456. '1 <a noise="[test-shortcode-tag]"> 2 <a noise=" [test-shortcode-tag] " >',
  457. ),
  458. array(
  459. '[gallery title="<div>hello</div>"]',
  460. '',
  461. ),
  462. array(
  463. '[caption caption="test" width="2"]<div>hello</div>[/caption]',
  464. '<div style="width: 12px" class="wp-caption alignnone"><div>hello</div><p class="wp-caption-text">test</p></div>',
  465. ),
  466. array(
  467. '<div [gallery]>',
  468. '<div >',
  469. ),
  470. array(
  471. '<div [[gallery]]>',
  472. '<div [gallery]>',
  473. ),
  474. array(
  475. '<[[gallery]]>',
  476. '<[gallery]>',
  477. ),
  478. array(
  479. '<div style="selector:url([[gallery]])">',
  480. '<div style="selector:url([[gallery]])">',
  481. ),
  482. array(
  483. '[gallery]<div>Hello</div>[/gallery]',
  484. '',
  485. ),
  486. array(
  487. '[url]',
  488. 'http://www.wordpress.org/',
  489. ),
  490. array(
  491. '<a href="[url]">',
  492. '<a href="http://www.wordpress.org/">',
  493. ),
  494. array(
  495. '<a href=[url] >',
  496. '<a href=http://www.wordpress.org/ >',
  497. ),
  498. array(
  499. '<a href="[url]plugins/">',
  500. '<a href="http://www.wordpress.org/plugins/">',
  501. ),
  502. array(
  503. '<a href="bad[url]">',
  504. '<a href="//www.wordpress.org/">',
  505. ),
  506. array(
  507. '<a onclick="bad[url]">',
  508. '<a onclick="bad[url]">',
  509. ),
  510. );
  511. }
  512. /**
  513. * Check for bugginess using normal input with latest patches.
  514. *
  515. * @dataProvider data_escaping2
  516. */
  517. function test_escaping2( $input, $output ) {
  518. return $this->assertSame( $output, strip_shortcodes( $input ) );
  519. }
  520. function data_escaping2() {
  521. return array(
  522. array(
  523. '<!--[if lt IE 7]>',
  524. '<!--[if lt IE 7]>',
  525. ),
  526. array(
  527. '[gallery title="<div>hello</div>"]',
  528. '',
  529. ),
  530. array(
  531. '[caption caption="test" width="2"]<div>hello</div>[/caption]',
  532. '',
  533. ),
  534. array(
  535. '<div [gallery]>', // Shortcodes will never be stripped inside elements.
  536. '<div [gallery]>',
  537. ),
  538. array(
  539. '<div [[gallery]]>', // Shortcodes will never be stripped inside elements.
  540. '<div [[gallery]]>',
  541. ),
  542. array(
  543. '<[[gallery]]>',
  544. '<[[gallery]]>',
  545. ),
  546. array(
  547. '[gallery]<div>Hello</div>[/gallery]',
  548. '',
  549. ),
  550. );
  551. }
  552. /**
  553. * @ticket 26343
  554. */
  555. function test_has_shortcode() {
  556. $content = 'This is a blob with [gallery] in it';
  557. $this->assertTrue( has_shortcode( $content, 'gallery' ) );
  558. add_shortcode( 'foo', '__return_false' );
  559. $content_nested = 'This is a blob with [foo] [gallery] [/foo] in it';
  560. $this->assertTrue( has_shortcode( $content_nested, 'gallery' ) );
  561. remove_shortcode( 'foo' );
  562. }
  563. /**
  564. * Make sure invalid shortcode names are not allowed.
  565. *
  566. * @dataProvider data_registration_bad
  567. * @expectedIncorrectUsage add_shortcode
  568. */
  569. function test_registration_bad( $input, $expected ) {
  570. return $this->sub_registration( $input, $expected );
  571. }
  572. /**
  573. * Make sure valid shortcode names are allowed.
  574. *
  575. * @dataProvider data_registration_good
  576. */
  577. function test_registration_good( $input, $expected ) {
  578. return $this->sub_registration( $input, $expected );
  579. }
  580. function sub_registration( $input, $expected ) {
  581. add_shortcode( $input, '' );
  582. $actual = shortcode_exists( $input );
  583. $test = $this->assertSame( $expected, $actual );
  584. if ( $actual ) {
  585. remove_shortcode( $input );
  586. }
  587. return $test;
  588. }
  589. function data_registration_bad() {
  590. return array(
  591. array(
  592. '<html>',
  593. false,
  594. ),
  595. array(
  596. '[shortcode]',
  597. false,
  598. ),
  599. array(
  600. 'bad/',
  601. false,
  602. ),
  603. array(
  604. '/bad',
  605. false,
  606. ),
  607. array(
  608. 'bad space',
  609. false,
  610. ),
  611. array(
  612. '&amp;',
  613. false,
  614. ),
  615. array(
  616. '',
  617. false,
  618. ),
  619. );
  620. }
  621. function data_registration_good() {
  622. return array(
  623. array(
  624. 'good!',
  625. true,
  626. ),
  627. array(
  628. 'plain',
  629. true,
  630. ),
  631. array(
  632. 'unreserved!#$%()*+,-.;?@^_{|}~chars',
  633. true,
  634. ),
  635. );
  636. }
  637. /**
  638. * Automated performance testing of the main regex.
  639. *
  640. * @dataProvider data_whole_posts
  641. */
  642. function test_pcre_performance( $input ) {
  643. $regex = '/' . get_shortcode_regex() . '/';
  644. $result = benchmark_pcre_backtracking( $regex, $input, 'match_all' );
  645. return $this->assertLessThan( 200, $result );
  646. }
  647. function data_whole_posts() {
  648. require_once DIR_TESTDATA . '/formatting/whole-posts.php';
  649. return data_whole_posts();
  650. }
  651. function test_php_and_js_shortcode_attribute_regexes_match() {
  652. $file = file_get_contents( ABSPATH . 'js/_enqueues/wp/shortcode.js' );
  653. $matched = preg_match( '|\s+pattern = (\/.+\/)g;|', $file, $matches );
  654. $php = get_shortcode_atts_regex();
  655. $this->assertSame( 1, $matched );
  656. $js = str_replace( "\'", "'", $matches[1] );
  657. $this->assertSame( $php, $js );
  658. }
  659. /**
  660. * @ticket 34939
  661. *
  662. * Test the (not recommended) [shortcode=XXX] format
  663. */
  664. function test_unnamed_attribute() {
  665. $out = do_shortcode( '[dumptag=https://wordpress.org/]' );
  666. $expected = "0 = =https://wordpress.org\n";
  667. $this->assertSame( $expected, $out );
  668. }
  669. /**
  670. * @ticket 36306
  671. */
  672. function test_smilies_arent_converted() {
  673. $out = apply_filters( 'the_content', '[img alt="Hello :-) World"]' );
  674. $expected = "<img alt=\"Hello :-) World\" />\n";
  675. $this->assertSame( $expected, $out );
  676. }
  677. /**
  678. * @ticket 37906
  679. */
  680. public function test_pre_do_shortcode_tag() {
  681. // Does nothing if no filters are set up.
  682. $str = 'pre_do_shortcode_tag';
  683. add_shortcode( $str, array( $this, '_shortcode_pre_do_shortcode_tag' ) );
  684. $result_nofilter = do_shortcode( "[{$str}]" );
  685. $this->assertSame( 'foo', $result_nofilter );
  686. // Short-circuit with filter.
  687. add_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_bar' ) );
  688. $result_filter = do_shortcode( "[{$str}]" );
  689. $this->assertSame( 'bar', $result_filter );
  690. // Respect priority.
  691. add_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_p11' ), 11 );
  692. $result_priority = do_shortcode( "[{$str}]" );
  693. $this->assertSame( 'p11', $result_priority );
  694. // Pass arguments.
  695. $arr = array(
  696. 'return' => 'p11',
  697. 'key' => $str,
  698. 'atts' => array(
  699. 'a' => 'b',
  700. 'c' => 'd',
  701. ),
  702. 'm' => array(
  703. "[{$str} a='b' c='d']",
  704. '',
  705. $str,
  706. " a='b' c='d'",
  707. '',
  708. '',
  709. '',
  710. ),
  711. );
  712. add_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_attr' ), 12, 4 );
  713. $result_atts = do_shortcode( "[{$str} a='b' c='d']" );
  714. $this->assertSame( wp_json_encode( $arr ), $result_atts );
  715. remove_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_attr' ), 12, 4 );
  716. remove_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_p11' ), 11 );
  717. remove_filter( 'pre_do_shortcode_tag', array( $this, '_filter_pre_do_shortcode_tag_bar' ) );
  718. remove_shortcode( $str );
  719. }
  720. public function _shortcode_pre_do_shortcode_tag( $atts = array(), $content = '' ) {
  721. return 'foo';
  722. }
  723. public function _filter_pre_do_shortcode_tag_bar() {
  724. return 'bar';
  725. }
  726. public function _filter_pre_do_shortcode_tag_p11() {
  727. return 'p11';
  728. }
  729. public function _filter_pre_do_shortcode_tag_attr( $return, $key, $atts, $m ) {
  730. $arr = array(
  731. 'return' => $return,
  732. 'key' => $key,
  733. 'atts' => $atts,
  734. 'm' => $m,
  735. );
  736. return wp_json_encode( $arr );
  737. }
  738. /**
  739. * @ticket 32790
  740. */
  741. public function test_do_shortcode_tag_filter() {
  742. // Does nothing if no filters are set up.
  743. $str = 'do_shortcode_tag';
  744. add_shortcode( $str, array( $this, '_shortcode_do_shortcode_tag' ) );
  745. $result_nofilter = do_shortcode( "[{$str}]" );
  746. $this->assertSame( 'foo', $result_nofilter );
  747. // Modify output with filter.
  748. add_filter( 'do_shortcode_tag', array( $this, '_filter_do_shortcode_tag_replace' ) );
  749. $result_filter = do_shortcode( "[{$str}]" );
  750. $this->assertSame( 'fee', $result_filter );
  751. // Respect priority.
  752. add_filter( 'do_shortcode_tag', array( $this, '_filter_do_shortcode_tag_generate' ), 11 );
  753. $result_priority = do_shortcode( "[{$str}]" );
  754. $this->assertSame( 'foobar', $result_priority );
  755. // Pass arguments.
  756. $arr = array(
  757. 'return' => 'foobar',
  758. 'key' => $str,
  759. 'atts' => array(
  760. 'a' => 'b',
  761. 'c' => 'd',
  762. ),
  763. 'm' => array(
  764. "[{$str} a='b' c='d']",
  765. '',
  766. $str,
  767. " a='b' c='d'",
  768. '',
  769. '',
  770. '',
  771. ),
  772. );
  773. add_filter( 'do_shortcode_tag', array( $this, '_filter_do_shortcode_tag_attr' ), 12, 4 );
  774. $result_atts = do_shortcode( "[{$str} a='b' c='d']" );
  775. $this->assertSame( wp_json_encode( $arr ), $result_atts );
  776. remove_filter( 'do_shortcode_tag', array( $this, '_filter_do_shortcode_tag_attr' ), 12 );
  777. remove_filter( 'do_shortcode_tag', array( $this, '_filter_do_shortcode_tag_generate' ), 11 );
  778. remove_filter( 'do_shortcode_tag', array( $this, '_filter_do_shortcode_tag_replace' ) );
  779. remove_shortcode( $str );
  780. }
  781. public function _shortcode_do_shortcode_tag( $atts = array(), $content = '' ) {
  782. return 'foo';
  783. }
  784. public function _filter_do_shortcode_tag_replace( $return ) {
  785. return str_replace( 'oo', 'ee', $return );
  786. }
  787. public function _filter_do_shortcode_tag_generate( $return ) {
  788. return 'foobar';
  789. }
  790. public function _filter_do_shortcode_tag_attr( $return, $key, $atts, $m ) {
  791. $arr = array(
  792. 'return' => $return,
  793. 'key' => $key,
  794. 'atts' => $atts,
  795. 'm' => $m,
  796. );
  797. return wp_json_encode( $arr );
  798. }
  799. /**
  800. * @ticket 37304
  801. *
  802. * Test 'value' syntax for empty attributes
  803. */
  804. function test_empty_single_quote_attribute() {
  805. $out = do_shortcode( '[test-shortcode-tag a="foo" b=\'bar\' c=baz foo \'bar\' "baz" ]test empty atts[/test-shortcode-tag]' );
  806. $this->assertSame(
  807. array(
  808. 'a' => 'foo',
  809. 'b' => 'bar',
  810. 'c' => 'baz',
  811. 0 => 'foo',
  812. 1 => 'bar',
  813. 2 => 'baz',
  814. ),
  815. $this->atts
  816. );
  817. }
  818. /**
  819. * @ticket 37304
  820. */
  821. function test_positional_atts_single_quotes() {
  822. $out = do_shortcode( "[test-shortcode-tag 'something in quotes' 'something else']" );
  823. $this->assertSame( '', $out );
  824. $this->assertSame(
  825. array(
  826. 0 => 'something in quotes',
  827. 1 => 'something else',
  828. ),
  829. $this->atts
  830. );
  831. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  832. }
  833. /**
  834. * @ticket 37304
  835. */
  836. function test_positional_atts_mixed_quotes() {
  837. $out = do_shortcode( "[test-shortcode-tag 'something in quotes' \"something else\" 123 foo bar='baz' example=\"test\" ]" );
  838. $this->assertSame( '', $out );
  839. $this->assertSame(
  840. array(
  841. 0 => 'something in quotes',
  842. 1 => 'something else',
  843. 2 => '123',
  844. 3 => 'foo',
  845. 'bar' => 'baz',
  846. 'example' => 'test',
  847. ),
  848. $this->atts
  849. );
  850. $this->assertSame( 'test-shortcode-tag', $this->tagname );
  851. }
  852. }