/includes/shortcodes.php

  1. <?php 
  2.  
  3. class WPCF7_ShortcodeManager { 
  4.  
  5. private static $instance; 
  6.  
  7. private $shortcode_tags = array(); 
  8.  
  9. // Taggs scanned at the last time of do_shortcode() 
  10. private $scanned_tags = null; 
  11.  
  12. // Executing shortcodes (true) or just scanning (false) 
  13. private $exec = true; 
  14.  
  15. private function __construct() {} 
  16.  
  17. public static function get_instance() { 
  18. if ( empty( self::$instance ) ) { 
  19. self::$instance = new self; 
  20.  
  21. return self::$instance; 
  22.  
  23. public function get_scanned_tags() { 
  24. return $this->scanned_tags; 
  25.  
  26. public function add_shortcode( $tag, $func, $has_name = false ) { 
  27. if ( ! is_callable( $func ) ) 
  28. return; 
  29.  
  30. $tags = array_filter( array_unique( (array) $tag ) ); 
  31.  
  32. foreach ( $tags as $tag ) { 
  33. $tag = $this->sanitize_tag_type( $tag ); 
  34.  
  35. $this->shortcode_tags[$tag] = array( 
  36. 'function' => $func,  
  37. 'has_name' => (boolean) $has_name ); 
  38.  
  39. private function sanitize_tag_type( $tag ) { 
  40. $tag = preg_replace( '/[^a-zA-Z0-9_*]+/', '_', $tag ); 
  41. $tag = rtrim( $tag, '_' ); 
  42. $tag = strtolower( $tag ); 
  43. return $tag; 
  44.  
  45. public function remove_shortcode( $tag ) { 
  46. unset( $this->shortcode_tags[$tag] ); 
  47.  
  48. public function normalize_shortcode( $content ) { 
  49. if ( empty( $this->shortcode_tags ) || ! is_array( $this->shortcode_tags ) ) 
  50. return $content; 
  51.  
  52. $pattern = $this->get_shortcode_regex(); 
  53. return preg_replace_callback( '/' . $pattern . '/s',  
  54. array( $this, 'normalize_space_cb' ), $content ); 
  55.  
  56. private function normalize_space_cb( $m ) { 
  57. // allow [[foo]] syntax for escaping a tag 
  58. if ( $m[1] == '[' && $m[6] == ']' ) 
  59. return $m[0]; 
  60.  
  61. $tag = $m[2]; 
  62. $attr = trim( preg_replace( '/[\r\n\t ]+/', ' ', $m[3] ) ); 
  63. $content = trim( $m[5] ); 
  64.  
  65. $content = str_replace( "\n", '<WPPreserveNewline />', $content ); 
  66.  
  67. $result = $m[1] . '[' . $tag 
  68. . ( $attr ? ' ' . $attr : '' ) 
  69. . ( $m[4] ? ' ' . $m[4] : '' ) 
  70. . ']' 
  71. . ( $content ? $content . '[/' . $tag . ']' : '' ) 
  72. . $m[6]; 
  73.  
  74. return $result; 
  75.  
  76. public function do_shortcode( $content, $exec = true ) { 
  77. $this->exec = (bool) $exec; 
  78. $this->scanned_tags = array(); 
  79.  
  80. if ( empty( $this->shortcode_tags ) || ! is_array( $this->shortcode_tags ) ) 
  81. return $content; 
  82.  
  83. $pattern = $this->get_shortcode_regex(); 
  84. return preg_replace_callback( '/' . $pattern . '/s',  
  85. array( $this, 'do_shortcode_tag' ), $content ); 
  86.  
  87. public function scan_shortcode( $content ) { 
  88. $this->do_shortcode( $content, false ); 
  89. return $this->scanned_tags; 
  90.  
  91. private function get_shortcode_regex() { 
  92. $tagnames = array_keys( $this->shortcode_tags ); 
  93. $tagregexp = join( '|', array_map( 'preg_quote', $tagnames ) ); 
  94.  
  95. return '(\[?)' 
  96. . '\[(' . $tagregexp . ')(?:[\r\n\t ](.*?))?(?:[\r\n\t ](\/))?\]' 
  97. . '(?:([^[]*?)\[\/\2\])?' 
  98. . '(\]?)'; 
  99.  
  100. private function do_shortcode_tag( $m ) { 
  101. // allow [[foo]] syntax for escaping a tag 
  102. if ( $m[1] == '[' && $m[6] == ']' ) { 
  103. return substr( $m[0], 1, -1 ); 
  104.  
  105. $tag = $m[2]; 
  106. $attr = $this->shortcode_parse_atts( $m[3] ); 
  107.  
  108. $scanned_tag = array( 
  109. 'type' => $tag,  
  110. 'basetype' => trim( $tag, '*' ),  
  111. 'name' => '',  
  112. 'options' => array(),  
  113. 'raw_values' => array(),  
  114. 'values' => array(),  
  115. 'pipes' => null,  
  116. 'labels' => array(),  
  117. 'attr' => '',  
  118. 'content' => '' ); 
  119.  
  120. if ( is_array( $attr ) ) { 
  121. if ( is_array( $attr['options'] ) ) { 
  122. if ( $this->shortcode_tags[$tag]['has_name'] && ! empty( $attr['options'] ) ) { 
  123. $scanned_tag['name'] = array_shift( $attr['options'] ); 
  124.  
  125. if ( ! wpcf7_is_name( $scanned_tag['name'] ) ) 
  126. return $m[0]; // Invalid name is used. Ignore this tag. 
  127.  
  128. $scanned_tag['options'] = (array) $attr['options']; 
  129.  
  130. $scanned_tag['raw_values'] = (array) $attr['values']; 
  131.  
  132. if ( WPCF7_USE_PIPE ) { 
  133. $pipes = new WPCF7_Pipes( $scanned_tag['raw_values'] ); 
  134. $scanned_tag['values'] = $pipes->collect_befores(); 
  135. $scanned_tag['pipes'] = $pipes; 
  136. } else { 
  137. $scanned_tag['values'] = $scanned_tag['raw_values']; 
  138.  
  139. $scanned_tag['labels'] = $scanned_tag['values']; 
  140.  
  141. } else { 
  142. $scanned_tag['attr'] = $attr; 
  143.  
  144. $scanned_tag['values'] = array_map( 'trim', $scanned_tag['values'] ); 
  145. $scanned_tag['labels'] = array_map( 'trim', $scanned_tag['labels'] ); 
  146.  
  147. $content = trim( $m[5] ); 
  148. $content = preg_replace( "/<br[\r\n\t ]*\/?>$/m", '', $content ); 
  149. $scanned_tag['content'] = $content; 
  150.  
  151. $scanned_tag = apply_filters( 'wpcf7_form_tag', $scanned_tag, $this->exec ); 
  152.  
  153. $this->scanned_tags[] = $scanned_tag; 
  154.  
  155. if ( $this->exec ) { 
  156. $func = $this->shortcode_tags[$tag]['function']; 
  157. return $m[1] . call_user_func( $func, $scanned_tag ) . $m[6]; 
  158. } else { 
  159. return $m[0]; 
  160.  
  161. private function shortcode_parse_atts( $text ) { 
  162. $atts = array( 'options' => array(), 'values' => array() ); 
  163. $text = preg_replace( "/[\x{00a0}\x{200b}]+/u", " ", $text ); 
  164. $text = stripcslashes( trim( $text ) ); 
  165.  
  166. $pattern = '%^([-+*=0-9a-zA-Z:.!?#$&@_/|\%\r\n\t ]*?)((?:[\r\n\t ]*"[^"]*"|[\r\n\t ]*\'[^\']*\')*)$%'; 
  167.  
  168. if ( preg_match( $pattern, $text, $match ) ) { 
  169. if ( ! empty( $match[1] ) ) { 
  170. $atts['options'] = preg_split( '/[\r\n\t ]+/', trim( $match[1] ) ); 
  171. if ( ! empty( $match[2] ) ) { 
  172. preg_match_all( '/"[^"]*"|\'[^\']*\'/', $match[2], $matched_values ); 
  173. $atts['values'] = wpcf7_strip_quote_deep( $matched_values[0] ); 
  174. } else { 
  175. $atts = $text; 
  176.  
  177. return $atts; 
  178.  
  179.  
  180. function wpcf7_add_shortcode( $tag, $func, $has_name = false ) { 
  181. $manager = WPCF7_ShortcodeManager::get_instance(); 
  182.  
  183. return $manager->add_shortcode( $tag, $func, $has_name ); 
  184.  
  185. function wpcf7_remove_shortcode( $tag ) { 
  186. $manager = WPCF7_ShortcodeManager::get_instance(); 
  187.  
  188. return $manager->remove_shortcode( $tag ); 
  189.  
  190. function wpcf7_do_shortcode( $content ) { 
  191. $manager = WPCF7_ShortcodeManager::get_instance(); 
  192.  
  193. return $manager->do_shortcode( $content ); 
  194.  
  195. class WPCF7_Shortcode { 
  196.  
  197. public $type; 
  198. public $basetype; 
  199. public $name = ''; 
  200. public $options = array(); 
  201. public $raw_values = array(); 
  202. public $values = array(); 
  203. public $pipes; 
  204. public $labels = array(); 
  205. public $attr = ''; 
  206. public $content = ''; 
  207.  
  208. public function __construct( $tag ) { 
  209. foreach ( $tag as $key => $value ) { 
  210. if ( property_exists( __CLASS__, $key ) ) 
  211. $this->{$key} = $value; 
  212.  
  213. public function is_required() { 
  214. return ( '*' == substr( $this->type, -1 ) ); 
  215.  
  216. public function has_option( $opt ) { 
  217. $pattern = sprintf( '/^%s(:.+)?$/i', preg_quote( $opt, '/' ) ); 
  218. return (bool) preg_grep( $pattern, $this->options ); 
  219.  
  220. public function get_option( $opt, $pattern = '', $single = false ) { 
  221. $preset_patterns = array( 
  222. 'date' => '([0-9]{4}-[0-9]{2}-[0-9]{2}|today(.*))',  
  223. 'int' => '[0-9]+',  
  224. 'signed_int' => '-?[0-9]+',  
  225. 'class' => '[-0-9a-zA-Z_]+',  
  226. 'id' => '[-0-9a-zA-Z_]+' ); 
  227.  
  228. if ( isset( $preset_patterns[$pattern] ) ) 
  229. $pattern = $preset_patterns[$pattern]; 
  230.  
  231. if ( '' == $pattern ) 
  232. $pattern = '.+'; 
  233.  
  234. $pattern = sprintf( '/^%s:%s$/i', preg_quote( $opt, '/' ), $pattern ); 
  235.  
  236. if ( $single ) { 
  237. $matches = $this->get_first_match_option( $pattern ); 
  238.  
  239. if ( ! $matches ) 
  240. return false; 
  241.  
  242. return substr( $matches[0], strlen( $opt ) + 1 ); 
  243. } else { 
  244. $matches_a = $this->get_all_match_options( $pattern ); 
  245.  
  246. if ( ! $matches_a ) 
  247. return false; 
  248.  
  249. $results = array(); 
  250.  
  251. foreach ( $matches_a as $matches ) 
  252. $results[] = substr( $matches[0], strlen( $opt ) + 1 ); 
  253.  
  254. return $results; 
  255.  
  256. public function get_id_option() { 
  257. return $this->get_option( 'id', 'id', true ); 
  258.  
  259. public function get_class_option( $default = '' ) { 
  260. if ( is_string( $default ) ) 
  261. $default = explode( ' ', $default ); 
  262.  
  263. $options = array_merge( 
  264. (array) $default,  
  265. (array) $this->get_option( 'class', 'class' ) ); 
  266.  
  267. $options = array_filter( array_unique( $options ) ); 
  268.  
  269. return implode( ' ', $options ); 
  270.  
  271. public function get_size_option( $default = '' ) { 
  272. $option = $this->get_option( 'size', 'int', true ); 
  273.  
  274. if ( $option ) { 
  275. return $option; 
  276.  
  277. $matches_a = $this->get_all_match_options( '%^([0-9]*)/[0-9]*$%' ); 
  278.  
  279. foreach ( (array) $matches_a as $matches ) { 
  280. if ( isset( $matches[1] ) && '' !== $matches[1] ) 
  281. return $matches[1]; 
  282.  
  283. return $default; 
  284.  
  285. public function get_maxlength_option( $default = '' ) { 
  286. $option = $this->get_option( 'maxlength', 'int', true ); 
  287.  
  288. if ( $option ) { 
  289. return $option; 
  290.  
  291. $matches_a = $this->get_all_match_options( 
  292. '%^(?:[0-9]*x?[0-9]*)?/([0-9]+)$%' ); 
  293.  
  294. foreach ( (array) $matches_a as $matches ) { 
  295. if ( isset( $matches[1] ) && '' !== $matches[1] ) 
  296. return $matches[1]; 
  297.  
  298. return $default; 
  299.  
  300. public function get_minlength_option( $default = '' ) { 
  301. $option = $this->get_option( 'minlength', 'int', true ); 
  302.  
  303. if ( $option ) { 
  304. return $option; 
  305. } else { 
  306. return $default; 
  307.  
  308. public function get_cols_option( $default = '' ) { 
  309. $option = $this->get_option( 'cols', 'int', true ); 
  310.  
  311. if ( $option ) { 
  312. return $option; 
  313.  
  314. $matches_a = $this->get_all_match_options( 
  315. '%^([0-9]*)x([0-9]*)(?:/[0-9]+)?$%' ); 
  316.  
  317. foreach ( (array) $matches_a as $matches ) { 
  318. if ( isset( $matches[1] ) && '' !== $matches[1] ) 
  319. return $matches[1]; 
  320.  
  321. return $default; 
  322.  
  323. public function get_rows_option( $default = '' ) { 
  324. $option = $this->get_option( 'rows', 'int', true ); 
  325.  
  326. if ( $option ) { 
  327. return $option; 
  328.  
  329. $matches_a = $this->get_all_match_options( 
  330. '%^([0-9]*)x([0-9]*)(?:/[0-9]+)?$%' ); 
  331.  
  332. foreach ( (array) $matches_a as $matches ) { 
  333. if ( isset( $matches[2] ) && '' !== $matches[2] ) 
  334. return $matches[2]; 
  335.  
  336. return $default; 
  337.  
  338. public function get_date_option( $opt ) { 
  339. $option = $this->get_option( $opt, 'date', true ); 
  340.  
  341. if ( preg_match( '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $option ) ) { 
  342. return $option; 
  343.  
  344. if ( preg_match( '/^today(?:([+-][0-9]+)([a-z]*))?/', $option, $matches ) ) { 
  345. $number = isset( $matches[1] ) ? (int) $matches[1] : 0; 
  346. $unit = isset( $matches[2] ) ? $matches[2] : ''; 
  347.  
  348. if ( ! preg_match( '/^(day|month|year|week)s?$/', $unit ) ) { 
  349. $unit = 'days'; 
  350.  
  351. $date = gmdate( 'Y-m-d',  
  352. strtotime( sprintf( 'today %1$s %2$s', $number, $unit ) ) ); 
  353. return $date; 
  354.  
  355. return false; 
  356.  
  357. public function get_default_option( $default = '', $args = '' ) { 
  358. $args = wp_parse_args( $args, array( 
  359. 'multiple' => false ) ); 
  360.  
  361. $options = (array) $this->get_option( 'default' ); 
  362. $values = array(); 
  363.  
  364. if ( empty( $options ) ) { 
  365. return $args['multiple'] ? $values : $default; 
  366.  
  367. foreach ( $options as $opt ) { 
  368. $opt = sanitize_key( $opt ); 
  369.  
  370. if ( 'user_' == substr( $opt, 0, 5 ) && is_user_logged_in() ) { 
  371. $primary_props = array( 'user_login', 'user_email', 'user_url' ); 
  372. $opt = in_array( $opt, $primary_props ) ? $opt : substr( $opt, 5 ); 
  373.  
  374. $user = wp_get_current_user(); 
  375. $user_prop = $user->get( $opt ); 
  376.  
  377. if ( ! empty( $user_prop ) ) { 
  378. if ( $args['multiple'] ) { 
  379. $values[] = $user_prop; 
  380. } else { 
  381. return $user_prop; 
  382.  
  383. } elseif ( 'post_meta' == $opt && in_the_loop() ) { 
  384. if ( $args['multiple'] ) { 
  385. $values = array_merge( $values,  
  386. get_post_meta( get_the_ID(), $this->name ) ); 
  387. } else { 
  388. $val = (string) get_post_meta( get_the_ID(), $this->name, true ); 
  389.  
  390. if ( strlen( $val ) ) { 
  391. return $val; 
  392.  
  393. } elseif ( 'get' == $opt && isset( $_GET[$this->name] ) ) { 
  394. $vals = (array) $_GET[$this->name]; 
  395. $vals = array_map( 'wpcf7_sanitize_query_var', $vals ); 
  396.  
  397. if ( $args['multiple'] ) { 
  398. $values = array_merge( $values, $vals ); 
  399. } else { 
  400. $val = isset( $vals[0] ) ? (string) $vals[0] : ''; 
  401.  
  402. if ( strlen( $val ) ) { 
  403. return $val; 
  404.  
  405. } elseif ( 'post' == $opt && isset( $_POST[$this->name] ) ) { 
  406. $vals = (array) $_POST[$this->name]; 
  407. $vals = array_map( 'wpcf7_sanitize_query_var', $vals ); 
  408.  
  409. if ( $args['multiple'] ) { 
  410. $values = array_merge( $values, $vals ); 
  411. } else { 
  412. $val = isset( $vals[0] ) ? (string) $vals[0] : ''; 
  413.  
  414. if ( strlen( $val ) ) { 
  415. return $val; 
  416.  
  417. if ( $args['multiple'] ) { 
  418. $values = array_unique( $values ); 
  419. return $values; 
  420. } else { 
  421. return $default; 
  422.  
  423. public function get_data_option( $args = '' ) { 
  424. $options = (array) $this->get_option( 'data' ); 
  425.  
  426. return apply_filters( 'wpcf7_form_tag_data_option', null, $options, $args ); 
  427.  
  428. public function get_first_match_option( $pattern ) { 
  429. foreach( (array) $this->options as $option ) { 
  430. if ( preg_match( $pattern, $option, $matches ) ) 
  431. return $matches; 
  432.  
  433. return false; 
  434.  
  435. public function get_all_match_options( $pattern ) { 
  436. $result = array(); 
  437.  
  438. foreach( (array) $this->options as $option ) { 
  439. if ( preg_match( $pattern, $option, $matches ) ) 
  440. $result[] = $matches; 
  441.  
  442. return $result; 
.